Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: code-design

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