[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(); |
---|
[4358] | 62 | printf("distance: %f\n", (camera->getTarget()->getAbsCoor()-camera->getAbsCoor()).len()); |
---|
[4294] | 63 | |
---|
[2748] | 64 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
| 65 | } |
---|
[4300] | 66 | |
---|
| 67 | |
---|
[4297] | 68 | float Framework::tick() |
---|
[2748] | 69 | { |
---|
[4293] | 70 | currFrame = SDL_GetTicks(); |
---|
[4300] | 71 | float dt = (float)(currFrame - lastFrame) / 1000.0; |
---|
| 72 | lastFrame = currFrame; |
---|
[3398] | 73 | |
---|
[4333] | 74 | this->moduleTick(dt); |
---|
[2963] | 75 | |
---|
[4294] | 76 | return dt; |
---|
[4293] | 77 | } |
---|
[2952] | 78 | |
---|
[2931] | 79 | |
---|
[4334] | 80 | bool Framework::eventHandler() |
---|
[4293] | 81 | { |
---|
[2748] | 82 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
| 83 | { |
---|
| 84 | // And poll for events |
---|
| 85 | SDL_Event event; |
---|
[4305] | 86 | while(SDL_PollEvent(&event)) |
---|
[4303] | 87 | { |
---|
[4334] | 88 | moduleEventHandler(&event); |
---|
| 89 | |
---|
[2748] | 90 | switch (event.type) { |
---|
[2931] | 91 | case SDL_MOUSEMOTION: |
---|
[4306] | 92 | { |
---|
| 93 | Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor(); |
---|
| 94 | Vector up = Vector(0, 1, 0); |
---|
| 95 | up = camera->getAbsDir().apply(up); |
---|
| 96 | Vector h = up.cross(view); |
---|
| 97 | Vector v = h.cross(view); |
---|
| 98 | h.normalize(); |
---|
| 99 | v.normalize(); |
---|
[4358] | 100 | float distance = view.len(); |
---|
| 101 | |
---|
| 102 | Vector newCameraPos = camera->getAbsCoor(); |
---|
| 103 | Vector newTargetPos = camera->getTarget()->getAbsCoor(); |
---|
| 104 | int changed = 0; |
---|
[4306] | 105 | |
---|
| 106 | if (mouseDown[1]) |
---|
[4358] | 107 | { |
---|
| 108 | newCameraPos = camera->getRelCoor()+ (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
---|
| 109 | changed += 1; |
---|
| 110 | } |
---|
[4306] | 111 | if (mouseDown[3]) |
---|
[4358] | 112 | { |
---|
| 113 | newTargetPos = camera->getTarget()->getRelCoor() + (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
---|
| 114 | changed += 2; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | Vector newView = newTargetPos - newCameraPos; |
---|
| 118 | |
---|
| 119 | if (changed == 1) |
---|
| 120 | camera->setRelCoor(newCameraPos + newView * (1- distance/newView.len())); |
---|
| 121 | else if (changed == 2) |
---|
| 122 | camera->getTarget()->setRelCoor(newTargetPos - newView * (1-distance/newView.len())); |
---|
| 123 | else if (changed == 3) |
---|
| 124 | { |
---|
| 125 | camera->setRelCoor(newCameraPos); |
---|
| 126 | camera->getTarget()->setRelCoor(newTargetPos); |
---|
| 127 | } |
---|
[4300] | 128 | |
---|
[4306] | 129 | } |
---|
[2931] | 130 | break; |
---|
| 131 | case SDL_MOUSEBUTTONDOWN: |
---|
[4306] | 132 | switch (event.button.button) |
---|
[2933] | 133 | { |
---|
[4306] | 134 | case 4: |
---|
[3656] | 135 | PRINTF(4)("MouseWheel up\n"); |
---|
[4310] | 136 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4306] | 137 | break; |
---|
| 138 | case 5: |
---|
[3656] | 139 | PRINTF(4)("MouseWheel down\n"); |
---|
[4310] | 140 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4306] | 141 | break; |
---|
| 142 | case 1: |
---|
| 143 | case 2: |
---|
| 144 | case 3: |
---|
| 145 | mouseDown[event.button.button] = true; |
---|
| 146 | break; |
---|
[2933] | 147 | } |
---|
[2932] | 148 | |
---|
[2931] | 149 | break; |
---|
[2952] | 150 | case SDL_MOUSEBUTTONUP: |
---|
[4306] | 151 | switch (event.button.button) |
---|
| 152 | { |
---|
| 153 | case 1: |
---|
| 154 | case 2: |
---|
| 155 | case 3: |
---|
| 156 | mouseDown[event.button.button] = false; |
---|
| 157 | break; |
---|
| 158 | } |
---|
[2931] | 159 | break; |
---|
[4302] | 160 | case SDL_VIDEORESIZE: |
---|
| 161 | GraphicsEngine::getInstance()->resolutionChanged(&event.resize); |
---|
| 162 | break; |
---|
[3211] | 163 | case SDL_KEYDOWN: |
---|
| 164 | switch (event.key.keysym.sym) |
---|
| 165 | { |
---|
[4331] | 166 | case SDLK_q: |
---|
| 167 | case SDLK_ESCAPE: |
---|
[4343] | 168 | #ifdef GUI_MODULE |
---|
[4331] | 169 | quitGui(NULL, NULL); |
---|
[4343] | 170 | #else |
---|
| 171 | this->quit(); |
---|
| 172 | #endif |
---|
[3211] | 173 | break; |
---|
[3418] | 174 | case SDLK_a: |
---|
[4310] | 175 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[3418] | 176 | break; |
---|
| 177 | case SDLK_z: |
---|
[4310] | 178 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4300] | 179 | break; |
---|
[4307] | 180 | case SDLK_h: |
---|
| 181 | this->printHelp(); |
---|
| 182 | break; |
---|
[3211] | 183 | } |
---|
| 184 | break; |
---|
[2952] | 185 | |
---|
[2748] | 186 | // If a quit event was recieved |
---|
[2863] | 187 | case SDL_QUIT: |
---|
| 188 | // then we're done and we'll end this program |
---|
[4343] | 189 | #ifdef GUI_MODULE |
---|
| 190 | quitGui(NULL, NULL); |
---|
| 191 | #else |
---|
| 192 | this->quit(); |
---|
| 193 | #endif |
---|
[4293] | 194 | break; |
---|
[2863] | 195 | default: |
---|
[4293] | 196 | break; |
---|
[2748] | 197 | } |
---|
[2952] | 198 | |
---|
[2748] | 199 | } |
---|
| 200 | |
---|
| 201 | // Get the state of the keyboard keys |
---|
| 202 | keys = SDL_GetKeyState(NULL); |
---|
| 203 | |
---|
| 204 | // and check if ESCAPE has been pressed. If so then quit |
---|
[4293] | 205 | if(keys[SDLK_ESCAPE]) return false; |
---|
[2748] | 206 | } |
---|
[4293] | 207 | return true; |
---|
| 208 | } |
---|
[2748] | 209 | |
---|
[4331] | 210 | void Framework::quit(void) |
---|
| 211 | { |
---|
| 212 | this->isFinished = true; |
---|
| 213 | } |
---|
| 214 | |
---|
[4317] | 215 | Framework* Framework::singletonRef = NULL; |
---|
[4293] | 216 | |
---|
[4317] | 217 | Framework* Framework::getInstance(void) |
---|
| 218 | { |
---|
| 219 | if (Framework::singletonRef == NULL) |
---|
| 220 | Framework::singletonRef = new Framework(); |
---|
| 221 | return Framework::singletonRef; |
---|
| 222 | } |
---|
| 223 | |
---|
[4300] | 224 | Framework::Framework() |
---|
[4293] | 225 | { |
---|
[4331] | 226 | this->isFinished = false; |
---|
| 227 | |
---|
[4304] | 228 | this->lastFrame = 0; |
---|
[4293] | 229 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
| 230 | // 640x480x32, fullscreen and check for errors along the way |
---|
| 231 | GraphicsEngine::getInstance(); |
---|
| 232 | |
---|
[4300] | 233 | LightManager::getInstance(); |
---|
| 234 | glEnable(GL_TEXTURE_2D); |
---|
[4293] | 235 | |
---|
| 236 | // Build the font from a TGA image font.tga in the data directory |
---|
| 237 | // Hide the mouse cursor |
---|
| 238 | SDL_ShowCursor(2); |
---|
| 239 | |
---|
[4306] | 240 | for (int i = 0; i <MOUSE_BUTTON_COUNT; i++) |
---|
| 241 | mouseDown[i] = false; |
---|
| 242 | |
---|
[4295] | 243 | ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); |
---|
[4294] | 244 | |
---|
[4297] | 245 | camera = new Camera(); |
---|
[4294] | 246 | |
---|
[4305] | 247 | State::getInstance()->setCamera(camera, camera->getTarget()); |
---|
[4294] | 248 | |
---|
| 249 | camera->setAbsCoor(Vector(10, 10, 0)); |
---|
[4297] | 250 | } |
---|
| 251 | |
---|
| 252 | Framework::~Framework() |
---|
| 253 | { |
---|
| 254 | delete GraphicsEngine::getInstance(); |
---|
| 255 | |
---|
| 256 | } |
---|
| 257 | |
---|
[4307] | 258 | |
---|
| 259 | |
---|
| 260 | void Framework::printHelp(void) const |
---|
| 261 | { |
---|
[4309] | 262 | PRINT(0)(" Help for the frameWork\n"); |
---|
| 263 | PRINT(0)("========================\n"); |
---|
| 264 | PRINT(0)("h - print thisHelp\n"); |
---|
[4359] | 265 | PRINT(0)("a - zoom in\n"); |
---|
| 266 | PRINT(0)("z - zoom out\n"); |
---|
[4307] | 267 | |
---|
[4333] | 268 | this->moduleHelp(); |
---|
[4307] | 269 | |
---|
| 270 | } |
---|
| 271 | |
---|
[4343] | 272 | #ifdef GUI_MODULE |
---|
[4330] | 273 | int quitGui(GtkWidget* widget, void* data) |
---|
| 274 | { |
---|
| 275 | #ifdef HAVE_GTK2 |
---|
| 276 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
[4331] | 277 | Framework::getInstance()->quit(); |
---|
[4330] | 278 | #endif /* HAVE_GTK2 */ |
---|
| 279 | } |
---|
[4343] | 280 | #endif |
---|
[4330] | 281 | |
---|
[4297] | 282 | int main(int argc, char *argv[]) |
---|
| 283 | { |
---|
| 284 | verbose = 3; |
---|
[4330] | 285 | |
---|
[4317] | 286 | Framework* framework = Framework::getInstance(); |
---|
[4316] | 287 | |
---|
[4343] | 288 | framework->moduleInit(argc, argv); |
---|
| 289 | #ifdef GUI_MODULE |
---|
| 290 | framework->moduleInitGui(argc, argv); |
---|
| 291 | #endif |
---|
[4331] | 292 | framework->mainLoop(NULL); |
---|
[4297] | 293 | |
---|
| 294 | delete framework; |
---|
[2748] | 295 | // Kill the GL & SDL screens |
---|
| 296 | // And quit |
---|
| 297 | return 0; |
---|
| 298 | } |
---|