Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

importer works again

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