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