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 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ORXONOX |
---|
28 | #include "orxonox.h" |
---|
29 | |
---|
30 | #include "globals.h" |
---|
31 | |
---|
32 | #include "gui.h" |
---|
33 | |
---|
34 | #include "world.h" |
---|
35 | #include "parser/ini_parser/ini_parser.h" |
---|
36 | #include "game_loader.h" |
---|
37 | |
---|
38 | //ENGINES |
---|
39 | #include "graphics_engine.h" |
---|
40 | #include "sound_engine.h" |
---|
41 | #include "resource_manager.h" |
---|
42 | #include "cd_engine.h" |
---|
43 | #include "text_engine.h" |
---|
44 | #include "event_handler.h" |
---|
45 | |
---|
46 | #include "factory.h" |
---|
47 | #include "fast_factory.h" |
---|
48 | |
---|
49 | #include "benchmark.h" |
---|
50 | |
---|
51 | #include "class_list.h" |
---|
52 | #include "shell_command_class.h" |
---|
53 | #include "shell_command.h" |
---|
54 | #include "shell_buffer.h" |
---|
55 | |
---|
56 | #include "load_param_description.h" |
---|
57 | |
---|
58 | #include "network_manager.h" |
---|
59 | |
---|
60 | #include <string.h> |
---|
61 | |
---|
62 | int verbose = 4; |
---|
63 | |
---|
64 | using namespace std; |
---|
65 | |
---|
66 | SHELL_COMMAND(restart, Orxonox, restart); |
---|
67 | |
---|
68 | /** |
---|
69 | * create a new Orxonox |
---|
70 | |
---|
71 | In this funcitons only global values are set. The game will not be started here. |
---|
72 | */ |
---|
73 | Orxonox::Orxonox () |
---|
74 | { |
---|
75 | this->setClassID(CL_ORXONOX, "Orxonox"); |
---|
76 | this->setName("orxonox-main"); |
---|
77 | |
---|
78 | this->iniParser = NULL; |
---|
79 | |
---|
80 | this->argc = 0; |
---|
81 | this->argv = NULL; |
---|
82 | |
---|
83 | /* this way, there is no network enabled: */ |
---|
84 | this->serverName = NULL; |
---|
85 | this->port = -1; |
---|
86 | |
---|
87 | this->configFileName = NULL; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * remove Orxonox from memory |
---|
92 | */ |
---|
93 | Orxonox::~Orxonox () |
---|
94 | { |
---|
95 | // game-specific |
---|
96 | delete GameLoader::getInstance(); |
---|
97 | |
---|
98 | // class-less services/factories |
---|
99 | Factory::deleteFactories(); |
---|
100 | FastFactory::deleteAll(); |
---|
101 | ShellCommandClass::unregisterAllCommands(); |
---|
102 | |
---|
103 | LoadClassDescription::deleteAllDescriptions(); |
---|
104 | |
---|
105 | // engines |
---|
106 | delete CDEngine::getInstance(); |
---|
107 | delete SoundEngine::getInstance(); |
---|
108 | delete GraphicsEngine::getInstance(); // deleting the Graphics |
---|
109 | delete EventHandler::getInstance(); |
---|
110 | |
---|
111 | // handlers |
---|
112 | delete ResourceManager::getInstance(); // deletes the Resource Manager |
---|
113 | // output-buffer |
---|
114 | delete ShellBuffer::getInstance(); |
---|
115 | |
---|
116 | // orxonox class-stuff |
---|
117 | delete this->iniParser; |
---|
118 | delete[] this->configFileName; |
---|
119 | |
---|
120 | SDL_QuitSubSystem(SDL_INIT_TIMER); |
---|
121 | ClassList::debug(); |
---|
122 | |
---|
123 | PRINT(3) |
---|
124 | ( |
---|
125 | "===================================================\n" \ |
---|
126 | "Thanks for playing orxonox.\n" \ |
---|
127 | "visit: http://www.orxonox.net for new versions.\n" \ |
---|
128 | "===================================================\n" \ |
---|
129 | ORXONOX_LICENSE_SHORT |
---|
130 | ); |
---|
131 | |
---|
132 | Orxonox::singletonRef = NULL; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * this is a singleton class to prevent duplicates |
---|
137 | */ |
---|
138 | Orxonox* Orxonox::singletonRef = NULL; |
---|
139 | |
---|
140 | // DANGEROUS |
---|
141 | void Orxonox::restart() |
---|
142 | { |
---|
143 | // int argc = this->argc; |
---|
144 | // char** argv = this->argv; |
---|
145 | // |
---|
146 | // Orxonox *orx = Orxonox::getInstance(); |
---|
147 | // |
---|
148 | // delete orx; |
---|
149 | // |
---|
150 | // orx = Orxonox::getInstance(); |
---|
151 | // |
---|
152 | // if((*orx).init(argc, argv) == -1) |
---|
153 | // { |
---|
154 | // PRINTF(1)("! Orxonox initialization failed\n"); |
---|
155 | // return; |
---|
156 | // } |
---|
157 | // |
---|
158 | // printf("finished inizialisation\n"); |
---|
159 | // orx->start(); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * this finds the config file |
---|
164 | * @returns the new config-fileName |
---|
165 | * Since the config file varies from user to user and since one may want to specify different config files |
---|
166 | * for certain occasions or platforms this function finds the right config file for every occasion and stores |
---|
167 | * it's path and name into configfilename |
---|
168 | */ |
---|
169 | const char* Orxonox::getConfigFile () |
---|
170 | { |
---|
171 | if (ResourceManager::isFile("orxonox.conf")) |
---|
172 | { |
---|
173 | this->configFileName = new char[strlen("orxonox.conf")+1]; |
---|
174 | strcpy(this->configFileName, "orxonox.conf"); |
---|
175 | } |
---|
176 | else |
---|
177 | this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE); |
---|
178 | this->iniParser = new IniParser(this->configFileName); |
---|
179 | PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * initialize Orxonox with command line |
---|
184 | */ |
---|
185 | int Orxonox::init (int argc, char** argv, const char* name, int port) |
---|
186 | { |
---|
187 | this->argc = argc; |
---|
188 | this->argv = argv; |
---|
189 | |
---|
190 | this->serverName = name; |
---|
191 | this->port = port; |
---|
192 | |
---|
193 | // initialize the Config-file |
---|
194 | this->getConfigFile(); |
---|
195 | |
---|
196 | // windows must not write into stdout.txt and stderr.txt |
---|
197 | #ifdef __WIN32__ |
---|
198 | freopen( "CON", "w", stdout ); |
---|
199 | freopen( "CON", "w", stderr ); |
---|
200 | #endif |
---|
201 | |
---|
202 | // initialize everything |
---|
203 | SDL_Init(0); |
---|
204 | if( initResources () == -1) |
---|
205 | return -1; |
---|
206 | if( initVideo() == -1) |
---|
207 | return -1; |
---|
208 | if( initSound() == -1) |
---|
209 | return -1; |
---|
210 | if( initInput() == -1) |
---|
211 | return -1; |
---|
212 | if( initNetworking () == -1) |
---|
213 | return -1; |
---|
214 | if( initMisc () == -1) |
---|
215 | return -1; |
---|
216 | |
---|
217 | return 0; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * initializes SDL and OpenGL |
---|
223 | */ |
---|
224 | int Orxonox::initVideo() |
---|
225 | { |
---|
226 | PRINTF(3)("> Initializing video\n"); |
---|
227 | |
---|
228 | GraphicsEngine::getInstance(); |
---|
229 | |
---|
230 | GraphicsEngine::getInstance()->initFromIniFile(this->iniParser); |
---|
231 | |
---|
232 | char* iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp"); |
---|
233 | if (iconName != NULL) |
---|
234 | { |
---|
235 | GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName); |
---|
236 | delete[] iconName; |
---|
237 | } |
---|
238 | return 0; |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | /** |
---|
243 | * initializes the sound engine |
---|
244 | */ |
---|
245 | int Orxonox::initSound() |
---|
246 | { |
---|
247 | PRINT(3)("> Initializing sound\n"); |
---|
248 | // SDL_InitSubSystem(SDL_INIT_AUDIO); |
---|
249 | SoundEngine::getInstance()->initAudio(); |
---|
250 | SoundEngine::getInstance()->allocateSources(1); |
---|
251 | |
---|
252 | SoundEngine::getInstance()->loadSettings(this->iniParser); |
---|
253 | return 0; |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | /** |
---|
258 | * initializes input functions |
---|
259 | */ |
---|
260 | int Orxonox::initInput() |
---|
261 | { |
---|
262 | PRINT(3)("> Initializing input\n"); |
---|
263 | |
---|
264 | EventHandler::getInstance()->init(this->iniParser); |
---|
265 | EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE); |
---|
266 | |
---|
267 | return 0; |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | /** |
---|
272 | * initializes network system |
---|
273 | */ |
---|
274 | int Orxonox::initNetworking() |
---|
275 | { |
---|
276 | PRINT(3)("> Initializing networking\n"); |
---|
277 | |
---|
278 | if( this->serverName != NULL) // we are a client |
---|
279 | NetworkManager::getInstance()->establishConnection(this->serverName, port); |
---|
280 | else if( this->port > 0) // we are a server |
---|
281 | NetworkManager::getInstance()->createServer(port); |
---|
282 | |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | /** |
---|
288 | * initializes and loads resource files |
---|
289 | */ |
---|
290 | int Orxonox::initResources() |
---|
291 | { |
---|
292 | PRINTF(3)("> Initializing resources\n"); |
---|
293 | |
---|
294 | PRINT(3)("initializing ResourceManager\n"); |
---|
295 | |
---|
296 | // init the resource manager |
---|
297 | const char* dataPath; |
---|
298 | if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL) |
---|
299 | { |
---|
300 | if (!ResourceManager::getInstance()->setDataDir(dataPath) && |
---|
301 | !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
302 | { |
---|
303 | PRINTF(1)("Data Could not be located in %s\n", dataPath); |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
308 | { |
---|
309 | PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \ |
---|
310 | "!!! Please Change in File %s Section %s Entry %s to a suitable value !!!\n", |
---|
311 | ResourceManager::getInstance()->getDataDir(), |
---|
312 | this->configFileName, |
---|
313 | CONFIG_SECTION_DATA, |
---|
314 | CONFIG_NAME_DATADIR ); |
---|
315 | Gui* gui = new Gui(argc, argv); |
---|
316 | gui->startGui(); |
---|
317 | delete gui; |
---|
318 | exit(-1); |
---|
319 | } |
---|
320 | //! @todo this is a hack and should be loadable |
---|
321 | char* imageDir = ResourceManager::getInstance()->getFullName("maps"); |
---|
322 | ResourceManager::getInstance()->addImageDir(imageDir); |
---|
323 | delete[] imageDir; |
---|
324 | |
---|
325 | // start the collision detection engine |
---|
326 | CDEngine::getInstance(); |
---|
327 | return 0; |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * initializes miscelaneous features |
---|
332 | * @return -1 on failure |
---|
333 | */ |
---|
334 | int Orxonox::initMisc() |
---|
335 | { |
---|
336 | ShellBuffer::getInstance(); |
---|
337 | return 0; |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * starts the orxonox game or menu |
---|
342 | * here is the central orxonox state manager. There are currently two states |
---|
343 | * - menu |
---|
344 | * - game-play |
---|
345 | * both states manage their states themselfs again. |
---|
346 | */ |
---|
347 | void Orxonox::start() |
---|
348 | { |
---|
349 | |
---|
350 | this->gameLoader = GameLoader::getInstance(); |
---|
351 | |
---|
352 | if( this->port != -1 || this->serverName != NULL) |
---|
353 | this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); /* actualy loadNetworkCampaign*/ |
---|
354 | else |
---|
355 | this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); |
---|
356 | |
---|
357 | // this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); |
---|
358 | this->gameLoader->init(); |
---|
359 | this->gameLoader->start(); |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | /** |
---|
364 | * handles sprecial events from localinput |
---|
365 | * @param event: an event not handled by the CommandNode |
---|
366 | */ |
---|
367 | // void Orxonox::graphicsHandler(SDL_Event* event) |
---|
368 | // { |
---|
369 | // // Handle special events such as reshape, quit, focus changes |
---|
370 | // switch (event->type) |
---|
371 | // { |
---|
372 | // case SDL_VIDEORESIZE: |
---|
373 | // GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); |
---|
374 | // tmpGEngine->resolutionChanged(event->resize); |
---|
375 | // break; |
---|
376 | // } |
---|
377 | // } |
---|
378 | |
---|
379 | |
---|
380 | |
---|
381 | |
---|
382 | |
---|
383 | |
---|
384 | bool showGui = false; |
---|
385 | |
---|
386 | |
---|
387 | |
---|
388 | /********************************** |
---|
389 | *** ORXONOX MAIN STARTING POINT *** |
---|
390 | **********************************/ |
---|
391 | /** |
---|
392 | * |
---|
393 | * main function |
---|
394 | * |
---|
395 | * here the journey begins |
---|
396 | */ |
---|
397 | int main(int argc, char** argv) |
---|
398 | { |
---|
399 | int i; |
---|
400 | for(i = 1; i < argc; ++i) |
---|
401 | { |
---|
402 | if( !strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) |
---|
403 | return showHelp(argc, argv); |
---|
404 | else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) |
---|
405 | showGui = true; |
---|
406 | else if(!strcmp( "--client", argv[i]) || !strcmp("-c", argv[i])) |
---|
407 | return startNetworkOrxonox(argc, argv); |
---|
408 | else if(!strcmp( "--server", argv[i]) || !strcmp("-s", argv[i])) |
---|
409 | return startNetworkOrxonox(argc, argv); |
---|
410 | else if(!strcmp( "--license", argv[i]) || !strcmp("-s", argv[i])) |
---|
411 | return PRINT(0)(ORXONOX_LICENSE_SHORT); |
---|
412 | } |
---|
413 | |
---|
414 | return startOrxonox(argc, argv, NULL, -1); |
---|
415 | } |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | int showHelp(int argc, char** argv) |
---|
420 | { |
---|
421 | PRINT(0)("Orxonox Version %s\n", PACKAGE_VERSION); |
---|
422 | PRINT(0)(" Starts Orxonox - The most furious 3D Action Game :)\n"); |
---|
423 | PRINT(0)("\n"); |
---|
424 | PRINT(0)("Common options:\n"); |
---|
425 | PRINT(0)(" -g, --gui starts the orxonox with the configuration GUI \n"); |
---|
426 | PRINT(0)(" -h, --help shows this help\n"); |
---|
427 | PRINT(0)("\n"); |
---|
428 | PRINT(0)("Network options:\n"); |
---|
429 | PRINT(0)(" -s, --server [port] starts Orxonox and listens on the [port] for players\n"); |
---|
430 | PRINT(0)(" -c, --client [hostname] [port] starts Orxonox as a Client\n"); |
---|
431 | PRINT(0)(" -c, --client [ip address] [port] starts Orxonox as a Client\n"); |
---|
432 | PRINT(0)("\n"); |
---|
433 | PRINT(0)("Other options:\n"); |
---|
434 | PRINT(0)(" --license prints the licence and exit\n\n"); |
---|
435 | PRINT(0)("\n"); |
---|
436 | |
---|
437 | // { |
---|
438 | // Gui* gui = new Gui(argc, argv); |
---|
439 | // gui->printHelp(); |
---|
440 | // delete gui; |
---|
441 | // } |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | |
---|
446 | |
---|
447 | /** |
---|
448 | * starts orxonox in network mode |
---|
449 | * @param argc parameters count given to orxonox |
---|
450 | * @param argv parameters given to orxonox |
---|
451 | */ |
---|
452 | int startNetworkOrxonox(int argc, char** argv) |
---|
453 | { |
---|
454 | |
---|
455 | int i; |
---|
456 | for(i = 0; i < argc; ++i ) |
---|
457 | { |
---|
458 | if( !strcmp( "--client", argv[i]) || !strcmp("-c", argv[i])) |
---|
459 | { |
---|
460 | if( argc <= (i+2)) |
---|
461 | { |
---|
462 | printf(" Wrong arguments try following notations:\n"); |
---|
463 | printf(" --client [server ip address] [port number]\n"); |
---|
464 | printf(" --client [dns name] [port number]\n"); |
---|
465 | return 0; |
---|
466 | } |
---|
467 | |
---|
468 | const char* name = argv[i+1]; |
---|
469 | int port = atoi(argv[i+2]); |
---|
470 | printf("Starting Orxonox as client: connecting to %s, on port %i\n", name, port); |
---|
471 | |
---|
472 | startOrxonox(argc, argv, name, port); |
---|
473 | } |
---|
474 | else if( !strcmp( "--server", argv[i]) || !strcmp("-s", argv[i])) |
---|
475 | { |
---|
476 | if( argc <= (i+1)) |
---|
477 | { |
---|
478 | printf(" Wrong arguments try following notations:\n"); |
---|
479 | printf(" --server [port number]\n"); |
---|
480 | return 0; |
---|
481 | } |
---|
482 | |
---|
483 | int port = atoi(argv[i+1]); |
---|
484 | printf("Starting Orxonox as server, listening on port %i\n", port); |
---|
485 | |
---|
486 | startOrxonox(argc, argv, NULL, port); |
---|
487 | } |
---|
488 | } |
---|
489 | } |
---|
490 | |
---|
491 | |
---|
492 | |
---|
493 | /** |
---|
494 | * starts orxonox |
---|
495 | * @param argc parameters count given to orxonox |
---|
496 | * @param argv parameters given to orxonox |
---|
497 | */ |
---|
498 | int startOrxonox(int argc, char** argv, const char* name, int port) |
---|
499 | { |
---|
500 | // checking for existence of the configuration-files, or if the lock file is still used |
---|
501 | if (showGui || (!ResourceManager::isFile("./orxonox.conf") && |
---|
502 | !ResourceManager::isFile(DEFAULT_CONFIG_FILE)) |
---|
503 | #if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails |
---|
504 | || ResourceManager::isFile(DEFAULT_LOCK_FILE) |
---|
505 | #endif |
---|
506 | ) |
---|
507 | { |
---|
508 | if (ResourceManager::isFile(DEFAULT_LOCK_FILE)) |
---|
509 | ResourceManager::deleteFile(DEFAULT_LOCK_FILE); |
---|
510 | |
---|
511 | // starting the GUI |
---|
512 | Gui* gui = new Gui(argc, argv); |
---|
513 | gui->startGui(); |
---|
514 | |
---|
515 | if (! gui->startOrxonox) |
---|
516 | return 0; |
---|
517 | |
---|
518 | delete gui; |
---|
519 | } |
---|
520 | |
---|
521 | PRINT(0)(">>> Starting Orxonox <<<\n"); |
---|
522 | |
---|
523 | ResourceManager::touchFile(DEFAULT_LOCK_FILE); |
---|
524 | |
---|
525 | Orxonox *orx = Orxonox::getInstance(); |
---|
526 | |
---|
527 | if( orx->init(argc, argv, name, port) == -1) |
---|
528 | { |
---|
529 | PRINTF(1)("! Orxonox initialization failed\n"); |
---|
530 | return -1; |
---|
531 | } |
---|
532 | |
---|
533 | printf("finished inizialisation\n"); |
---|
534 | orx->start(); |
---|
535 | |
---|
536 | delete orx; |
---|
537 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
538 | } |
---|