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