Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/orxonox.cc @ 2738

Last change on this file since 2738 was 2636, checked in by patrick, 20 years ago
  • Added a GameLoader to the game. This enables orxonox to load a campaign consisting of multimple worlds and cinematics etc. However, cinematics are not yet implemented.

In the game you can jump from one level to the other by pressing x. Currently there are only two very simple levels defined. (DEBUG_LEVEL_0, DEBUG_LEVEL_1).

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