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 "data_tank.h" |
---|
33 | #include "command_node.h" |
---|
34 | #include "ini_parser.h" |
---|
35 | #include "game_loader.h" |
---|
36 | #include "graphics_engine.h" |
---|
37 | #include "sound_engine.h" |
---|
38 | #include "resource_manager.h" |
---|
39 | #include "text_engine.h" |
---|
40 | #include "factory.h" |
---|
41 | #include "benchmark.h" |
---|
42 | |
---|
43 | #include <string.h> |
---|
44 | |
---|
45 | int verbose = 4; |
---|
46 | |
---|
47 | using namespace std; |
---|
48 | |
---|
49 | /** |
---|
50 | \brief create a new Orxonox |
---|
51 | |
---|
52 | In this funcitons only global values are set. The game will not be started here. |
---|
53 | */ |
---|
54 | Orxonox::Orxonox () |
---|
55 | { |
---|
56 | this->pause = false; |
---|
57 | |
---|
58 | this->world = NULL; |
---|
59 | this->resources = NULL; |
---|
60 | this->localinput = NULL; |
---|
61 | |
---|
62 | this->argc = 0; |
---|
63 | this->argv = NULL; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | \brief remove Orxonox from memory |
---|
68 | */ |
---|
69 | Orxonox::~Orxonox () |
---|
70 | { |
---|
71 | int i =0; |
---|
72 | Orxonox::singletonRef = NULL; |
---|
73 | if( world != NULL) delete world; |
---|
74 | if( localinput != NULL) delete localinput; |
---|
75 | if( resources != NULL) delete resources; |
---|
76 | delete GraphicsEngine::getInstance(); // deleting the Graphics |
---|
77 | delete ResourceManager::getInstance(); // deletes the Resource Manager |
---|
78 | delete TextEngine::getInstance(); |
---|
79 | } |
---|
80 | |
---|
81 | /** \brief this is a singleton class to prevent duplicates */ |
---|
82 | Orxonox* Orxonox::singletonRef = 0; |
---|
83 | |
---|
84 | /** |
---|
85 | \returns reference or new Object of Orxonox if not existent. |
---|
86 | */ |
---|
87 | Orxonox* Orxonox::getInstance (void) |
---|
88 | { |
---|
89 | if (singletonRef == NULL) |
---|
90 | singletonRef = new Orxonox(); |
---|
91 | return singletonRef; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | \brief this finds the config file |
---|
96 | |
---|
97 | Since the config file varies from user to user and since one may want to specify different config files |
---|
98 | for certain occasions or platforms this function finds the right config file for every occasion and stores |
---|
99 | it's path and name into configfilename |
---|
100 | */ |
---|
101 | void Orxonox::getConfigFile (int argc, char** argv) |
---|
102 | { |
---|
103 | strcpy (configfilename, "~/.orxonox/orxonox.conf"); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | \brief initialize Orxonox with command line |
---|
108 | */ |
---|
109 | int Orxonox::init (int argc, char** argv) |
---|
110 | { |
---|
111 | this->argc = argc; |
---|
112 | this->argv = argv; |
---|
113 | // parse command line |
---|
114 | // config file |
---|
115 | |
---|
116 | getConfigFile (argc, argv); |
---|
117 | SDL_Init (SDL_INIT_TIMER); |
---|
118 | // initialize everything |
---|
119 | printf("> Initializing resources\n"); |
---|
120 | if( initResources () == -1) return -1; |
---|
121 | |
---|
122 | if( initVideo() == -1) return -1; |
---|
123 | if( initSound() == -1) return -1; |
---|
124 | printf("> Initializing input\n"); |
---|
125 | if( initInput() == -1) return -1; |
---|
126 | printf("> Initializing networking\n"); |
---|
127 | if( initNetworking () == -1) return -1; |
---|
128 | //printf("> Initializing world\n"); |
---|
129 | //if( init_world () == -1) return -1; PB: world will be initialized when started |
---|
130 | |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | \brief initializes SDL and OpenGL |
---|
136 | */ |
---|
137 | int Orxonox::initVideo() |
---|
138 | { |
---|
139 | PRINTF(3)("> Initializing video\n"); |
---|
140 | |
---|
141 | GraphicsEngine::getInstance(); |
---|
142 | |
---|
143 | return 0; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | /** |
---|
148 | \brief initializes the sound engine |
---|
149 | */ |
---|
150 | int Orxonox::initSound() |
---|
151 | { |
---|
152 | printf("> Initializing sound\n"); |
---|
153 | // SDL_Init(SDL_INIT_AUDIO); |
---|
154 | SoundEngine::getInstance(); |
---|
155 | return 0; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | \brief initializes input functions |
---|
161 | */ |
---|
162 | int Orxonox::initInput() |
---|
163 | { |
---|
164 | // create localinput |
---|
165 | localinput = new CommandNode(configfilename); |
---|
166 | |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | /** |
---|
172 | \brief initializes network system |
---|
173 | */ |
---|
174 | int Orxonox::initNetworking() |
---|
175 | { |
---|
176 | printf("Not yet implemented\n"); |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /** |
---|
182 | \brief initializes and loads resource files |
---|
183 | */ |
---|
184 | int Orxonox::initResources() |
---|
185 | { |
---|
186 | PRINT(3)("initializing ResourceManager\n"); |
---|
187 | resourceManager = ResourceManager::getInstance(); |
---|
188 | |
---|
189 | // create parser |
---|
190 | IniParser parser (DEFAULT_CONFIG_FILE); |
---|
191 | if( parser.getSection (CONFIG_SECTION_DATA) == -1) |
---|
192 | { |
---|
193 | PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE); |
---|
194 | return -1; |
---|
195 | } |
---|
196 | char namebuf[256]; |
---|
197 | char valuebuf[256]; |
---|
198 | memset (namebuf, 0, 256); |
---|
199 | memset (valuebuf, 0, 256); |
---|
200 | |
---|
201 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
202 | { |
---|
203 | if (!strcmp(namebuf, CONFIG_NAME_DATADIR)) |
---|
204 | { |
---|
205 | // printf("Not yet implemented\n"); |
---|
206 | if (!resourceManager->setDataDir(valuebuf)) |
---|
207 | { |
---|
208 | PRINTF(1)("Data Could not be located\n"); |
---|
209 | exit(-1); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | memset (namebuf, 0, 256); |
---|
214 | memset (valuebuf, 0, 256); |
---|
215 | } |
---|
216 | |
---|
217 | if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
218 | { |
---|
219 | PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n", |
---|
220 | resourceManager->getDataDir(), |
---|
221 | DEFAULT_CONFIG_FILE, |
---|
222 | CONFIG_SECTION_DATA, |
---|
223 | CONFIG_NAME_DATADIR); |
---|
224 | exit(-1); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | PRINT(3)("initializing TextEngine\n"); |
---|
229 | TextEngine::getInstance(); |
---|
230 | |
---|
231 | return 0; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | /** |
---|
236 | \brief initializes the world |
---|
237 | */ |
---|
238 | int Orxonox::initWorld() |
---|
239 | { |
---|
240 | //world = new World(); |
---|
241 | |
---|
242 | // TO DO: replace this with a menu/intro |
---|
243 | //world->load_debug_level(); |
---|
244 | |
---|
245 | return 0; |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | /** |
---|
250 | \brief starts the orxonox game or menu |
---|
251 | |
---|
252 | here is the central orxonox state manager. There are currently two states |
---|
253 | - menu |
---|
254 | - game-play |
---|
255 | both states manage their states themselfs again. |
---|
256 | */ |
---|
257 | void Orxonox::start() |
---|
258 | { |
---|
259 | |
---|
260 | this->gameLoader = GameLoader::getInstance(); |
---|
261 | this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); |
---|
262 | // this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); |
---|
263 | this->gameLoader->init(); |
---|
264 | this->gameLoader->start(); |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | /** |
---|
269 | \brief exits Orxonox |
---|
270 | */ |
---|
271 | void Orxonox::quitGame() |
---|
272 | { |
---|
273 | bQuitOrxonox = true; |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | /** |
---|
279 | \brief handles sprecial events from localinput |
---|
280 | \param event: an event not handled by the CommandNode |
---|
281 | */ |
---|
282 | void Orxonox::eventHandler(SDL_Event* event) |
---|
283 | { |
---|
284 | // Handle special events such as reshape, quit, focus changes |
---|
285 | switch (event->type) |
---|
286 | { |
---|
287 | case SDL_VIDEORESIZE: |
---|
288 | GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); |
---|
289 | tmpGEngine->resolutionChanged(&event->resize); |
---|
290 | break; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | /** |
---|
296 | \brief handle keyboard commands that are not meant for WorldEntities |
---|
297 | \param cmd: the command to handle |
---|
298 | \return true if the command was handled by the system or false if it may be passed to the WorldEntities |
---|
299 | */ |
---|
300 | bool Orxonox::systemCommand(Command* cmd) |
---|
301 | { |
---|
302 | /* |
---|
303 | if( !strcmp( cmd->cmd, "quit")) |
---|
304 | { |
---|
305 | if( !cmd->bUp) this->gameLoader->stop(); |
---|
306 | return true; |
---|
307 | } |
---|
308 | return false; |
---|
309 | */ |
---|
310 | return false; |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | \brief retrieve a pointer to the local CommandNode |
---|
315 | \return a pointer to localinput |
---|
316 | */ |
---|
317 | CommandNode* Orxonox::getLocalInput() |
---|
318 | { |
---|
319 | return localinput; |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | /** |
---|
324 | \brief retrieve a pointer to the local World |
---|
325 | \return a pointer to world |
---|
326 | */ |
---|
327 | World* Orxonox::getWorld() |
---|
328 | { |
---|
329 | return world; |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | \return The reference of the SDL-screen of orxonox |
---|
334 | */ |
---|
335 | SDL_Surface* Orxonox::getScreen () |
---|
336 | { |
---|
337 | return this->screen; |
---|
338 | } |
---|
339 | |
---|
340 | |
---|
341 | bool showGui = false; |
---|
342 | |
---|
343 | /** |
---|
344 | \brief main function |
---|
345 | |
---|
346 | here the journey begins |
---|
347 | */ |
---|
348 | int main(int argc, char** argv) |
---|
349 | { |
---|
350 | |
---|
351 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
352 | int i; |
---|
353 | for(i = 1; i < argc; ++i) |
---|
354 | { |
---|
355 | if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); |
---|
356 | else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); |
---|
357 | else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
358 | // else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); |
---|
359 | } |
---|
360 | |
---|
361 | return startOrxonox(argc, argv); |
---|
362 | } |
---|
363 | |
---|
364 | |
---|
365 | |
---|
366 | int startHelp(int argc, char** argv) |
---|
367 | { |
---|
368 | PRINT(0)("orxonox: starts the orxonox game - rules\n"); |
---|
369 | PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); |
---|
370 | PRINT(0)("valid options:\n"); |
---|
371 | { |
---|
372 | Gui* gui = new Gui(argc, argv); |
---|
373 | gui->printHelp(); |
---|
374 | delete gui; |
---|
375 | } |
---|
376 | PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); |
---|
377 | PRINT(0)(" -h|--help:\t\t\tshows this help\n"); |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | int startOrxonox(int argc, char** argv) |
---|
382 | { |
---|
383 | // checking for existence of the configuration-files |
---|
384 | if (showGui || |
---|
385 | !ResourceManager::isFile("~/.orxonox/orxonox.conf") || |
---|
386 | ResourceManager::isFile("~/.orxonox/orxonox.lock")) |
---|
387 | { |
---|
388 | if (ResourceManager::isFile("~/.orxonox/orxonox.lock")) |
---|
389 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
390 | |
---|
391 | // starting the GUI |
---|
392 | Gui* gui = new Gui(argc, argv); |
---|
393 | gui->startGui(); |
---|
394 | |
---|
395 | if (! gui->startOrxonox) |
---|
396 | return 0; |
---|
397 | |
---|
398 | delete gui; |
---|
399 | } |
---|
400 | |
---|
401 | PRINT(0)(">>> Starting Orxonox <<<\n"); |
---|
402 | |
---|
403 | ResourceManager::touchFile("~/.orxonox/orxonox.lock"); |
---|
404 | |
---|
405 | Orxonox *orx = Orxonox::getInstance(); |
---|
406 | |
---|
407 | if((*orx).init(argc, argv) == -1) |
---|
408 | { |
---|
409 | PRINTF(1)("! Orxonox initialization failed\n"); |
---|
410 | return -1; |
---|
411 | } |
---|
412 | |
---|
413 | orx->start(); |
---|
414 | |
---|
415 | delete orx; |
---|
416 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
417 | |
---|
418 | } |
---|