| [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] | 34 | using namespace std; | 
|---|
 | 35 |  | 
|---|
| [2190] | 36 | /** | 
|---|
| [2636] | 37 |    \brief create a new Orxonox | 
|---|
| [2190] | 38 | */ | 
|---|
 | 39 | Orxonox::Orxonox () | 
|---|
| [1872] | 40 | { | 
|---|
 | 41 |   pause = false; | 
|---|
 | 42 | } | 
|---|
| [1803] | 43 |  | 
|---|
| [2190] | 44 | /** | 
|---|
| [2636] | 45 |    \brief remove Orxonox from memory | 
|---|
| [2190] | 46 | */ | 
|---|
| [1875] | 47 | Orxonox::~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] | 58 | Orxonox* Orxonox::singleton_ref = 0; | 
|---|
| [1872] | 59 |  | 
|---|
| [1850] | 60 | Orxonox* 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 | */ | 
|---|
 | 74 | void 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 | */ | 
|---|
 | 93 | int 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 | */ | 
|---|
 | 128 | int 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 |    | 
|---|
| [3279] | 137 |   int bpp = 32; | 
|---|
 | 138 |   int width = 1024; | 
|---|
 | 139 |   int height = 768; | 
|---|
| [2190] | 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 | 
|---|
| [3151] | 150 |   SDL_WM_SetCaption( "Orxonox " PACKAGE_VERSION, "Orxonox " PACKAGE_VERSION); | 
|---|
| [2190] | 151 |    | 
|---|
 | 152 |   // TO DO: Create a cool icon and use it here | 
|---|
 | 153 |   // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask);   | 
|---|
| [1850] | 154 |  | 
|---|
| [2190] | 155 |   // OpenGL stuff | 
|---|
 | 156 |   // (Is this all we initialize globally???) | 
|---|
| [1872] | 157 |   glClearColor(0.0, 0.0, 0.0, 0.0); | 
|---|
| [2190] | 158 |   glEnable(GL_DEPTH_TEST); | 
|---|
| [2860] | 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); | 
|---|
| [1899] | 175 |    | 
|---|
| [2190] | 176 |   // create camera | 
|---|
| [2636] | 177 |   localcamera = new Camera(world); | 
|---|
| [2190] | 178 |    | 
|---|
 | 179 |   return 0; | 
|---|
 | 180 | } | 
|---|
| [1850] | 181 |  | 
|---|
| [2190] | 182 | /** | 
|---|
| [2636] | 183 |    \brief initializes the sound engine | 
|---|
| [2190] | 184 | */ | 
|---|
 | 185 | int Orxonox::init_sound ()  | 
|---|
 | 186 | { | 
|---|
| [2636] | 187 |   printf("Not yet implemented\n"); | 
|---|
 | 188 |   return 0; | 
|---|
| [2190] | 189 | } | 
|---|
| [1900] | 190 |  | 
|---|
| [2190] | 191 | /** | 
|---|
| [2636] | 192 |    \brief initializes input functions | 
|---|
| [2190] | 193 | */ | 
|---|
 | 194 | int Orxonox::init_input ()  | 
|---|
 | 195 | { | 
|---|
| [2636] | 196 |   // create localinput | 
|---|
 | 197 |   localinput = new CommandNode( configfilename); | 
|---|
 | 198 |    | 
|---|
 | 199 |   return 0; | 
|---|
| [1803] | 200 | } | 
|---|
 | 201 |  | 
|---|
| [2190] | 202 | /** | 
|---|
| [2636] | 203 |    \brief initializes network system | 
|---|
| [2190] | 204 | */ | 
|---|
 | 205 | int Orxonox::init_networking ()  | 
|---|
| [1897] | 206 | { | 
|---|
| [2636] | 207 |   printf("Not yet implemented\n"); | 
|---|
 | 208 |   return 0; | 
|---|
| [1897] | 209 | } | 
|---|
 | 210 |  | 
|---|
| [2190] | 211 | /** | 
|---|
| [2636] | 212 |    \brief initializes and loads resource files | 
|---|
| [2190] | 213 | */ | 
|---|
 | 214 | int Orxonox::init_resources ()  | 
|---|
| [1858] | 215 | { | 
|---|
| [2636] | 216 |   printf("Not yet implemented\n"); | 
|---|
 | 217 |   return 0; | 
|---|
| [1858] | 218 | } | 
|---|
| [1849] | 219 |  | 
|---|
| [2190] | 220 | /** | 
|---|
| [2636] | 221 |    \brief initializes the world | 
|---|
| [2190] | 222 | */ | 
|---|
 | 223 | int Orxonox::init_world ()  | 
|---|
| [1896] | 224 | { | 
|---|
| [2636] | 225 |   //world = new World(); | 
|---|
 | 226 |    | 
|---|
 | 227 |   // TO DO: replace this with a menu/intro | 
|---|
 | 228 |   //world->load_debug_level(); | 
|---|
 | 229 |    | 
|---|
 | 230 |   return 0; | 
|---|
| [1896] | 231 | } | 
|---|
 | 232 |  | 
|---|
| [2636] | 233 |  | 
|---|
| [2190] | 234 | /** | 
|---|
| [2636] | 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. | 
|---|
| [2190] | 241 | */ | 
|---|
| [2636] | 242 | void 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 | */ | 
|---|
| [1875] | 254 | void Orxonox::quitGame()  | 
|---|
 | 255 | { | 
|---|
| [2636] | 256 |   bQuitOrxonox = true; | 
|---|
| [1875] | 257 | } | 
|---|
 | 258 |  | 
|---|
| [2190] | 259 | /** | 
|---|
| [2636] | 260 |    \brief this runs all of Orxonox | 
|---|
| [2190] | 261 | */ | 
|---|
 | 262 | void Orxonox::mainLoop() | 
|---|
 | 263 | { | 
|---|
| [2551] | 264 |   lastframe = SDL_GetTicks(); | 
|---|
 | 265 |   bQuitOrxonox = false; | 
|---|
| [2190] | 266 |   // This is where everything is run | 
|---|
| [2551] | 267 |   printf("Orxonox|Entering main loop\n"); | 
|---|
| [2190] | 268 |   while( !bQuitOrxonox) | 
|---|
| [2551] | 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"); | 
|---|
| [1875] | 282 | } | 
|---|
 | 283 |  | 
|---|
| [2190] | 284 | /** | 
|---|
| [2636] | 285 |    \brief handles sprecial events from localinput | 
|---|
 | 286 |    \param event: an event not handled by the CommandNode  | 
|---|
| [2190] | 287 | */ | 
|---|
 | 288 | void Orxonox::event_handler (SDL_Event* event) | 
|---|
 | 289 | { | 
|---|
| [2636] | 290 |   // Handle special events such as reshape, quit, focus changes | 
|---|
| [2190] | 291 | } | 
|---|
| [1875] | 292 |  | 
|---|
| [2190] | 293 | /** | 
|---|
| [2636] | 294 |    \brief synchronize local data with remote data | 
|---|
| [1872] | 295 | */ | 
|---|
| [2190] | 296 | void Orxonox::synchronize () | 
|---|
| [1859] | 297 | { | 
|---|
| [2636] | 298 |   // Get remote input | 
|---|
 | 299 |   // Update synchronizables | 
|---|
| [1859] | 300 | } | 
|---|
 | 301 |  | 
|---|
| [2190] | 302 | /** | 
|---|
| [2636] | 303 |    \brief run all input processing | 
|---|
| [2190] | 304 | */ | 
|---|
 | 305 | void Orxonox::handle_input () | 
|---|
 | 306 | { | 
|---|
| [2636] | 307 |   // localinput | 
|---|
 | 308 |   localinput->process(); | 
|---|
 | 309 |   // remoteinput | 
|---|
| [2190] | 310 | } | 
|---|
| [1859] | 311 |  | 
|---|
| [2190] | 312 | /** | 
|---|
| [2636] | 313 |    \brief advance the timeline | 
|---|
| [2190] | 314 | */ | 
|---|
 | 315 | void Orxonox::time_slice () | 
|---|
| [1803] | 316 | { | 
|---|
| [2551] | 317 |   Uint32 curframe = SDL_GetTicks(); | 
|---|
 | 318 |   if( !pause) | 
|---|
 | 319 |     { | 
|---|
 | 320 |       Uint32 dt = curframe - lastframe; | 
|---|
 | 321 |        | 
|---|
 | 322 |       if(dt > 0) | 
|---|
| [2190] | 323 |         { | 
|---|
| [2551] | 324 |           float fps = 1000/dt; | 
|---|
| [2636] | 325 |           printf("fps = %f\n", fps); | 
|---|
| [2190] | 326 |         } | 
|---|
| [2551] | 327 |        | 
|---|
 | 328 |       world->time_slice (dt); | 
|---|
 | 329 |       world->update (); | 
|---|
 | 330 |       localcamera->time_slice (dt); | 
|---|
 | 331 |     } | 
|---|
 | 332 |   lastframe = curframe; | 
|---|
| [2190] | 333 | } | 
|---|
| [1879] | 334 |  | 
|---|
| [2190] | 335 | /** | 
|---|
| [2636] | 336 |    \brief compute collision detection | 
|---|
| [2190] | 337 | */ | 
|---|
 | 338 | void Orxonox::collision () | 
|---|
 | 339 | { | 
|---|
| [2636] | 340 |   world->collide (); | 
|---|
| [2190] | 341 | } | 
|---|
| [1879] | 342 |  | 
|---|
| [2190] | 343 | /** | 
|---|
| [2636] | 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 | 
|---|
| [2190] | 347 | */ | 
|---|
 | 348 | bool Orxonox::system_command (Command* cmd) | 
|---|
 | 349 | { | 
|---|
| [2636] | 350 |   if( !strcmp( cmd->cmd, "quit")) | 
|---|
 | 351 |     { | 
|---|
 | 352 |       if( !cmd->bUp) this->gameLoader->stop(); | 
|---|
 | 353 |       return true; | 
|---|
 | 354 |     } | 
|---|
 | 355 |   return false; | 
|---|
| [2190] | 356 | } | 
|---|
| [1803] | 357 |  | 
|---|
| [2190] | 358 | /** | 
|---|
| [2636] | 359 |    \brief render the current frame | 
|---|
| [2190] | 360 | */ | 
|---|
 | 361 | void Orxonox::display () | 
|---|
 | 362 | { | 
|---|
| [2636] | 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(); | 
|---|
| [2190] | 372 | } | 
|---|
| [1850] | 373 |  | 
|---|
| [2190] | 374 | /** | 
|---|
| [2636] | 375 |    \brief retrieve a pointer to the local Camera | 
|---|
 | 376 |    \return a pointer to localcamera | 
|---|
| [2190] | 377 | */ | 
|---|
 | 378 | Camera* Orxonox::get_camera () | 
|---|
| [1872] | 379 | { | 
|---|
| [2636] | 380 |   return localcamera; | 
|---|
| [1872] | 381 | } | 
|---|
| [1850] | 382 |  | 
|---|
| [2190] | 383 | /** | 
|---|
| [2636] | 384 |    \brief retrieve a pointer to the local CommandNode | 
|---|
 | 385 |    \return a pointer to localinput | 
|---|
| [2190] | 386 | */ | 
|---|
 | 387 | CommandNode* Orxonox::get_localinput () | 
|---|
| [1850] | 388 | { | 
|---|
| [2636] | 389 |   return localinput; | 
|---|
| [1803] | 390 | } | 
|---|
 | 391 |  | 
|---|
| [2190] | 392 | /** | 
|---|
| [2636] | 393 |    \brief retrieve a pointer to the local World | 
|---|
 | 394 |    \return a pointer to world | 
|---|
| [2190] | 395 | */ | 
|---|
 | 396 | World* Orxonox::get_world () | 
|---|
| [1872] | 397 | { | 
|---|
| [2636] | 398 |   return world; | 
|---|
| [1872] | 399 | } | 
|---|
| [1850] | 400 |  | 
|---|
 | 401 | int main (int argc, char** argv)  | 
|---|
| [1803] | 402 | {   | 
|---|
| [2636] | 403 |   printf(">>> Starting Orxonox <<<\n"); | 
|---|
| [1850] | 404 |   Orxonox *orx = Orxonox::getInstance(); | 
|---|
| [2190] | 405 |    | 
|---|
 | 406 |   if( (*orx).init(argc, argv) == -1) | 
|---|
| [2636] | 407 |     { | 
|---|
 | 408 |       printf("! Orxonox initialization failed\n"); | 
|---|
 | 409 |       return -1; | 
|---|
 | 410 |     } | 
|---|
 | 411 |    | 
|---|
 | 412 |   //(*orx).mainLoop(); | 
|---|
| [1856] | 413 |  | 
|---|
| [2636] | 414 |   orx->start(); | 
|---|
 | 415 |    | 
|---|
| [2190] | 416 |   //delete orx; | 
|---|
 | 417 |    | 
|---|
| [1803] | 418 |   return 0; | 
|---|
 | 419 | } | 
|---|