| [4554] | 1 | /* | 
|---|
| [3140] | 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()) | 
|---|
| [4554] | 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 | 
|---|
| [4554] | 58 |  | 
|---|
| [4349] | 59 |   this->moduleDraw(); | 
|---|
| [4554] | 60 |  | 
|---|
| [4297] | 61 |   camera->apply(); | 
|---|
| [4554] | 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: | 
|---|
| [4554] | 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 |           float distance = view.len(); | 
|---|
| [4306] | 100 |  | 
|---|
| [4554] | 101 |           Vector newCameraPos = camera->getAbsCoor(); | 
|---|
 | 102 |           Vector newTargetPos = camera->getTarget()->getAbsCoor(); | 
|---|
 | 103 |           int changed = 0; | 
|---|
| [4358] | 104 |  | 
|---|
| [4554] | 105 |           if (mouseDown[1]) | 
|---|
 | 106 |             { | 
|---|
 | 107 |               newCameraPos = camera->getRelCoor()+ (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; | 
|---|
 | 108 |               changed += 1; | 
|---|
 | 109 |             } | 
|---|
 | 110 |           if (mouseDown[3]) | 
|---|
 | 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 |             } | 
|---|
 | 127 |  | 
|---|
 | 128 |         } | 
|---|
 | 129 |         break; | 
|---|
| [2931] | 130 |       case SDL_MOUSEBUTTONDOWN: | 
|---|
| [4554] | 131 |         switch (event.button.button) | 
|---|
 | 132 |           { | 
|---|
 | 133 |           case 4: | 
|---|
 | 134 |             PRINTF(4)("MouseWheel up\n"); | 
|---|
 | 135 |             camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); | 
|---|
 | 136 |             break; | 
|---|
 | 137 |           case 5: | 
|---|
 | 138 |             PRINTF(4)("MouseWheel down\n"); | 
|---|
 | 139 |             camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); | 
|---|
 | 140 |             break; | 
|---|
 | 141 |           case 1: | 
|---|
 | 142 |           case 2: | 
|---|
 | 143 |           case 3: | 
|---|
 | 144 |             mouseDown[event.button.button] = true; | 
|---|
 | 145 |             break; | 
|---|
 | 146 |           } | 
|---|
 | 147 |  | 
|---|
 | 148 |         break; | 
|---|
| [2952] | 149 |       case SDL_MOUSEBUTTONUP: | 
|---|
| [4554] | 150 |         switch (event.button.button) | 
|---|
 | 151 |           { | 
|---|
 | 152 |           case 1: | 
|---|
 | 153 |           case 2: | 
|---|
 | 154 |           case 3: | 
|---|
 | 155 |             mouseDown[event.button.button] = false; | 
|---|
 | 156 |             break; | 
|---|
 | 157 |           } | 
|---|
 | 158 |         break; | 
|---|
| [4302] | 159 |       case SDL_VIDEORESIZE: | 
|---|
| [4554] | 160 |         GraphicsEngine::getInstance()->resolutionChanged(&event.resize); | 
|---|
 | 161 |         break; | 
|---|
| [3211] | 162 |       case SDL_KEYDOWN: | 
|---|
| [4554] | 163 |         switch (event.key.keysym.sym) | 
|---|
 | 164 |           { | 
|---|
 | 165 |           case SDLK_q: | 
|---|
 | 166 |           case SDLK_ESCAPE: | 
|---|
| [4343] | 167 | #ifdef GUI_MODULE | 
|---|
| [4554] | 168 |             quitGui(NULL, NULL); | 
|---|
| [4343] | 169 | #else | 
|---|
| [4554] | 170 |             this->quit(); | 
|---|
| [4343] | 171 | #endif | 
|---|
| [4554] | 172 |             break; | 
|---|
| [4636] | 173 |           case SDLK_t: | 
|---|
| [4554] | 174 |             camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); | 
|---|
 | 175 |             break; | 
|---|
 | 176 |           case SDLK_z: | 
|---|
 | 177 |             camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); | 
|---|
 | 178 |             break; | 
|---|
 | 179 |           case SDLK_r: | 
|---|
 | 180 |             camera->setAbsCoor(Vector(10, 10, 10)); | 
|---|
 | 181 |             camera->getTarget()->setAbsCoor(Vector()); | 
|---|
 | 182 |             break; | 
|---|
 | 183 |           case SDLK_h: | 
|---|
 | 184 |             this->printHelp(); | 
|---|
 | 185 |             break; | 
|---|
| [4636] | 186 |           case SDLK_l: | 
|---|
| [4554] | 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; | 
|---|
 | 204 |           } | 
|---|
 | 205 |         break; | 
|---|
 | 206 |  | 
|---|
| [2748] | 207 |         // If a quit event was recieved | 
|---|
| [2863] | 208 |       case SDL_QUIT: | 
|---|
| [4554] | 209 |         // then we're done and we'll end this program | 
|---|
| [4343] | 210 | #ifdef GUI_MODULE | 
|---|
| [4554] | 211 |             quitGui(NULL, NULL); | 
|---|
| [4343] | 212 | #else | 
|---|
| [4554] | 213 |             this->quit(); | 
|---|
| [4343] | 214 | #endif | 
|---|
| [4554] | 215 |         break; | 
|---|
| [2863] | 216 |       default: | 
|---|
| [4554] | 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 |  | 
|---|
| [4300] | 238 | Framework::Framework() | 
|---|
| [4293] | 239 | { | 
|---|
| [4331] | 240 |   this->isFinished = false; | 
|---|
 | 241 |  | 
|---|
| [4304] | 242 |   this->lastFrame = 0; | 
|---|
| [4293] | 243 |   // Create a new OpenGL window with the title "Cone3D Basecode" at | 
|---|
 | 244 |   // 640x480x32, fullscreen and check for errors along the way | 
|---|
 | 245 |   GraphicsEngine::getInstance(); | 
|---|
 | 246 |  | 
|---|
| [4300] | 247 |   LightManager::getInstance(); | 
|---|
 | 248 |   glEnable(GL_TEXTURE_2D); | 
|---|
| [4293] | 249 |  | 
|---|
 | 250 |   // Build the font from a TGA image font.tga in the data directory | 
|---|
 | 251 |   // Hide the mouse cursor | 
|---|
 | 252 |   SDL_ShowCursor(2); | 
|---|
 | 253 |  | 
|---|
| [4374] | 254 |   for (int i = 0; i < MOUSE_BUTTON_COUNT; i++) | 
|---|
| [4306] | 255 |     mouseDown[i] = false; | 
|---|
| [4374] | 256 |   for (int i = 0; i < 4; i++) | 
|---|
 | 257 |     backgroundColor[i] = 0; | 
|---|
| [4306] | 258 |  | 
|---|
| [4295] | 259 |   ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); | 
|---|
| [4554] | 260 |  | 
|---|
| [4297] | 261 |   camera = new Camera(); | 
|---|
| [4294] | 262 |  | 
|---|
| [4305] | 263 |   State::getInstance()->setCamera(camera, camera->getTarget()); | 
|---|
| [4294] | 264 |  | 
|---|
| [4360] | 265 |   camera->setAbsCoor(Vector(10, 10, 10)); | 
|---|
| [4374] | 266 |  | 
|---|
| [4297] | 267 | } | 
|---|
 | 268 |  | 
|---|
 | 269 | Framework::~Framework() | 
|---|
 | 270 | { | 
|---|
 | 271 |   delete GraphicsEngine::getInstance(); | 
|---|
 | 272 |  | 
|---|
 | 273 | } | 
|---|
 | 274 |  | 
|---|
| [4307] | 275 |  | 
|---|
 | 276 |  | 
|---|
 | 277 | void Framework::printHelp(void) const | 
|---|
 | 278 | { | 
|---|
| [4309] | 279 |   PRINT(0)(" Help for the frameWork\n"); | 
|---|
 | 280 |   PRINT(0)("========================\n"); | 
|---|
| [4374] | 281 |   PRINT(0)("h - print this Help\n"); | 
|---|
| [4359] | 282 |   PRINT(0)("a - zoom in\n"); | 
|---|
 | 283 |   PRINT(0)("z - zoom out\n"); | 
|---|
| [4360] | 284 |   PRINT(0)("r - reset camera position\n"); | 
|---|
| [4374] | 285 |   PRINT(0)("1 - background color darker\n"); | 
|---|
 | 286 |   PRINT(0)("2 - background color brighter\n"); | 
|---|
| [4307] | 287 |  | 
|---|
| [4374] | 288 |  | 
|---|
| [4360] | 289 |   PRINT(0)("\n"); | 
|---|
 | 290 |   PRINT(0)("mouse wheel - zoom\n"); | 
|---|
 | 291 |   PRINT(0)("mouse left button - rotate the camera around its target\n"); | 
|---|
 | 292 |   PRINT(0)("mouse right button - rotate the camera's target around the camera\n"); | 
|---|
 | 293 |   PRINT(0)("mouse left-and-right button - move the camera and the target\n"); | 
|---|
| [4554] | 294 |  | 
|---|
| [4333] | 295 |   this->moduleHelp(); | 
|---|
| [4307] | 296 |  | 
|---|
 | 297 | } | 
|---|
 | 298 |  | 
|---|
| [4343] | 299 | #ifdef GUI_MODULE | 
|---|
| [4330] | 300 | int quitGui(GtkWidget* widget, void* data) | 
|---|
 | 301 | { | 
|---|
 | 302 | #ifdef HAVE_GTK2 | 
|---|
 | 303 |   while(gtk_events_pending()) gtk_main_iteration(); | 
|---|
| [4331] | 304 |   Framework::getInstance()->quit(); | 
|---|
| [4330] | 305 | #endif /* HAVE_GTK2 */ | 
|---|
 | 306 | } | 
|---|
| [4343] | 307 | #endif | 
|---|
| [4330] | 308 |  | 
|---|
| [4297] | 309 | int main(int argc, char *argv[]) | 
|---|
 | 310 | { | 
|---|
 | 311 |   verbose = 3; | 
|---|
| [4554] | 312 |  | 
|---|
| [4317] | 313 |   Framework* framework = Framework::getInstance(); | 
|---|
| [4316] | 314 |  | 
|---|
| [4343] | 315 |   framework->moduleInit(argc, argv); | 
|---|
 | 316 | #ifdef GUI_MODULE | 
|---|
 | 317 |   framework->moduleInitGui(argc, argv); | 
|---|
 | 318 | #endif | 
|---|
| [4331] | 319 |   framework->mainLoop(NULL); | 
|---|
| [4297] | 320 |  | 
|---|
 | 321 |   delete framework; | 
|---|
| [2748] | 322 |   // Kill the GL & SDL screens | 
|---|
 | 323 |   // And quit | 
|---|
 | 324 |   return 0; | 
|---|
 | 325 | } | 
|---|