Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/physics: made the framework more modular

File size: 5.6 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
19#include "p_node.h"
20#include "null_parent.h"
21#include "state.h"
22#include "debug.h"
23#include "graphics_engine.h"
24#include "light.h"
25#include "resource_manager.h"
26#include "camera.h"
27
28
29int verbose;
30
31void* Framework::mainLoop(void* tmp)
32{
33  Framework* framework = Framework::getInstance();
34  while(!framework->isFinished)
35    {
36      while(gtk_events_pending()) 
37        gtk_main_iteration();
38
39      // keyhandler returns false if sdl gets quit by some event
40      framework->keyHandler();
41
42      // tick the scene
43      float dt = framework->tick();
44
45      NullParent::getInstance()->update(dt);
46
47      // Draw the scene
48      framework->draw(dt);
49
50    }
51}
52
53bool Framework::draw(float dt)
54{
55  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
56  glLoadIdentity(); // Reset the view
57 
58  this->moduleDraw(dt);
59 
60  camera->apply();
61 
62  SDL_GL_SwapBuffers(); // Swap the buffers
63}
64
65
66float Framework::tick()
67{
68  currFrame = SDL_GetTicks();
69  float dt = (float)(currFrame - lastFrame) / 1000.0;
70  lastFrame = currFrame;
71
72  this->moduleTick(dt);
73
74  return dt;
75}
76
77
78bool Framework::keyHandler()
79{
80  // This is the main loop for the entire program and it will run until done==TRUE
81  {
82    // And poll for events
83    SDL_Event event;
84    while(SDL_PollEvent(&event))
85    {
86      switch (event.type) {
87      case SDL_MOUSEMOTION:
88        {
89          Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor();
90          Vector up = Vector(0, 1, 0);
91          up = camera->getAbsDir().apply(up);
92          Vector h = up.cross(view);
93          Vector v = h.cross(view);
94          h.normalize();
95          v.normalize();
96
97          if (mouseDown[1])
98            camera->setRelCoor(camera->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05);
99          if (mouseDown[3])
100            camera->getTarget()->setRelCoor(camera->getTarget()->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05);
101           
102        }
103        break;
104      case SDL_MOUSEBUTTONDOWN:
105        switch (event.button.button)
106          {
107          case 4:
108            PRINTF(4)("MouseWheel up\n");
109            camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
110            break;
111          case 5:
112            PRINTF(4)("MouseWheel down\n");
113            camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
114            break;
115          case 1:
116          case 2:
117          case 3:
118            mouseDown[event.button.button] = true;
119            break;
120          }
121           
122        break;
123      case SDL_MOUSEBUTTONUP:
124        switch (event.button.button)
125          {
126          case 1:
127          case 2:
128          case 3:
129            mouseDown[event.button.button] = false;
130            break;
131          }
132        break;
133      case SDL_VIDEORESIZE:
134        GraphicsEngine::getInstance()->resolutionChanged(&event.resize);
135        break;
136      case SDL_KEYDOWN:
137        switch (event.key.keysym.sym)
138          {
139          case SDLK_q:
140          case SDLK_ESCAPE:
141            quitGui(NULL, NULL);
142            break;
143          case SDLK_c:
144
145            break;
146          case SDLK_i:
147            //      ParticleEngine::getInstance()->debug();
148            break;
149          case SDLK_a:
150            camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
151            break;
152          case SDLK_z:
153            camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
154            break;
155          case SDLK_h:
156            this->printHelp();
157            break;
158          }
159        break;
160           
161        // If a quit event was recieved
162      case SDL_QUIT:
163        // then we're done and we'll end this program
164        quitGui(NULL, NULL);
165        break;
166      default:
167        break;
168      }
169
170    }
171
172    // Get the state of the keyboard keys
173    keys = SDL_GetKeyState(NULL);
174
175    // and check if ESCAPE has been pressed. If so then quit
176    if(keys[SDLK_ESCAPE]) return false;
177  }
178  return true;
179}
180
181void Framework::quit(void)
182{
183  this->isFinished = true;
184}
185
186Framework* Framework::singletonRef = NULL;
187
188Framework* Framework::getInstance(void)
189{
190  if (Framework::singletonRef == NULL)
191    Framework::singletonRef = new Framework();
192  return Framework::singletonRef;
193}
194
195Framework::Framework()
196{
197  this->isFinished = false;
198
199  this->lastFrame = 0;
200  // Create a new OpenGL window with the title "Cone3D Basecode" at
201  // 640x480x32, fullscreen and check for errors along the way
202  GraphicsEngine::getInstance();
203
204  LightManager::getInstance();
205  glEnable(GL_TEXTURE_2D);
206
207  // Build the font from a TGA image font.tga in the data directory
208  // Hide the mouse cursor
209  SDL_ShowCursor(2);
210
211  for (int i = 0; i <MOUSE_BUTTON_COUNT; i++)
212    mouseDown[i] = false;
213
214  ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY);
215 
216
217  moduleInit();
218
219  camera = new Camera();
220
221  State::getInstance()->setCamera(camera, camera->getTarget());
222
223  camera->setAbsCoor(Vector(10, 10, 0));
224}
225
226Framework::~Framework()
227{
228  delete GraphicsEngine::getInstance();
229
230}
231
232
233
234void Framework::printHelp(void) const
235{
236  PRINT(0)(" Help for the frameWork\n");
237  PRINT(0)("========================\n");
238  PRINT(0)("h - print thisHelp\n");
239
240  this->moduleHelp();
241
242}
243
244int quitGui(GtkWidget* widget, void* data)
245{
246#ifdef HAVE_GTK2
247  gtk_main_quit();
248  while(gtk_events_pending()) gtk_main_iteration();
249  Framework::getInstance()->quit();
250#endif /* HAVE_GTK2 */
251}
252
253
254int main(int argc, char *argv[])
255{
256  verbose = 3;
257 
258  Framework* framework = Framework::getInstance();
259
260  framework->moduleInitGui();
261  //  framework->mainloopGui(NULL);
262
263  framework->mainLoop(NULL);
264
265  delete framework;
266  // Kill the GL & SDL screens
267  // And quit
268  return 0;
269}
Note: See TracBrowser for help on using the repository browser.