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