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