Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/physics/src/subprojects/particles/framework.cc @ 4304

Last change on this file since 4304 was 4304, checked in by bensch, 19 years ago

orxonox/branches/physics: timaestepae initailisation was corrupt

File size: 6.4 KB
Line 
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
16#include "framework.h"
17
18#include "physics_engine.h"
19#include "particle_engine.h"
20
21#include "p_node.h"
22#include "null_parent.h"
23#include "state.h"
24#include "debug.h"
25#include "graphics_engine.h"
26#include "light.h"
27#include "resource_manager.h"
28#include "camera.h"
29
30int verbose;
31
32bool Framework::mainLoop()
33{
34  while(true)
35    {
36      // keyhandler returns false if sdl gets quit by some event
37      if (!this->keyHandler())
38        return false;
39
40      // tick the scene
41      float dt = this->tick();
42
43      // Draw the scene
44      this->draw(dt);
45    }
46}
47
48
49bool Framework::draw(float dt)
50{
51  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
52  glLoadIdentity(); // Reset the view
53 
54  ParticleEngine::getInstance()->draw(dt);
55
56  camera->apply();
57 
58  SDL_GL_SwapBuffers(); // Swap the buffers
59}
60
61
62float Framework::tick()
63{
64  currFrame = SDL_GetTicks();
65  float dt = (float)(currFrame - lastFrame) / 1000.0;
66  lastFrame = currFrame;
67
68  ParticleEngine::getInstance()->tick(dt);
69
70  NullParent::getInstance()->update(dt);
71  return dt;
72}
73
74
75bool Framework::keyHandler()
76{
77  // This is the main loop for the entire program and it will run until done==TRUE
78  {
79    // And poll for events
80    SDL_Event event;
81    SDL_PollEvent(&event) ;
82    {
83      switch (event.type) {
84      case SDL_MOUSEMOTION:
85        PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", 
86                  event.motion.xrel, event.motion.yrel,
87                  event.motion.x, event.motion.y);
88        // TRACKBALL
89        if (mouse1Down)
90          {
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)
105              {
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);
116              }
117              rotQlast = rotQ;
118              p1 = p2;
119            */
120           
121
122          }
123        break;
124      case SDL_MOUSEBUTTONDOWN:
125        if (event.button.button == 4)
126          {
127            PRINTF(4)("MouseWheel up\n");
128            //      zoomTo *= .5;
129          }
130        else if (event.button.button == 5)
131          {
132            PRINTF(4)("MouseWheel down\n");
133            //      zoomTo *= 2.0;
134          }
135        else if (event.button.button == 1)
136          {
137            mouse1Down = true;
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            */
152          }
153        else
154          {
155            PRINTF(0)("MouseButton %d pressed at (%d,%d).\n",
156                   event.button.button, event.button.x, event.button.y);
157          }
158           
159        break;
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            {
167              PRINTF(4)("MouseButton %d released at (%d,%d).\n",
168                        event.button.button, event.button.x, event.button.y);
169            }
170        break;
171
172      case SDL_VIDEORESIZE:
173        GraphicsEngine::getInstance()->resolutionChanged(&event.resize);
174        break;
175      case SDL_KEYDOWN:
176        switch (event.key.keysym.sym)
177          {
178          case SDLK_x:
179           
180            break;
181          case SDLK_c:
182
183            break;
184          case SDLK_i:
185            ParticleEngine::getInstance()->debug();
186            break;
187          case SDLK_a:
188            //zoomTo /=2;
189            break;
190          case SDLK_z:
191            //zoomTo *=2;
192            break;
193          }
194        break;
195           
196        // If a quit event was recieved
197      case SDL_QUIT:
198        // then we're done and we'll end this program
199        return false;
200        break;
201      default:
202        break;
203      }
204
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
211    if(keys[SDLK_ESCAPE]) return false;
212  }
213  return true;
214}
215
216
217Framework::Framework()
218{
219  this->lastFrame = 0;
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  LightManager::getInstance();
225  glEnable(GL_TEXTURE_2D);
226
227  // Build the font from a TGA image font.tga in the data directory
228  // Hide the mouse cursor
229  SDL_ShowCursor(2);
230  mouse1Down = false;
231
232  ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY);
233 
234  // Creating a Test Particle System
235  ParticleSystem* system = new ParticleSystem(100000, PARTICLE_SPRITE);
236  system->setLifeSpan(5);
237  system->setConserve(.99);
238  system->setRadius(4, 3, 1, 2);
239  system->setColor(.5,0,0,.5, 1,1,0,1, 0,0,0,0);
240
241  // Creating a Test Particle Emitter
242  ParticleEmitter* emitter = new ParticleEmitter(Vector(0 , 1, 0), M_PI_4, 20, .1);
243  emitter->setType(EMITTER_DOT  );
244  emitter->setSize(0);
245  emitter->setRelCoor(Vector(0,0,0));
246 
247  // Add the Flow from the Emitter into the System
248  ParticleEngine::getInstance()->addConnection(emitter, system);
249
250  camera = new Camera();
251
252  State::getInstance()->setCamera(camera, emitter);
253
254  camera->setAbsCoor(Vector(10, 10, 0));
255
256}
257
258Framework::~Framework()
259{
260  delete GraphicsEngine::getInstance();
261
262}
263
264int main(int argc, char *argv[])
265{
266  verbose = 3;
267  Framework* framework = new Framework();
268  framework->mainLoop();
269
270  delete framework;
271  // Kill the GL & SDL screens
272  // And quit
273  return 0;
274}
Note: See TracBrowser for help on using the repository browser.