[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 |
---|
[4331] | 37 | while(gtk_events_pending()) |
---|
| 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 | |
---|
[4333] | 59 | this->moduleDraw(dt); |
---|
| 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(); |
---|
| 99 | |
---|
| 100 | if (mouseDown[1]) |
---|
| 101 | camera->setRelCoor(camera->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05); |
---|
| 102 | if (mouseDown[3]) |
---|
| 103 | camera->getTarget()->setRelCoor(camera->getTarget()->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05); |
---|
[4300] | 104 | |
---|
[4306] | 105 | } |
---|
[2931] | 106 | break; |
---|
| 107 | case SDL_MOUSEBUTTONDOWN: |
---|
[4306] | 108 | switch (event.button.button) |
---|
[2933] | 109 | { |
---|
[4306] | 110 | case 4: |
---|
[3656] | 111 | PRINTF(4)("MouseWheel up\n"); |
---|
[4310] | 112 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4306] | 113 | break; |
---|
| 114 | case 5: |
---|
[3656] | 115 | PRINTF(4)("MouseWheel down\n"); |
---|
[4310] | 116 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4306] | 117 | break; |
---|
| 118 | case 1: |
---|
| 119 | case 2: |
---|
| 120 | case 3: |
---|
| 121 | mouseDown[event.button.button] = true; |
---|
| 122 | break; |
---|
[2933] | 123 | } |
---|
[2932] | 124 | |
---|
[2931] | 125 | break; |
---|
[2952] | 126 | case SDL_MOUSEBUTTONUP: |
---|
[4306] | 127 | switch (event.button.button) |
---|
| 128 | { |
---|
| 129 | case 1: |
---|
| 130 | case 2: |
---|
| 131 | case 3: |
---|
| 132 | mouseDown[event.button.button] = false; |
---|
| 133 | break; |
---|
| 134 | } |
---|
[2931] | 135 | break; |
---|
[4302] | 136 | case SDL_VIDEORESIZE: |
---|
| 137 | GraphicsEngine::getInstance()->resolutionChanged(&event.resize); |
---|
| 138 | break; |
---|
[3211] | 139 | case SDL_KEYDOWN: |
---|
| 140 | switch (event.key.keysym.sym) |
---|
| 141 | { |
---|
[4331] | 142 | case SDLK_q: |
---|
| 143 | case SDLK_ESCAPE: |
---|
[4343] | 144 | #ifdef GUI_MODULE |
---|
[4331] | 145 | quitGui(NULL, NULL); |
---|
[4343] | 146 | #else |
---|
| 147 | this->quit(); |
---|
| 148 | #endif |
---|
[3211] | 149 | break; |
---|
[3418] | 150 | case SDLK_a: |
---|
[4310] | 151 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[3418] | 152 | break; |
---|
| 153 | case SDLK_z: |
---|
[4310] | 154 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
[4300] | 155 | break; |
---|
[4307] | 156 | case SDLK_h: |
---|
| 157 | this->printHelp(); |
---|
| 158 | break; |
---|
[3211] | 159 | } |
---|
| 160 | break; |
---|
[2952] | 161 | |
---|
[2748] | 162 | // If a quit event was recieved |
---|
[2863] | 163 | case SDL_QUIT: |
---|
| 164 | // then we're done and we'll end this program |
---|
[4343] | 165 | #ifdef GUI_MODULE |
---|
| 166 | quitGui(NULL, NULL); |
---|
| 167 | #else |
---|
| 168 | this->quit(); |
---|
| 169 | #endif |
---|
[4293] | 170 | break; |
---|
[2863] | 171 | default: |
---|
[4293] | 172 | break; |
---|
[2748] | 173 | } |
---|
[2952] | 174 | |
---|
[2748] | 175 | } |
---|
| 176 | |
---|
| 177 | // Get the state of the keyboard keys |
---|
| 178 | keys = SDL_GetKeyState(NULL); |
---|
| 179 | |
---|
| 180 | // and check if ESCAPE has been pressed. If so then quit |
---|
[4293] | 181 | if(keys[SDLK_ESCAPE]) return false; |
---|
[2748] | 182 | } |
---|
[4293] | 183 | return true; |
---|
| 184 | } |
---|
[2748] | 185 | |
---|
[4331] | 186 | void Framework::quit(void) |
---|
| 187 | { |
---|
| 188 | this->isFinished = true; |
---|
| 189 | } |
---|
| 190 | |
---|
[4317] | 191 | Framework* Framework::singletonRef = NULL; |
---|
[4293] | 192 | |
---|
[4317] | 193 | Framework* Framework::getInstance(void) |
---|
| 194 | { |
---|
| 195 | if (Framework::singletonRef == NULL) |
---|
| 196 | Framework::singletonRef = new Framework(); |
---|
| 197 | return Framework::singletonRef; |
---|
| 198 | } |
---|
| 199 | |
---|
[4300] | 200 | Framework::Framework() |
---|
[4293] | 201 | { |
---|
[4331] | 202 | this->isFinished = false; |
---|
| 203 | |
---|
[4304] | 204 | this->lastFrame = 0; |
---|
[4293] | 205 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
| 206 | // 640x480x32, fullscreen and check for errors along the way |
---|
| 207 | GraphicsEngine::getInstance(); |
---|
| 208 | |
---|
[4300] | 209 | LightManager::getInstance(); |
---|
| 210 | glEnable(GL_TEXTURE_2D); |
---|
[4293] | 211 | |
---|
| 212 | // Build the font from a TGA image font.tga in the data directory |
---|
| 213 | // Hide the mouse cursor |
---|
| 214 | SDL_ShowCursor(2); |
---|
| 215 | |
---|
[4306] | 216 | for (int i = 0; i <MOUSE_BUTTON_COUNT; i++) |
---|
| 217 | mouseDown[i] = false; |
---|
| 218 | |
---|
[4295] | 219 | ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); |
---|
[4294] | 220 | |
---|
[4297] | 221 | camera = new Camera(); |
---|
[4294] | 222 | |
---|
[4305] | 223 | State::getInstance()->setCamera(camera, camera->getTarget()); |
---|
[4294] | 224 | |
---|
| 225 | camera->setAbsCoor(Vector(10, 10, 0)); |
---|
[4297] | 226 | } |
---|
| 227 | |
---|
| 228 | Framework::~Framework() |
---|
| 229 | { |
---|
| 230 | delete GraphicsEngine::getInstance(); |
---|
| 231 | |
---|
| 232 | } |
---|
| 233 | |
---|
[4307] | 234 | |
---|
| 235 | |
---|
| 236 | void Framework::printHelp(void) const |
---|
| 237 | { |
---|
[4309] | 238 | PRINT(0)(" Help for the frameWork\n"); |
---|
| 239 | PRINT(0)("========================\n"); |
---|
| 240 | PRINT(0)("h - print thisHelp\n"); |
---|
[4307] | 241 | |
---|
[4333] | 242 | this->moduleHelp(); |
---|
[4307] | 243 | |
---|
| 244 | } |
---|
| 245 | |
---|
[4343] | 246 | #ifdef GUI_MODULE |
---|
[4330] | 247 | int quitGui(GtkWidget* widget, void* data) |
---|
| 248 | { |
---|
| 249 | #ifdef HAVE_GTK2 |
---|
| 250 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
[4331] | 251 | Framework::getInstance()->quit(); |
---|
[4330] | 252 | #endif /* HAVE_GTK2 */ |
---|
| 253 | } |
---|
[4343] | 254 | #endif |
---|
[4330] | 255 | |
---|
[4297] | 256 | int main(int argc, char *argv[]) |
---|
| 257 | { |
---|
| 258 | verbose = 3; |
---|
[4330] | 259 | |
---|
[4317] | 260 | Framework* framework = Framework::getInstance(); |
---|
[4316] | 261 | |
---|
[4343] | 262 | framework->moduleInit(argc, argv); |
---|
| 263 | #ifdef GUI_MODULE |
---|
| 264 | framework->moduleInitGui(argc, argv); |
---|
| 265 | #endif |
---|
[4331] | 266 | framework->mainLoop(NULL); |
---|
[4297] | 267 | |
---|
| 268 | delete framework; |
---|
[2748] | 269 | // Kill the GL & SDL screens |
---|
| 270 | // And quit |
---|
| 271 | return 0; |
---|
| 272 | } |
---|