Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3200 was 3174, checked in by bensch, 20 years ago

orxonox/trunk: SDL_Init - fix. it's better that way

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  SDL_Init (SDL_INIT_TIMER);
100  // initialize everything
101  if( init_video () == -1) return -1;
102  if( init_sound () == -1) return -1;
103  printf("> Initializing input\n");
104  if( init_input () == -1) return -1;
105  printf("> Initializing networking\n");
106  if( init_networking () == -1) return -1;
107  printf("> Initializing resources\n");
108  if( init_resources () == -1) return -1;
109  //printf("> Initializing world\n");
110  //if( init_world () == -1) return -1; PB: world will be initialized when started
111 
112  return 0;
113}
114
115/**
116   \brief initializes SDL and OpenGL
117*/
118int Orxonox::init_video () 
119{
120  printf("> Initializing video\n");
121  if (SDL_Init (SDL_INIT_VIDEO) == -1)
122    {
123      printf ("could not initialize SDL Video\n");
124      return -1;
125    }
126  // Set video mode
127  // TO DO: parse arguments for settings
128  SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
129  SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
130  SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
131  SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
132 
133  int bpp = 16;
134  int width = 640;
135  int height = 480;
136  Uint32 flags = SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
137 
138  if( (screen = SDL_SetVideoMode (width, height, bpp, flags)) == NULL)
139  {
140    printf ("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", width, height, bpp, flags, SDL_GetError());
141    SDL_Quit();
142    return -1;
143  }
144 
145  // Set window labeling
146  SDL_WM_SetCaption( "Orxonox " PACKAGE_VERSION, "Orxonox " PACKAGE_VERSION);
147 
148  // TO DO: Create a cool icon and use it here
149  // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask); 
150
151  // OpenGL stuff
152  // (Is this all we initialize globally???)
153  glClearColor(0.0, 0.0, 0.0, 0.0);
154  glEnable(GL_DEPTH_TEST);
155 
156  // LIGHTING
157  GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0};
158  GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0};
159  GLfloat lightPosition[] = {10.0, 10, 19.0, 0.0};
160
161  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
162  glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight);
163  glEnable(GL_LIGHTING);
164  glEnable(GL_LIGHT0);
165  glEnable(GL_DEPTH_TEST);
166  glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
167  glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
168   
169  //  glEnable(GL_COLOR);
170  //  glShadeModel(GL_SMOOTH);
171 
172  // create camera
173  localcamera = new Camera(world);
174 
175  return 0;
176}
177
178/**
179   \brief initializes the sound engine
180*/
181int Orxonox::init_sound () 
182{
183  printf("> Initializing sound\n");
184  // SDL_Init (SDL_INIT_AUDIO);
185  printf("Not yet implemented\n");
186  return 0;
187}
188
189/**
190   \brief initializes input functions
191*/
192int Orxonox::init_input () 
193{
194  // create localinput
195  localinput = new CommandNode( configfilename);
196 
197  return 0;
198}
199
200/**
201   \brief initializes network system
202*/
203int Orxonox::init_networking () 
204{
205  printf("Not yet implemented\n");
206  return 0;
207}
208
209/**
210   \brief initializes and loads resource files
211*/
212int Orxonox::init_resources () 
213{
214  printf("Not yet implemented\n");
215  return 0;
216}
217
218/**
219   \brief initializes the world
220*/
221int Orxonox::init_world () 
222{
223  //world = new World();
224 
225  // TO DO: replace this with a menu/intro
226  //world->load_debug_level();
227 
228  return 0;
229}
230
231
232/**
233   \brief starts the orxonox game or menu
234
235   here is the central orxonox state manager. There are currently two states
236   - menu
237   - game-play
238   both states manage their states themselfs again.
239*/
240void Orxonox::start()
241{
242 
243  this->gameLoader = GameLoader::getInstance();
244  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
245  this->gameLoader->init();
246  this->gameLoader->start();
247}
248
249/**
250   \brief exits Orxonox
251*/
252void Orxonox::quitGame() 
253{
254  bQuitOrxonox = true;
255}
256
257/**
258   \brief this runs all of Orxonox
259*/
260void Orxonox::mainLoop()
261{
262  lastframe = SDL_GetTicks();
263  bQuitOrxonox = false;
264  // This is where everything is run
265  printf("Orxonox|Entering main loop\n");
266  while( !bQuitOrxonox)
267    {
268      // Network
269      synchronize();
270      // Process input
271      handle_input();
272      // Process time
273      time_slice();
274      // Process collision
275      collision();
276      // Draw
277      display();
278    }
279  printf("Orxonox|Exiting the main loop\n");
280}
281
282/**
283   \brief handles sprecial events from localinput
284   \param event: an event not handled by the CommandNode
285*/
286void Orxonox::event_handler (SDL_Event* event)
287{
288  // Handle special events such as reshape, quit, focus changes
289}
290
291/**
292   \brief synchronize local data with remote data
293*/
294void Orxonox::synchronize ()
295{
296  // Get remote input
297  // Update synchronizables
298}
299
300/**
301   \brief run all input processing
302*/
303void Orxonox::handle_input ()
304{
305  // localinput
306  localinput->process();
307  // remoteinput
308}
309
310/**
311   \brief advance the timeline
312*/
313void Orxonox::time_slice ()
314{
315  Uint32 curframe = SDL_GetTicks();
316  if( !pause)
317    {
318      Uint32 dt = curframe - lastframe;
319     
320      if(dt > 0)
321        {
322          float fps = 1000/dt;
323          printf("fps = %f\n", fps);
324        }
325     
326      world->time_slice (dt);
327      world->update ();
328      localcamera->time_slice (dt);
329    }
330  lastframe = curframe;
331}
332
333/**
334   \brief compute collision detection
335*/
336void Orxonox::collision ()
337{
338  world->collide ();
339}
340
341/**
342   \brief handle keyboard commands that are not meant for WorldEntities
343   \param cmd: the command to handle
344   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
345*/
346bool Orxonox::system_command (Command* cmd)
347{
348  if( !strcmp( cmd->cmd, "quit"))
349    {
350      if( !cmd->bUp) this->gameLoader->stop();
351      return true;
352    }
353  return false;
354}
355
356/**
357   \brief render the current frame
358*/
359void Orxonox::display ()
360{
361  // clear buffer
362  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
363  // set camera
364  localcamera->apply ();
365  // draw world
366  world->draw ();
367  // draw HUD
368  // flip buffers
369  SDL_GL_SwapBuffers();
370}
371
372/**
373   \brief retrieve a pointer to the local Camera
374   \return a pointer to localcamera
375*/
376Camera* Orxonox::get_camera ()
377{
378  return localcamera;
379}
380
381/**
382   \brief retrieve a pointer to the local CommandNode
383   \return a pointer to localinput
384*/
385CommandNode* Orxonox::get_localinput ()
386{
387  return localinput;
388}
389
390/**
391   \brief retrieve a pointer to the local World
392   \return a pointer to world
393*/
394World* Orxonox::get_world ()
395{
396  return world;
397}
398
399int main (int argc, char** argv) 
400{ 
401  printf(">>> Starting Orxonox <<<\n");
402  Orxonox *orx = Orxonox::getInstance();
403 
404  if( (*orx).init(argc, argv) == -1)
405    {
406      printf("! Orxonox initialization failed\n");
407      return -1;
408    }
409 
410  //(*orx).mainLoop();
411
412  orx->start();
413 
414  //delete orx;
415 
416  return 0;
417}
Note: See TracBrowser for help on using the repository browser.