Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2838 was 2817, checked in by bensch, 20 years ago

orxonox/trunk: added lighting and shading (pre-test-version)

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