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