Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4903 was 4903, checked in by patrick, 19 years ago

orxonox/trunk: wanted to test the terrain quadtree in the subproject but the subproject do not work anymore because of the fast development speed of orxonox :)

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