Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7256 was 7256, checked in by bensch, 18 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
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  const char* dataPath;
37  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_DATA, CONFIG_NAME_DATADIR, ""))!= NULL)
38  {
39    if (!ResourceManager::getInstance()->setDataDir(dataPath))
40    {
41      PRINTF(1)("Data Could not be located\n");
42      exit(-1);
43    }
44  }
45
46  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
47  {
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",
50    ResourceManager::getInstance()->getDataDir(),
51    DEFAULT_CONFIG_FILE,
52    CONFIG_SECTION_DATA,
53    CONFIG_NAME_DATADIR);
54    exit(-1);
55  }
56}
57
58
59void* Framework::mainLoop(void* tmp)
60{
61  Framework* framework = Framework::getInstance();
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
71  while(!framework->isFinished)
72    {
73#ifdef GUI_MODULE
74      while(gtk_events_pending())
75        gtk_main_iteration();
76#endif
77      // keyhandler returns false if sdl gets quit by some event
78      framework->eventHandler();
79
80      // tick the scene
81      float dt = framework->tick();
82
83      PNode::getNullParent()->updateNode(dt);
84
85      // Draw the scene
86      framework->draw(dt);
87
88    }
89}
90
91bool Framework::draw(float dt)
92{
93  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
94
95  camera->apply();
96  camera->project();
97
98
99  this->moduleDraw();
100
101
102  SDL_GL_SwapBuffers(); // Swap the buffers
103}
104
105
106float Framework::tick()
107{
108      // CALCULATE FRAMERATE
109  Uint32 frameTimesIndex;
110  Uint32 getTicks;
111  Uint32 i;
112
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;
122
123  this->camera->tick(dtS);
124
125  this->moduleTick(dtS);
126
127  return dtS;
128}
129
130
131bool Framework::eventHandler()
132{
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;
137    while(SDL_PollEvent(&event))
138    {
139      moduleEventHandler(&event);
140
141      switch (event.type) {
142      case SDL_MOUSEMOTION:
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();
152
153          Vector newCameraPos = camera->getAbsCoor();
154          Vector newTargetPos = camera->getTarget()->getAbsCoor();
155          int changed = 0;
156
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;
182      case SDL_MOUSEBUTTONDOWN:
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;
201      case SDL_MOUSEBUTTONUP:
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;
211      case SDL_VIDEORESIZE:
212        GraphicsEngine::getInstance()->resolutionChanged(event.resize);
213        break;
214      case SDL_KEYDOWN:
215        switch (event.key.keysym.sym)
216          {
217          case SDLK_q:
218          case SDLK_ESCAPE:
219#ifdef GUI_MODULE
220            quitGui(NULL, NULL);
221#else
222            this->quit();
223#endif
224            break;
225          case SDLK_a:
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;
238          case SDLK_c:
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;
247          case SDLK_x:
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
259        // If a quit event was recieved
260      case SDL_QUIT:
261        // then we're done and we'll end this program
262#ifdef GUI_MODULE
263            quitGui(NULL, NULL);
264#else
265            this->quit();
266#endif
267        break;
268      default:
269        break;
270      }
271
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
278    if(keys[SDLK_ESCAPE]) return false;
279  }
280  return true;
281}
282
283void Framework::quit(void)
284{
285  this->isFinished = true;
286}
287
288Framework* Framework::singletonRef = NULL;
289
290Framework::Framework()
291{
292  this->init();
293
294  camera = new Camera();
295  State::setCamera(camera, camera->getTarget());
296  camera->setAbsCoor(Vector(10, 10, 10));
297
298  this->isFinished = false;
299
300  this->lastFrame = 0;
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
307  for (int i = 0; i < MOUSE_BUTTON_COUNT; i++)
308    mouseDown[i] = false;
309  for (int i = 0; i < 4; i++)
310    backgroundColor[i] = 0;
311}
312
313Framework::~Framework()
314{
315  delete GraphicsEngine::getInstance();
316
317}
318
319
320
321void Framework::printHelp(void) const
322{
323  PRINT(0)(" Help for the frameWork\n");
324  PRINT(0)("========================\n");
325  PRINT(0)("h - print this Help\n");
326  PRINT(0)("a - zoom in\n");
327  PRINT(0)("z - zoom out\n");
328  PRINT(0)("r - reset camera position\n");
329  PRINT(0)("x - background color darker\n");
330  PRINT(0)("c - background color brighter\n");
331
332
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");
338
339  this->moduleHelp();
340
341}
342
343#ifdef GUI_MODULE
344int quitGui(GtkWidget* widget, void* data)
345{
346#ifdef HAVE_GTK2
347  while(gtk_events_pending()) gtk_main_iteration();
348  Framework::getInstance()->quit();
349#endif /* HAVE_GTK2 */
350}
351#endif
352
353int main(int argc, char *argv[])
354{
355  verbose = 3;
356
357  Framework* framework = Framework::getInstance();
358
359  framework->moduleInit(argc, argv);
360#ifdef GUI_MODULE
361  framework->moduleInitGui(argc, argv);
362#endif
363  framework->mainLoop(NULL);
364
365  delete framework;
366  // Kill the GL & SDL screens
367  // And quit
368  return 0;
369}
Note: See TracBrowser for help on using the repository browser.