Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2614 was 2551, checked in by patrick, 20 years ago

orxonox/trunk: minor changes - enhanced sc controll, fixed uncontrolled rotation effect, added some debug outputs for testing purposes, reformatted some src files from win style but not all

File size: 7.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 <string.h>
32
33using namespace std;
34
35/**
36        \brief create a new Orxonox
37*/
38Orxonox::Orxonox ()
39{
40  pause = false;
41}
42
43/**
44        \brief remove Orxonox from memory
45*/
46Orxonox::~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 */
57Orxonox* Orxonox::singleton_ref = 0;
58
59Orxonox* 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*/
73void 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*/
92int 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*/
127int 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*/
171int Orxonox::init_sound () 
172{
173        printf("Not yet implemented\n");
174        return 0;
175}
176
177/**
178        \brief initializes input functions
179*/
180int 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*/
191int Orxonox::init_networking () 
192{
193        printf("Not yet implemented\n");
194        return 0;
195}
196
197/**
198        \brief initializes and loads resource files
199*/
200int Orxonox::init_resources () 
201{
202        printf("Not yet implemented\n");
203        return 0;
204}
205
206/**
207        \brief initializes the world
208*/
209int 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*/
222void Orxonox::quitGame() 
223{
224        bQuitOrxonox = true;
225}
226
227/**
228        \brief this runs all of Orxonox
229*/
230void 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      printf("<==");
239
240      // Network
241      synchronize();
242      // Process input
243      handle_input();
244      // Process time
245      time_slice();
246      // Process collision
247      collision();
248      // Draw
249      display();
250
251      printf(">\n");
252    }
253  printf("Orxonox|Exiting the main loop\n");
254}
255
256/**
257        \brief handles sprecial events from localinput
258        \param event: an event not handled by the CommandNode
259*/
260void Orxonox::event_handler (SDL_Event* event)
261{
262        // Handle special events such as reshape, quit, focus changes
263}
264
265/**
266        \brief synchronize local data with remote data
267*/
268void Orxonox::synchronize ()
269{
270        // Get remote input
271        // Update synchronizables
272}
273
274/**
275        \brief run all input processing
276*/
277void Orxonox::handle_input ()
278{
279        // localinput
280                localinput->process();
281        // remoteinput
282}
283
284/**
285        \brief advance the timeline
286*/
287void Orxonox::time_slice ()
288{
289  Uint32 curframe = SDL_GetTicks();
290  if( !pause)
291    {
292      Uint32 dt = curframe - lastframe;
293     
294      if(dt > 0)
295        {
296          float fps = 1000/dt;
297          printf("%f", fps);
298        }
299     
300      world->time_slice (dt);
301      world->update ();
302      localcamera->time_slice (dt);
303    }
304  lastframe = curframe;
305}
306
307/**
308        \brief compute collision detection
309*/
310void Orxonox::collision ()
311{
312        world->collide ();
313}
314
315/**
316        \brief handle keyboard commands that are not meant for WorldEntities
317        \param cmd: the command to handle
318        \return true if the command was handled by the system or false if it may be passed to the WorldEntities
319*/
320bool Orxonox::system_command (Command* cmd)
321{
322        if( !strcmp( cmd->cmd, "quit"))
323        {
324                if( !cmd->bUp) bQuitOrxonox = true;
325                return true;
326        }
327        return false;
328}
329
330/**
331        \brief render the current frame
332*/
333void Orxonox::display ()
334{
335                // clear buffer
336        glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
337                // set camera
338        localcamera->apply ();
339                // draw world
340        world->draw ();
341                // draw HUD
342                // flip buffers
343        SDL_GL_SwapBuffers();
344}
345
346/**
347        \brief retrieve a pointer to the local Camera
348        \return a pointer to localcamera
349*/
350Camera* Orxonox::get_camera ()
351{
352        return localcamera;
353}
354
355/**
356        \brief retrieve a pointer to the local CommandNode
357        \return a pointer to localinput
358*/
359CommandNode* Orxonox::get_localinput ()
360{
361        return localinput;
362}
363
364/**
365        \brief retrieve a pointer to the local World
366        \return a pointer to world
367*/
368World* Orxonox::get_world ()
369{
370        return world;
371}
372
373int main (int argc, char** argv) 
374{ 
375        printf(">>> Starting Orxonox <<<\n");
376  Orxonox *orx = Orxonox::getInstance();
377 
378  if( (*orx).init(argc, argv) == -1)
379  {
380    printf("! Orxonox initialization failed\n");
381    return -1;
382  }
383       
384  (*orx).mainLoop();
385
386  //delete orx;
387 
388  return 0;
389}
Note: See TracBrowser for help on using the repository browser.