[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); |
---|
[3174] | 99 | SDL_Init (SDL_INIT_TIMER); |
---|
[2636] | 100 | // initialize everything |
---|
| 101 | if( init_video () == -1) return -1; |
---|
| 102 | if( init_sound () == -1) return -1; |
---|
[2190] | 103 | printf("> Initializing input\n"); |
---|
[2636] | 104 | if( init_input () == -1) return -1; |
---|
[2190] | 105 | printf("> Initializing networking\n"); |
---|
[2636] | 106 | if( init_networking () == -1) return -1; |
---|
[2190] | 107 | printf("> Initializing resources\n"); |
---|
[2636] | 108 | if( init_resources () == -1) return -1; |
---|
| 109 | //printf("> Initializing world\n"); |
---|
| 110 | //if( init_world () == -1) return -1; PB: world will be initialized when started |
---|
| 111 | |
---|
| 112 | return 0; |
---|
[1850] | 113 | } |
---|
[1849] | 114 | |
---|
[2190] | 115 | /** |
---|
[2636] | 116 | \brief initializes SDL and OpenGL |
---|
[2190] | 117 | */ |
---|
| 118 | int Orxonox::init_video () |
---|
| 119 | { |
---|
[3174] | 120 | printf("> Initializing video\n"); |
---|
| 121 | if (SDL_Init (SDL_INIT_VIDEO) == -1) |
---|
| 122 | { |
---|
| 123 | printf ("could not initialize SDL Video\n"); |
---|
| 124 | return -1; |
---|
| 125 | } |
---|
[2190] | 126 | // Set video mode |
---|
| 127 | // TO DO: parse arguments for settings |
---|
| 128 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); |
---|
| 129 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); |
---|
| 130 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); |
---|
| 131 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
---|
| 132 | |
---|
| 133 | int bpp = 16; |
---|
| 134 | int width = 640; |
---|
| 135 | int height = 480; |
---|
| 136 | Uint32 flags = SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER; |
---|
| 137 | |
---|
| 138 | if( (screen = SDL_SetVideoMode (width, height, bpp, flags)) == NULL) |
---|
| 139 | { |
---|
| 140 | printf ("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", width, height, bpp, flags, SDL_GetError()); |
---|
| 141 | SDL_Quit(); |
---|
| 142 | return -1; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | // Set window labeling |
---|
[3022] | 146 | SDL_WM_SetCaption( "Orxonox " PACKAGE_VERSION, "Orxonox " PACKAGE_VERSION); |
---|
[2190] | 147 | |
---|
| 148 | // TO DO: Create a cool icon and use it here |
---|
| 149 | // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask); |
---|
[1850] | 150 | |
---|
[2190] | 151 | // OpenGL stuff |
---|
| 152 | // (Is this all we initialize globally???) |
---|
[1872] | 153 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
[2190] | 154 | glEnable(GL_DEPTH_TEST); |
---|
[2817] | 155 | |
---|
| 156 | // LIGHTING |
---|
| 157 | GLfloat lmodelAmbient[] = {.1, .1, .1, 1.0}; |
---|
| 158 | GLfloat whiteLight[] = {1.0, 1.0, 1.0,1.0}; |
---|
| 159 | GLfloat lightPosition[] = {10.0, 10, 19.0, 0.0}; |
---|
| 160 | |
---|
| 161 | glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); |
---|
| 162 | glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); |
---|
| 163 | glEnable(GL_LIGHTING); |
---|
| 164 | glEnable(GL_LIGHT0); |
---|
| 165 | glEnable(GL_DEPTH_TEST); |
---|
| 166 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
---|
| 167 | glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); |
---|
| 168 | |
---|
[2792] | 169 | // glEnable(GL_COLOR); |
---|
| 170 | // glShadeModel(GL_SMOOTH); |
---|
[1899] | 171 | |
---|
[2190] | 172 | // create camera |
---|
[2636] | 173 | localcamera = new Camera(world); |
---|
[2190] | 174 | |
---|
| 175 | return 0; |
---|
| 176 | } |
---|
[1850] | 177 | |
---|
[2190] | 178 | /** |
---|
[2636] | 179 | \brief initializes the sound engine |
---|
[2190] | 180 | */ |
---|
| 181 | int Orxonox::init_sound () |
---|
| 182 | { |
---|
[3174] | 183 | printf("> Initializing sound\n"); |
---|
| 184 | // SDL_Init (SDL_INIT_AUDIO); |
---|
[2636] | 185 | printf("Not yet implemented\n"); |
---|
| 186 | return 0; |
---|
[2190] | 187 | } |
---|
[1900] | 188 | |
---|
[2190] | 189 | /** |
---|
[2636] | 190 | \brief initializes input functions |
---|
[2190] | 191 | */ |
---|
| 192 | int Orxonox::init_input () |
---|
| 193 | { |
---|
[2636] | 194 | // create localinput |
---|
| 195 | localinput = new CommandNode( configfilename); |
---|
| 196 | |
---|
| 197 | return 0; |
---|
[1803] | 198 | } |
---|
| 199 | |
---|
[2190] | 200 | /** |
---|
[2636] | 201 | \brief initializes network system |
---|
[2190] | 202 | */ |
---|
| 203 | int Orxonox::init_networking () |
---|
[1897] | 204 | { |
---|
[2636] | 205 | printf("Not yet implemented\n"); |
---|
| 206 | return 0; |
---|
[1897] | 207 | } |
---|
| 208 | |
---|
[2190] | 209 | /** |
---|
[2636] | 210 | \brief initializes and loads resource files |
---|
[2190] | 211 | */ |
---|
| 212 | int Orxonox::init_resources () |
---|
[1858] | 213 | { |
---|
[2636] | 214 | printf("Not yet implemented\n"); |
---|
| 215 | return 0; |
---|
[1858] | 216 | } |
---|
[1849] | 217 | |
---|
[2190] | 218 | /** |
---|
[2636] | 219 | \brief initializes the world |
---|
[2190] | 220 | */ |
---|
| 221 | int Orxonox::init_world () |
---|
[1896] | 222 | { |
---|
[2636] | 223 | //world = new World(); |
---|
| 224 | |
---|
| 225 | // TO DO: replace this with a menu/intro |
---|
| 226 | //world->load_debug_level(); |
---|
| 227 | |
---|
| 228 | return 0; |
---|
[1896] | 229 | } |
---|
| 230 | |
---|
[2636] | 231 | |
---|
[2190] | 232 | /** |
---|
[2636] | 233 | \brief starts the orxonox game or menu |
---|
| 234 | |
---|
| 235 | here is the central orxonox state manager. There are currently two states |
---|
| 236 | - menu |
---|
| 237 | - game-play |
---|
| 238 | both states manage their states themselfs again. |
---|
[2190] | 239 | */ |
---|
[2636] | 240 | void Orxonox::start() |
---|
| 241 | { |
---|
| 242 | |
---|
| 243 | this->gameLoader = GameLoader::getInstance(); |
---|
| 244 | this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); |
---|
| 245 | this->gameLoader->init(); |
---|
| 246 | this->gameLoader->start(); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | \brief exits Orxonox |
---|
| 251 | */ |
---|
[1875] | 252 | void Orxonox::quitGame() |
---|
| 253 | { |
---|
[2636] | 254 | bQuitOrxonox = true; |
---|
[1875] | 255 | } |
---|
| 256 | |
---|
[2190] | 257 | /** |
---|
[2636] | 258 | \brief this runs all of Orxonox |
---|
[2190] | 259 | */ |
---|
| 260 | void Orxonox::mainLoop() |
---|
| 261 | { |
---|
[2551] | 262 | lastframe = SDL_GetTicks(); |
---|
| 263 | bQuitOrxonox = false; |
---|
[2190] | 264 | // This is where everything is run |
---|
[2551] | 265 | printf("Orxonox|Entering main loop\n"); |
---|
[2190] | 266 | while( !bQuitOrxonox) |
---|
[2551] | 267 | { |
---|
| 268 | // Network |
---|
| 269 | synchronize(); |
---|
| 270 | // Process input |
---|
| 271 | handle_input(); |
---|
| 272 | // Process time |
---|
| 273 | time_slice(); |
---|
| 274 | // Process collision |
---|
| 275 | collision(); |
---|
| 276 | // Draw |
---|
| 277 | display(); |
---|
| 278 | } |
---|
| 279 | printf("Orxonox|Exiting the main loop\n"); |
---|
[1875] | 280 | } |
---|
| 281 | |
---|
[2190] | 282 | /** |
---|
[2636] | 283 | \brief handles sprecial events from localinput |
---|
| 284 | \param event: an event not handled by the CommandNode |
---|
[2190] | 285 | */ |
---|
| 286 | void Orxonox::event_handler (SDL_Event* event) |
---|
| 287 | { |
---|
[2636] | 288 | // Handle special events such as reshape, quit, focus changes |
---|
[2190] | 289 | } |
---|
[1875] | 290 | |
---|
[2190] | 291 | /** |
---|
[2636] | 292 | \brief synchronize local data with remote data |
---|
[1872] | 293 | */ |
---|
[2190] | 294 | void Orxonox::synchronize () |
---|
[1859] | 295 | { |
---|
[2636] | 296 | // Get remote input |
---|
| 297 | // Update synchronizables |
---|
[1859] | 298 | } |
---|
| 299 | |
---|
[2190] | 300 | /** |
---|
[2636] | 301 | \brief run all input processing |
---|
[2190] | 302 | */ |
---|
| 303 | void Orxonox::handle_input () |
---|
| 304 | { |
---|
[2636] | 305 | // localinput |
---|
| 306 | localinput->process(); |
---|
| 307 | // remoteinput |
---|
[2190] | 308 | } |
---|
[1859] | 309 | |
---|
[2190] | 310 | /** |
---|
[2636] | 311 | \brief advance the timeline |
---|
[2190] | 312 | */ |
---|
| 313 | void Orxonox::time_slice () |
---|
[1803] | 314 | { |
---|
[2551] | 315 | Uint32 curframe = SDL_GetTicks(); |
---|
| 316 | if( !pause) |
---|
| 317 | { |
---|
| 318 | Uint32 dt = curframe - lastframe; |
---|
| 319 | |
---|
| 320 | if(dt > 0) |
---|
[2190] | 321 | { |
---|
[2551] | 322 | float fps = 1000/dt; |
---|
[2636] | 323 | printf("fps = %f\n", fps); |
---|
[2190] | 324 | } |
---|
[2551] | 325 | |
---|
| 326 | world->time_slice (dt); |
---|
| 327 | world->update (); |
---|
| 328 | localcamera->time_slice (dt); |
---|
| 329 | } |
---|
| 330 | lastframe = curframe; |
---|
[2190] | 331 | } |
---|
[1879] | 332 | |
---|
[2190] | 333 | /** |
---|
[2636] | 334 | \brief compute collision detection |
---|
[2190] | 335 | */ |
---|
| 336 | void Orxonox::collision () |
---|
| 337 | { |
---|
[2636] | 338 | world->collide (); |
---|
[2190] | 339 | } |
---|
[1879] | 340 | |
---|
[2190] | 341 | /** |
---|
[2636] | 342 | \brief handle keyboard commands that are not meant for WorldEntities |
---|
| 343 | \param cmd: the command to handle |
---|
| 344 | \return true if the command was handled by the system or false if it may be passed to the WorldEntities |
---|
[2190] | 345 | */ |
---|
| 346 | bool Orxonox::system_command (Command* cmd) |
---|
| 347 | { |
---|
[2636] | 348 | if( !strcmp( cmd->cmd, "quit")) |
---|
| 349 | { |
---|
| 350 | if( !cmd->bUp) this->gameLoader->stop(); |
---|
| 351 | return true; |
---|
| 352 | } |
---|
| 353 | return false; |
---|
[2190] | 354 | } |
---|
[1803] | 355 | |
---|
[2190] | 356 | /** |
---|
[2636] | 357 | \brief render the current frame |
---|
[2190] | 358 | */ |
---|
| 359 | void Orxonox::display () |
---|
| 360 | { |
---|
[2636] | 361 | // clear buffer |
---|
| 362 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
| 363 | // set camera |
---|
| 364 | localcamera->apply (); |
---|
| 365 | // draw world |
---|
| 366 | world->draw (); |
---|
| 367 | // draw HUD |
---|
| 368 | // flip buffers |
---|
| 369 | SDL_GL_SwapBuffers(); |
---|
[2190] | 370 | } |
---|
[1850] | 371 | |
---|
[2190] | 372 | /** |
---|
[2636] | 373 | \brief retrieve a pointer to the local Camera |
---|
| 374 | \return a pointer to localcamera |
---|
[2190] | 375 | */ |
---|
| 376 | Camera* Orxonox::get_camera () |
---|
[1872] | 377 | { |
---|
[2636] | 378 | return localcamera; |
---|
[1872] | 379 | } |
---|
[1850] | 380 | |
---|
[2190] | 381 | /** |
---|
[2636] | 382 | \brief retrieve a pointer to the local CommandNode |
---|
| 383 | \return a pointer to localinput |
---|
[2190] | 384 | */ |
---|
| 385 | CommandNode* Orxonox::get_localinput () |
---|
[1850] | 386 | { |
---|
[2636] | 387 | return localinput; |
---|
[1803] | 388 | } |
---|
| 389 | |
---|
[2190] | 390 | /** |
---|
[2636] | 391 | \brief retrieve a pointer to the local World |
---|
| 392 | \return a pointer to world |
---|
[2190] | 393 | */ |
---|
| 394 | World* Orxonox::get_world () |
---|
[1872] | 395 | { |
---|
[2636] | 396 | return world; |
---|
[1872] | 397 | } |
---|
[1850] | 398 | |
---|
| 399 | int main (int argc, char** argv) |
---|
[1803] | 400 | { |
---|
[2636] | 401 | printf(">>> Starting Orxonox <<<\n"); |
---|
[1850] | 402 | Orxonox *orx = Orxonox::getInstance(); |
---|
[2190] | 403 | |
---|
| 404 | if( (*orx).init(argc, argv) == -1) |
---|
[2636] | 405 | { |
---|
| 406 | printf("! Orxonox initialization failed\n"); |
---|
| 407 | return -1; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | //(*orx).mainLoop(); |
---|
[1856] | 411 | |
---|
[2636] | 412 | orx->start(); |
---|
| 413 | |
---|
[2190] | 414 | //delete orx; |
---|
| 415 | |
---|
[1803] | 416 | return 0; |
---|
| 417 | } |
---|