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