| 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 | bool Orxonox::pause = false; |
|---|
| 52 | bool Orxonox::upWeGo = false; |
|---|
| 53 | bool Orxonox::downWeGo = false; |
|---|
| 54 | bool Orxonox::rightWeGo = false; |
|---|
| 55 | bool Orxonox::leftWeGo = false; |
|---|
| 56 | |
|---|
| 57 | Orxonox* Orxonox::getInstance (void) |
|---|
| 58 | { |
|---|
| 59 | if (singleton_ref == NULL) |
|---|
| 60 | singleton_ref = new Orxonox(); |
|---|
| 61 | return singleton_ref; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | int Orxonox::globalInit (int argc, char** argv) |
|---|
| 66 | { |
|---|
| 67 | glutInit(&argc, argv); |
|---|
| 68 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
|---|
| 69 | glutInitWindowSize(500, 500); |
|---|
| 70 | glutInitWindowPosition(100, 100); |
|---|
| 71 | glutCreateWindow("orxOnox"); |
|---|
| 72 | glShadeModel(GL_FLAT); |
|---|
| 73 | /* window event dispatchers */ |
|---|
| 74 | glutDisplayFunc(display); |
|---|
| 75 | glutReshapeFunc(reshape); |
|---|
| 76 | glutKeyboardFunc(keyboard); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | int Orxonox::menuInit (void) |
|---|
| 81 | { |
|---|
| 82 | glClearColor(0.0, 0.0, 0.0, 0.0); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | int Orxonox::gameInit (void) |
|---|
| 87 | { |
|---|
| 88 | glClearColor(0.0, 0.0, 0.0, 0.0); |
|---|
| 89 | world = new World; |
|---|
| 90 | Player* localPlayer = new Player; |
|---|
| 91 | io = new InputOutput(world, localPlayer); |
|---|
| 92 | (*world).addPlayer(localPlayer); |
|---|
| 93 | // (*localPlayer).addIO(io); |
|---|
| 94 | |
|---|
| 95 | glutIgnoreKeyRepeat(1); /* for win32 */ |
|---|
| 96 | glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF); /* for linux X11 */ |
|---|
| 97 | glutSpecialFunc(specFunc); |
|---|
| 98 | glutSpecialUpFunc(releaseKey); |
|---|
| 99 | |
|---|
| 100 | //for testing purps only |
|---|
| 101 | //testTheShit(); |
|---|
| 102 | glutIdleFunc(continousRedraw); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | void Orxonox::keyboard(unsigned char key, int x, int y) |
|---|
| 107 | { |
|---|
| 108 | switch(key) { |
|---|
| 109 | |
|---|
| 110 | case 'p': |
|---|
| 111 | if (pause) |
|---|
| 112 | { |
|---|
| 113 | cout << "unset pause" << endl; |
|---|
| 114 | glutIdleFunc(continousRedraw); |
|---|
| 115 | pause = false; |
|---|
| 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | cout << "set pause" << endl; |
|---|
| 120 | glutIdleFunc(NULL); |
|---|
| 121 | pause = true; |
|---|
| 122 | } |
|---|
| 123 | break; |
|---|
| 124 | case 27: |
|---|
| 125 | quitGame(); |
|---|
| 126 | break; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | void Orxonox::quitGame() |
|---|
| 132 | { |
|---|
| 133 | glutSetKeyRepeat(GLUT_KEY_REPEAT_DEFAULT); /*do not remove or you will have |
|---|
| 134 | no repeating keys anymore...*/ |
|---|
| 135 | cout << "finished garbage colletion, quitting..." << endl; |
|---|
| 136 | exit(0); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | void Orxonox::releaseKey(int key, int x, int y) |
|---|
| 141 | { |
|---|
| 142 | switch(key) { |
|---|
| 143 | case GLUT_KEY_UP: |
|---|
| 144 | upWeGo = false; |
|---|
| 145 | break; |
|---|
| 146 | case GLUT_KEY_DOWN: |
|---|
| 147 | downWeGo = false; |
|---|
| 148 | break; |
|---|
| 149 | case GLUT_KEY_RIGHT: |
|---|
| 150 | rightWeGo = false; |
|---|
| 151 | break; |
|---|
| 152 | case GLUT_KEY_LEFT: |
|---|
| 153 | leftWeGo = false; |
|---|
| 154 | break; |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | \brief special keys function. called by glut |
|---|
| 161 | |
|---|
| 162 | Here are all special key function defined. |
|---|
| 163 | */ |
|---|
| 164 | void Orxonox::specFunc(int key, int x, int y) |
|---|
| 165 | { |
|---|
| 166 | switch(key) { |
|---|
| 167 | case GLUT_KEY_UP: |
|---|
| 168 | upWeGo = true; |
|---|
| 169 | break; |
|---|
| 170 | case GLUT_KEY_DOWN: |
|---|
| 171 | downWeGo = true; |
|---|
| 172 | break; |
|---|
| 173 | case GLUT_KEY_RIGHT: |
|---|
| 174 | rightWeGo = true; |
|---|
| 175 | break; |
|---|
| 176 | case GLUT_KEY_LEFT: |
|---|
| 177 | leftWeGo = true; |
|---|
| 178 | break; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | void Orxonox::display() |
|---|
| 184 | { |
|---|
| 185 | glClear(GL_COLOR_BUFFER_BIT); |
|---|
| 186 | (*world).drawWorld(); |
|---|
| 187 | glutSwapBuffers(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | void Orxonox::continousRedraw() |
|---|
| 192 | { |
|---|
| 193 | /* check for input to pass it over */ |
|---|
| 194 | if ( !pause && (rightWeGo || leftWeGo || upWeGo || downWeGo)) { |
|---|
| 195 | if (upWeGo) |
|---|
| 196 | (*io).goUp(); |
|---|
| 197 | if (downWeGo) |
|---|
| 198 | (*io).goDown(); |
|---|
| 199 | if (rightWeGo) |
|---|
| 200 | (*io).goRight(); |
|---|
| 201 | if (leftWeGo) |
|---|
| 202 | (*io).goLeft(); |
|---|
| 203 | } |
|---|
| 204 | /* request repaint */ |
|---|
| 205 | glutPostRedisplay(); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | void Orxonox::reshape (int w, int h) |
|---|
| 210 | { |
|---|
| 211 | glViewport(0, 0, (GLsizei) w, (GLsizei) h); |
|---|
| 212 | glMatrixMode(GL_PROJECTION); |
|---|
| 213 | glLoadIdentity(); |
|---|
| 214 | glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); |
|---|
| 215 | glMatrixMode(GL_MODELVIEW); |
|---|
| 216 | glLoadIdentity(); //pb why a second time? |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | void Orxonox::testTheShit() |
|---|
| 221 | { |
|---|
| 222 | Player* pl = new Player; |
|---|
| 223 | (*pl).setPosition(1, 1, 1); |
|---|
| 224 | (*world).addPlayer(pl); |
|---|
| 225 | |
|---|
| 226 | //NPC* nl = new NPC; |
|---|
| 227 | //(*world).addNPC(nl); |
|---|
| 228 | //(*world).addNPC(nl); |
|---|
| 229 | //(*world).addNPC(nl); |
|---|
| 230 | //(*world).addNPC(nl); |
|---|
| 231 | //(*world).addNPC(nl); |
|---|
| 232 | //(*world).addNPC(nl); |
|---|
| 233 | //(*world).testThaTest(); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | int main (int argc, char** argv) |
|---|
| 238 | { |
|---|
| 239 | Orxonox *orx = Orxonox::getInstance(); |
|---|
| 240 | (*orx).globalInit(argc, argv); |
|---|
| 241 | //(*orx).menuInit(); pb: directly jump to the game, no menu |
|---|
| 242 | (*orx).gameInit(); |
|---|
| 243 | |
|---|
| 244 | glutMainLoop(); |
|---|
| 245 | return 0; |
|---|
| 246 | } |
|---|