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