Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2700 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
RevLine 
[1850]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
[1855]20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
[2190]23   co-programmer: Christian Meyer
[1850]24*/
25
[2190]26#include "orxonox.h"
[2036]27#include "world.h"
[2190]28#include "camera.h"
[2036]29#include "data_tank.h"
[2190]30#include "command_node.h"
[2636]31#include "game_loader.h"
[2190]32#include <string.h>
[2036]33
[1803]34using namespace std;
35
[2190]36/**
[2636]37   \brief create a new Orxonox
[2190]38*/
39Orxonox::Orxonox ()
[1872]40{
41  pause = false;
42}
[1803]43
[2190]44/**
[2636]45   \brief remove Orxonox from memory
[2190]46*/
[1875]47Orxonox::~Orxonox () 
[2190]48{
[2636]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;
[2190]54}
[1850]55
56
[2190]57/* this is a singleton class to prevent duplicates */
[1850]58Orxonox* Orxonox::singleton_ref = 0;
[1872]59
[1850]60Orxonox* Orxonox::getInstance (void)
[1803]61{
[1850]62  if (singleton_ref == NULL)
63    singleton_ref = new Orxonox();
64  return singleton_ref;
65}
66
[2190]67/**
[2636]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
[2190]73*/
74void Orxonox::get_config_file (int argc, char** argv)
[1850]75{
[2636]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");
[1803]88}
89
[2190]90/**
[2636]91   \brief initialize Orxonox with command line
[2190]92*/
93int Orxonox::init (int argc, char** argv)
[1803]94{
[2636]95  // parse command line
96  // config file
97 
98  get_config_file (argc, argv);
99 
100  // initialize SDL
[2190]101  printf("> Initializing SDL\n");
102  if( SDL_Init (SDL_INIT_EVERYTHING) == -1)
[2636]103    {
104      printf ("Could not SDL_Init(): %s\n", SDL_GetError());
105      return -1;
106    }
[2190]107 
[2636]108  // initialize everything
[2190]109  printf("> Initializing video\n");
[2636]110  if( init_video () == -1) return -1;
[2190]111  printf("> Initializing sound\n");
[2636]112  if( init_sound () == -1) return -1;
[2190]113  printf("> Initializing input\n");
[2636]114  if( init_input () == -1) return -1;
[2190]115  printf("> Initializing networking\n");
[2636]116  if( init_networking () == -1) return -1;
[2190]117  printf("> Initializing resources\n");
[2636]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;
[1850]123}
[1849]124
[2190]125/**
[2636]126   \brief initializes SDL and OpenGL
[2190]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); 
[1850]155
[2190]156  // OpenGL stuff
157  // (Is this all we initialize globally???)
[1872]158  glClearColor(0.0, 0.0, 0.0, 0.0);
[2190]159  glEnable(GL_DEPTH_TEST);
160  glEnable(GL_COLOR);
161  glShadeModel(GL_FLAT);
[1899]162 
[2190]163  // create camera
[2636]164  localcamera = new Camera(world);
[2190]165 
166  return 0;
167}
[1850]168
[2190]169/**
[2636]170   \brief initializes the sound engine
[2190]171*/
172int Orxonox::init_sound () 
173{
[2636]174  printf("Not yet implemented\n");
175  return 0;
[2190]176}
[1900]177
[2190]178/**
[2636]179   \brief initializes input functions
[2190]180*/
181int Orxonox::init_input () 
182{
[2636]183  // create localinput
184  localinput = new CommandNode( configfilename);
185 
186  return 0;
[1803]187}
188
[2190]189/**
[2636]190   \brief initializes network system
[2190]191*/
192int Orxonox::init_networking () 
[1897]193{
[2636]194  printf("Not yet implemented\n");
195  return 0;
[1897]196}
197
[2190]198/**
[2636]199   \brief initializes and loads resource files
[2190]200*/
201int Orxonox::init_resources () 
[1858]202{
[2636]203  printf("Not yet implemented\n");
204  return 0;
[1858]205}
[1849]206
[2190]207/**
[2636]208   \brief initializes the world
[2190]209*/
210int Orxonox::init_world () 
[1896]211{
[2636]212  //world = new World();
213 
214  // TO DO: replace this with a menu/intro
215  //world->load_debug_level();
216 
217  return 0;
[1896]218}
219
[2636]220
[2190]221/**
[2636]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.
[2190]228*/
[2636]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*/
[1875]241void Orxonox::quitGame() 
242{
[2636]243  bQuitOrxonox = true;
[1875]244}
245
[2190]246/**
[2636]247   \brief this runs all of Orxonox
[2190]248*/
249void Orxonox::mainLoop()
250{
[2551]251  lastframe = SDL_GetTicks();
252  bQuitOrxonox = false;
[2190]253  // This is where everything is run
[2551]254  printf("Orxonox|Entering main loop\n");
[2190]255  while( !bQuitOrxonox)
[2551]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");
[1875]269}
270
[2190]271/**
[2636]272   \brief handles sprecial events from localinput
273   \param event: an event not handled by the CommandNode
[2190]274*/
275void Orxonox::event_handler (SDL_Event* event)
276{
[2636]277  // Handle special events such as reshape, quit, focus changes
[2190]278}
[1875]279
[2190]280/**
[2636]281   \brief synchronize local data with remote data
[1872]282*/
[2190]283void Orxonox::synchronize ()
[1859]284{
[2636]285  // Get remote input
286  // Update synchronizables
[1859]287}
288
[2190]289/**
[2636]290   \brief run all input processing
[2190]291*/
292void Orxonox::handle_input ()
293{
[2636]294  // localinput
295  localinput->process();
296  // remoteinput
[2190]297}
[1859]298
[2190]299/**
[2636]300   \brief advance the timeline
[2190]301*/
302void Orxonox::time_slice ()
[1803]303{
[2551]304  Uint32 curframe = SDL_GetTicks();
305  if( !pause)
306    {
307      Uint32 dt = curframe - lastframe;
308     
309      if(dt > 0)
[2190]310        {
[2551]311          float fps = 1000/dt;
[2636]312          printf("fps = %f\n", fps);
[2190]313        }
[2551]314     
315      world->time_slice (dt);
316      world->update ();
317      localcamera->time_slice (dt);
318    }
319  lastframe = curframe;
[2190]320}
[1879]321
[2190]322/**
[2636]323   \brief compute collision detection
[2190]324*/
325void Orxonox::collision ()
326{
[2636]327  world->collide ();
[2190]328}
[1879]329
[2190]330/**
[2636]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
[2190]334*/
335bool Orxonox::system_command (Command* cmd)
336{
[2636]337  if( !strcmp( cmd->cmd, "quit"))
338    {
339      if( !cmd->bUp) this->gameLoader->stop();
340      return true;
341    }
342  return false;
[2190]343}
[1803]344
[2190]345/**
[2636]346   \brief render the current frame
[2190]347*/
348void Orxonox::display ()
349{
[2636]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();
[2190]359}
[1850]360
[2190]361/**
[2636]362   \brief retrieve a pointer to the local Camera
363   \return a pointer to localcamera
[2190]364*/
365Camera* Orxonox::get_camera ()
[1872]366{
[2636]367  return localcamera;
[1872]368}
[1850]369
[2190]370/**
[2636]371   \brief retrieve a pointer to the local CommandNode
372   \return a pointer to localinput
[2190]373*/
374CommandNode* Orxonox::get_localinput ()
[1850]375{
[2636]376  return localinput;
[1803]377}
378
[2190]379/**
[2636]380   \brief retrieve a pointer to the local World
381   \return a pointer to world
[2190]382*/
383World* Orxonox::get_world ()
[1872]384{
[2636]385  return world;
[1872]386}
[1850]387
388int main (int argc, char** argv) 
[1803]389{ 
[2636]390  printf(">>> Starting Orxonox <<<\n");
[1850]391  Orxonox *orx = Orxonox::getInstance();
[2190]392 
393  if( (*orx).init(argc, argv) == -1)
[2636]394    {
395      printf("! Orxonox initialization failed\n");
396      return -1;
397    }
398 
399  //(*orx).mainLoop();
[1856]400
[2636]401  orx->start();
402 
[2190]403  //delete orx;
404 
[1803]405  return 0;
406}
Note: See TracBrowser for help on using the repository browser.