Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4361 was 4360, checked in by bensch, 20 years ago

orxonox/trunk: reset function for the camera

File size: 6.8 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          }
187        break;
188           
189        // If a quit event was recieved
190      case SDL_QUIT:
191        // then we're done and we'll end this program
192#ifdef GUI_MODULE
193            quitGui(NULL, NULL);
194#else
195            this->quit();
196#endif
197        break;
198      default:
199        break;
200      }
201
202    }
203
204    // Get the state of the keyboard keys
205    keys = SDL_GetKeyState(NULL);
206
207    // and check if ESCAPE has been pressed. If so then quit
208    if(keys[SDLK_ESCAPE]) return false;
209  }
210  return true;
211}
212
213void Framework::quit(void)
214{
215  this->isFinished = true;
216}
217
218Framework* Framework::singletonRef = NULL;
219
220Framework* Framework::getInstance(void)
221{
222  if (Framework::singletonRef == NULL)
223    Framework::singletonRef = new Framework();
224  return Framework::singletonRef;
225}
226
227Framework::Framework()
228{
229  this->isFinished = false;
230
231  this->lastFrame = 0;
232  // Create a new OpenGL window with the title "Cone3D Basecode" at
233  // 640x480x32, fullscreen and check for errors along the way
234  GraphicsEngine::getInstance();
235
236  LightManager::getInstance();
237  glEnable(GL_TEXTURE_2D);
238
239  // Build the font from a TGA image font.tga in the data directory
240  // Hide the mouse cursor
241  SDL_ShowCursor(2);
242
243  for (int i = 0; i <MOUSE_BUTTON_COUNT; i++)
244    mouseDown[i] = false;
245
246  ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY);
247 
248  camera = new Camera();
249
250  State::getInstance()->setCamera(camera, camera->getTarget());
251
252  camera->setAbsCoor(Vector(10, 10, 10));
253}
254
255Framework::~Framework()
256{
257  delete GraphicsEngine::getInstance();
258
259}
260
261
262
263void Framework::printHelp(void) const
264{
265  PRINT(0)(" Help for the frameWork\n");
266  PRINT(0)("========================\n");
267  PRINT(0)("h - print thisHelp\n");
268  PRINT(0)("a - zoom in\n");
269  PRINT(0)("z - zoom out\n");
270  PRINT(0)("r - reset camera position\n");
271
272  PRINT(0)("\n");
273  PRINT(0)("mouse wheel - zoom\n");
274  PRINT(0)("mouse left button - rotate the camera around its target\n");
275  PRINT(0)("mouse right button - rotate the camera's target around the camera\n");
276  PRINT(0)("mouse left-and-right button - move the camera and the target\n");
277 
278  this->moduleHelp();
279
280}
281
282#ifdef GUI_MODULE
283int quitGui(GtkWidget* widget, void* data)
284{
285#ifdef HAVE_GTK2
286  while(gtk_events_pending()) gtk_main_iteration();
287  Framework::getInstance()->quit();
288#endif /* HAVE_GTK2 */
289}
290#endif
291
292int main(int argc, char *argv[])
293{
294  verbose = 3;
295 
296  Framework* framework = Framework::getInstance();
297
298  framework->moduleInit(argc, argv);
299#ifdef GUI_MODULE
300  framework->moduleInitGui(argc, argv);
301#endif
302  framework->mainLoop(NULL);
303
304  delete framework;
305  // Kill the GL & SDL screens
306  // And quit
307  return 0;
308}
Note: See TracBrowser for help on using the repository browser.