Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the preferences back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/preferences . -r7233:HEAD
no conflicts… nice work

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