1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | This program is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with this program; if not, write to the Free Software Foundation, |
---|
18 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | |
---|
20 | |
---|
21 | ### File Specific: |
---|
22 | main-programmer: Patrick Boenzli |
---|
23 | co-programmer: Christian Meyer |
---|
24 | co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI |
---|
25 | */ |
---|
26 | |
---|
27 | #include "orxonox.h" |
---|
28 | |
---|
29 | #include "gui.h" |
---|
30 | |
---|
31 | #include "world.h" |
---|
32 | #include "ini_parser.h" |
---|
33 | #include "game_loader.h" |
---|
34 | #include "graphics_engine.h" |
---|
35 | #include "sound_engine.h" |
---|
36 | #include "resource_manager.h" |
---|
37 | #include "object_manager.h" |
---|
38 | #include "text_engine.h" |
---|
39 | #include "factory.h" |
---|
40 | #include "benchmark.h" |
---|
41 | #include "event_handler.h" |
---|
42 | #include "event.h" |
---|
43 | #include "cd_engine.h" |
---|
44 | |
---|
45 | #include "class_list.h" |
---|
46 | |
---|
47 | #include <string.h> |
---|
48 | |
---|
49 | int verbose = 4; |
---|
50 | |
---|
51 | using namespace std; |
---|
52 | |
---|
53 | /** |
---|
54 | \brief create a new Orxonox |
---|
55 | |
---|
56 | In this funcitons only global values are set. The game will not be started here. |
---|
57 | */ |
---|
58 | Orxonox::Orxonox () |
---|
59 | { |
---|
60 | this->setClassID(CL_ORXONOX, "Orxonox"); |
---|
61 | this->setName("orxonox"); |
---|
62 | |
---|
63 | this->resourceManager = NULL; |
---|
64 | this->objectManager = NULL; |
---|
65 | this->eventHandler = NULL; |
---|
66 | |
---|
67 | this->argc = 0; |
---|
68 | this->argv = NULL; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | \brief remove Orxonox from memory |
---|
73 | */ |
---|
74 | Orxonox::~Orxonox () |
---|
75 | { |
---|
76 | int i = 0; |
---|
77 | Orxonox::singletonRef = NULL; |
---|
78 | delete GraphicsEngine::getInstance(); // deleting the Graphics |
---|
79 | delete TextEngine::getInstance(); |
---|
80 | delete SoundEngine::getInstance(); |
---|
81 | delete ResourceManager::getInstance(); // deletes the Resource Manager |
---|
82 | delete ObjectManager::getInstance(); |
---|
83 | delete TextEngine::getInstance(); |
---|
84 | delete EventHandler::getInstance(); |
---|
85 | delete Factory::getFirst(); |
---|
86 | |
---|
87 | ClassList::debug(0); |
---|
88 | } |
---|
89 | |
---|
90 | /** \brief this is a singleton class to prevent duplicates */ |
---|
91 | Orxonox* Orxonox::singletonRef = 0; |
---|
92 | |
---|
93 | /** |
---|
94 | \returns reference or new Object of Orxonox if not existent. |
---|
95 | */ |
---|
96 | Orxonox* Orxonox::getInstance () |
---|
97 | { |
---|
98 | if (singletonRef == NULL) |
---|
99 | singletonRef = new Orxonox(); |
---|
100 | return singletonRef; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | \brief this finds the config file |
---|
105 | |
---|
106 | Since the config file varies from user to user and since one may want to specify different config files |
---|
107 | for certain occasions or platforms this function finds the right config file for every occasion and stores |
---|
108 | it's path and name into configfilename |
---|
109 | */ |
---|
110 | void Orxonox::getConfigFile (int argc, char** argv) |
---|
111 | { |
---|
112 | strcpy (configfilename, "~/.orxonox/orxonox.conf"); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | \brief initialize Orxonox with command line |
---|
117 | */ |
---|
118 | int Orxonox::init (int argc, char** argv) |
---|
119 | { |
---|
120 | this->argc = argc; |
---|
121 | this->argv = argv; |
---|
122 | // parse command line |
---|
123 | // config file |
---|
124 | |
---|
125 | getConfigFile (argc, argv); |
---|
126 | SDL_Init (SDL_INIT_TIMER); |
---|
127 | // initialize everything |
---|
128 | printf("> Initializing resources\n"); |
---|
129 | if( initResources () == -1) return -1; |
---|
130 | |
---|
131 | if( initVideo() == -1) return -1; |
---|
132 | if( initSound() == -1) return -1; |
---|
133 | PRINT(3)("> Initializing input\n"); |
---|
134 | if( initInput() == -1) return -1; |
---|
135 | PRINT(3)("> Initializing networking\n"); |
---|
136 | if( initNetworking () == -1) return -1; |
---|
137 | //printf("> Initializing world\n"); |
---|
138 | //if( init_world () == -1) return -1; PB: world will be initialized when started |
---|
139 | |
---|
140 | return 0; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | \brief initializes SDL and OpenGL |
---|
145 | */ |
---|
146 | int Orxonox::initVideo() |
---|
147 | { |
---|
148 | PRINTF(3)("> Initializing video\n"); |
---|
149 | |
---|
150 | GraphicsEngine::getInstance(); |
---|
151 | GraphicsEngine::getInstance()->setWindowName("orxonox " PACKAGE_VERSION, "orxonox " PACKAGE_VERSION); |
---|
152 | |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /** |
---|
158 | \brief initializes the sound engine |
---|
159 | */ |
---|
160 | int Orxonox::initSound() |
---|
161 | { |
---|
162 | PRINT(3)("> Initializing sound\n"); |
---|
163 | // SDL_Init(SDL_INIT_AUDIO); |
---|
164 | SoundEngine::getInstance()->initAudio(); |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | /** |
---|
170 | \brief initializes input functions |
---|
171 | */ |
---|
172 | int Orxonox::initInput() |
---|
173 | { |
---|
174 | this->eventHandler = EventHandler::getInstance(); |
---|
175 | this->eventHandler->init(); |
---|
176 | |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /** |
---|
182 | \brief initializes network system |
---|
183 | */ |
---|
184 | int Orxonox::initNetworking() |
---|
185 | { |
---|
186 | printf("Not yet implemented\n"); |
---|
187 | return 0; |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | /** |
---|
192 | \brief initializes and loads resource files |
---|
193 | */ |
---|
194 | int Orxonox::initResources() |
---|
195 | { |
---|
196 | PRINT(3)("initializing ResourceManager\n"); |
---|
197 | resourceManager = ResourceManager::getInstance(); |
---|
198 | |
---|
199 | // create parser |
---|
200 | IniParser parser (DEFAULT_CONFIG_FILE); |
---|
201 | if( parser.getSection (CONFIG_SECTION_DATA) == -1) |
---|
202 | { |
---|
203 | PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE); |
---|
204 | return -1; |
---|
205 | } |
---|
206 | char namebuf[256]; |
---|
207 | char valuebuf[256]; |
---|
208 | memset (namebuf, 0, 256); |
---|
209 | memset (valuebuf, 0, 256); |
---|
210 | |
---|
211 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
212 | { |
---|
213 | if (!strcmp(namebuf, CONFIG_NAME_DATADIR)) |
---|
214 | { |
---|
215 | // printf("Not yet implemented\n"); |
---|
216 | if (!resourceManager->setDataDir(valuebuf)) |
---|
217 | { |
---|
218 | PRINTF(1)("Data Could not be located\n"); |
---|
219 | exit(-1); |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | memset (namebuf, 0, 256); |
---|
224 | memset (valuebuf, 0, 256); |
---|
225 | } |
---|
226 | |
---|
227 | if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
228 | { |
---|
229 | PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n", |
---|
230 | resourceManager->getDataDir(), |
---|
231 | DEFAULT_CONFIG_FILE, |
---|
232 | CONFIG_SECTION_DATA, |
---|
233 | CONFIG_NAME_DATADIR); |
---|
234 | exit(-1); |
---|
235 | } |
---|
236 | //! \todo this is a hack and should be loadable |
---|
237 | resourceManager->addImageDir(ResourceManager::getInstance()->getFullName("maps/")); |
---|
238 | resourceManager->debug(); |
---|
239 | |
---|
240 | PRINT(3)("initializing TextEngine\n"); |
---|
241 | TextEngine::getInstance(); |
---|
242 | |
---|
243 | PRINT(3)("initializing ObjectManager\n"); |
---|
244 | this->objectManager = ObjectManager::getInstance(); |
---|
245 | |
---|
246 | CDEngine::getInstance(); |
---|
247 | |
---|
248 | return 0; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | /** |
---|
255 | \brief starts the orxonox game or menu |
---|
256 | |
---|
257 | here is the central orxonox state manager. There are currently two states |
---|
258 | - menu |
---|
259 | - game-play |
---|
260 | both states manage their states themselfs again. |
---|
261 | */ |
---|
262 | void Orxonox::start() |
---|
263 | { |
---|
264 | |
---|
265 | this->gameLoader = GameLoader::getInstance(); |
---|
266 | this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); |
---|
267 | // this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); |
---|
268 | this->gameLoader->init(); |
---|
269 | this->gameLoader->start(); |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | /** |
---|
274 | \brief handles sprecial events from localinput |
---|
275 | \param event: an event not handled by the CommandNode |
---|
276 | */ |
---|
277 | void Orxonox::graphicsHandler(SDL_Event* event) |
---|
278 | { |
---|
279 | // Handle special events such as reshape, quit, focus changes |
---|
280 | switch (event->type) |
---|
281 | { |
---|
282 | case SDL_VIDEORESIZE: |
---|
283 | GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); |
---|
284 | tmpGEngine->resolutionChanged(&event->resize); |
---|
285 | break; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | /** |
---|
291 | \brief processes the events for orxonox main class |
---|
292 | \param the event to handle |
---|
293 | */ |
---|
294 | void Orxonox::process(const Event &event) |
---|
295 | {} |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | bool showGui = false; |
---|
300 | |
---|
301 | /** |
---|
302 | \brief main function |
---|
303 | |
---|
304 | here the journey begins |
---|
305 | */ |
---|
306 | int main(int argc, char** argv) |
---|
307 | { |
---|
308 | |
---|
309 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
310 | int i; |
---|
311 | for(i = 1; i < argc; ++i) |
---|
312 | { |
---|
313 | if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); |
---|
314 | else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); |
---|
315 | else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
316 | // else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); |
---|
317 | } |
---|
318 | |
---|
319 | return startOrxonox(argc, argv); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | |
---|
324 | int startHelp(int argc, char** argv) |
---|
325 | { |
---|
326 | PRINT(0)("orxonox: starts the orxonox game - rules\n"); |
---|
327 | PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); |
---|
328 | PRINT(0)("valid options:\n"); |
---|
329 | { |
---|
330 | Gui* gui = new Gui(argc, argv); |
---|
331 | gui->printHelp(); |
---|
332 | delete gui; |
---|
333 | } |
---|
334 | PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); |
---|
335 | PRINT(0)(" -h|--help:\t\t\tshows this help\n"); |
---|
336 | } |
---|
337 | |
---|
338 | |
---|
339 | int startOrxonox(int argc, char** argv) |
---|
340 | { |
---|
341 | // checking for existence of the configuration-files |
---|
342 | if (showGui || |
---|
343 | !ResourceManager::isFile("~/.orxonox/orxonox.conf") || |
---|
344 | ResourceManager::isFile("~/.orxonox/orxonox.lock")) |
---|
345 | { |
---|
346 | if (ResourceManager::isFile("~/.orxonox/orxonox.lock")) |
---|
347 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
348 | |
---|
349 | // starting the GUI |
---|
350 | Gui* gui = new Gui(argc, argv); |
---|
351 | gui->startGui(); |
---|
352 | |
---|
353 | if (! gui->startOrxonox) |
---|
354 | return 0; |
---|
355 | |
---|
356 | delete gui; |
---|
357 | } |
---|
358 | |
---|
359 | PRINT(0)(">>> Starting Orxonox <<<\n"); |
---|
360 | |
---|
361 | ResourceManager::touchFile("~/.orxonox/orxonox.lock"); |
---|
362 | |
---|
363 | Orxonox *orx = Orxonox::getInstance(); |
---|
364 | |
---|
365 | if((*orx).init(argc, argv) == -1) |
---|
366 | { |
---|
367 | PRINTF(1)("! Orxonox initialization failed\n"); |
---|
368 | return -1; |
---|
369 | } |
---|
370 | |
---|
371 | orx->start(); |
---|
372 | |
---|
373 | delete orx; |
---|
374 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
375 | |
---|
376 | } |
---|