[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 |
---|
| 23 | co-programmer: |
---|
[1850] | 24 | */ |
---|
| 25 | |
---|
[2058] | 26 | #include <iostream> |
---|
| 27 | #include <cstdio> |
---|
| 28 | #include <GL/glut.h> |
---|
| 29 | #include <SDL/SDL.h> |
---|
| 30 | |
---|
| 31 | #include "environment.h" |
---|
| 32 | #include "world.h" |
---|
| 33 | #include "input_output.h" |
---|
| 34 | #include "data_tank.h" |
---|
| 35 | #include "stdincl.h" |
---|
| 36 | #include "player.h" |
---|
| 37 | #include "npc.h" |
---|
| 38 | #include "shoot_laser.h" |
---|
| 39 | |
---|
[1803] | 40 | #include "orxonox.h" |
---|
| 41 | |
---|
| 42 | using namespace std; |
---|
| 43 | |
---|
| 44 | |
---|
[1872] | 45 | Orxonox::Orxonox () |
---|
| 46 | { |
---|
| 47 | pause = false; |
---|
| 48 | } |
---|
[1803] | 49 | |
---|
| 50 | |
---|
[1850] | 51 | |
---|
[1875] | 52 | Orxonox::~Orxonox () |
---|
[2058] | 53 | {} |
---|
[1850] | 54 | |
---|
| 55 | |
---|
| 56 | /* this is a singleton class to prevent dublicates */ |
---|
| 57 | Orxonox* Orxonox::singleton_ref = 0; |
---|
[1872] | 58 | World* Orxonox::world = 0; |
---|
| 59 | InputOutput* Orxonox::io = 0; |
---|
[1896] | 60 | Player* Orxonox::localPlayer = 0; |
---|
[1872] | 61 | bool Orxonox::pause = false; |
---|
[1900] | 62 | bool Orxonox::inputEnabled = false; |
---|
[1875] | 63 | bool Orxonox::upWeGo = false; |
---|
| 64 | bool Orxonox::downWeGo = false; |
---|
| 65 | bool Orxonox::rightWeGo = false; |
---|
| 66 | bool Orxonox::leftWeGo = false; |
---|
[1896] | 67 | bool Orxonox::shoot1 = false; |
---|
[1897] | 68 | int Orxonox::fps = 0; |
---|
[1879] | 69 | int Orxonox::alpha = 0; |
---|
| 70 | int Orxonox::beta = 0; |
---|
| 71 | //int Orxonox::offsetX = 0; |
---|
| 72 | //int Orxonox::offsetY = 0; |
---|
[1872] | 73 | |
---|
[2058] | 74 | |
---|
[1850] | 75 | Orxonox* Orxonox::getInstance (void) |
---|
[1803] | 76 | { |
---|
[1850] | 77 | if (singleton_ref == NULL) |
---|
| 78 | singleton_ref = new Orxonox(); |
---|
| 79 | return singleton_ref; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | int Orxonox::globalInit (int argc, char** argv) |
---|
| 84 | { |
---|
[2058] | 85 | if( SDL_Init (SDL_INIT_EVERYTHING) == -1) |
---|
| 86 | { |
---|
| 87 | printf ("Could not SDL_Init(): %s\n", SDL_GetError()); |
---|
| 88 | return -1; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // Set video mode |
---|
| 92 | // TO DO: parse arguments for settings |
---|
| 93 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); |
---|
| 94 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); |
---|
| 95 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); |
---|
| 96 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
---|
| 97 | |
---|
| 98 | int bpp = 16; |
---|
| 99 | int width = 640; |
---|
| 100 | int height = 480; |
---|
| 101 | Uint32 flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER; |
---|
| 102 | |
---|
| 103 | if( (screen = SDL_SetVideoMode (width, height, bpp, flags)) == NULL) |
---|
| 104 | { |
---|
| 105 | printf ("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", width, height, bpp, flags, SDL_GetError()); |
---|
| 106 | SDL_Quit(); |
---|
| 107 | return -1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | // Set window labeling |
---|
| 111 | // TO DO: Add version information to caption |
---|
| 112 | SDL_WM_SetCaption( "Orxonox", "Orxonox"); |
---|
| 113 | |
---|
| 114 | // TO DO: Create a cool icon and use it here |
---|
| 115 | // SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask); |
---|
| 116 | |
---|
| 117 | // OpenGL stuff |
---|
| 118 | // (Is this all we initialize globally???) |
---|
[1883] | 119 | glEnable(GL_DEPTH_TEST); |
---|
[1872] | 120 | glShadeModel(GL_FLAT); |
---|
[2058] | 121 | |
---|
| 122 | // glutInit(&argc, argv); |
---|
| 123 | // glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); |
---|
| 124 | // glutInitWindowSize(500, 500); |
---|
| 125 | // //glutFullScreen(); |
---|
| 126 | // glutInitWindowPosition(100, 100); |
---|
| 127 | // glutCreateWindow("OrxonoX"); |
---|
| 128 | // /* window event dispatchers */ |
---|
| 129 | // glutDisplayFunc(display); |
---|
| 130 | // glutReshapeFunc(reshape); |
---|
| 131 | // glutKeyboardFunc(keyboard); |
---|
| 132 | // glutKeyboardUpFunc(upKeyboard); |
---|
| 133 | // |
---|
| 134 | // glutTimerFunc(1000, timeSlice, 0); |
---|
| 135 | // cout << "measuring performance..."; |
---|
[1897] | 136 | |
---|
[1803] | 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[1850] | 140 | int Orxonox::menuInit (void) |
---|
[1803] | 141 | { |
---|
| 142 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
[1850] | 143 | } |
---|
[1849] | 144 | |
---|
[1850] | 145 | |
---|
| 146 | int Orxonox::gameInit (void) |
---|
| 147 | { |
---|
[1872] | 148 | glClearColor(0.0, 0.0, 0.0, 0.0); |
---|
[1899] | 149 | |
---|
| 150 | /* world init, shouldnt be done here later */ |
---|
[1872] | 151 | world = new World; |
---|
[1883] | 152 | (*world).initEnvironement(); |
---|
[1896] | 153 | localPlayer = new Player; |
---|
[1899] | 154 | localPlayer->setPosition(0.0, -10.0, 3.0); |
---|
[1902] | 155 | localPlayer->setCollisionRadius(2.0); |
---|
[1872] | 156 | io = new InputOutput(world, localPlayer); |
---|
| 157 | (*world).addPlayer(localPlayer); |
---|
[1883] | 158 | Environment *env = new Environment; |
---|
| 159 | (*world).addEnv(env); |
---|
[1899] | 160 | NPC* npc = new NPC; |
---|
| 161 | npc->setPosition(3.0, 0.0, 3.0); |
---|
| 162 | npc->setCollisionRadius(1.0); |
---|
| 163 | world->addNPC(npc); |
---|
[1850] | 164 | |
---|
[1900] | 165 | NPC* npc2 = new NPC; |
---|
| 166 | npc2->setPosition(-2.0, 10.0, 3.0); |
---|
| 167 | npc2->setCollisionRadius(1.0); |
---|
| 168 | world->addNPC(npc2); |
---|
| 169 | |
---|
[1872] | 170 | glutSpecialFunc(specFunc); |
---|
[1875] | 171 | glutSpecialUpFunc(releaseKey); |
---|
[1872] | 172 | |
---|
[1873] | 173 | glutIdleFunc(continousRedraw); |
---|
[1896] | 174 | //cout << "Orxonox::gameInit" << endl; |
---|
[1803] | 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
[1897] | 178 | /* this is the time triggered function. heart beat*/ |
---|
| 179 | |
---|
| 180 | void Orxonox::timeSlice(int value) |
---|
| 181 | { |
---|
[1917] | 182 | cout << "got " << fps << " fps" << endl; |
---|
[1900] | 183 | /* this is very very unsafe: io could be uninit */ |
---|
[1931] | 184 | io->setPlayerStep(19.2/fps); /* set player to propper speed */ |
---|
| 185 | localPlayer->shootLaser->setShootStep(20.0/fps); /* set shoot speed */ |
---|
| 186 | world->setWorldStep(7.0/fps); /* set the speed of the terrain moving away */ |
---|
[1897] | 187 | fps = 0; |
---|
[1900] | 188 | inputEnabled = true; |
---|
[1897] | 189 | glutTimerFunc(1000, timeSlice, 0); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
[1858] | 194 | void Orxonox::keyboard(unsigned char key, int x, int y) |
---|
| 195 | { |
---|
| 196 | switch(key) { |
---|
[1872] | 197 | |
---|
[1879] | 198 | /* perspectiv control */ |
---|
| 199 | case 'w': |
---|
| 200 | beta -= 1; |
---|
| 201 | break; |
---|
| 202 | case 's': |
---|
| 203 | beta += 1; |
---|
| 204 | break; |
---|
| 205 | case 'a': |
---|
| 206 | alpha -= 1; |
---|
| 207 | break; |
---|
| 208 | case 'd': |
---|
| 209 | alpha += 1; |
---|
| 210 | break; |
---|
| 211 | |
---|
| 212 | /* game controls */ |
---|
[1872] | 213 | case 'p': |
---|
| 214 | if (pause) |
---|
| 215 | { |
---|
| 216 | cout << "unset pause" << endl; |
---|
| 217 | glutIdleFunc(continousRedraw); |
---|
| 218 | pause = false; |
---|
| 219 | } |
---|
| 220 | else |
---|
| 221 | { |
---|
| 222 | cout << "set pause" << endl; |
---|
| 223 | glutIdleFunc(NULL); |
---|
| 224 | pause = true; |
---|
| 225 | } |
---|
| 226 | break; |
---|
[1896] | 227 | case 32: |
---|
| 228 | shoot1 = true; |
---|
| 229 | break; |
---|
[1858] | 230 | case 27: |
---|
[1883] | 231 | case 'q': |
---|
[1875] | 232 | quitGame(); |
---|
[1858] | 233 | break; |
---|
| 234 | } |
---|
| 235 | } |
---|
[1849] | 236 | |
---|
[1858] | 237 | |
---|
[1896] | 238 | void Orxonox::upKeyboard(unsigned char key, int x, int y) |
---|
| 239 | { |
---|
| 240 | switch(key) { |
---|
| 241 | case 32: |
---|
| 242 | shoot1 = false; |
---|
| 243 | break; |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
[1875] | 248 | void Orxonox::quitGame() |
---|
| 249 | { |
---|
[1896] | 250 | //cout << "finished garbage colletion, quitting..." << endl; |
---|
[1875] | 251 | exit(0); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | void Orxonox::releaseKey(int key, int x, int y) |
---|
| 256 | { |
---|
| 257 | switch(key) { |
---|
| 258 | case GLUT_KEY_UP: |
---|
| 259 | upWeGo = false; |
---|
| 260 | break; |
---|
| 261 | case GLUT_KEY_DOWN: |
---|
| 262 | downWeGo = false; |
---|
| 263 | break; |
---|
| 264 | case GLUT_KEY_RIGHT: |
---|
| 265 | rightWeGo = false; |
---|
| 266 | break; |
---|
| 267 | case GLUT_KEY_LEFT: |
---|
| 268 | leftWeGo = false; |
---|
| 269 | break; |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | |
---|
[1872] | 274 | /** |
---|
| 275 | \brief special keys function. called by glut |
---|
| 276 | |
---|
| 277 | Here are all special key function defined. |
---|
| 278 | */ |
---|
[1859] | 279 | void Orxonox::specFunc(int key, int x, int y) |
---|
| 280 | { |
---|
| 281 | switch(key) { |
---|
[1879] | 282 | /* spacecraft controls */ |
---|
[1859] | 283 | case GLUT_KEY_UP: |
---|
[1875] | 284 | upWeGo = true; |
---|
[1859] | 285 | break; |
---|
| 286 | case GLUT_KEY_DOWN: |
---|
[1875] | 287 | downWeGo = true; |
---|
[1859] | 288 | break; |
---|
| 289 | case GLUT_KEY_RIGHT: |
---|
[1875] | 290 | rightWeGo = true; |
---|
[1859] | 291 | break; |
---|
| 292 | case GLUT_KEY_LEFT: |
---|
[1875] | 293 | leftWeGo = true; |
---|
[1859] | 294 | break; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | |
---|
[1872] | 299 | void Orxonox::display() |
---|
[1803] | 300 | { |
---|
[1883] | 301 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
[1879] | 302 | |
---|
| 303 | glColor3f(0.0, 0.5, 0.6); |
---|
| 304 | glLoadIdentity(); |
---|
[1918] | 305 | gluLookAt(0.0, -14.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); |
---|
[1872] | 306 | (*world).drawWorld(); |
---|
[1879] | 307 | |
---|
[2058] | 308 | SDL_Flip( screen); |
---|
| 309 | } |
---|
[1803] | 310 | |
---|
[1850] | 311 | |
---|
[1872] | 312 | void Orxonox::continousRedraw() |
---|
| 313 | { |
---|
[1897] | 314 | /* increment the frames-per-second counter*/ |
---|
| 315 | fps++; |
---|
[1899] | 316 | /* check for collisions */ |
---|
| 317 | world->detectCollision(); |
---|
| 318 | |
---|
[1875] | 319 | /* check for input to pass it over */ |
---|
[1900] | 320 | if ( !pause && inputEnabled && |
---|
| 321 | (rightWeGo || leftWeGo || upWeGo || downWeGo || shoot1)) |
---|
| 322 | { |
---|
| 323 | if (upWeGo) |
---|
| 324 | (*io).goUp(); |
---|
| 325 | if (downWeGo) |
---|
[1875] | 326 | (*io).goDown(); |
---|
[1900] | 327 | if (rightWeGo) |
---|
| 328 | (*io).goRight(); |
---|
| 329 | if (leftWeGo) |
---|
[1875] | 330 | (*io).goLeft(); |
---|
[1900] | 331 | if (shoot1) |
---|
| 332 | (*io).shoot(); |
---|
| 333 | } |
---|
[1875] | 334 | /* request repaint */ |
---|
[1872] | 335 | glutPostRedisplay(); |
---|
[1896] | 336 | //cout << "Orxonox::continousRedraw" << endl; |
---|
[1872] | 337 | } |
---|
[1850] | 338 | |
---|
[1872] | 339 | |
---|
[1850] | 340 | void Orxonox::reshape (int w, int h) |
---|
| 341 | { |
---|
| 342 | glViewport(0, 0, (GLsizei) w, (GLsizei) h); |
---|
| 343 | glMatrixMode(GL_PROJECTION); |
---|
| 344 | glLoadIdentity(); |
---|
[1879] | 345 | glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0); |
---|
[1850] | 346 | glMatrixMode(GL_MODELVIEW); |
---|
| 347 | glLoadIdentity(); //pb why a second time? |
---|
[1803] | 348 | } |
---|
| 349 | |
---|
| 350 | |
---|
[1872] | 351 | void Orxonox::testTheShit() |
---|
| 352 | { |
---|
[1879] | 353 | //Player* pl = new Player; |
---|
| 354 | //(*pl).setPosition(1, 1, 1); |
---|
| 355 | //(*world).addPlayer(pl); |
---|
[1872] | 356 | |
---|
| 357 | //NPC* nl = new NPC; |
---|
| 358 | //(*world).addNPC(nl); |
---|
| 359 | //(*world).addNPC(nl); |
---|
| 360 | //(*world).addNPC(nl); |
---|
| 361 | //(*world).addNPC(nl); |
---|
| 362 | //(*world).addNPC(nl); |
---|
| 363 | //(*world).addNPC(nl); |
---|
| 364 | //(*world).testThaTest(); |
---|
| 365 | } |
---|
[1850] | 366 | |
---|
[2058] | 367 | void Orxonox::mainLoop() |
---|
| 368 | { |
---|
| 369 | // This is where everything is run |
---|
| 370 | while( !bQuitOrxonox) |
---|
| 371 | { |
---|
| 372 | // Process input |
---|
| 373 | // Process time |
---|
| 374 | // Process collision |
---|
| 375 | // Draw |
---|
| 376 | display(); |
---|
| 377 | } |
---|
| 378 | } |
---|
[1872] | 379 | |
---|
[1850] | 380 | int main (int argc, char** argv) |
---|
[1803] | 381 | { |
---|
[1850] | 382 | Orxonox *orx = Orxonox::getInstance(); |
---|
[2058] | 383 | if( (*orx).globalInit(argc, argv) == -1) |
---|
| 384 | { |
---|
| 385 | printf("! Global initialization failed\n"); |
---|
| 386 | return -1; |
---|
| 387 | } |
---|
| 388 | |
---|
[1872] | 389 | //(*orx).menuInit(); pb: directly jump to the game, no menu |
---|
[2058] | 390 | |
---|
| 391 | if( (*orx).gameInit() == -1) |
---|
| 392 | { |
---|
| 393 | printf("! Game initialization failed\n"); |
---|
| 394 | return -1; |
---|
| 395 | } |
---|
[1856] | 396 | |
---|
[2058] | 397 | (*orx).mainLoop(); |
---|
| 398 | |
---|
[1803] | 399 | return 0; |
---|
| 400 | } |
---|
[2058] | 401 | |
---|