Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/subprojects/framework.cc @ 4519

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

orxonox/trunk: changed all getInstances into inline functions to save some (minor) time

File size: 7.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
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#ifdef GUI_MODULE
37      while(gtk_events_pending())
38        gtk_main_iteration();
39#endif
40      // keyhandler returns false if sdl gets quit by some event
41      framework->eventHandler();
42
43      // tick the scene
44      float dt = framework->tick();
45
46      NullParent::getInstance()->update(dt);
47
48      // Draw the scene
49      framework->draw(dt);
50
51    }
52}
53
54bool Framework::draw(float dt)
55{
56  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
57  glLoadIdentity(); // Reset the view
58 
59  this->moduleDraw();
60 
61  camera->apply();
62 
63  SDL_GL_SwapBuffers(); // Swap the buffers
64}
65
66
67float Framework::tick()
68{
69  currFrame = SDL_GetTicks();
70  float dt = (float)(currFrame - lastFrame) / 1000.0;
71  lastFrame = currFrame;
72
73  this->moduleTick(dt);
74
75  return dt;
76}
77
78
79bool Framework::eventHandler()
80{
81  // This is the main loop for the entire program and it will run until done==TRUE
82  {
83    // And poll for events
84    SDL_Event event;
85    while(SDL_PollEvent(&event))
86    {
87      moduleEventHandler(&event);
88
89      switch (event.type) {
90      case SDL_MOUSEMOTION:
91        {
92          Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor();
93          Vector up = Vector(0, 1, 0);
94          up = camera->getAbsDir().apply(up);
95          Vector h = up.cross(view);
96          Vector v = h.cross(view);
97          h.normalize();
98          v.normalize();
99          float distance = view.len();
100         
101          Vector newCameraPos = camera->getAbsCoor();
102          Vector newTargetPos = camera->getTarget()->getAbsCoor();
103          int changed = 0;
104
105          if (mouseDown[1])
106            {
107              newCameraPos = camera->getRelCoor()+ (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance;
108              changed += 1;
109            }
110          if (mouseDown[3])
111            {
112              newTargetPos = camera->getTarget()->getRelCoor() + (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance;
113              changed += 2;
114            }
115         
116          Vector newView = newTargetPos - newCameraPos;
117
118          if (changed == 1)
119            camera->setRelCoor(newCameraPos + newView * (1- distance/newView.len()));
120          else if (changed == 2)
121            camera->getTarget()->setRelCoor(newTargetPos - newView * (1-distance/newView.len()));
122          else if (changed == 3)
123            {
124              camera->setRelCoor(newCameraPos);
125              camera->getTarget()->setRelCoor(newTargetPos);
126            }
127           
128        }
129        break;
130      case SDL_MOUSEBUTTONDOWN:
131        switch (event.button.button)
132          {
133          case 4:
134            PRINTF(4)("MouseWheel up\n");
135            camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
136            break;
137          case 5:
138            PRINTF(4)("MouseWheel down\n");
139            camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
140            break;
141          case 1:
142          case 2:
143          case 3:
144            mouseDown[event.button.button] = true;
145            break;
146          }
147           
148        break;
149      case SDL_MOUSEBUTTONUP:
150        switch (event.button.button)
151          {
152          case 1:
153          case 2:
154          case 3:
155            mouseDown[event.button.button] = false;
156            break;
157          }
158        break;
159      case SDL_VIDEORESIZE:
160        GraphicsEngine::getInstance()->resolutionChanged(&event.resize);
161        break;
162      case SDL_KEYDOWN:
163        switch (event.key.keysym.sym)
164          {
165          case SDLK_q:
166          case SDLK_ESCAPE:
167#ifdef GUI_MODULE
168            quitGui(NULL, NULL);
169#else
170            this->quit();
171#endif
172            break;
173          case SDLK_a:
174            camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
175            break;
176          case SDLK_z:
177            camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
178            break;
179          case SDLK_r:
180            camera->setAbsCoor(Vector(10, 10, 10));
181            camera->getTarget()->setAbsCoor(Vector());
182            break;
183          case SDLK_h:
184            this->printHelp();
185            break;
186          case SDLK_2:
187            for (int i = 0; i < 3; i++)
188              {
189                backgroundColor[i] += .1;
190                if (backgroundColor[i] > 1.0)
191                  backgroundColor[i] = 1.0;
192                GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
193              }
194            break;
195          case SDLK_1:
196            for (int i = 0; i < 3; i++)
197              {
198                backgroundColor[i] -= .1;
199                if (backgroundColor[i] < 0.0)
200                  backgroundColor[i] = 0.0;
201                GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
202              }
203            break;
204          }
205        break;
206           
207        // If a quit event was recieved
208      case SDL_QUIT:
209        // then we're done and we'll end this program
210#ifdef GUI_MODULE
211            quitGui(NULL, NULL);
212#else
213            this->quit();
214#endif
215        break;
216      default:
217        break;
218      }
219
220    }
221
222    // Get the state of the keyboard keys
223    keys = SDL_GetKeyState(NULL);
224
225    // and check if ESCAPE has been pressed. If so then quit
226    if(keys[SDLK_ESCAPE]) return false;
227  }
228  return true;
229}
230
231void Framework::quit(void)
232{
233  this->isFinished = true;
234}
235
236Framework* Framework::singletonRef = NULL;
237
238Framework::Framework()
239{
240  this->isFinished = false;
241
242  this->lastFrame = 0;
243  // Create a new OpenGL window with the title "Cone3D Basecode" at
244  // 640x480x32, fullscreen and check for errors along the way
245  GraphicsEngine::getInstance();
246
247  LightManager::getInstance();
248  glEnable(GL_TEXTURE_2D);
249
250  // Build the font from a TGA image font.tga in the data directory
251  // Hide the mouse cursor
252  SDL_ShowCursor(2);
253
254  for (int i = 0; i < MOUSE_BUTTON_COUNT; i++)
255    mouseDown[i] = false;
256  for (int i = 0; i < 4; i++)
257    backgroundColor[i] = 0;
258
259  ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY);
260 
261  camera = new Camera();
262
263  State::getInstance()->setCamera(camera, camera->getTarget());
264
265  camera->setAbsCoor(Vector(10, 10, 10));
266
267}
268
269Framework::~Framework()
270{
271  delete GraphicsEngine::getInstance();
272
273}
274
275
276
277void Framework::printHelp(void) const
278{
279  PRINT(0)(" Help for the frameWork\n");
280  PRINT(0)("========================\n");
281  PRINT(0)("h - print this Help\n");
282  PRINT(0)("a - zoom in\n");
283  PRINT(0)("z - zoom out\n");
284  PRINT(0)("r - reset camera position\n");
285  PRINT(0)("1 - background color darker\n");
286  PRINT(0)("2 - background color brighter\n");
287
288
289  PRINT(0)("\n");
290  PRINT(0)("mouse wheel - zoom\n");
291  PRINT(0)("mouse left button - rotate the camera around its target\n");
292  PRINT(0)("mouse right button - rotate the camera's target around the camera\n");
293  PRINT(0)("mouse left-and-right button - move the camera and the target\n");
294 
295  this->moduleHelp();
296
297}
298
299#ifdef GUI_MODULE
300int quitGui(GtkWidget* widget, void* data)
301{
302#ifdef HAVE_GTK2
303  while(gtk_events_pending()) gtk_main_iteration();
304  Framework::getInstance()->quit();
305#endif /* HAVE_GTK2 */
306}
307#endif
308
309int main(int argc, char *argv[])
310{
311  verbose = 3;
312 
313  Framework* framework = Framework::getInstance();
314
315  framework->moduleInit(argc, argv);
316#ifdef GUI_MODULE
317  framework->moduleInitGui(argc, argv);
318#endif
319  framework->mainLoop(NULL);
320
321  delete framework;
322  // Kill the GL & SDL screens
323  // And quit
324  return 0;
325}
Note: See TracBrowser for help on using the repository browser.