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 | */ |
---|
25 | |
---|
26 | #include "orxonox.h" |
---|
27 | #include "world.h" |
---|
28 | #include "camera.h" |
---|
29 | #include "data_tank.h" |
---|
30 | #include "command_node.h" |
---|
31 | #include <string.h> |
---|
32 | |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | /** |
---|
36 | \brief create a new Orxonox |
---|
37 | */ |
---|
38 | Orxonox::Orxonox () |
---|
39 | { |
---|
40 | pause = false; |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | \brief remove Orxonox from memory |
---|
45 | */ |
---|
46 | Orxonox::~Orxonox () |
---|
47 | { |
---|
48 | Orxonox::singleton_ref = NULL; |
---|
49 | if( world != NULL) delete world; |
---|
50 | if( localinput != NULL) delete world; |
---|
51 | if( localcamera != NULL) delete localcamera; |
---|
52 | if( resources != NULL) delete resources; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /* this is a singleton class to prevent duplicates */ |
---|
57 | Orxonox* Orxonox::singleton_ref = 0; |
---|
58 | |
---|
59 | Orxonox* Orxonox::getInstance (void) |
---|
60 | { |
---|
61 | if (singleton_ref == NULL) |
---|
62 | singleton_ref = new Orxonox(); |
---|
63 | return singleton_ref; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | \brief this finds the config file |
---|
68 | |
---|
69 | Since the config file varies from user to user and since one may want to specify different config files |
---|
70 | for certain occasions or platforms this function finds the right config file for every occasion and stores |
---|
71 | it's path and name into configfilename |
---|
72 | */ |
---|
73 | void Orxonox::get_config_file (int argc, char** argv) |
---|
74 | { |
---|
75 | /* char* path; |
---|
76 | #ifdef __WIN32__ |
---|
77 | path = getenv(""); |
---|
78 | #else |
---|
79 | path = getenv("HOME"); |
---|
80 | #endif |
---|
81 | |
---|
82 | if( path != NULL) strcpy (configfilename, path); |
---|
83 | else strcpy (configfilename, "./"); |
---|
84 | strcat (configfilename, "/.orxonox.conf");*/ |
---|
85 | |
---|
86 | strcpy (configfilename, "orxonox.conf"); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | \brief initialize Orxonox with command line |
---|
91 | */ |
---|
92 | int Orxonox::init (int argc, char** argv) |
---|
93 | { |
---|
94 | // parse command line |
---|
95 | // config file |
---|
96 | |
---|
97 | get_config_file (argc, argv); |
---|
98 | |
---|
99 | // initialize SDL |
---|
100 | printf("> Initializing SDL\n"); |
---|
101 | if( SDL_Init (SDL_INIT_EVERYTHING) == -1) |
---|
102 | { |
---|
103 | printf ("Could not SDL_Init(): %s\n", SDL_GetError()); |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | // initialize everything |
---|
108 | printf("> Initializing video\n"); |
---|
109 | if( init_video () == -1) return -1; |
---|
110 | printf("> Initializing sound\n"); |
---|
111 | if( init_sound () == -1) return -1; |
---|
112 | printf("> Initializing input\n"); |
---|
113 | if( init_input () == -1) return -1; |
---|
114 | printf("> Initializing networking\n"); |
---|
115 | if( init_networking () == -1) return -1; |
---|
116 | printf("> Initializing resources\n"); |
---|
117 | if( init_resources () == -1) return -1; |
---|
118 | printf("> Initializing world\n"); |
---|
119 | if( init_world () == -1) return -1; |
---|
120 | |
---|
121 | return 0; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | \brief initializes SDL and OpenGL |
---|
126 | */ |
---|
127 | int Orxonox::init_video () |
---|
128 | { |
---|
129 | // Set video mode |
---|
130 | // TO DO: parse arguments for settings |
---|
131 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); |
---|
132 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); |
---|
133 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); |
---|
134 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
---|
135 | |
---|
136 | int bpp = 16; |
---|
137 | int width = 640; |
---|
138 | int height = 480; |
---|
139 | Uint32 flags = SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER; |
---|
140 | |
---|
141 | if( (screen = SDL_SetVideoMode (width, height, bpp, flags)) == NULL) |
---|
142 | { |
---|
143 | printf ("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", width, height, bpp, flags, SDL_GetError()); |
---|
144 | SDL_Quit(); |
---|
145 | return -1; |
---|
146 | } |
---|
147 | |
---|
148 | // Set window labeling |
---|
149 | // TO DO: Add version information to caption |
---|
150 | SDL_WM_SetCaption( "Orxonox", "Orxonox"); |
---|
151 | |
---|
152 | // TO DO: Create a cool icon and use it here |
---|
153 | // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask); |
---|
154 | |
---|
155 | // OpenGL stuff |
---|
156 | // (Is this all we initialize globally???) |
---|
157 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
158 | glEnable(GL_DEPTH_TEST); |
---|
159 | glEnable(GL_COLOR); |
---|
160 | glShadeModel(GL_FLAT); |
---|
161 | |
---|
162 | // create camera |
---|
163 | localcamera = new Camera(); |
---|
164 | |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | \brief initializes the sound engine |
---|
170 | */ |
---|
171 | int Orxonox::init_sound () |
---|
172 | { |
---|
173 | printf("Not yet implemented\n"); |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | \brief initializes input functions |
---|
179 | */ |
---|
180 | int Orxonox::init_input () |
---|
181 | { |
---|
182 | // create localinput |
---|
183 | localinput = new CommandNode( configfilename); |
---|
184 | |
---|
185 | return 0; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | \brief initializes network system |
---|
190 | */ |
---|
191 | int Orxonox::init_networking () |
---|
192 | { |
---|
193 | printf("Not yet implemented\n"); |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | \brief initializes and loads resource files |
---|
199 | */ |
---|
200 | int Orxonox::init_resources () |
---|
201 | { |
---|
202 | printf("Not yet implemented\n"); |
---|
203 | return 0; |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | \brief initializes the world |
---|
208 | */ |
---|
209 | int Orxonox::init_world () |
---|
210 | { |
---|
211 | world = new World(); |
---|
212 | |
---|
213 | // TO DO: replace this with a menu/intro |
---|
214 | world->load_debug_level(); |
---|
215 | |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | \brief exits Orxonox |
---|
221 | */ |
---|
222 | void Orxonox::quitGame() |
---|
223 | { |
---|
224 | bQuitOrxonox = true; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | \brief this runs all of Orxonox |
---|
229 | */ |
---|
230 | void Orxonox::mainLoop() |
---|
231 | { |
---|
232 | lastframe = SDL_GetTicks(); |
---|
233 | bQuitOrxonox = false; |
---|
234 | // This is where everything is run |
---|
235 | printf("Orxonox|Entering main loop\n"); |
---|
236 | while( !bQuitOrxonox) |
---|
237 | { |
---|
238 | // Network |
---|
239 | synchronize(); |
---|
240 | // Process input |
---|
241 | handle_input(); |
---|
242 | // Process time |
---|
243 | time_slice(); |
---|
244 | // Process collision |
---|
245 | collision(); |
---|
246 | // Draw |
---|
247 | display(); |
---|
248 | } |
---|
249 | printf("Orxonox|Exiting the main loop\n"); |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | \brief handles sprecial events from localinput |
---|
254 | \param event: an event not handled by the CommandNode |
---|
255 | */ |
---|
256 | void Orxonox::event_handler (SDL_Event* event) |
---|
257 | { |
---|
258 | // Handle special events such as reshape, quit, focus changes |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | \brief synchronize local data with remote data |
---|
263 | */ |
---|
264 | void Orxonox::synchronize () |
---|
265 | { |
---|
266 | // Get remote input |
---|
267 | // Update synchronizables |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | \brief run all input processing |
---|
272 | */ |
---|
273 | void Orxonox::handle_input () |
---|
274 | { |
---|
275 | // localinput |
---|
276 | localinput->process(); |
---|
277 | // remoteinput |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | \brief advance the timeline |
---|
282 | */ |
---|
283 | void Orxonox::time_slice () |
---|
284 | { |
---|
285 | Uint32 curframe = SDL_GetTicks(); |
---|
286 | if( !pause) |
---|
287 | { |
---|
288 | world->time_slice (curframe - lastframe); |
---|
289 | world->update (); |
---|
290 | localcamera->time_slice (curframe - lastframe); |
---|
291 | } |
---|
292 | lastframe = curframe; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | \brief compute collision detection |
---|
297 | */ |
---|
298 | void Orxonox::collision () |
---|
299 | { |
---|
300 | world->collide (); |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | \brief handle keyboard commands that are not meant for WorldEntities |
---|
305 | \param cmd: the command to handle |
---|
306 | \return true if the command was handled by the system or false if it may be passed to the WorldEntities |
---|
307 | */ |
---|
308 | bool Orxonox::system_command (Command* cmd) |
---|
309 | { |
---|
310 | if( !strcmp( cmd->cmd, "quit")) |
---|
311 | { |
---|
312 | if( !cmd->bUp) bQuitOrxonox = true; |
---|
313 | return true; |
---|
314 | } |
---|
315 | return false; |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | \brief render the current frame |
---|
320 | */ |
---|
321 | void Orxonox::display () |
---|
322 | { |
---|
323 | // clear buffer |
---|
324 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
325 | // set camera |
---|
326 | localcamera->apply (); |
---|
327 | // draw world |
---|
328 | world->draw (); |
---|
329 | // draw HUD |
---|
330 | // flip buffers |
---|
331 | SDL_GL_SwapBuffers(); |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | \brief retrieve a pointer to the local Camera |
---|
336 | \return a pointer to localcamera |
---|
337 | */ |
---|
338 | Camera* Orxonox::get_camera () |
---|
339 | { |
---|
340 | return localcamera; |
---|
341 | } |
---|
342 | |
---|
343 | /** |
---|
344 | \brief retrieve a pointer to the local CommandNode |
---|
345 | \return a pointer to localinput |
---|
346 | */ |
---|
347 | CommandNode* Orxonox::get_localinput () |
---|
348 | { |
---|
349 | return localinput; |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | \brief retrieve a pointer to the local World |
---|
354 | \return a pointer to world |
---|
355 | */ |
---|
356 | World* Orxonox::get_world () |
---|
357 | { |
---|
358 | return world; |
---|
359 | } |
---|
360 | |
---|
361 | int main (int argc, char** argv) |
---|
362 | { |
---|
363 | printf(">>> Starting Orxonox <<<\n"); |
---|
364 | Orxonox *orx = Orxonox::getInstance(); |
---|
365 | |
---|
366 | if( (*orx).init(argc, argv) == -1) |
---|
367 | { |
---|
368 | printf("! Orxonox initialization failed\n"); |
---|
369 | return -1; |
---|
370 | } |
---|
371 | |
---|
372 | (*orx).mainLoop(); |
---|
373 | |
---|
374 | //delete orx; |
---|
375 | |
---|
376 | return 0; |
---|
377 | } |
---|