/* 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 "physics_engine.h" #include "particle_engine.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; bool Framework::mainLoop() { while(true) { // keyhandler returns false if sdl gets quit by some event if (!this->keyHandler()) return false; // tick the scene float dt = this->tick(); // Draw the scene this->draw(dt); } } bool Framework::draw(float dt) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glLoadIdentity(); // Reset the view ParticleEngine::getInstance()->draw(dt); camera->apply(); SDL_GL_SwapBuffers(); // Swap the buffers } float Framework::tick() { currFrame = SDL_GetTicks(); float dt = (float)(currFrame - lastFrame) / 1000.0; lastFrame = currFrame; ParticleEngine::getInstance()->tick(dt); NullParent::getInstance()->update(dt); return dt; } bool Framework::keyHandler() { // This is the main loop for the entire program and it will run until done==TRUE { // And poll for events SDL_Event event; SDL_PollEvent(&event) ; { switch (event.type) { case SDL_MOUSEMOTION: PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", event.motion.xrel, event.motion.yrel, event.motion.x, event.motion.y); // TRACKBALL if (mouse1Down) { /* int mX = event.button.x; int mY = event.button.y; int wH = GraphicsEngine::getInstance()->getResolutionY(); int wW = GraphicsEngine::getInstance()->getResolutionX(); Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); // PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z); p2 = tmpV-M; p2.y = -p2.y; rotAxis = p1.cross(p2); // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z); // in case that there is no rotation-axis defined if (rotAxis.x != 0 || rotAxis.y != 0 || rotAxis.z != 0) { rotAxis.normalize(); // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle); rotAngle = angleRad (p1, p2); rotQ = Quaternion (rotAngle, rotAxis); rotQ = rotQ * rotQlast; rotQ.matrix (matQ); // dir = rotQ.apply(dir); // dir.normalize(); // PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle); } rotQlast = rotQ; p1 = p2; */ } break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == 4) { PRINTF(4)("MouseWheel up\n"); // zoomTo *= .5; } else if (event.button.button == 5) { PRINTF(4)("MouseWheel down\n"); // zoomTo *= 2.0; } else if (event.button.button == 1) { mouse1Down = true; movement[0] = event.button.x; movement[1] = event.button.y; movement[2] = 0; movement[3] = 0; printf("test %d %d\n", movement[0], movement[1]); /* int mX = event.button.x; int mY = event.button.y; int wH = GraphicsEngine::getInstance()->getResolutionY(); int wW = GraphicsEngine::getInstance()->getResolutionX(); Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); p1 = tmpV-M; p1.y = -p1.y; */ } else { PRINTF(0)("MouseButton %d pressed at (%d,%d).\n", event.button.button, event.button.x, event.button.y); } break; case SDL_MOUSEBUTTONUP: if (event.button.button == 4); else if (event.button.button == 5); else if (event.button.button == 1) mouse1Down =false; else { PRINTF(4)("MouseButton %d released at (%d,%d).\n", event.button.button, event.button.x, event.button.y); } break; case SDL_VIDEORESIZE: GraphicsEngine::getInstance()->resolutionChanged(&event.resize); break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_x: break; case SDLK_c: break; case SDLK_i: ParticleEngine::getInstance()->debug(); break; case SDLK_a: //zoomTo /=2; break; case SDLK_z: //zoomTo *=2; break; } break; // If a quit event was recieved case SDL_QUIT: // then we're done and we'll end this program return false; 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; } Framework::Framework() { 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); mouse1Down = false; ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); // Creating a Test Particle System ParticleSystem* system = new ParticleSystem(100000, PARTICLE_SPRITE); system->setLifeSpan(5); system->setConserve(.99); system->setRadius(4, 3, 1, 2); system->setColor(.5,0,0,.5, 1,1,0,1, 0,0,0,0); // Creating a Test Particle Emitter ParticleEmitter* emitter = new ParticleEmitter(Vector(0 , 1, 0), M_PI_4, 20, .1); emitter->setType(EMITTER_DOT ); emitter->setSize(0); emitter->setRelCoor(Vector(0,0,0)); // Add the Flow from the Emitter into the System ParticleEngine::getInstance()->addConnection(emitter, system); camera = new Camera(); State::getInstance()->setCamera(camera, emitter); camera->setAbsCoor(Vector(10, 10, 0)); } Framework::~Framework() { delete GraphicsEngine::getInstance(); } int main(int argc, char *argv[]) { verbose = 3; Framework* framework = new Framework(); framework->mainLoop(); delete framework; // Kill the GL & SDL screens // And quit return 0; }