[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 |
---|
[2068] | 23 | co-programmer: Christian Meyer |
---|
[1850] | 24 | */ |
---|
| 25 | |
---|
[2101] | 26 | #include "orxonox.h" |
---|
[2058] | 27 | #include "world.h" |
---|
[2101] | 28 | #include "camera.h" |
---|
[2058] | 29 | #include "data_tank.h" |
---|
[2101] | 30 | #include "command_node.h" |
---|
[2166] | 31 | #include <string.h> |
---|
[2058] | 32 | |
---|
[1803] | 33 | using namespace std; |
---|
| 34 | |
---|
[2141] | 35 | /** |
---|
| 36 | \brief create a new Orxonox |
---|
| 37 | */ |
---|
[2166] | 38 | Orxonox::Orxonox () |
---|
[1872] | 39 | { |
---|
| 40 | pause = false; |
---|
| 41 | } |
---|
[1803] | 42 | |
---|
[2141] | 43 | /** |
---|
| 44 | \brief remove Orxonox from memory |
---|
| 45 | */ |
---|
[1875] | 46 | Orxonox::~Orxonox () |
---|
[2068] | 47 | { |
---|
| 48 | Orxonox::singleton_ref = NULL; |
---|
| 49 | if( world != NULL) delete world; |
---|
| 50 | if( localinput != NULL) delete world; |
---|
[2101] | 51 | if( localcamera != NULL) delete localcamera; |
---|
[2068] | 52 | if( resources != NULL) delete resources; |
---|
| 53 | } |
---|
[1850] | 54 | |
---|
| 55 | |
---|
[2068] | 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 | |
---|
[2141] | 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 | */ |
---|
[2068] | 73 | void Orxonox::get_config_file (int argc, char** argv) |
---|
| 74 | { |
---|
[2101] | 75 | /* char* path; |
---|
[2068] | 76 | #ifdef __WIN32__ |
---|
| 77 | path = getenv(""); |
---|
| 78 | #else |
---|
| 79 | path = getenv("HOME"); |
---|
| 80 | #endif |
---|
| 81 | |
---|
| 82 | if( path != NULL) strcpy (configfilename, path); |
---|
[2080] | 83 | else strcpy (configfilename, "./"); |
---|
[2101] | 84 | strcat (configfilename, "/.orxonox.conf");*/ |
---|
| 85 | |
---|
[2105] | 86 | strcpy (configfilename, "orxonox.conf"); |
---|
[2068] | 87 | } |
---|
[1850] | 88 | |
---|
[2141] | 89 | /** |
---|
| 90 | \brief initialize Orxonox with command line |
---|
| 91 | */ |
---|
[2068] | 92 | int Orxonox::init (int argc, char** argv) |
---|
[1850] | 93 | { |
---|
[2068] | 94 | // parse command line |
---|
| 95 | // config file |
---|
| 96 | |
---|
| 97 | get_config_file (argc, argv); |
---|
| 98 | |
---|
| 99 | // initialize SDL |
---|
| 100 | printf("> Initializing SDL\n"); |
---|
[2058] | 101 | if( SDL_Init (SDL_INIT_EVERYTHING) == -1) |
---|
| 102 | { |
---|
| 103 | printf ("Could not SDL_Init(): %s\n", SDL_GetError()); |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
[2068] | 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; |
---|
| 122 | } |
---|
| 123 | |
---|
[2141] | 124 | /** |
---|
| 125 | \brief initializes SDL and OpenGL |
---|
| 126 | */ |
---|
[2068] | 127 | int Orxonox::init_video () |
---|
| 128 | { |
---|
[2058] | 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; |
---|
[2105] | 139 | Uint32 flags = SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER; |
---|
[2058] | 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); |
---|
| 154 | |
---|
| 155 | // OpenGL stuff |
---|
| 156 | // (Is this all we initialize globally???) |
---|
[2068] | 157 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
[1883] | 158 | glEnable(GL_DEPTH_TEST); |
---|
[2112] | 159 | glEnable(GL_COLOR); |
---|
[1872] | 160 | glShadeModel(GL_FLAT); |
---|
[2058] | 161 | |
---|
[2068] | 162 | // create camera |
---|
| 163 | localcamera = new Camera(); |
---|
| 164 | |
---|
| 165 | return 0; |
---|
[1803] | 166 | } |
---|
| 167 | |
---|
[2141] | 168 | /** |
---|
| 169 | \brief initializes the sound engine |
---|
| 170 | */ |
---|
[2068] | 171 | int Orxonox::init_sound () |
---|
[1803] | 172 | { |
---|
[2068] | 173 | printf("Not yet implemented\n"); |
---|
| 174 | return 0; |
---|
[1850] | 175 | } |
---|
[1849] | 176 | |
---|
[2141] | 177 | /** |
---|
| 178 | \brief initializes input functions |
---|
| 179 | */ |
---|
[2068] | 180 | int Orxonox::init_input () |
---|
[1850] | 181 | { |
---|
[2068] | 182 | // create localinput |
---|
| 183 | localinput = new CommandNode( configfilename); |
---|
| 184 | |
---|
| 185 | return 0; |
---|
[1803] | 186 | } |
---|
| 187 | |
---|
[2141] | 188 | /** |
---|
| 189 | \brief initializes network system |
---|
| 190 | */ |
---|
[2068] | 191 | int Orxonox::init_networking () |
---|
[1897] | 192 | { |
---|
[2068] | 193 | printf("Not yet implemented\n"); |
---|
| 194 | return 0; |
---|
[1897] | 195 | } |
---|
| 196 | |
---|
[2141] | 197 | /** |
---|
| 198 | \brief initializes and loads resource files |
---|
| 199 | */ |
---|
[2068] | 200 | int Orxonox::init_resources () |
---|
[1858] | 201 | { |
---|
[2068] | 202 | printf("Not yet implemented\n"); |
---|
| 203 | return 0; |
---|
[1858] | 204 | } |
---|
[1849] | 205 | |
---|
[2141] | 206 | /** |
---|
| 207 | \brief initializes the world |
---|
| 208 | */ |
---|
[2068] | 209 | int Orxonox::init_world () |
---|
[1896] | 210 | { |
---|
[2080] | 211 | world = new World(); |
---|
| 212 | |
---|
| 213 | // TO DO: replace this with a menu/intro |
---|
| 214 | world->load_debug_level(); |
---|
| 215 | |
---|
[2068] | 216 | return 0; |
---|
[1896] | 217 | } |
---|
| 218 | |
---|
[2141] | 219 | /** |
---|
| 220 | \brief exits Orxonox |
---|
| 221 | */ |
---|
[1875] | 222 | void Orxonox::quitGame() |
---|
| 223 | { |
---|
[2068] | 224 | bQuitOrxonox = true; |
---|
[1875] | 225 | } |
---|
[2141] | 226 | |
---|
| 227 | /** |
---|
| 228 | \brief this runs all of Orxonox |
---|
| 229 | */ |
---|
[2068] | 230 | void Orxonox::mainLoop() |
---|
| 231 | { |
---|
[2101] | 232 | lastframe = SDL_GetTicks(); |
---|
[2105] | 233 | bQuitOrxonox = false; |
---|
[2068] | 234 | // This is where everything is run |
---|
[2105] | 235 | printf("Orxonox|Entering main loop\n"); |
---|
[2068] | 236 | while( !bQuitOrxonox) |
---|
| 237 | { |
---|
| 238 | // Network |
---|
| 239 | synchronize(); |
---|
| 240 | // Process input |
---|
| 241 | handle_input(); |
---|
| 242 | // Process time |
---|
| 243 | time_slice(); |
---|
| 244 | // Process collision |
---|
| 245 | collision(); |
---|
| 246 | // Draw |
---|
| 247 | display(); |
---|
[1875] | 248 | } |
---|
[2105] | 249 | printf("Orxonox|Exiting the main loop\n"); |
---|
[1875] | 250 | } |
---|
| 251 | |
---|
[2141] | 252 | /** |
---|
| 253 | \brief handles sprecial events from localinput |
---|
| 254 | \param event: an event not handled by the CommandNode |
---|
| 255 | */ |
---|
[2068] | 256 | void Orxonox::event_handler (SDL_Event* event) |
---|
| 257 | { |
---|
| 258 | // Handle special events such as reshape, quit, focus changes |
---|
| 259 | } |
---|
[1875] | 260 | |
---|
[2141] | 261 | /** |
---|
| 262 | \brief synchronize local data with remote data |
---|
| 263 | */ |
---|
[2068] | 264 | void Orxonox::synchronize () |
---|
[1859] | 265 | { |
---|
[2068] | 266 | // Get remote input |
---|
| 267 | // Update synchronizables |
---|
[1859] | 268 | } |
---|
| 269 | |
---|
[2141] | 270 | /** |
---|
| 271 | \brief run all input processing |
---|
| 272 | */ |
---|
[2068] | 273 | void Orxonox::handle_input () |
---|
| 274 | { |
---|
| 275 | // localinput |
---|
[2101] | 276 | localinput->process(); |
---|
[2068] | 277 | // remoteinput |
---|
| 278 | } |
---|
[1859] | 279 | |
---|
[2141] | 280 | /** |
---|
| 281 | \brief advance the timeline |
---|
| 282 | */ |
---|
[2068] | 283 | void Orxonox::time_slice () |
---|
[1803] | 284 | { |
---|
[2068] | 285 | Uint32 curframe = SDL_GetTicks(); |
---|
| 286 | if( !pause) |
---|
| 287 | { |
---|
| 288 | world->time_slice (curframe - lastframe); |
---|
| 289 | world->update (); |
---|
| 290 | localcamera->time_slice (curframe - lastframe); |
---|
| 291 | } |
---|
| 292 | lastframe = curframe; |
---|
[2058] | 293 | } |
---|
[1803] | 294 | |
---|
[2141] | 295 | /** |
---|
| 296 | \brief compute collision detection |
---|
| 297 | */ |
---|
[2068] | 298 | void Orxonox::collision () |
---|
[1872] | 299 | { |
---|
[2068] | 300 | world->collide (); |
---|
[1872] | 301 | } |
---|
[1850] | 302 | |
---|
[2141] | 303 | /** |
---|
| 304 | \brief handle keyboard commands that are not meant for WorldEntities |
---|
| 305 | \param cmd: the command to handle |
---|
| 306 | \return true if the command was handled by the system or false if it may be passed to the WorldEntities |
---|
| 307 | */ |
---|
[2105] | 308 | bool Orxonox::system_command (Command* cmd) |
---|
| 309 | { |
---|
[2141] | 310 | if( !strcmp( cmd->cmd, "quit")) |
---|
[2105] | 311 | { |
---|
[2141] | 312 | if( !cmd->bUp) bQuitOrxonox = true; |
---|
[2105] | 313 | return true; |
---|
| 314 | } |
---|
[2115] | 315 | return false; |
---|
[2105] | 316 | } |
---|
| 317 | |
---|
[2141] | 318 | /** |
---|
| 319 | \brief render the current frame |
---|
| 320 | */ |
---|
[2068] | 321 | void Orxonox::display () |
---|
[1850] | 322 | { |
---|
[2068] | 323 | // clear buffer |
---|
[2101] | 324 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
[2068] | 325 | // set camera |
---|
| 326 | localcamera->apply (); |
---|
| 327 | // draw world |
---|
| 328 | world->draw (); |
---|
| 329 | // draw HUD |
---|
| 330 | // flip buffers |
---|
[2105] | 331 | SDL_GL_SwapBuffers(); |
---|
[1803] | 332 | } |
---|
| 333 | |
---|
[2141] | 334 | /** |
---|
| 335 | \brief retrieve a pointer to the local Camera |
---|
| 336 | \return a pointer to localcamera |
---|
| 337 | */ |
---|
[2068] | 338 | Camera* Orxonox::get_camera () |
---|
[1872] | 339 | { |
---|
[2068] | 340 | return localcamera; |
---|
[1872] | 341 | } |
---|
[1850] | 342 | |
---|
[2141] | 343 | /** |
---|
| 344 | \brief retrieve a pointer to the local CommandNode |
---|
| 345 | \return a pointer to localinput |
---|
| 346 | */ |
---|
[2101] | 347 | CommandNode* Orxonox::get_localinput () |
---|
[2096] | 348 | { |
---|
| 349 | return localinput; |
---|
| 350 | } |
---|
| 351 | |
---|
[2141] | 352 | /** |
---|
| 353 | \brief retrieve a pointer to the local World |
---|
| 354 | \return a pointer to world |
---|
| 355 | */ |
---|
[2101] | 356 | World* Orxonox::get_world () |
---|
[2058] | 357 | { |
---|
[2068] | 358 | return world; |
---|
[2058] | 359 | } |
---|
[1872] | 360 | |
---|
[1850] | 361 | int main (int argc, char** argv) |
---|
[1803] | 362 | { |
---|
[2105] | 363 | printf(">>> Starting Orxonox <<<\n"); |
---|
[1850] | 364 | Orxonox *orx = Orxonox::getInstance(); |
---|
[2058] | 365 | |
---|
[2068] | 366 | if( (*orx).init(argc, argv) == -1) |
---|
[2058] | 367 | { |
---|
[2068] | 368 | printf("! Orxonox initialization failed\n"); |
---|
[2058] | 369 | return -1; |
---|
| 370 | } |
---|
[2068] | 371 | |
---|
[2058] | 372 | (*orx).mainLoop(); |
---|
[2141] | 373 | |
---|
[2146] | 374 | //delete orx; |
---|
[2058] | 375 | |
---|
[1803] | 376 | return 0; |
---|
[2101] | 377 | } |
---|