Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cd/src/subprojects/framework.cc @ 7554

Last change on this file since 7554 was 7554, checked in by patrick, 18 years ago

cd: debuging like hell

File size: 9.3 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 "state.h"
21#include "debug.h"
22#include "light.h"
23#include "util/loading/resource_manager.h"
24#include "camera.h"
25#include "util/preferences.h"
26#include "globals.h"
27
28int verbose;
29
30void Framework::init(void)
31{
32  GraphicsEngine::getInstance()->initFromPreferences();
33
34  LightManager::getInstance();
35
36  std::string dataPath = //Preferences::getInstance()->getString(CONFIG_SECTION_DATA, CONFIG_NAME_DATADIR, "");
37      "/home/boenzlip/orxonox/data/trunk/";
38  printf("%s\n", dataPath.c_str());
39  if (!dataPath.empty())
40  {
41    if (!ResourceManager::getInstance()->setDataDir(dataPath))
42    {
43      PRINTF(1)("Data Could not be located\n");
44      exit(-1);
45    }
46  }
47
48  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
49  {
50    PRINTF(1)("The DataDirectory %s could not be verified\n" \
51        "  Please Change in File %s Section %s Entry %s to a suitable value\n",
52    ResourceManager::getInstance()->getDataDir().c_str(),
53    DEFAULT_CONFIG_FILE,
54    CONFIG_SECTION_DATA,
55    CONFIG_NAME_DATADIR);
56    exit(-1);
57  }
58}
59
60
61void* Framework::mainLoop(void* tmp)
62{
63  Framework* framework = Framework::getInstance();
64
65  // initialize Timing
66  framework->cycle = 0;
67  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
68    framework->frameTimes[i] = 100;
69  framework->dtS = 0.0f;
70  framework->lastFrame = SDL_GetTicks ();
71
72
73  while(!framework->isFinished)
74    {
75#ifdef GUI_MODULE
76      while(gtk_events_pending())
77        gtk_main_iteration();
78#endif
79      // keyhandler returns false if sdl gets quit by some event
80      framework->eventHandler();
81
82      // tick the scene
83      float dt = framework->tick();
84
85      PNode::getNullParent()->updateNode(dt);
86
87      // Draw the scene
88      framework->draw(dt);
89
90    }
91}
92
93bool Framework::draw(float dt)
94{
95  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
96
97  camera->apply();
98  camera->project();
99
100
101  this->moduleDraw();
102
103
104  SDL_GL_SwapBuffers(); // Swap the buffers
105}
106
107
108float Framework::tick()
109{
110      // CALCULATE FRAMERATE
111  Uint32 frameTimesIndex;
112  Uint32 getTicks;
113  Uint32 i;
114
115  frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
116  getTicks = SDL_GetTicks();
117  this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
118  this->lastFrame = getTicks;
119  ++this->cycle;
120  this->dtS = 0;
121  for (i = 0; i < TICK_SMOOTH_VALUE; i++)
122    this->dtS += this->frameTimes[i];
123  this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
124
125  this->camera->tick(dtS);
126
127  this->moduleTick(dtS);
128
129  return dtS;
130}
131
132
133bool Framework::eventHandler()
134{
135  // This is the main loop for the entire program and it will run until done==TRUE
136  {
137    // And poll for events
138    SDL_Event event;
139    while(SDL_PollEvent(&event))
140    {
141      moduleEventHandler(&event);
142
143      switch (event.type) {
144      case SDL_MOUSEMOTION:
145        {
146          Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor();
147          Vector up = Vector(0, 1, 0);
148          up = camera->getAbsDir().apply(up);
149          Vector h = up.cross(view);
150          Vector v = h.cross(view);
151          h.normalize();
152          v.normalize();
153          float distance = view.len();
154
155          Vector newCameraPos = camera->getAbsCoor();
156          Vector newTargetPos = camera->getTarget()->getAbsCoor();
157          int changed = 0;
158
159          if (mouseDown[1])
160            {
161              newCameraPos = camera->getRelCoor()+ (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance;
162              changed += 1;
163            }
164          if (mouseDown[3])
165            {
166              newTargetPos = camera->getTarget()->getRelCoor() + (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance;
167              changed += 2;
168            }
169
170          Vector newView = newTargetPos - newCameraPos;
171
172          if (changed == 1)
173            camera->setRelCoor(newCameraPos + newView * (1- distance/newView.len()));
174          else if (changed == 2)
175            camera->getTarget()->setRelCoor(newTargetPos - newView * (1-distance/newView.len()));
176          else if (changed == 3)
177            {
178              camera->setRelCoor(newCameraPos);
179              camera->getTarget()->setRelCoor(newTargetPos);
180            }
181
182        }
183        break;
184      case SDL_MOUSEBUTTONDOWN:
185        switch (event.button.button)
186          {
187          case 4:
188            PRINTF(4)("MouseWheel up\n");
189            camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
190            break;
191          case 5:
192            PRINTF(4)("MouseWheel down\n");
193            camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
194            break;
195          case 1:
196          case 2:
197          case 3:
198            mouseDown[event.button.button] = true;
199            break;
200          }
201
202        break;
203      case SDL_MOUSEBUTTONUP:
204        switch (event.button.button)
205          {
206          case 1:
207          case 2:
208          case 3:
209            mouseDown[event.button.button] = false;
210            break;
211          }
212        break;
213      case SDL_VIDEORESIZE:
214        GraphicsEngine::getInstance()->resolutionChanged(event.resize);
215        break;
216      case SDL_KEYDOWN:
217        switch (event.key.keysym.sym)
218          {
219          case SDLK_q:
220          case SDLK_ESCAPE:
221#ifdef GUI_MODULE
222            quitGui(NULL, NULL);
223#else
224            this->quit();
225#endif
226            break;
227          case SDLK_a:
228            camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
229            break;
230          case SDLK_z:
231            camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1);
232            break;
233          case SDLK_r:
234            camera->setAbsCoor(Vector(10, 10, 10));
235            camera->getTarget()->setAbsCoor(Vector());
236            break;
237          case SDLK_h:
238            this->printHelp();
239            break;
240          case SDLK_c:
241            for (int i = 0; i < 3; i++)
242              {
243                backgroundColor[i] += .1;
244                if (backgroundColor[i] > 1.0)
245                  backgroundColor[i] = 1.0;
246                GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
247              }
248            break;
249          case SDLK_x:
250            for (int i = 0; i < 3; i++)
251              {
252                backgroundColor[i] -= .1;
253                if (backgroundColor[i] < 0.0)
254                  backgroundColor[i] = 0.0;
255                GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
256              }
257            break;
258          }
259        break;
260
261        // If a quit event was recieved
262      case SDL_QUIT:
263        // then we're done and we'll end this program
264#ifdef GUI_MODULE
265            quitGui(NULL, NULL);
266#else
267            this->quit();
268#endif
269        break;
270      default:
271        break;
272      }
273
274    }
275
276    // Get the state of the keyboard keys
277    keys = SDL_GetKeyState(NULL);
278
279    // and check if ESCAPE has been pressed. If so then quit
280    if(keys[SDLK_ESCAPE]) return false;
281  }
282  return true;
283}
284
285void Framework::quit(void)
286{
287  this->isFinished = true;
288}
289
290Framework* Framework::singletonRef = NULL;
291
292Framework::Framework()
293{
294  this->init();
295
296  camera = new Camera();
297  State::setCamera(camera, camera->getTarget());
298  camera->setAbsCoor(Vector(10, 10, 10));
299
300  this->isFinished = false;
301
302  this->lastFrame = 0;
303
304
305  // Build the font from a TGA image font.tga in the data directory
306  // Hide the mouse cursor
307  SDL_ShowCursor(2);
308
309  for (int i = 0; i < MOUSE_BUTTON_COUNT; i++)
310    mouseDown[i] = false;
311  for (int i = 0; i < 4; i++)
312    backgroundColor[i] = 0;
313}
314
315Framework::~Framework()
316{
317  delete GraphicsEngine::getInstance();
318
319}
320
321
322
323void Framework::printHelp(void) const
324{
325  PRINT(0)(" Help for the frameWork\n");
326  PRINT(0)("========================\n");
327  PRINT(0)("h - print this Help\n");
328  PRINT(0)("a - zoom in\n");
329  PRINT(0)("z - zoom out\n");
330  PRINT(0)("r - reset camera position\n");
331  PRINT(0)("x - background color darker\n");
332  PRINT(0)("c - background color brighter\n");
333
334
335  PRINT(0)("\n");
336  PRINT(0)("mouse wheel - zoom\n");
337  PRINT(0)("mouse left button - rotate the camera around its target\n");
338  PRINT(0)("mouse right button - rotate the camera's target around the camera\n");
339  PRINT(0)("mouse left-and-right button - move the camera and the target\n");
340
341  this->moduleHelp();
342
343}
344
345#ifdef GUI_MODULE
346int quitGui(GtkWidget* widget, void* data)
347{
348#ifdef HAVE_GTK2
349  while(gtk_events_pending()) gtk_main_iteration();
350  Framework::getInstance()->quit();
351#endif /* HAVE_GTK2 */
352}
353#endif
354
355int main(int argc, char *argv[])
356{
357  verbose = 3;
358
359  Framework* framework = Framework::getInstance();
360
361  framework->moduleInit(argc, argv);
362#ifdef GUI_MODULE
363  framework->moduleInitGui(argc, argv);
364#endif
365  framework->mainLoop(NULL);
366
367  delete framework;
368  // Kill the GL & SDL screens
369  // And quit
370  return 0;
371}
Note: See TracBrowser for help on using the repository browser.