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