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 | GraphicsEngine::getInstance()->loadFromIniFile(this->iniParser, CONFIG_SECTION_VIDEO); |
---|
156 | |
---|
157 | return 0; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | \brief initializes the sound engine |
---|
162 | */ |
---|
163 | int Orxonox::initSound() |
---|
164 | { |
---|
165 | PRINT(3)("> Initializing sound\n"); |
---|
166 | // SDL_Init(SDL_INIT_AUDIO); |
---|
167 | SoundEngine::getInstance()->initAudio(); |
---|
168 | return 0; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | * @brief initializes input functions |
---|
174 | */ |
---|
175 | int Orxonox::initInput() |
---|
176 | { |
---|
177 | PRINT(3)("> Initializing input\n"); |
---|
178 | |
---|
179 | this->eventHandler = EventHandler::getInstance(); |
---|
180 | this->eventHandler->init(); |
---|
181 | |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | /** |
---|
187 | * @brief initializes network system |
---|
188 | */ |
---|
189 | int Orxonox::initNetworking() |
---|
190 | { |
---|
191 | PRINT(3)("> Initializing networking\n"); |
---|
192 | |
---|
193 | printf(" ---Not yet implemented-FIXME--\n"); |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | /** |
---|
199 | * @brief initializes and loads resource files |
---|
200 | */ |
---|
201 | int Orxonox::initResources() |
---|
202 | { |
---|
203 | PRINTF(3)("> Initializing resources\n"); |
---|
204 | |
---|
205 | PRINT(3)("initializing ResourceManager\n"); |
---|
206 | resourceManager = ResourceManager::getInstance(); |
---|
207 | |
---|
208 | // create parser |
---|
209 | if( this->iniParser->getSection (CONFIG_SECTION_DATA) == -1) |
---|
210 | { |
---|
211 | PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE); |
---|
212 | return -1; |
---|
213 | } |
---|
214 | char namebuf[256]; |
---|
215 | char valuebuf[256]; |
---|
216 | memset (namebuf, 0, 256); |
---|
217 | memset (valuebuf, 0, 256); |
---|
218 | |
---|
219 | while( this->iniParser->nextVar (namebuf, valuebuf) != -1) |
---|
220 | { |
---|
221 | if (!strcmp(namebuf, CONFIG_NAME_DATADIR)) |
---|
222 | { |
---|
223 | // printf("Not yet implemented\n"); |
---|
224 | if (!resourceManager->setDataDir(valuebuf)) |
---|
225 | { |
---|
226 | PRINTF(1)("Data Could not be located\n"); |
---|
227 | exit(-1); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | memset (namebuf, 0, 256); |
---|
232 | memset (valuebuf, 0, 256); |
---|
233 | } |
---|
234 | |
---|
235 | if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
236 | { |
---|
237 | PRINTF(1)("The DataDirectory %s could not be verified\n" \ |
---|
238 | " Please Change in File %s Section %s Entry %s to a suitable value\n", |
---|
239 | resourceManager->getDataDir(), |
---|
240 | DEFAULT_CONFIG_FILE, |
---|
241 | CONFIG_SECTION_DATA, |
---|
242 | CONFIG_NAME_DATADIR); |
---|
243 | exit(-1); |
---|
244 | } |
---|
245 | //! \todo this is a hack and should be loadable |
---|
246 | resourceManager->addImageDir(ResourceManager::getInstance()->getFullName("maps/")); |
---|
247 | resourceManager->debug(); |
---|
248 | |
---|
249 | PRINT(3)("initializing TextEngine\n"); |
---|
250 | TextEngine::getInstance(); |
---|
251 | |
---|
252 | PRINT(3)("initializing ObjectManager\n"); |
---|
253 | this->objectManager = ObjectManager::getInstance(); |
---|
254 | |
---|
255 | CDEngine::getInstance(); |
---|
256 | |
---|
257 | return 0; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | \brief starts the orxonox game or menu |
---|
262 | |
---|
263 | here is the central orxonox state manager. There are currently two states |
---|
264 | - menu |
---|
265 | - game-play |
---|
266 | both states manage their states themselfs again. |
---|
267 | */ |
---|
268 | void Orxonox::start() |
---|
269 | { |
---|
270 | |
---|
271 | this->gameLoader = GameLoader::getInstance(); |
---|
272 | this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); |
---|
273 | // this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); |
---|
274 | this->gameLoader->init(); |
---|
275 | this->gameLoader->start(); |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /** |
---|
280 | \brief handles sprecial events from localinput |
---|
281 | \param event: an event not handled by the CommandNode |
---|
282 | */ |
---|
283 | void Orxonox::graphicsHandler(SDL_Event* event) |
---|
284 | { |
---|
285 | // Handle special events such as reshape, quit, focus changes |
---|
286 | switch (event->type) |
---|
287 | { |
---|
288 | case SDL_VIDEORESIZE: |
---|
289 | GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); |
---|
290 | tmpGEngine->resolutionChanged(&event->resize); |
---|
291 | break; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | /** |
---|
297 | \brief processes the events for orxonox main class |
---|
298 | \param the event to handle |
---|
299 | */ |
---|
300 | void Orxonox::process(const Event &event) |
---|
301 | {} |
---|
302 | |
---|
303 | |
---|
304 | |
---|
305 | bool showGui = false; |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | /********************************** |
---|
310 | *** ORXONOX MAIN STARTING POINT *** |
---|
311 | **********************************/ |
---|
312 | /** |
---|
313 | \brief main function |
---|
314 | |
---|
315 | here the journey begins |
---|
316 | */ |
---|
317 | int main(int argc, char** argv) |
---|
318 | { |
---|
319 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
320 | int i; |
---|
321 | for(i = 1; i < argc; ++i) |
---|
322 | { |
---|
323 | if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); |
---|
324 | else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); |
---|
325 | else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
326 | // else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); |
---|
327 | } |
---|
328 | |
---|
329 | return startOrxonox(argc, argv); |
---|
330 | } |
---|
331 | |
---|
332 | |
---|
333 | |
---|
334 | int startHelp(int argc, char** argv) |
---|
335 | { |
---|
336 | PRINT(0)("orxonox: starts the orxonox game - rules\n"); |
---|
337 | PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); |
---|
338 | PRINT(0)("valid options:\n"); |
---|
339 | { |
---|
340 | Gui* gui = new Gui(argc, argv); |
---|
341 | gui->printHelp(); |
---|
342 | delete gui; |
---|
343 | } |
---|
344 | PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); |
---|
345 | PRINT(0)(" -h|--help:\t\t\tshows this help\n"); |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | |
---|
350 | /** |
---|
351 | * starts orxonox |
---|
352 | * @param argc parameters count given to orxonox |
---|
353 | * @param argv parameters given to orxonox |
---|
354 | */ |
---|
355 | int startOrxonox(int argc, char** argv) |
---|
356 | { |
---|
357 | // checking for existence of the configuration-files |
---|
358 | if (showGui || |
---|
359 | !ResourceManager::isFile(DEFAULT_CONFIG_FILE) || |
---|
360 | ResourceManager::isFile(DEFAULT_LOCK_FILE)) |
---|
361 | { |
---|
362 | if (ResourceManager::isFile(DEFAULT_LOCK_FILE)) |
---|
363 | ResourceManager::deleteFile(DEFAULT_LOCK_FILE); |
---|
364 | |
---|
365 | // starting the GUI |
---|
366 | Gui* gui = new Gui(argc, argv); |
---|
367 | gui->startGui(); |
---|
368 | |
---|
369 | if (! gui->startOrxonox) |
---|
370 | return 0; |
---|
371 | |
---|
372 | delete gui; |
---|
373 | } |
---|
374 | |
---|
375 | PRINT(0)(">>> Starting Orxonox <<<\n"); |
---|
376 | |
---|
377 | ResourceManager::touchFile(DEFAULT_LOCK_FILE); |
---|
378 | |
---|
379 | Orxonox *orx = Orxonox::getInstance(); |
---|
380 | |
---|
381 | if((*orx).init(argc, argv) == -1) |
---|
382 | { |
---|
383 | PRINTF(1)("! Orxonox initialization failed\n"); |
---|
384 | return -1; |
---|
385 | } |
---|
386 | |
---|
387 | orx->start(); |
---|
388 | |
---|
389 | delete orx; |
---|
390 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
391 | |
---|
392 | } |
---|