Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7165 was 7159, checked in by bensch, 18 years ago

better tick in framework

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