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 "substring.h" |
---|
52 | #include "shell.h" |
---|
53 | #include "shell_command.h" |
---|
54 | #include "shell_buffer.h" |
---|
55 | |
---|
56 | #include "load_param.h" |
---|
57 | |
---|
58 | #include <string.h> |
---|
59 | |
---|
60 | int verbose = 4; |
---|
61 | |
---|
62 | using namespace std; |
---|
63 | |
---|
64 | SHELL_COMMAND(restart, Orxonox, restart); |
---|
65 | |
---|
66 | /** |
---|
67 | * create a new Orxonox |
---|
68 | |
---|
69 | In this funcitons only global values are set. The game will not be started here. |
---|
70 | */ |
---|
71 | Orxonox::Orxonox () |
---|
72 | { |
---|
73 | this->setClassID(CL_ORXONOX, "Orxonox"); |
---|
74 | this->setName("orxonox-main"); |
---|
75 | |
---|
76 | this->iniParser = NULL; |
---|
77 | |
---|
78 | this->argc = 0; |
---|
79 | this->argv = NULL; |
---|
80 | |
---|
81 | this->configFileName = NULL; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * remove Orxonox from memory |
---|
86 | */ |
---|
87 | Orxonox::~Orxonox () |
---|
88 | { |
---|
89 | // game-specific |
---|
90 | delete GameLoader::getInstance(); |
---|
91 | delete GarbageCollector::getInstance(); |
---|
92 | |
---|
93 | // class-less services/factories |
---|
94 | delete Factory::getFirst(); |
---|
95 | FastFactory::deleteAll(); |
---|
96 | ShellCommandClass::unregisterAllCommands(); |
---|
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 | this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE); |
---|
166 | this->iniParser = new IniParser(this->configFileName); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * initialize Orxonox with command line |
---|
171 | */ |
---|
172 | int Orxonox::init (int argc, char** argv) |
---|
173 | { |
---|
174 | this->argc = argc; |
---|
175 | this->argv = argv; |
---|
176 | // parse command line |
---|
177 | // config file |
---|
178 | |
---|
179 | // initialize the Config-file |
---|
180 | this->getConfigFile(); |
---|
181 | |
---|
182 | // initialize everything |
---|
183 | SDL_Init(SDL_INIT_TIMER); |
---|
184 | if( initResources () == -1) return -1; |
---|
185 | if( initVideo() == -1) return -1; |
---|
186 | if( initSound() == -1) return -1; |
---|
187 | if( initInput() == -1) return -1; |
---|
188 | if( initNetworking () == -1) return -1; |
---|
189 | if( initMisc () == -1) return -1; |
---|
190 | |
---|
191 | return 0; |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * initializes SDL and OpenGL |
---|
196 | */ |
---|
197 | int Orxonox::initVideo() |
---|
198 | { |
---|
199 | PRINTF(3)("> Initializing video\n"); |
---|
200 | |
---|
201 | GraphicsEngine::getInstance(); |
---|
202 | |
---|
203 | GraphicsEngine::getInstance()->initFromIniFile(this->iniParser); |
---|
204 | |
---|
205 | char* iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp"); |
---|
206 | if (iconName != NULL) |
---|
207 | { |
---|
208 | GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName); |
---|
209 | delete[] iconName; |
---|
210 | } |
---|
211 | return 0; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * initializes the sound engine |
---|
216 | */ |
---|
217 | int Orxonox::initSound() |
---|
218 | { |
---|
219 | PRINT(3)("> Initializing sound\n"); |
---|
220 | // SDL_InitSubSystem(SDL_INIT_AUDIO); |
---|
221 | SoundEngine::getInstance()->initAudio(); |
---|
222 | |
---|
223 | SoundEngine::getInstance()->loadSettings(this->iniParser); |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | /** |
---|
229 | * initializes input functions |
---|
230 | */ |
---|
231 | int Orxonox::initInput() |
---|
232 | { |
---|
233 | PRINT(3)("> Initializing input\n"); |
---|
234 | |
---|
235 | EventHandler::getInstance()->init(this->iniParser); |
---|
236 | EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE); |
---|
237 | |
---|
238 | |
---|
239 | return 0; |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | /** |
---|
244 | * initializes network system |
---|
245 | */ |
---|
246 | int Orxonox::initNetworking() |
---|
247 | { |
---|
248 | PRINT(3)("> Initializing networking\n"); |
---|
249 | |
---|
250 | printf(" ---Not yet implemented-FIXME--\n"); |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | /** |
---|
256 | * initializes and loads resource files |
---|
257 | */ |
---|
258 | int Orxonox::initResources() |
---|
259 | { |
---|
260 | PRINTF(3)("> Initializing resources\n"); |
---|
261 | |
---|
262 | PRINT(3)("initializing ResourceManager\n"); |
---|
263 | |
---|
264 | const char* dataPath; |
---|
265 | if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL) |
---|
266 | { |
---|
267 | if (!ResourceManager::getInstance()->setDataDir(dataPath)) |
---|
268 | { |
---|
269 | PRINTF(1)("Data Could not be located\n"); |
---|
270 | exit(-1); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | if (!ResourceManager::getInstance()->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
275 | { |
---|
276 | PRINTF(1)("The DataDirectory %s could not be verified\n" \ |
---|
277 | " Please Change in File %s Section %s Entry %s to a suitable value\n", |
---|
278 | ResourceManager::getInstance()->getDataDir(), |
---|
279 | DEFAULT_CONFIG_FILE, |
---|
280 | CONFIG_SECTION_DATA, |
---|
281 | CONFIG_NAME_DATADIR); |
---|
282 | exit(-1); |
---|
283 | } |
---|
284 | //! @todo this is a hack and should be loadable |
---|
285 | char* imageDir = ResourceManager::getInstance()->getFullName("maps/"); |
---|
286 | ResourceManager::getInstance()->addImageDir(imageDir); |
---|
287 | delete[] imageDir; |
---|
288 | |
---|
289 | CDEngine::getInstance(); |
---|
290 | return 0; |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | * initializes miscelaneous features |
---|
295 | * @return -1 on failure |
---|
296 | */ |
---|
297 | int Orxonox::initMisc() |
---|
298 | { |
---|
299 | ShellBuffer::getInstance(); |
---|
300 | return 0; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * starts the orxonox game or menu |
---|
305 | * here is the central orxonox state manager. There are currently two states |
---|
306 | * - menu |
---|
307 | * - game-play |
---|
308 | * both states manage their states themselfs again. |
---|
309 | */ |
---|
310 | void Orxonox::start() |
---|
311 | { |
---|
312 | |
---|
313 | this->gameLoader = GameLoader::getInstance(); |
---|
314 | this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); |
---|
315 | // this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); |
---|
316 | this->gameLoader->init(); |
---|
317 | this->gameLoader->start(); |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | /** |
---|
322 | * handles sprecial events from localinput |
---|
323 | * @param event: an event not handled by the CommandNode |
---|
324 | */ |
---|
325 | // void Orxonox::graphicsHandler(SDL_Event* event) |
---|
326 | // { |
---|
327 | // // Handle special events such as reshape, quit, focus changes |
---|
328 | // switch (event->type) |
---|
329 | // { |
---|
330 | // case SDL_VIDEORESIZE: |
---|
331 | // GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); |
---|
332 | // tmpGEngine->resolutionChanged(event->resize); |
---|
333 | // break; |
---|
334 | // } |
---|
335 | // } |
---|
336 | |
---|
337 | |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | |
---|
342 | bool showGui = false; |
---|
343 | |
---|
344 | |
---|
345 | |
---|
346 | /********************************** |
---|
347 | *** ORXONOX MAIN STARTING POINT *** |
---|
348 | **********************************/ |
---|
349 | /** |
---|
350 | * |
---|
351 | * main function |
---|
352 | * |
---|
353 | * here the journey begins |
---|
354 | */ |
---|
355 | int main(int argc, char** argv) |
---|
356 | { |
---|
357 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
358 | int i; |
---|
359 | for(i = 1; i < argc; ++i) |
---|
360 | { |
---|
361 | if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); |
---|
362 | else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); |
---|
363 | else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
364 | // else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); |
---|
365 | } |
---|
366 | |
---|
367 | return startOrxonox(argc, argv); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | |
---|
372 | int startHelp(int argc, char** argv) |
---|
373 | { |
---|
374 | PRINT(0)("orxonox: starts the orxonox game - rules\n"); |
---|
375 | PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); |
---|
376 | PRINT(0)("valid options:\n"); |
---|
377 | { |
---|
378 | Gui* gui = new Gui(argc, argv); |
---|
379 | gui->printHelp(); |
---|
380 | delete gui; |
---|
381 | } |
---|
382 | PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); |
---|
383 | PRINT(0)(" -h|--help:\t\t\tshows this help\n"); |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | |
---|
388 | /** |
---|
389 | * starts orxonox |
---|
390 | * @param argc parameters count given to orxonox |
---|
391 | * @param argv parameters given to orxonox |
---|
392 | */ |
---|
393 | int startOrxonox(int argc, char** argv) |
---|
394 | { |
---|
395 | // checking for existence of the configuration-files, or if the lock file is still used |
---|
396 | if (showGui || |
---|
397 | !ResourceManager::isFile(DEFAULT_CONFIG_FILE) |
---|
398 | #if DEBUG < 3 |
---|
399 | || ResourceManager::isFile(DEFAULT_LOCK_FILE) |
---|
400 | #endif |
---|
401 | ) |
---|
402 | { |
---|
403 | if (ResourceManager::isFile(DEFAULT_LOCK_FILE)) |
---|
404 | ResourceManager::deleteFile(DEFAULT_LOCK_FILE); |
---|
405 | |
---|
406 | // starting the GUI |
---|
407 | Gui* gui = new Gui(argc, argv); |
---|
408 | gui->startGui(); |
---|
409 | |
---|
410 | if (! gui->startOrxonox) |
---|
411 | return 0; |
---|
412 | |
---|
413 | delete gui; |
---|
414 | } |
---|
415 | |
---|
416 | PRINT(0)(">>> Starting Orxonox <<<\n"); |
---|
417 | |
---|
418 | ResourceManager::touchFile(DEFAULT_LOCK_FILE); |
---|
419 | |
---|
420 | Orxonox *orx = Orxonox::getInstance(); |
---|
421 | |
---|
422 | if((*orx).init(argc, argv) == -1) |
---|
423 | { |
---|
424 | PRINTF(1)("! Orxonox initialization failed\n"); |
---|
425 | return -1; |
---|
426 | } |
---|
427 | |
---|
428 | printf("finished inizialisation\n"); |
---|
429 | orx->start(); |
---|
430 | |
---|
431 | delete orx; |
---|
432 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
433 | } |
---|