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