Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3154 was 3022, checked in by bensch, 20 years ago

orxonox/trunk/src: Version-information added to captions

File size: 8.7 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  SDL_WM_SetCaption( "Orxonox " PACKAGE_VERSION, "Orxonox " PACKAGE_VERSION);
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 
160  // LIGHTING
161  GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0};
162  GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0};
163  GLfloat lightPosition[] = {10.0, 10, 19.0, 0.0};
164
165  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
166  glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight);
167  glEnable(GL_LIGHTING);
168  glEnable(GL_LIGHT0);
169  glEnable(GL_DEPTH_TEST);
170  glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
171  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
172   
173  //  glEnable(GL_COLOR);
174  //  glShadeModel(GL_SMOOTH);
175 
176  // create camera
177  localcamera = new Camera(world);
178 
179  return 0;
180}
181
182/**
183   \brief initializes the sound engine
184*/
185int Orxonox::init_sound () 
186{
187  printf("Not yet implemented\n");
188  return 0;
189}
190
191/**
192   \brief initializes input functions
193*/
194int Orxonox::init_input () 
195{
196  // create localinput
197  localinput = new CommandNode( configfilename);
198 
199  return 0;
200}
201
202/**
203   \brief initializes network system
204*/
205int Orxonox::init_networking () 
206{
207  printf("Not yet implemented\n");
208  return 0;
209}
210
211/**
212   \brief initializes and loads resource files
213*/
214int Orxonox::init_resources () 
215{
216  printf("Not yet implemented\n");
217  return 0;
218}
219
220/**
221   \brief initializes the world
222*/
223int Orxonox::init_world () 
224{
225  //world = new World();
226 
227  // TO DO: replace this with a menu/intro
228  //world->load_debug_level();
229 
230  return 0;
231}
232
233
234/**
235   \brief starts the orxonox game or menu
236
237   here is the central orxonox state manager. There are currently two states
238   - menu
239   - game-play
240   both states manage their states themselfs again.
241*/
242void Orxonox::start()
243{
244 
245  this->gameLoader = GameLoader::getInstance();
246  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
247  this->gameLoader->init();
248  this->gameLoader->start();
249}
250
251/**
252   \brief exits Orxonox
253*/
254void Orxonox::quitGame() 
255{
256  bQuitOrxonox = true;
257}
258
259/**
260   \brief this runs all of Orxonox
261*/
262void Orxonox::mainLoop()
263{
264  lastframe = SDL_GetTicks();
265  bQuitOrxonox = false;
266  // This is where everything is run
267  printf("Orxonox|Entering main loop\n");
268  while( !bQuitOrxonox)
269    {
270      // Network
271      synchronize();
272      // Process input
273      handle_input();
274      // Process time
275      time_slice();
276      // Process collision
277      collision();
278      // Draw
279      display();
280    }
281  printf("Orxonox|Exiting the main loop\n");
282}
283
284/**
285   \brief handles sprecial events from localinput
286   \param event: an event not handled by the CommandNode
287*/
288void Orxonox::event_handler (SDL_Event* event)
289{
290  // Handle special events such as reshape, quit, focus changes
291}
292
293/**
294   \brief synchronize local data with remote data
295*/
296void Orxonox::synchronize ()
297{
298  // Get remote input
299  // Update synchronizables
300}
301
302/**
303   \brief run all input processing
304*/
305void Orxonox::handle_input ()
306{
307  // localinput
308  localinput->process();
309  // remoteinput
310}
311
312/**
313   \brief advance the timeline
314*/
315void Orxonox::time_slice ()
316{
317  Uint32 curframe = SDL_GetTicks();
318  if( !pause)
319    {
320      Uint32 dt = curframe - lastframe;
321     
322      if(dt > 0)
323        {
324          float fps = 1000/dt;
325          printf("fps = %f\n", fps);
326        }
327     
328      world->time_slice (dt);
329      world->update ();
330      localcamera->time_slice (dt);
331    }
332  lastframe = curframe;
333}
334
335/**
336   \brief compute collision detection
337*/
338void Orxonox::collision ()
339{
340  world->collide ();
341}
342
343/**
344   \brief handle keyboard commands that are not meant for WorldEntities
345   \param cmd: the command to handle
346   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
347*/
348bool Orxonox::system_command (Command* cmd)
349{
350  if( !strcmp( cmd->cmd, "quit"))
351    {
352      if( !cmd->bUp) this->gameLoader->stop();
353      return true;
354    }
355  return false;
356}
357
358/**
359   \brief render the current frame
360*/
361void Orxonox::display ()
362{
363  // clear buffer
364  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
365  // set camera
366  localcamera->apply ();
367  // draw world
368  world->draw ();
369  // draw HUD
370  // flip buffers
371  SDL_GL_SwapBuffers();
372}
373
374/**
375   \brief retrieve a pointer to the local Camera
376   \return a pointer to localcamera
377*/
378Camera* Orxonox::get_camera ()
379{
380  return localcamera;
381}
382
383/**
384   \brief retrieve a pointer to the local CommandNode
385   \return a pointer to localinput
386*/
387CommandNode* Orxonox::get_localinput ()
388{
389  return localinput;
390}
391
392/**
393   \brief retrieve a pointer to the local World
394   \return a pointer to world
395*/
396World* Orxonox::get_world ()
397{
398  return world;
399}
400
401int main (int argc, char** argv) 
402{ 
403  printf(">>> Starting Orxonox <<<\n");
404  Orxonox *orx = Orxonox::getInstance();
405 
406  if( (*orx).init(argc, argv) == -1)
407    {
408      printf("! Orxonox initialization failed\n");
409      return -1;
410    }
411 
412  //(*orx).mainLoop();
413
414  orx->start();
415 
416  //delete orx;
417 
418  return 0;
419}
Note: See TracBrowser for help on using the repository browser.