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