[3140] | 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 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[2931] | 16 | #include "framework.h" |
---|
[3427] | 17 | |
---|
| 18 | |
---|
[4294] | 19 | #include "p_node.h" |
---|
| 20 | #include "null_parent.h" |
---|
| 21 | #include "state.h" |
---|
[4272] | 22 | #include "debug.h" |
---|
| 23 | #include "graphics_engine.h" |
---|
[4300] | 24 | #include "light.h" |
---|
[4295] | 25 | #include "resource_manager.h" |
---|
[4297] | 26 | #include "camera.h" |
---|
[3657] | 27 | |
---|
[4316] | 28 | |
---|
[3398] | 29 | int verbose; |
---|
[4293] | 30 | |
---|
[4330] | 31 | void* Framework::mainLoop(void* tmp) |
---|
[2748] | 32 | { |
---|
[4330] | 33 | Framework* framework = Framework::getInstance(); |
---|
[4331] | 34 | while(!framework->isFinished) |
---|
[4293] | 35 | { |
---|
[4343] | 36 | #ifdef GUI_MODULE |
---|
[4358] | 37 | while(gtk_events_pending()) |
---|
[4331] | 38 | gtk_main_iteration(); |
---|
[4343] | 39 | #endif |
---|
[4297] | 40 | // keyhandler returns false if sdl gets quit by some event |
---|
[4334] | 41 | framework->eventHandler(); |
---|
[2939] | 42 | |
---|
[4297] | 43 | // tick the scene |
---|
[4330] | 44 | float dt = framework->tick(); |
---|
[4297] | 45 | |
---|
[4333] | 46 | NullParent::getInstance()->update(dt); |
---|
| 47 | |
---|
[4293] | 48 | // Draw the scene |
---|
[4330] | 49 | framework->draw(dt); |
---|
[4331] | 50 | |
---|
[4293] | 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
[4297] | 54 | bool Framework::draw(float dt) |
---|
[4293] | 55 | { |
---|
[2748] | 56 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
| 57 | glLoadIdentity(); // Reset the view |
---|
| 58 | |
---|
[4349] | 59 | this->moduleDraw(); |
---|
[4333] | 60 | |
---|
[4297] | 61 | camera->apply(); |
---|
[4294] | 62 | |
---|
[2748] | 63 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
| 64 | } |
---|
[4300] | 65 | |
---|
| 66 | |
---|
[4297] | 67 | float Framework::tick() |
---|
[2748] | 68 | { |
---|
[4293] | 69 | currFrame = SDL_GetTicks(); |
---|
[4300] | 70 | float dt = (float)(currFrame - lastFrame) / 1000.0; |
---|
| 71 | lastFrame = currFrame; |
---|
[3398] | 72 | |
---|
[4333] | 73 | this->moduleTick(dt); |
---|
[2963] | 74 | |
---|
[4294] | 75 | return dt; |
---|
[4293] | 76 | } |
---|
[2952] | 77 | |
---|
[2931] | 78 | |
---|
[4334] | 79 | bool Framework::eventHandler() |
---|
[4293] | 80 | { |
---|
[2748] | 81 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
| 82 | { |
---|
| 83 | // And poll for events |
---|
| 84 | SDL_Event event; |
---|
[4305] | 85 | while(SDL_PollEvent(&event)) |
---|
[4303] | 86 | { |
---|
[4334] | 87 | moduleEventHandler(&event); |
---|
| 88 | |
---|
[2748] | 89 | switch (event.type) { |
---|
[2931] | 90 | case SDL_MOUSEMOTION: |
---|
[4306] | 91 | { |
---|
| 92 | Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor(); |
---|
| 93 | Vector up = Vector(0, 1, 0); |
---|
| 94 | up = camera->getAbsDir().apply(up); |
---|
| 95 | Vector h = up.cross(view); |
---|
| 96 | Vector v = h.cross(view); |
---|
| 97 | h.normalize(); |
---|
| 98 | v.normalize(); |
---|
[4358] | 99 | float distance = view.len(); |
---|
| 100 | |
---|
| 101 | Vector newCameraPos = camera->getAbsCoor(); |
---|
| 102 | Vector newTargetPos = camera->getTarget()->getAbsCoor(); |
---|
| 103 | int changed = 0; |
---|
[4306] | 104 | |
---|
| 105 | if (mouseDown[1]) |
---|
[4358] | 106 | { |
---|
| 107 | newCameraPos = camera->getRelCoor()+ (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
---|
| 108 | changed += 1; |
---|
| 109 | } |
---|
[4306] | 110 | if (mouseDown[3]) |
---|
[4358] | 111 | { |
---|
| 112 | newTargetPos = camera->getTarget()->getRelCoor() + (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
---|
| 113 | changed += 2; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | Vector newView = newTargetPos - newCameraPos; |
---|
| 117 | |
---|
| 118 | if (changed == 1) |
---|
| 119 | camera->setRelCoor(newCameraPos + newView * (1- distance/newView.len())); |
---|
| 120 | else if (changed == 2) |
---|
| 121 | camera->getTarget()->setRelCoor(newTargetPos - newView * (1-distance/newView.len())); |
---|
| 122 | else if (changed == 3) |
---|
| 123 | { |
---|
| 124 | camera->setRelCoor(newCameraPos); |
---|
| 125 | camera->getTarget()->setRelCoor(newTargetPos); |
---|
| 126 | } |
---|
[4300] | 127 | |
---|
[4306] | 128 | } |
---|
[2931] | 129 | break; |
---|
| 130 | case SDL_MOUSEBUTTONDOWN: |
---|
[4306] | 131 | switch (event.button.button) |
---|
[2933] | 132 | { |
---|
[4306] | 133 | case 4: |
---|
[3656] | 134 | PRINTF(4)("MouseWheel up\n"); |
---|
[4310] | 135 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4306] | 136 | break; |
---|
| 137 | case 5: |
---|
[3656] | 138 | PRINTF(4)("MouseWheel down\n"); |
---|
[4310] | 139 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4306] | 140 | break; |
---|
| 141 | case 1: |
---|
| 142 | case 2: |
---|
| 143 | case 3: |
---|
| 144 | mouseDown[event.button.button] = true; |
---|
| 145 | break; |
---|
[2933] | 146 | } |
---|
[2932] | 147 | |
---|
[2931] | 148 | break; |
---|
[2952] | 149 | case SDL_MOUSEBUTTONUP: |
---|
[4306] | 150 | switch (event.button.button) |
---|
| 151 | { |
---|
| 152 | case 1: |
---|
| 153 | case 2: |
---|
| 154 | case 3: |
---|
| 155 | mouseDown[event.button.button] = false; |
---|
| 156 | break; |
---|
| 157 | } |
---|
[2931] | 158 | break; |
---|
[4302] | 159 | case SDL_VIDEORESIZE: |
---|
| 160 | GraphicsEngine::getInstance()->resolutionChanged(&event.resize); |
---|
| 161 | break; |
---|
[3211] | 162 | case SDL_KEYDOWN: |
---|
| 163 | switch (event.key.keysym.sym) |
---|
| 164 | { |
---|
[4331] | 165 | case SDLK_q: |
---|
| 166 | case SDLK_ESCAPE: |
---|
[4343] | 167 | #ifdef GUI_MODULE |
---|
[4331] | 168 | quitGui(NULL, NULL); |
---|
[4343] | 169 | #else |
---|
| 170 | this->quit(); |
---|
| 171 | #endif |
---|
[3211] | 172 | break; |
---|
[3418] | 173 | case SDLK_a: |
---|
[4310] | 174 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[3418] | 175 | break; |
---|
| 176 | case SDLK_z: |
---|
[4310] | 177 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4300] | 178 | break; |
---|
[4360] | 179 | case SDLK_r: |
---|
| 180 | camera->setAbsCoor(Vector(10, 10, 10)); |
---|
| 181 | camera->getTarget()->setAbsCoor(Vector()); |
---|
| 182 | break; |
---|
[4307] | 183 | case SDLK_h: |
---|
| 184 | this->printHelp(); |
---|
| 185 | break; |
---|
[4374] | 186 | case SDLK_2: |
---|
| 187 | for (int i = 0; i < 3; i++) |
---|
| 188 | { |
---|
| 189 | backgroundColor[i] += .1; |
---|
| 190 | if (backgroundColor[i] > 1.0) |
---|
| 191 | backgroundColor[i] = 1.0; |
---|
| 192 | GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]); |
---|
| 193 | } |
---|
| 194 | break; |
---|
| 195 | case SDLK_1: |
---|
| 196 | for (int i = 0; i < 3; i++) |
---|
| 197 | { |
---|
| 198 | backgroundColor[i] -= .1; |
---|
| 199 | if (backgroundColor[i] < 0.0) |
---|
| 200 | backgroundColor[i] = 0.0; |
---|
| 201 | GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]); |
---|
| 202 | } |
---|
| 203 | break; |
---|
[3211] | 204 | } |
---|
| 205 | break; |
---|
[2952] | 206 | |
---|
[2748] | 207 | // If a quit event was recieved |
---|
[2863] | 208 | case SDL_QUIT: |
---|
| 209 | // then we're done and we'll end this program |
---|
[4343] | 210 | #ifdef GUI_MODULE |
---|
| 211 | quitGui(NULL, NULL); |
---|
| 212 | #else |
---|
| 213 | this->quit(); |
---|
| 214 | #endif |
---|
[4293] | 215 | break; |
---|
[2863] | 216 | default: |
---|
[4293] | 217 | break; |
---|
[2748] | 218 | } |
---|
[2952] | 219 | |
---|
[2748] | 220 | } |
---|
| 221 | |
---|
| 222 | // Get the state of the keyboard keys |
---|
| 223 | keys = SDL_GetKeyState(NULL); |
---|
| 224 | |
---|
| 225 | // and check if ESCAPE has been pressed. If so then quit |
---|
[4293] | 226 | if(keys[SDLK_ESCAPE]) return false; |
---|
[2748] | 227 | } |
---|
[4293] | 228 | return true; |
---|
| 229 | } |
---|
[2748] | 230 | |
---|
[4331] | 231 | void Framework::quit(void) |
---|
| 232 | { |
---|
| 233 | this->isFinished = true; |
---|
| 234 | } |
---|
| 235 | |
---|
[4317] | 236 | Framework* Framework::singletonRef = NULL; |
---|
[4293] | 237 | |
---|
[4317] | 238 | Framework* Framework::getInstance(void) |
---|
| 239 | { |
---|
| 240 | if (Framework::singletonRef == NULL) |
---|
| 241 | Framework::singletonRef = new Framework(); |
---|
| 242 | return Framework::singletonRef; |
---|
| 243 | } |
---|
| 244 | |
---|
[4300] | 245 | Framework::Framework() |
---|
[4293] | 246 | { |
---|
[4331] | 247 | this->isFinished = false; |
---|
| 248 | |
---|
[4304] | 249 | this->lastFrame = 0; |
---|
[4293] | 250 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
| 251 | // 640x480x32, fullscreen and check for errors along the way |
---|
| 252 | GraphicsEngine::getInstance(); |
---|
| 253 | |
---|
[4300] | 254 | LightManager::getInstance(); |
---|
| 255 | glEnable(GL_TEXTURE_2D); |
---|
[4293] | 256 | |
---|
| 257 | // Build the font from a TGA image font.tga in the data directory |
---|
| 258 | // Hide the mouse cursor |
---|
| 259 | SDL_ShowCursor(2); |
---|
| 260 | |
---|
[4374] | 261 | for (int i = 0; i < MOUSE_BUTTON_COUNT; i++) |
---|
[4306] | 262 | mouseDown[i] = false; |
---|
[4374] | 263 | for (int i = 0; i < 4; i++) |
---|
| 264 | backgroundColor[i] = 0; |
---|
[4306] | 265 | |
---|
[4295] | 266 | ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); |
---|
[4294] | 267 | |
---|
[4297] | 268 | camera = new Camera(); |
---|
[4294] | 269 | |
---|
[4305] | 270 | State::getInstance()->setCamera(camera, camera->getTarget()); |
---|
[4294] | 271 | |
---|
[4360] | 272 | camera->setAbsCoor(Vector(10, 10, 10)); |
---|
[4374] | 273 | |
---|
[4297] | 274 | } |
---|
| 275 | |
---|
| 276 | Framework::~Framework() |
---|
| 277 | { |
---|
| 278 | delete GraphicsEngine::getInstance(); |
---|
| 279 | |
---|
| 280 | } |
---|
| 281 | |
---|
[4307] | 282 | |
---|
| 283 | |
---|
| 284 | void Framework::printHelp(void) const |
---|
| 285 | { |
---|
[4309] | 286 | PRINT(0)(" Help for the frameWork\n"); |
---|
| 287 | PRINT(0)("========================\n"); |
---|
[4374] | 288 | PRINT(0)("h - print this Help\n"); |
---|
[4359] | 289 | PRINT(0)("a - zoom in\n"); |
---|
| 290 | PRINT(0)("z - zoom out\n"); |
---|
[4360] | 291 | PRINT(0)("r - reset camera position\n"); |
---|
[4374] | 292 | PRINT(0)("1 - background color darker\n"); |
---|
| 293 | PRINT(0)("2 - background color brighter\n"); |
---|
[4307] | 294 | |
---|
[4374] | 295 | |
---|
[4360] | 296 | PRINT(0)("\n"); |
---|
| 297 | PRINT(0)("mouse wheel - zoom\n"); |
---|
| 298 | PRINT(0)("mouse left button - rotate the camera around its target\n"); |
---|
| 299 | PRINT(0)("mouse right button - rotate the camera's target around the camera\n"); |
---|
| 300 | PRINT(0)("mouse left-and-right button - move the camera and the target\n"); |
---|
| 301 | |
---|
[4333] | 302 | this->moduleHelp(); |
---|
[4307] | 303 | |
---|
| 304 | } |
---|
| 305 | |
---|
[4343] | 306 | #ifdef GUI_MODULE |
---|
[4330] | 307 | int quitGui(GtkWidget* widget, void* data) |
---|
| 308 | { |
---|
| 309 | #ifdef HAVE_GTK2 |
---|
| 310 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
[4331] | 311 | Framework::getInstance()->quit(); |
---|
[4330] | 312 | #endif /* HAVE_GTK2 */ |
---|
| 313 | } |
---|
[4343] | 314 | #endif |
---|
[4330] | 315 | |
---|
[4297] | 316 | int main(int argc, char *argv[]) |
---|
| 317 | { |
---|
| 318 | verbose = 3; |
---|
[4330] | 319 | |
---|
[4317] | 320 | Framework* framework = Framework::getInstance(); |
---|
[4316] | 321 | |
---|
[4343] | 322 | framework->moduleInit(argc, argv); |
---|
| 323 | #ifdef GUI_MODULE |
---|
| 324 | framework->moduleInitGui(argc, argv); |
---|
| 325 | #endif |
---|
[4331] | 326 | framework->mainLoop(NULL); |
---|
[4297] | 327 | |
---|
| 328 | delete framework; |
---|
[2748] | 329 | // Kill the GL & SDL screens |
---|
| 330 | // And quit |
---|
| 331 | return 0; |
---|
| 332 | } |
---|