| [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 "state.h" |
|---|
| [4272] | 21 | #include "debug.h" |
|---|
| [4300] | 22 | #include "light.h" |
|---|
| [7193] | 23 | #include "util/loading/resource_manager.h" |
|---|
| [4297] | 24 | #include "camera.h" |
|---|
| [7256] | 25 | #include "util/preferences.h" |
|---|
| [5485] | 26 | #include "globals.h" |
|---|
| [3657] | 27 | |
|---|
| [3398] | 28 | int verbose; |
|---|
| [4293] | 29 | |
|---|
| [4650] | 30 | void Framework::init(void) |
|---|
| 31 | { |
|---|
| [7256] | 32 | GraphicsEngine::getInstance()->initFromPreferences(); |
|---|
| [4785] | 33 | |
|---|
| 34 | LightManager::getInstance(); |
|---|
| 35 | |
|---|
| [7711] | 36 | std::string dataPath = //Preferences::getInstance()->getString(CONFIG_SECTION_DATA, CONFIG_NAME_DATADIR, ""); |
|---|
| 37 | "/home/boenzlip/orxonox/data/trunk/"; |
|---|
| 38 | printf("%s\n", dataPath.c_str()); |
|---|
| 39 | if (!dataPath.empty()) |
|---|
| [4650] | 40 | { |
|---|
| [5030] | 41 | if (!ResourceManager::getInstance()->setDataDir(dataPath)) |
|---|
| [4650] | 42 | { |
|---|
| [5030] | 43 | PRINTF(1)("Data Could not be located\n"); |
|---|
| 44 | exit(-1); |
|---|
| [4650] | 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| [5484] | 48 | if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
|---|
| [4650] | 49 | { |
|---|
| [5030] | 50 | PRINTF(1)("The DataDirectory %s could not be verified\n" \ |
|---|
| 51 | " Please Change in File %s Section %s Entry %s to a suitable value\n", |
|---|
| [7711] | 52 | ResourceManager::getInstance()->getDataDir().c_str(), |
|---|
| [4650] | 53 | DEFAULT_CONFIG_FILE, |
|---|
| 54 | CONFIG_SECTION_DATA, |
|---|
| 55 | CONFIG_NAME_DATADIR); |
|---|
| 56 | exit(-1); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| [4330] | 61 | void* Framework::mainLoop(void* tmp) |
|---|
| [2748] | 62 | { |
|---|
| [4330] | 63 | Framework* framework = Framework::getInstance(); |
|---|
| [7159] | 64 | |
|---|
| 65 | // initialize Timing |
|---|
| 66 | framework->cycle = 0; |
|---|
| 67 | for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++) |
|---|
| 68 | framework->frameTimes[i] = 100; |
|---|
| 69 | framework->dtS = 0.0f; |
|---|
| 70 | framework->lastFrame = SDL_GetTicks (); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| [4331] | 73 | while(!framework->isFinished) |
|---|
| [4293] | 74 | { |
|---|
| [4343] | 75 | #ifdef GUI_MODULE |
|---|
| [4358] | 76 | while(gtk_events_pending()) |
|---|
| [4554] | 77 | gtk_main_iteration(); |
|---|
| [4343] | 78 | #endif |
|---|
| [4297] | 79 | // keyhandler returns false if sdl gets quit by some event |
|---|
| [4334] | 80 | framework->eventHandler(); |
|---|
| [2939] | 81 | |
|---|
| [4297] | 82 | // tick the scene |
|---|
| [4330] | 83 | float dt = framework->tick(); |
|---|
| [4297] | 84 | |
|---|
| [6074] | 85 | PNode::getNullParent()->updateNode(dt); |
|---|
| [4333] | 86 | |
|---|
| [4293] | 87 | // Draw the scene |
|---|
| [4330] | 88 | framework->draw(dt); |
|---|
| [4331] | 89 | |
|---|
| [4293] | 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| [4297] | 93 | bool Framework::draw(float dt) |
|---|
| [4293] | 94 | { |
|---|
| [2748] | 95 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
|---|
| [7158] | 96 | |
|---|
| [7157] | 97 | camera->apply(); |
|---|
| 98 | camera->project(); |
|---|
| [4554] | 99 | |
|---|
| [7158] | 100 | |
|---|
| [4349] | 101 | this->moduleDraw(); |
|---|
| [4554] | 102 | |
|---|
| 103 | |
|---|
| [2748] | 104 | SDL_GL_SwapBuffers(); // Swap the buffers |
|---|
| 105 | } |
|---|
| [4300] | 106 | |
|---|
| 107 | |
|---|
| [4297] | 108 | float Framework::tick() |
|---|
| [2748] | 109 | { |
|---|
| [7159] | 110 | // CALCULATE FRAMERATE |
|---|
| 111 | Uint32 frameTimesIndex; |
|---|
| 112 | Uint32 getTicks; |
|---|
| 113 | Uint32 i; |
|---|
| [3398] | 114 | |
|---|
| [7159] | 115 | frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE; |
|---|
| 116 | getTicks = SDL_GetTicks(); |
|---|
| 117 | this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame; |
|---|
| 118 | this->lastFrame = getTicks; |
|---|
| 119 | ++this->cycle; |
|---|
| 120 | this->dtS = 0; |
|---|
| 121 | for (i = 0; i < TICK_SMOOTH_VALUE; i++) |
|---|
| 122 | this->dtS += this->frameTimes[i]; |
|---|
| 123 | this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed; |
|---|
| [2963] | 124 | |
|---|
| [7159] | 125 | this->camera->tick(dtS); |
|---|
| 126 | |
|---|
| 127 | this->moduleTick(dtS); |
|---|
| 128 | |
|---|
| 129 | return dtS; |
|---|
| [4293] | 130 | } |
|---|
| [2952] | 131 | |
|---|
| [2931] | 132 | |
|---|
| [4334] | 133 | bool Framework::eventHandler() |
|---|
| [4293] | 134 | { |
|---|
| [2748] | 135 | // This is the main loop for the entire program and it will run until done==TRUE |
|---|
| 136 | { |
|---|
| 137 | // And poll for events |
|---|
| 138 | SDL_Event event; |
|---|
| [4305] | 139 | while(SDL_PollEvent(&event)) |
|---|
| [4303] | 140 | { |
|---|
| [4334] | 141 | moduleEventHandler(&event); |
|---|
| 142 | |
|---|
| [2748] | 143 | switch (event.type) { |
|---|
| [2931] | 144 | case SDL_MOUSEMOTION: |
|---|
| [4554] | 145 | { |
|---|
| 146 | Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor(); |
|---|
| 147 | Vector up = Vector(0, 1, 0); |
|---|
| 148 | up = camera->getAbsDir().apply(up); |
|---|
| 149 | Vector h = up.cross(view); |
|---|
| 150 | Vector v = h.cross(view); |
|---|
| 151 | h.normalize(); |
|---|
| 152 | v.normalize(); |
|---|
| 153 | float distance = view.len(); |
|---|
| [4306] | 154 | |
|---|
| [4554] | 155 | Vector newCameraPos = camera->getAbsCoor(); |
|---|
| 156 | Vector newTargetPos = camera->getTarget()->getAbsCoor(); |
|---|
| 157 | int changed = 0; |
|---|
| [4358] | 158 | |
|---|
| [4554] | 159 | if (mouseDown[1]) |
|---|
| 160 | { |
|---|
| 161 | newCameraPos = camera->getRelCoor()+ (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
|---|
| 162 | changed += 1; |
|---|
| 163 | } |
|---|
| 164 | if (mouseDown[3]) |
|---|
| 165 | { |
|---|
| 166 | newTargetPos = camera->getTarget()->getRelCoor() + (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
|---|
| 167 | changed += 2; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | Vector newView = newTargetPos - newCameraPos; |
|---|
| 171 | |
|---|
| 172 | if (changed == 1) |
|---|
| 173 | camera->setRelCoor(newCameraPos + newView * (1- distance/newView.len())); |
|---|
| 174 | else if (changed == 2) |
|---|
| 175 | camera->getTarget()->setRelCoor(newTargetPos - newView * (1-distance/newView.len())); |
|---|
| 176 | else if (changed == 3) |
|---|
| 177 | { |
|---|
| 178 | camera->setRelCoor(newCameraPos); |
|---|
| 179 | camera->getTarget()->setRelCoor(newTargetPos); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | } |
|---|
| 183 | break; |
|---|
| [2931] | 184 | case SDL_MOUSEBUTTONDOWN: |
|---|
| [4554] | 185 | switch (event.button.button) |
|---|
| 186 | { |
|---|
| 187 | case 4: |
|---|
| 188 | PRINTF(4)("MouseWheel up\n"); |
|---|
| 189 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
|---|
| 190 | break; |
|---|
| 191 | case 5: |
|---|
| 192 | PRINTF(4)("MouseWheel down\n"); |
|---|
| 193 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
|---|
| 194 | break; |
|---|
| 195 | case 1: |
|---|
| 196 | case 2: |
|---|
| 197 | case 3: |
|---|
| 198 | mouseDown[event.button.button] = true; |
|---|
| 199 | break; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | break; |
|---|
| [2952] | 203 | case SDL_MOUSEBUTTONUP: |
|---|
| [4554] | 204 | switch (event.button.button) |
|---|
| 205 | { |
|---|
| 206 | case 1: |
|---|
| 207 | case 2: |
|---|
| 208 | case 3: |
|---|
| 209 | mouseDown[event.button.button] = false; |
|---|
| 210 | break; |
|---|
| 211 | } |
|---|
| 212 | break; |
|---|
| [4302] | 213 | case SDL_VIDEORESIZE: |
|---|
| [4785] | 214 | GraphicsEngine::getInstance()->resolutionChanged(event.resize); |
|---|
| [4554] | 215 | break; |
|---|
| [3211] | 216 | case SDL_KEYDOWN: |
|---|
| [4554] | 217 | switch (event.key.keysym.sym) |
|---|
| 218 | { |
|---|
| 219 | case SDLK_q: |
|---|
| 220 | case SDLK_ESCAPE: |
|---|
| [4343] | 221 | #ifdef GUI_MODULE |
|---|
| [4554] | 222 | quitGui(NULL, NULL); |
|---|
| [4343] | 223 | #else |
|---|
| [4554] | 224 | this->quit(); |
|---|
| [4343] | 225 | #endif |
|---|
| [4554] | 226 | break; |
|---|
| [4724] | 227 | case SDLK_a: |
|---|
| [4554] | 228 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
|---|
| 229 | break; |
|---|
| 230 | case SDLK_z: |
|---|
| 231 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
|---|
| 232 | break; |
|---|
| 233 | case SDLK_r: |
|---|
| 234 | camera->setAbsCoor(Vector(10, 10, 10)); |
|---|
| 235 | camera->getTarget()->setAbsCoor(Vector()); |
|---|
| 236 | break; |
|---|
| 237 | case SDLK_h: |
|---|
| 238 | this->printHelp(); |
|---|
| 239 | break; |
|---|
| [4647] | 240 | case SDLK_c: |
|---|
| [4554] | 241 | for (int i = 0; i < 3; i++) |
|---|
| 242 | { |
|---|
| 243 | backgroundColor[i] += .1; |
|---|
| 244 | if (backgroundColor[i] > 1.0) |
|---|
| 245 | backgroundColor[i] = 1.0; |
|---|
| 246 | GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]); |
|---|
| 247 | } |
|---|
| 248 | break; |
|---|
| [4647] | 249 | case SDLK_x: |
|---|
| [4554] | 250 | for (int i = 0; i < 3; i++) |
|---|
| 251 | { |
|---|
| 252 | backgroundColor[i] -= .1; |
|---|
| 253 | if (backgroundColor[i] < 0.0) |
|---|
| 254 | backgroundColor[i] = 0.0; |
|---|
| 255 | GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]); |
|---|
| 256 | } |
|---|
| 257 | break; |
|---|
| 258 | } |
|---|
| 259 | break; |
|---|
| 260 | |
|---|
| [2748] | 261 | // If a quit event was recieved |
|---|
| [2863] | 262 | case SDL_QUIT: |
|---|
| [4554] | 263 | // then we're done and we'll end this program |
|---|
| [4343] | 264 | #ifdef GUI_MODULE |
|---|
| [4554] | 265 | quitGui(NULL, NULL); |
|---|
| [4343] | 266 | #else |
|---|
| [4554] | 267 | this->quit(); |
|---|
| [4343] | 268 | #endif |
|---|
| [4554] | 269 | break; |
|---|
| [2863] | 270 | default: |
|---|
| [4554] | 271 | break; |
|---|
| [2748] | 272 | } |
|---|
| [2952] | 273 | |
|---|
| [2748] | 274 | } |
|---|
| 275 | |
|---|
| 276 | // Get the state of the keyboard keys |
|---|
| 277 | keys = SDL_GetKeyState(NULL); |
|---|
| 278 | |
|---|
| 279 | // and check if ESCAPE has been pressed. If so then quit |
|---|
| [4293] | 280 | if(keys[SDLK_ESCAPE]) return false; |
|---|
| [2748] | 281 | } |
|---|
| [4293] | 282 | return true; |
|---|
| 283 | } |
|---|
| [2748] | 284 | |
|---|
| [4331] | 285 | void Framework::quit(void) |
|---|
| 286 | { |
|---|
| 287 | this->isFinished = true; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| [4317] | 290 | Framework* Framework::singletonRef = NULL; |
|---|
| [4293] | 291 | |
|---|
| [4300] | 292 | Framework::Framework() |
|---|
| [4293] | 293 | { |
|---|
| [4650] | 294 | this->init(); |
|---|
| 295 | |
|---|
| [4785] | 296 | camera = new Camera(); |
|---|
| [4903] | 297 | State::setCamera(camera, camera->getTarget()); |
|---|
| [4785] | 298 | camera->setAbsCoor(Vector(10, 10, 10)); |
|---|
| 299 | |
|---|
| [4331] | 300 | this->isFinished = false; |
|---|
| 301 | |
|---|
| [4304] | 302 | this->lastFrame = 0; |
|---|
| [4293] | 303 | |
|---|
| 304 | |
|---|
| 305 | // Build the font from a TGA image font.tga in the data directory |
|---|
| 306 | // Hide the mouse cursor |
|---|
| 307 | SDL_ShowCursor(2); |
|---|
| 308 | |
|---|
| [4374] | 309 | for (int i = 0; i < MOUSE_BUTTON_COUNT; i++) |
|---|
| [4306] | 310 | mouseDown[i] = false; |
|---|
| [4374] | 311 | for (int i = 0; i < 4; i++) |
|---|
| 312 | backgroundColor[i] = 0; |
|---|
| [4297] | 313 | } |
|---|
| 314 | |
|---|
| 315 | Framework::~Framework() |
|---|
| 316 | { |
|---|
| 317 | delete GraphicsEngine::getInstance(); |
|---|
| 318 | |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| [4307] | 321 | |
|---|
| 322 | |
|---|
| 323 | void Framework::printHelp(void) const |
|---|
| 324 | { |
|---|
| [4309] | 325 | PRINT(0)(" Help for the frameWork\n"); |
|---|
| 326 | PRINT(0)("========================\n"); |
|---|
| [4374] | 327 | PRINT(0)("h - print this Help\n"); |
|---|
| [4359] | 328 | PRINT(0)("a - zoom in\n"); |
|---|
| 329 | PRINT(0)("z - zoom out\n"); |
|---|
| [4360] | 330 | PRINT(0)("r - reset camera position\n"); |
|---|
| [4647] | 331 | PRINT(0)("x - background color darker\n"); |
|---|
| 332 | PRINT(0)("c - background color brighter\n"); |
|---|
| [4307] | 333 | |
|---|
| [4374] | 334 | |
|---|
| [4360] | 335 | PRINT(0)("\n"); |
|---|
| 336 | PRINT(0)("mouse wheel - zoom\n"); |
|---|
| 337 | PRINT(0)("mouse left button - rotate the camera around its target\n"); |
|---|
| 338 | PRINT(0)("mouse right button - rotate the camera's target around the camera\n"); |
|---|
| 339 | PRINT(0)("mouse left-and-right button - move the camera and the target\n"); |
|---|
| [4554] | 340 | |
|---|
| [4333] | 341 | this->moduleHelp(); |
|---|
| [4307] | 342 | |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| [4343] | 345 | #ifdef GUI_MODULE |
|---|
| [4330] | 346 | int quitGui(GtkWidget* widget, void* data) |
|---|
| 347 | { |
|---|
| 348 | #ifdef HAVE_GTK2 |
|---|
| 349 | while(gtk_events_pending()) gtk_main_iteration(); |
|---|
| [4331] | 350 | Framework::getInstance()->quit(); |
|---|
| [4330] | 351 | #endif /* HAVE_GTK2 */ |
|---|
| 352 | } |
|---|
| [4343] | 353 | #endif |
|---|
| [4330] | 354 | |
|---|
| [4297] | 355 | int main(int argc, char *argv[]) |
|---|
| 356 | { |
|---|
| 357 | verbose = 3; |
|---|
| [4554] | 358 | |
|---|
| [4317] | 359 | Framework* framework = Framework::getInstance(); |
|---|
| [4316] | 360 | |
|---|
| [4343] | 361 | framework->moduleInit(argc, argv); |
|---|
| 362 | #ifdef GUI_MODULE |
|---|
| 363 | framework->moduleInitGui(argc, argv); |
|---|
| 364 | #endif |
|---|
| [4331] | 365 | framework->mainLoop(NULL); |
|---|
| [4297] | 366 | |
|---|
| 367 | delete framework; |
|---|
| [2748] | 368 | // Kill the GL & SDL screens |
|---|
| 369 | // And quit |
|---|
| 370 | return 0; |
|---|
| 371 | } |
|---|