Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6302 was 6074, checked in by bensch, 18 years ago

orxonox/trunk: taken out NullParent.
THE TRUNK IS NOT RUNNING FOR THE TIME BEING. DO NOT BE ALARMED, THIS IS TEMPORARY

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