| 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: |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #include <iostream> |
|---|
| 27 | #include <cstdio> |
|---|
| 28 | #include <GL/glut.h> |
|---|
| 29 | |
|---|
| 30 | #include "environment.h" |
|---|
| 31 | #include "world.h" |
|---|
| 32 | #include "input_output.h" |
|---|
| 33 | #include "data_tank.h" |
|---|
| 34 | #include "stdincl.h" |
|---|
| 35 | #include "player.h" |
|---|
| 36 | #include "npc.h" |
|---|
| 37 | #include "shoot_laser.h" |
|---|
| 38 | |
|---|
| 39 | #include "orxonox.h" |
|---|
| 40 | |
|---|
| 41 | using namespace std; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | Orxonox::Orxonox () |
|---|
| 45 | { |
|---|
| 46 | pause = false; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | Orxonox::~Orxonox () |
|---|
| 52 | {} |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | /* this is a singleton class to prevent dublicates */ |
|---|
| 56 | Orxonox* Orxonox::singleton_ref = 0; |
|---|
| 57 | World* Orxonox::world = 0; |
|---|
| 58 | InputOutput* Orxonox::io = 0; |
|---|
| 59 | Player* Orxonox::localPlayer = 0; |
|---|
| 60 | bool Orxonox::pause = false; |
|---|
| 61 | bool Orxonox::inputEnabled = false; |
|---|
| 62 | bool Orxonox::upWeGo = false; |
|---|
| 63 | bool Orxonox::downWeGo = false; |
|---|
| 64 | bool Orxonox::rightWeGo = false; |
|---|
| 65 | bool Orxonox::leftWeGo = false; |
|---|
| 66 | bool Orxonox::shoot1 = false; |
|---|
| 67 | int Orxonox::fps = 0; |
|---|
| 68 | int Orxonox::alpha = 0; |
|---|
| 69 | int Orxonox::beta = 0; |
|---|
| 70 | //int Orxonox::offsetX = 0; |
|---|
| 71 | //int Orxonox::offsetY = 0; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | Orxonox* Orxonox::getInstance (void) |
|---|
| 75 | { |
|---|
| 76 | if (singleton_ref == NULL) |
|---|
| 77 | singleton_ref = new Orxonox(); |
|---|
| 78 | return singleton_ref; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | int Orxonox::globalInit (int argc, char** argv) |
|---|
| 83 | { |
|---|
| 84 | glutInit(&argc, argv); |
|---|
| 85 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); |
|---|
| 86 | glEnable(GL_DEPTH_TEST); |
|---|
| 87 | glutInitWindowSize(500, 500); |
|---|
| 88 | //glutFullScreen(); |
|---|
| 89 | glutInitWindowPosition(100, 100); |
|---|
| 90 | glutCreateWindow("OrxonoX"); |
|---|
| 91 | glShadeModel(GL_FLAT); |
|---|
| 92 | /* window event dispatchers */ |
|---|
| 93 | glutDisplayFunc(display); |
|---|
| 94 | glutReshapeFunc(reshape); |
|---|
| 95 | glutKeyboardFunc(keyboard); |
|---|
| 96 | glutKeyboardUpFunc(upKeyboard); |
|---|
| 97 | |
|---|
| 98 | glutTimerFunc(1000, timeSlice, 0); |
|---|
| 99 | cout << "measuring performance..."; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | int Orxonox::menuInit (void) |
|---|
| 104 | { |
|---|
| 105 | glClearColor(0.0, 0.0, 0.0, 0.0); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | int Orxonox::gameInit (void) |
|---|
| 110 | { |
|---|
| 111 | glClearColor(0.0, 0.0, 0.0, 0.0); |
|---|
| 112 | |
|---|
| 113 | /* world init, shouldnt be done here later */ |
|---|
| 114 | world = new World; |
|---|
| 115 | (*world).initEnvironement(); |
|---|
| 116 | localPlayer = new Player; |
|---|
| 117 | localPlayer->setPosition(0.0, -10.0, 3.0); |
|---|
| 118 | localPlayer->setCollisionRadius(2.0); |
|---|
| 119 | io = new InputOutput(world, localPlayer); |
|---|
| 120 | (*world).addPlayer(localPlayer); |
|---|
| 121 | Environment *env = new Environment; |
|---|
| 122 | (*world).addEnv(env); |
|---|
| 123 | NPC* npc = new NPC; |
|---|
| 124 | npc->setPosition(3.0, 0.0, 3.0); |
|---|
| 125 | npc->setCollisionRadius(1.0); |
|---|
| 126 | world->addNPC(npc); |
|---|
| 127 | |
|---|
| 128 | NPC* npc2 = new NPC; |
|---|
| 129 | npc2->setPosition(-2.0, 10.0, 3.0); |
|---|
| 130 | npc2->setCollisionRadius(1.0); |
|---|
| 131 | world->addNPC(npc2); |
|---|
| 132 | |
|---|
| 133 | glutSpecialFunc(specFunc); |
|---|
| 134 | glutSpecialUpFunc(releaseKey); |
|---|
| 135 | |
|---|
| 136 | glutIdleFunc(continousRedraw); |
|---|
| 137 | //cout << "Orxonox::gameInit" << endl; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | /* this is the time triggered function. heart beat*/ |
|---|
| 142 | |
|---|
| 143 | void Orxonox::timeSlice(int value) |
|---|
| 144 | { |
|---|
| 145 | cout << "got " << fps << " fps" << endl; |
|---|
| 146 | /* this is very very unsafe: io could be uninit */ |
|---|
| 147 | io->setPlayerStep(19.2/fps); /* set player to propper speed */ |
|---|
| 148 | localPlayer->shootLaser->setShootStep(20.0/fps); /* set shoot speed */ |
|---|
| 149 | world->setWorldStep(7.0/fps); /* set the speed of the terrain moving away */ |
|---|
| 150 | fps = 0; |
|---|
| 151 | inputEnabled = true; |
|---|
| 152 | glutTimerFunc(1000, timeSlice, 0); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | void Orxonox::keyboard(unsigned char key, int x, int y) |
|---|
| 158 | { |
|---|
| 159 | switch(key) { |
|---|
| 160 | |
|---|
| 161 | /* perspectiv control */ |
|---|
| 162 | case 'w': |
|---|
| 163 | beta -= 1; |
|---|
| 164 | break; |
|---|
| 165 | case 's': |
|---|
| 166 | beta += 1; |
|---|
| 167 | break; |
|---|
| 168 | case 'a': |
|---|
| 169 | alpha -= 1; |
|---|
| 170 | break; |
|---|
| 171 | case 'd': |
|---|
| 172 | alpha += 1; |
|---|
| 173 | break; |
|---|
| 174 | |
|---|
| 175 | /* game controls */ |
|---|
| 176 | case 'p': |
|---|
| 177 | if (pause) |
|---|
| 178 | { |
|---|
| 179 | cout << "unset pause" << endl; |
|---|
| 180 | glutIdleFunc(continousRedraw); |
|---|
| 181 | pause = false; |
|---|
| 182 | } |
|---|
| 183 | else |
|---|
| 184 | { |
|---|
| 185 | cout << "set pause" << endl; |
|---|
| 186 | glutIdleFunc(NULL); |
|---|
| 187 | pause = true; |
|---|
| 188 | } |
|---|
| 189 | break; |
|---|
| 190 | case 32: |
|---|
| 191 | shoot1 = true; |
|---|
| 192 | break; |
|---|
| 193 | case 27: |
|---|
| 194 | case 'q': |
|---|
| 195 | quitGame(); |
|---|
| 196 | break; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | void Orxonox::upKeyboard(unsigned char key, int x, int y) |
|---|
| 202 | { |
|---|
| 203 | switch(key) { |
|---|
| 204 | case 32: |
|---|
| 205 | shoot1 = false; |
|---|
| 206 | break; |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | void Orxonox::quitGame() |
|---|
| 212 | { |
|---|
| 213 | //cout << "finished garbage colletion, quitting..." << endl; |
|---|
| 214 | exit(0); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | void Orxonox::releaseKey(int key, int x, int y) |
|---|
| 219 | { |
|---|
| 220 | switch(key) { |
|---|
| 221 | case GLUT_KEY_UP: |
|---|
| 222 | upWeGo = false; |
|---|
| 223 | break; |
|---|
| 224 | case GLUT_KEY_DOWN: |
|---|
| 225 | downWeGo = false; |
|---|
| 226 | break; |
|---|
| 227 | case GLUT_KEY_RIGHT: |
|---|
| 228 | rightWeGo = false; |
|---|
| 229 | break; |
|---|
| 230 | case GLUT_KEY_LEFT: |
|---|
| 231 | leftWeGo = false; |
|---|
| 232 | break; |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | \brief special keys function. called by glut |
|---|
| 239 | |
|---|
| 240 | Here are all special key function defined. |
|---|
| 241 | */ |
|---|
| 242 | void Orxonox::specFunc(int key, int x, int y) |
|---|
| 243 | { |
|---|
| 244 | switch(key) { |
|---|
| 245 | /* spacecraft controls */ |
|---|
| 246 | case GLUT_KEY_UP: |
|---|
| 247 | upWeGo = true; |
|---|
| 248 | break; |
|---|
| 249 | case GLUT_KEY_DOWN: |
|---|
| 250 | downWeGo = true; |
|---|
| 251 | break; |
|---|
| 252 | case GLUT_KEY_RIGHT: |
|---|
| 253 | rightWeGo = true; |
|---|
| 254 | break; |
|---|
| 255 | case GLUT_KEY_LEFT: |
|---|
| 256 | leftWeGo = true; |
|---|
| 257 | break; |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | void Orxonox::display() |
|---|
| 263 | { |
|---|
| 264 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 265 | |
|---|
| 266 | glColor3f(0.0, 0.5, 0.6); |
|---|
| 267 | glLoadIdentity(); |
|---|
| 268 | gluLookAt(0.0, -14.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); |
|---|
| 269 | (*world).drawWorld(); |
|---|
| 270 | |
|---|
| 271 | glutSwapBuffers(); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | void Orxonox::continousRedraw() |
|---|
| 276 | { |
|---|
| 277 | /* increment the frames-per-second counter*/ |
|---|
| 278 | fps++; |
|---|
| 279 | /* check for collisions */ |
|---|
| 280 | world->detectCollision(); |
|---|
| 281 | |
|---|
| 282 | /* check for input to pass it over */ |
|---|
| 283 | if ( !pause && inputEnabled && |
|---|
| 284 | (rightWeGo || leftWeGo || upWeGo || downWeGo || shoot1)) |
|---|
| 285 | { |
|---|
| 286 | if (upWeGo) |
|---|
| 287 | (*io).goUp(); |
|---|
| 288 | if (downWeGo) |
|---|
| 289 | (*io).goDown(); |
|---|
| 290 | if (rightWeGo) |
|---|
| 291 | (*io).goRight(); |
|---|
| 292 | if (leftWeGo) |
|---|
| 293 | (*io).goLeft(); |
|---|
| 294 | if (shoot1) |
|---|
| 295 | (*io).shoot(); |
|---|
| 296 | } |
|---|
| 297 | /* request repaint */ |
|---|
| 298 | glutPostRedisplay(); |
|---|
| 299 | //cout << "Orxonox::continousRedraw" << endl; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | void Orxonox::reshape (int w, int h) |
|---|
| 304 | { |
|---|
| 305 | glViewport(0, 0, (GLsizei) w, (GLsizei) h); |
|---|
| 306 | glMatrixMode(GL_PROJECTION); |
|---|
| 307 | glLoadIdentity(); |
|---|
| 308 | glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0); |
|---|
| 309 | glMatrixMode(GL_MODELVIEW); |
|---|
| 310 | glLoadIdentity(); //pb why a second time? |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | void Orxonox::testTheShit() |
|---|
| 315 | { |
|---|
| 316 | //Player* pl = new Player; |
|---|
| 317 | //(*pl).setPosition(1, 1, 1); |
|---|
| 318 | //(*world).addPlayer(pl); |
|---|
| 319 | |
|---|
| 320 | //NPC* nl = new NPC; |
|---|
| 321 | //(*world).addNPC(nl); |
|---|
| 322 | //(*world).addNPC(nl); |
|---|
| 323 | //(*world).addNPC(nl); |
|---|
| 324 | //(*world).addNPC(nl); |
|---|
| 325 | //(*world).addNPC(nl); |
|---|
| 326 | //(*world).addNPC(nl); |
|---|
| 327 | //(*world).testThaTest(); |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | int main (int argc, char** argv) |
|---|
| 332 | { |
|---|
| 333 | Orxonox *orx = Orxonox::getInstance(); |
|---|
| 334 | (*orx).globalInit(argc, argv); |
|---|
| 335 | //(*orx).menuInit(); pb: directly jump to the game, no menu |
|---|
| 336 | (*orx).gameInit(); |
|---|
| 337 | |
|---|
| 338 | glutMainLoop(); |
|---|
| 339 | return 0; |
|---|
| 340 | } |
|---|