[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 | |
---|
[4293] | 18 | #include "physics_engine.h" |
---|
| 19 | #include "particle_engine.h" |
---|
[3427] | 20 | |
---|
[4294] | 21 | #include "p_node.h" |
---|
| 22 | #include "null_parent.h" |
---|
| 23 | #include "state.h" |
---|
[4272] | 24 | #include "debug.h" |
---|
| 25 | #include "graphics_engine.h" |
---|
[4300] | 26 | #include "light.h" |
---|
[4295] | 27 | #include "resource_manager.h" |
---|
[4297] | 28 | #include "camera.h" |
---|
[3657] | 29 | |
---|
[3398] | 30 | int verbose; |
---|
[4293] | 31 | |
---|
[4297] | 32 | bool Framework::mainLoop() |
---|
[2748] | 33 | { |
---|
[4293] | 34 | while(true) |
---|
| 35 | { |
---|
[4297] | 36 | // keyhandler returns false if sdl gets quit by some event |
---|
[4293] | 37 | if (!keyHandler()) |
---|
| 38 | return false; |
---|
[2939] | 39 | |
---|
[4297] | 40 | // tick the scene |
---|
[4294] | 41 | float dt = tick(); |
---|
[4297] | 42 | |
---|
[4293] | 43 | // Draw the scene |
---|
[4300] | 44 | draw(dt); |
---|
[4293] | 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
[4297] | 49 | bool Framework::draw(float dt) |
---|
[4293] | 50 | { |
---|
[2748] | 51 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
| 52 | glLoadIdentity(); // Reset the view |
---|
| 53 | |
---|
[4294] | 54 | ParticleEngine::getInstance()->draw(dt); |
---|
[4297] | 55 | |
---|
| 56 | camera->apply(); |
---|
[4294] | 57 | |
---|
[2748] | 58 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
[4293] | 59 | |
---|
[2748] | 60 | } |
---|
[4300] | 61 | |
---|
| 62 | |
---|
[4297] | 63 | float Framework::tick() |
---|
[2748] | 64 | { |
---|
[4293] | 65 | currFrame = SDL_GetTicks(); |
---|
[4300] | 66 | float dt = (float)(currFrame - lastFrame) / 1000.0; |
---|
| 67 | lastFrame = currFrame; |
---|
[3398] | 68 | |
---|
[4293] | 69 | ParticleEngine::getInstance()->tick(dt); |
---|
[2963] | 70 | |
---|
[4294] | 71 | NullParent::getInstance()->update(dt); |
---|
| 72 | return dt; |
---|
[4293] | 73 | } |
---|
[2952] | 74 | |
---|
[2931] | 75 | |
---|
[4297] | 76 | bool Framework::keyHandler() |
---|
[4293] | 77 | { |
---|
[2748] | 78 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
| 79 | { |
---|
| 80 | // And poll for events |
---|
| 81 | SDL_Event event; |
---|
| 82 | while ( SDL_PollEvent(&event) ) { |
---|
| 83 | switch (event.type) { |
---|
[2931] | 84 | case SDL_MOUSEMOTION: |
---|
[3656] | 85 | PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", |
---|
[3206] | 86 | event.motion.xrel, event.motion.yrel, |
---|
| 87 | event.motion.x, event.motion.y); |
---|
[2952] | 88 | // TRACKBALL |
---|
| 89 | if (mouse1Down) |
---|
| 90 | { |
---|
[4298] | 91 | /* |
---|
| 92 | int mX = event.button.x; |
---|
| 93 | int mY = event.button.y; |
---|
| 94 | int wH = GraphicsEngine::getInstance()->getResolutionY(); |
---|
| 95 | int wW = GraphicsEngine::getInstance()->getResolutionX(); |
---|
| 96 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
| 97 | // PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z); |
---|
| 98 | p2 = tmpV-M; |
---|
| 99 | p2.y = -p2.y; |
---|
| 100 | rotAxis = p1.cross(p2); |
---|
| 101 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z); |
---|
| 102 | |
---|
| 103 | // in case that there is no rotation-axis defined |
---|
| 104 | if (rotAxis.x != 0 || rotAxis.y != 0 || rotAxis.z != 0) |
---|
[2963] | 105 | { |
---|
[4298] | 106 | rotAxis.normalize(); |
---|
| 107 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle); |
---|
| 108 | |
---|
| 109 | rotAngle = angleRad (p1, p2); |
---|
| 110 | rotQ = Quaternion (rotAngle, rotAxis); |
---|
| 111 | rotQ = rotQ * rotQlast; |
---|
| 112 | rotQ.matrix (matQ); |
---|
| 113 | // dir = rotQ.apply(dir); |
---|
| 114 | // dir.normalize(); |
---|
| 115 | // PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle); |
---|
[2963] | 116 | } |
---|
[4298] | 117 | rotQlast = rotQ; |
---|
| 118 | p1 = p2; |
---|
| 119 | */ |
---|
[4300] | 120 | |
---|
[2963] | 121 | |
---|
[2952] | 122 | } |
---|
[2931] | 123 | break; |
---|
| 124 | case SDL_MOUSEBUTTONDOWN: |
---|
[2932] | 125 | if (event.button.button == 4) |
---|
[2933] | 126 | { |
---|
[3656] | 127 | PRINTF(4)("MouseWheel up\n"); |
---|
[4300] | 128 | // zoomTo *= .5; |
---|
[2933] | 129 | } |
---|
[2932] | 130 | else if (event.button.button == 5) |
---|
[2933] | 131 | { |
---|
[3656] | 132 | PRINTF(4)("MouseWheel down\n"); |
---|
[4300] | 133 | // zoomTo *= 2.0; |
---|
[2933] | 134 | } |
---|
[2952] | 135 | else if (event.button.button == 1) |
---|
| 136 | { |
---|
| 137 | mouse1Down = true; |
---|
[4300] | 138 | movement[0] = event.button.x; |
---|
| 139 | movement[1] = event.button.y; |
---|
| 140 | movement[2] = 0; |
---|
| 141 | movement[3] = 0; |
---|
| 142 | printf("test %d %d\n", movement[0], movement[1]); |
---|
| 143 | /* |
---|
| 144 | int mX = event.button.x; |
---|
| 145 | int mY = event.button.y; |
---|
| 146 | int wH = GraphicsEngine::getInstance()->getResolutionY(); |
---|
| 147 | int wW = GraphicsEngine::getInstance()->getResolutionX(); |
---|
| 148 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
| 149 | p1 = tmpV-M; |
---|
| 150 | p1.y = -p1.y; |
---|
| 151 | */ |
---|
[2952] | 152 | } |
---|
[2932] | 153 | else |
---|
| 154 | { |
---|
[3208] | 155 | PRINTF(0)("MouseButton %d pressed at (%d,%d).\n", |
---|
[2932] | 156 | event.button.button, event.button.x, event.button.y); |
---|
| 157 | } |
---|
| 158 | |
---|
[2931] | 159 | break; |
---|
[2952] | 160 | case SDL_MOUSEBUTTONUP: |
---|
| 161 | if (event.button.button == 4); |
---|
| 162 | else if (event.button.button == 5); |
---|
| 163 | else if (event.button.button == 1) |
---|
| 164 | mouse1Down =false; |
---|
| 165 | else |
---|
| 166 | { |
---|
[3656] | 167 | PRINTF(4)("MouseButton %d released at (%d,%d).\n", |
---|
[3208] | 168 | event.button.button, event.button.x, event.button.y); |
---|
[2952] | 169 | } |
---|
[2931] | 170 | break; |
---|
[3211] | 171 | |
---|
[4302] | 172 | case SDL_VIDEORESIZE: |
---|
| 173 | GraphicsEngine::getInstance()->resolutionChanged(&event.resize); |
---|
| 174 | break; |
---|
[3211] | 175 | case SDL_KEYDOWN: |
---|
| 176 | switch (event.key.keysym.sym) |
---|
| 177 | { |
---|
| 178 | case SDLK_x: |
---|
[4298] | 179 | |
---|
[3211] | 180 | break; |
---|
| 181 | case SDLK_c: |
---|
[4293] | 182 | |
---|
[3211] | 183 | break; |
---|
[4296] | 184 | case SDLK_i: |
---|
| 185 | ParticleEngine::getInstance()->debug(); |
---|
| 186 | break; |
---|
[3418] | 187 | case SDLK_a: |
---|
[4300] | 188 | //zoomTo /=2; |
---|
[3418] | 189 | break; |
---|
| 190 | case SDLK_z: |
---|
[4300] | 191 | //zoomTo *=2; |
---|
| 192 | break; |
---|
[3211] | 193 | } |
---|
| 194 | break; |
---|
[2952] | 195 | |
---|
[2748] | 196 | // If a quit event was recieved |
---|
[2863] | 197 | case SDL_QUIT: |
---|
| 198 | // then we're done and we'll end this program |
---|
[4293] | 199 | return false; |
---|
| 200 | break; |
---|
[2863] | 201 | default: |
---|
[4293] | 202 | break; |
---|
[2748] | 203 | } |
---|
[2952] | 204 | |
---|
[2748] | 205 | } |
---|
| 206 | |
---|
| 207 | // Get the state of the keyboard keys |
---|
| 208 | keys = SDL_GetKeyState(NULL); |
---|
| 209 | |
---|
| 210 | // and check if ESCAPE has been pressed. If so then quit |
---|
[4293] | 211 | if(keys[SDLK_ESCAPE]) return false; |
---|
[2748] | 212 | } |
---|
[4293] | 213 | return true; |
---|
| 214 | } |
---|
[2748] | 215 | |
---|
[4293] | 216 | |
---|
[4300] | 217 | Framework::Framework() |
---|
[4293] | 218 | { |
---|
| 219 | |
---|
| 220 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
| 221 | // 640x480x32, fullscreen and check for errors along the way |
---|
| 222 | GraphicsEngine::getInstance(); |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | |
---|
[4300] | 226 | LightManager::getInstance(); |
---|
| 227 | glEnable(GL_TEXTURE_2D); |
---|
[4293] | 228 | |
---|
| 229 | // Build the font from a TGA image font.tga in the data directory |
---|
| 230 | // Hide the mouse cursor |
---|
| 231 | SDL_ShowCursor(2); |
---|
| 232 | mouse1Down = false; |
---|
| 233 | |
---|
[4295] | 234 | ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); |
---|
[4294] | 235 | |
---|
[4300] | 236 | // Creating a Test Particle System |
---|
[4294] | 237 | ParticleSystem* system = new ParticleSystem(100000, PARTICLE_SPRITE); |
---|
[4296] | 238 | system->setLifeSpan(5); |
---|
[4300] | 239 | system->setConserve(.99); |
---|
[4294] | 240 | system->setRadius(4, 3, 1, 2); |
---|
| 241 | system->setColor(.5,0,0,.5, 1,1,0,1, 0,0,0,0); |
---|
| 242 | |
---|
| 243 | // Creating a Test Particle Emitter |
---|
[4300] | 244 | ParticleEmitter* emitter = new ParticleEmitter(Vector(0 , 1, 0), M_PI_4, 20, .1); |
---|
[4296] | 245 | emitter->setType(EMITTER_DOT ); |
---|
| 246 | emitter->setSize(0); |
---|
[4294] | 247 | emitter->setRelCoor(Vector(0,0,0)); |
---|
| 248 | |
---|
| 249 | // Add the Flow from the Emitter into the System |
---|
| 250 | ParticleEngine::getInstance()->addConnection(emitter, system); |
---|
| 251 | |
---|
[4297] | 252 | camera = new Camera(); |
---|
[4294] | 253 | |
---|
| 254 | State::getInstance()->setCamera(camera, emitter); |
---|
| 255 | |
---|
| 256 | camera->setAbsCoor(Vector(10, 10, 0)); |
---|
| 257 | |
---|
[4297] | 258 | } |
---|
| 259 | |
---|
| 260 | Framework::~Framework() |
---|
| 261 | { |
---|
| 262 | delete GraphicsEngine::getInstance(); |
---|
| 263 | |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | int main(int argc, char *argv[]) |
---|
| 267 | { |
---|
| 268 | verbose = 3; |
---|
| 269 | Framework* framework =new Framework(); |
---|
| 270 | framework->mainLoop(); |
---|
| 271 | |
---|
| 272 | delete framework; |
---|
[2748] | 273 | // Kill the GL & SDL screens |
---|
| 274 | // And quit |
---|
| 275 | return 0; |
---|
| 276 | } |
---|