Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/chris/src/orxonox.cc @ 2161

Last change on this file since 2161 was 2146, checked in by chris, 21 years ago

orxonox/branches/chris: fixed a little something that kept the code from being compiled

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