| 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 | |
|---|
| 34 | using namespace std; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | \brief create a new Orxonox |
|---|
| 38 | */ |
|---|
| 39 | Orxonox::Orxonox () |
|---|
| 40 | { |
|---|
| 41 | pause = false; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | \brief remove Orxonox from memory |
|---|
| 46 | */ |
|---|
| 47 | Orxonox::~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 */ |
|---|
| 58 | Orxonox* Orxonox::singleton_ref = 0; |
|---|
| 59 | |
|---|
| 60 | Orxonox* 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 | */ |
|---|
| 74 | void 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 | */ |
|---|
| 93 | int Orxonox::init (int argc, char** argv) |
|---|
| 94 | { |
|---|
| 95 | // parse command line |
|---|
| 96 | // config file |
|---|
| 97 | |
|---|
| 98 | get_config_file (argc, argv); |
|---|
| 99 | |
|---|
| 100 | // initialize SDL |
|---|
| 101 | printf("> Initializing SDL\n"); |
|---|
| 102 | if( SDL_Init (SDL_INIT_EVERYTHING) == -1) |
|---|
| 103 | { |
|---|
| 104 | printf ("Could not SDL_Init(): %s\n", SDL_GetError()); |
|---|
| 105 | return -1; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | // initialize everything |
|---|
| 109 | printf("> Initializing video\n"); |
|---|
| 110 | if( init_video () == -1) return -1; |
|---|
| 111 | printf("> Initializing sound\n"); |
|---|
| 112 | if( init_sound () == -1) return -1; |
|---|
| 113 | printf("> Initializing input\n"); |
|---|
| 114 | if( init_input () == -1) return -1; |
|---|
| 115 | printf("> Initializing networking\n"); |
|---|
| 116 | if( init_networking () == -1) return -1; |
|---|
| 117 | printf("> Initializing resources\n"); |
|---|
| 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; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | \brief initializes SDL and OpenGL |
|---|
| 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 | |
|---|
| 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); |
|---|
| 155 | |
|---|
| 156 | // OpenGL stuff |
|---|
| 157 | // (Is this all we initialize globally???) |
|---|
| 158 | glClearColor(0.0, 0.0, 0.0, 0.0); |
|---|
| 159 | glEnable(GL_DEPTH_TEST); |
|---|
| 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 | |
|---|
| 174 | // glEnable(GL_COLOR); |
|---|
| 175 | // glShadeModel(GL_SMOOTH); |
|---|
| 176 | |
|---|
| 177 | // create camera |
|---|
| 178 | localcamera = new Camera(world); |
|---|
| 179 | |
|---|
| 180 | return 0; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /** |
|---|
| 184 | \brief initializes the sound engine |
|---|
| 185 | */ |
|---|
| 186 | int Orxonox::init_sound () |
|---|
| 187 | { |
|---|
| 188 | printf("Not yet implemented\n"); |
|---|
| 189 | return 0; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | \brief initializes input functions |
|---|
| 194 | */ |
|---|
| 195 | int Orxonox::init_input () |
|---|
| 196 | { |
|---|
| 197 | // create localinput |
|---|
| 198 | localinput = new CommandNode( configfilename); |
|---|
| 199 | |
|---|
| 200 | return 0; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | \brief initializes network system |
|---|
| 205 | */ |
|---|
| 206 | int Orxonox::init_networking () |
|---|
| 207 | { |
|---|
| 208 | printf("Not yet implemented\n"); |
|---|
| 209 | return 0; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | /** |
|---|
| 213 | \brief initializes and loads resource files |
|---|
| 214 | */ |
|---|
| 215 | int Orxonox::init_resources () |
|---|
| 216 | { |
|---|
| 217 | printf("Not yet implemented\n"); |
|---|
| 218 | return 0; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | \brief initializes the world |
|---|
| 223 | */ |
|---|
| 224 | int Orxonox::init_world () |
|---|
| 225 | { |
|---|
| 226 | //world = new World(); |
|---|
| 227 | |
|---|
| 228 | // TO DO: replace this with a menu/intro |
|---|
| 229 | //world->load_debug_level(); |
|---|
| 230 | |
|---|
| 231 | return 0; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | /** |
|---|
| 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. |
|---|
| 242 | */ |
|---|
| 243 | void 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 | */ |
|---|
| 255 | void Orxonox::quitGame() |
|---|
| 256 | { |
|---|
| 257 | bQuitOrxonox = true; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | \brief this runs all of Orxonox |
|---|
| 262 | */ |
|---|
| 263 | void Orxonox::mainLoop() |
|---|
| 264 | { |
|---|
| 265 | lastframe = SDL_GetTicks(); |
|---|
| 266 | bQuitOrxonox = false; |
|---|
| 267 | // This is where everything is run |
|---|
| 268 | printf("Orxonox|Entering main loop\n"); |
|---|
| 269 | while( !bQuitOrxonox) |
|---|
| 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"); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | /** |
|---|
| 286 | \brief handles sprecial events from localinput |
|---|
| 287 | \param event: an event not handled by the CommandNode |
|---|
| 288 | */ |
|---|
| 289 | void Orxonox::event_handler (SDL_Event* event) |
|---|
| 290 | { |
|---|
| 291 | // Handle special events such as reshape, quit, focus changes |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | \brief synchronize local data with remote data |
|---|
| 296 | */ |
|---|
| 297 | void Orxonox::synchronize () |
|---|
| 298 | { |
|---|
| 299 | // Get remote input |
|---|
| 300 | // Update synchronizables |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | /** |
|---|
| 304 | \brief run all input processing |
|---|
| 305 | */ |
|---|
| 306 | void Orxonox::handle_input () |
|---|
| 307 | { |
|---|
| 308 | // localinput |
|---|
| 309 | localinput->process(); |
|---|
| 310 | // remoteinput |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /** |
|---|
| 314 | \brief advance the timeline |
|---|
| 315 | */ |
|---|
| 316 | void Orxonox::time_slice () |
|---|
| 317 | { |
|---|
| 318 | Uint32 curframe = SDL_GetTicks(); |
|---|
| 319 | if( !pause) |
|---|
| 320 | { |
|---|
| 321 | Uint32 dt = curframe - lastframe; |
|---|
| 322 | |
|---|
| 323 | if(dt > 0) |
|---|
| 324 | { |
|---|
| 325 | float fps = 1000/dt; |
|---|
| 326 | printf("fps = %f\n", fps); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | world->time_slice (dt); |
|---|
| 330 | world->update (); |
|---|
| 331 | localcamera->time_slice (dt); |
|---|
| 332 | } |
|---|
| 333 | lastframe = curframe; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | /** |
|---|
| 337 | \brief compute collision detection |
|---|
| 338 | */ |
|---|
| 339 | void Orxonox::collision () |
|---|
| 340 | { |
|---|
| 341 | world->collide (); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | /** |
|---|
| 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 |
|---|
| 348 | */ |
|---|
| 349 | bool Orxonox::system_command (Command* cmd) |
|---|
| 350 | { |
|---|
| 351 | if( !strcmp( cmd->cmd, "quit")) |
|---|
| 352 | { |
|---|
| 353 | if( !cmd->bUp) this->gameLoader->stop(); |
|---|
| 354 | return true; |
|---|
| 355 | } |
|---|
| 356 | return false; |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | /** |
|---|
| 360 | \brief render the current frame |
|---|
| 361 | */ |
|---|
| 362 | void Orxonox::display () |
|---|
| 363 | { |
|---|
| 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(); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | /** |
|---|
| 376 | \brief retrieve a pointer to the local Camera |
|---|
| 377 | \return a pointer to localcamera |
|---|
| 378 | */ |
|---|
| 379 | Camera* Orxonox::get_camera () |
|---|
| 380 | { |
|---|
| 381 | return localcamera; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | /** |
|---|
| 385 | \brief retrieve a pointer to the local CommandNode |
|---|
| 386 | \return a pointer to localinput |
|---|
| 387 | */ |
|---|
| 388 | CommandNode* Orxonox::get_localinput () |
|---|
| 389 | { |
|---|
| 390 | return localinput; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | /** |
|---|
| 394 | \brief retrieve a pointer to the local World |
|---|
| 395 | \return a pointer to world |
|---|
| 396 | */ |
|---|
| 397 | World* Orxonox::get_world () |
|---|
| 398 | { |
|---|
| 399 | return world; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | int main (int argc, char** argv) |
|---|
| 403 | { |
|---|
| 404 | printf(">>> Starting Orxonox <<<\n"); |
|---|
| 405 | Orxonox *orx = Orxonox::getInstance(); |
|---|
| 406 | |
|---|
| 407 | if( (*orx).init(argc, argv) == -1) |
|---|
| 408 | { |
|---|
| 409 | printf("! Orxonox initialization failed\n"); |
|---|
| 410 | return -1; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | //(*orx).mainLoop(); |
|---|
| 414 | |
|---|
| 415 | orx->start(); |
|---|
| 416 | |
|---|
| 417 | //delete orx; |
|---|
| 418 | |
|---|
| 419 | return 0; |
|---|
| 420 | } |
|---|