/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #include "framework.h" #include "p_node.h" #include "null_parent.h" #include "state.h" #include "debug.h" #include "graphics_engine.h" #include "light.h" #include "resource_manager.h" #include "camera.h" int verbose; void* Framework::mainLoop(void* tmp) { Framework* framework = Framework::getInstance(); while(!framework->isFinished) { #ifdef GUI_MODULE while(gtk_events_pending()) gtk_main_iteration(); #endif // keyhandler returns false if sdl gets quit by some event framework->eventHandler(); // tick the scene float dt = framework->tick(); NullParent::getInstance()->update(dt); // Draw the scene framework->draw(dt); } } bool Framework::draw(float dt) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glLoadIdentity(); // Reset the view this->moduleDraw(dt); camera->apply(); SDL_GL_SwapBuffers(); // Swap the buffers } float Framework::tick() { currFrame = SDL_GetTicks(); float dt = (float)(currFrame - lastFrame) / 1000.0; lastFrame = currFrame; this->moduleTick(dt); return dt; } bool Framework::eventHandler() { // This is the main loop for the entire program and it will run until done==TRUE { // And poll for events SDL_Event event; while(SDL_PollEvent(&event)) { moduleEventHandler(&event); switch (event.type) { case SDL_MOUSEMOTION: { Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor(); Vector up = Vector(0, 1, 0); up = camera->getAbsDir().apply(up); Vector h = up.cross(view); Vector v = h.cross(view); h.normalize(); v.normalize(); if (mouseDown[1]) camera->setRelCoor(camera->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05); if (mouseDown[3]) camera->getTarget()->setRelCoor(camera->getTarget()->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05); } break; case SDL_MOUSEBUTTONDOWN: switch (event.button.button) { case 4: PRINTF(4)("MouseWheel up\n"); camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); break; case 5: PRINTF(4)("MouseWheel down\n"); camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); break; case 1: case 2: case 3: mouseDown[event.button.button] = true; break; } break; case SDL_MOUSEBUTTONUP: switch (event.button.button) { case 1: case 2: case 3: mouseDown[event.button.button] = false; break; } break; case SDL_VIDEORESIZE: GraphicsEngine::getInstance()->resolutionChanged(&event.resize); break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_q: case SDLK_ESCAPE: #ifdef GUI_MODULE quitGui(NULL, NULL); #else this->quit(); #endif break; case SDLK_a: camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); break; case SDLK_z: camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); break; case SDLK_h: this->printHelp(); break; } break; // If a quit event was recieved case SDL_QUIT: // then we're done and we'll end this program #ifdef GUI_MODULE quitGui(NULL, NULL); #else this->quit(); #endif break; default: break; } } // Get the state of the keyboard keys keys = SDL_GetKeyState(NULL); // and check if ESCAPE has been pressed. If so then quit if(keys[SDLK_ESCAPE]) return false; } return true; } void Framework::quit(void) { this->isFinished = true; } Framework* Framework::singletonRef = NULL; Framework* Framework::getInstance(void) { if (Framework::singletonRef == NULL) Framework::singletonRef = new Framework(); return Framework::singletonRef; } Framework::Framework() { this->isFinished = false; this->lastFrame = 0; // Create a new OpenGL window with the title "Cone3D Basecode" at // 640x480x32, fullscreen and check for errors along the way GraphicsEngine::getInstance(); LightManager::getInstance(); glEnable(GL_TEXTURE_2D); // Build the font from a TGA image font.tga in the data directory // Hide the mouse cursor SDL_ShowCursor(2); for (int i = 0; i setDataDir(DATA_DIRECTORY); camera = new Camera(); State::getInstance()->setCamera(camera, camera->getTarget()); camera->setAbsCoor(Vector(10, 10, 0)); } Framework::~Framework() { delete GraphicsEngine::getInstance(); } void Framework::printHelp(void) const { PRINT(0)(" Help for the frameWork\n"); PRINT(0)("========================\n"); PRINT(0)("h - print thisHelp\n"); this->moduleHelp(); } #ifdef GUI_MODULE int quitGui(GtkWidget* widget, void* data) { #ifdef HAVE_GTK2 while(gtk_events_pending()) gtk_main_iteration(); Framework::getInstance()->quit(); #endif /* HAVE_GTK2 */ } #endif int main(int argc, char *argv[]) { verbose = 3; Framework* framework = Framework::getInstance(); framework->moduleInit(argc, argv); #ifdef GUI_MODULE framework->moduleInitGui(argc, argv); #endif framework->mainLoop(NULL); delete framework; // Kill the GL & SDL screens // And quit return 0; }