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