Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

better tick in framework

File size: 9.3 KB
RevLine 
[4554]1/*
[3140]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
[2931]16#include "framework.h"
[3427]17
18
[4294]19#include "p_node.h"
20#include "state.h"
[4272]21#include "debug.h"
[4300]22#include "light.h"
[4295]23#include "resource_manager.h"
[4297]24#include "camera.h"
[5944]25#include "parser/ini_parser/ini_parser.h"
[5485]26#include "globals.h"
[3657]27
[3398]28int verbose;
[4293]29
[4650]30void Framework::init(void)
31{
32  // create parser
[5030]33  char* configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
[4785]34
[5030]35  IniParser iniParser (configFileName);
36  delete configFileName;
37
38  GraphicsEngine::getInstance()->initFromIniFile(&iniParser);
39
[4785]40  LightManager::getInstance();
41
[5030]42  const char* dataPath;
43  if ((dataPath = iniParser.getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
[4650]44  {
[5030]45    if (!ResourceManager::getInstance()->setDataDir(dataPath))
[4650]46    {
[5030]47      PRINTF(1)("Data Could not be located\n");
48      exit(-1);
[4650]49    }
50  }
51
[5484]52  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4650]53  {
[5030]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",
[4650]56    ResourceManager::getInstance()->getDataDir(),
57    DEFAULT_CONFIG_FILE,
58    CONFIG_SECTION_DATA,
59    CONFIG_NAME_DATADIR);
60    exit(-1);
61  }
62}
63
64
[4330]65void* Framework::mainLoop(void* tmp)
[2748]66{
[4330]67  Framework* framework = Framework::getInstance();
[7159]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
[4331]77  while(!framework->isFinished)
[4293]78    {
[4343]79#ifdef GUI_MODULE
[4358]80      while(gtk_events_pending())
[4554]81        gtk_main_iteration();
[4343]82#endif
[4297]83      // keyhandler returns false if sdl gets quit by some event
[4334]84      framework->eventHandler();
[2939]85
[4297]86      // tick the scene
[4330]87      float dt = framework->tick();
[4297]88
[6074]89      PNode::getNullParent()->updateNode(dt);
[4333]90
[4293]91      // Draw the scene
[4330]92      framework->draw(dt);
[4331]93
[4293]94    }
95}
96
[4297]97bool Framework::draw(float dt)
[4293]98{
[2748]99  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
[7158]100
[7157]101  camera->apply();
102  camera->project();
[4554]103
[7158]104
[4349]105  this->moduleDraw();
[4554]106
107
[2748]108  SDL_GL_SwapBuffers(); // Swap the buffers
109}
[4300]110
111
[4297]112float Framework::tick()
[2748]113{
[7159]114      // CALCULATE FRAMERATE
115  Uint32 frameTimesIndex;
116  Uint32 getTicks;
117  Uint32 i;
[3398]118
[7159]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;
[2963]128
[7159]129  this->camera->tick(dtS);
130
131  this->moduleTick(dtS);
132
133  return dtS;
[4293]134}
[2952]135
[2931]136
[4334]137bool Framework::eventHandler()
[4293]138{
[2748]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;
[4305]143    while(SDL_PollEvent(&event))
[4303]144    {
[4334]145      moduleEventHandler(&event);
146
[2748]147      switch (event.type) {
[2931]148      case SDL_MOUSEMOTION:
[4554]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();
[4306]158
[4554]159          Vector newCameraPos = camera->getAbsCoor();
160          Vector newTargetPos = camera->getTarget()->getAbsCoor();
161          int changed = 0;
[4358]162
[4554]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;
[2931]188      case SDL_MOUSEBUTTONDOWN:
[4554]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;
[2952]207      case SDL_MOUSEBUTTONUP:
[4554]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;
[4302]217      case SDL_VIDEORESIZE:
[4785]218        GraphicsEngine::getInstance()->resolutionChanged(event.resize);
[4554]219        break;
[3211]220      case SDL_KEYDOWN:
[4554]221        switch (event.key.keysym.sym)
222          {
223          case SDLK_q:
224          case SDLK_ESCAPE:
[4343]225#ifdef GUI_MODULE
[4554]226            quitGui(NULL, NULL);
[4343]227#else
[4554]228            this->quit();
[4343]229#endif
[4554]230            break;
[4724]231          case SDLK_a:
[4554]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;
[4647]244          case SDLK_c:
[4554]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;
[4647]253          case SDLK_x:
[4554]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
[2748]265        // If a quit event was recieved
[2863]266      case SDL_QUIT:
[4554]267        // then we're done and we'll end this program
[4343]268#ifdef GUI_MODULE
[4554]269            quitGui(NULL, NULL);
[4343]270#else
[4554]271            this->quit();
[4343]272#endif
[4554]273        break;
[2863]274      default:
[4554]275        break;
[2748]276      }
[2952]277
[2748]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
[4293]284    if(keys[SDLK_ESCAPE]) return false;
[2748]285  }
[4293]286  return true;
287}
[2748]288
[4331]289void Framework::quit(void)
290{
291  this->isFinished = true;
292}
293
[4317]294Framework* Framework::singletonRef = NULL;
[4293]295
[4300]296Framework::Framework()
[4293]297{
[4650]298  this->init();
299
[4785]300  camera = new Camera();
[4903]301  State::setCamera(camera, camera->getTarget());
[4785]302  camera->setAbsCoor(Vector(10, 10, 10));
303
[4331]304  this->isFinished = false;
305
[4304]306  this->lastFrame = 0;
[4293]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
[4374]313  for (int i = 0; i < MOUSE_BUTTON_COUNT; i++)
[4306]314    mouseDown[i] = false;
[4374]315  for (int i = 0; i < 4; i++)
316    backgroundColor[i] = 0;
[4297]317}
318
319Framework::~Framework()
320{
321  delete GraphicsEngine::getInstance();
322
323}
324
[4307]325
326
327void Framework::printHelp(void) const
328{
[4309]329  PRINT(0)(" Help for the frameWork\n");
330  PRINT(0)("========================\n");
[4374]331  PRINT(0)("h - print this Help\n");
[4359]332  PRINT(0)("a - zoom in\n");
333  PRINT(0)("z - zoom out\n");
[4360]334  PRINT(0)("r - reset camera position\n");
[4647]335  PRINT(0)("x - background color darker\n");
336  PRINT(0)("c - background color brighter\n");
[4307]337
[4374]338
[4360]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");
[4554]344
[4333]345  this->moduleHelp();
[4307]346
347}
348
[4343]349#ifdef GUI_MODULE
[4330]350int quitGui(GtkWidget* widget, void* data)
351{
352#ifdef HAVE_GTK2
353  while(gtk_events_pending()) gtk_main_iteration();
[4331]354  Framework::getInstance()->quit();
[4330]355#endif /* HAVE_GTK2 */
356}
[4343]357#endif
[4330]358
[4297]359int main(int argc, char *argv[])
360{
361  verbose = 3;
[4554]362
[4317]363  Framework* framework = Framework::getInstance();
[4316]364
[4343]365  framework->moduleInit(argc, argv);
366#ifdef GUI_MODULE
367  framework->moduleInitGui(argc, argv);
368#endif
[4331]369  framework->mainLoop(NULL);
[4297]370
371  delete framework;
[2748]372  // Kill the GL & SDL screens
373  // And quit
374  return 0;
375}
Note: See TracBrowser for help on using the repository browser.