Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4918 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
RevLine 
[4554]1/*
[3140]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
[2931]16#include "framework.h"
[3427]17
18
[4294]19#include "p_node.h"
20#include "null_parent.h"
21#include "state.h"
[4272]22#include "debug.h"
[4300]23#include "light.h"
[4295]24#include "resource_manager.h"
[4297]25#include "camera.h"
[4650]26#include "ini_parser.h"
[3657]27
[4316]28
[3398]29int verbose;
[4293]30
[4650]31void Framework::init(void)
32{
33  // create parser
34  IniParser parser (DEFAULT_CONFIG_FILE);
[4785]35
36  GraphicsEngine::getInstance()->initFromIniFile(&parser);
37
38  LightManager::getInstance();
39
[4650]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
[4330]76void* Framework::mainLoop(void* tmp)
[2748]77{
[4330]78  Framework* framework = Framework::getInstance();
[4331]79  while(!framework->isFinished)
[4293]80    {
[4343]81#ifdef GUI_MODULE
[4358]82      while(gtk_events_pending())
[4554]83        gtk_main_iteration();
[4343]84#endif
[4297]85      // keyhandler returns false if sdl gets quit by some event
[4334]86      framework->eventHandler();
[2939]87
[4297]88      // tick the scene
[4330]89      float dt = framework->tick();
[4297]90
[4333]91      NullParent::getInstance()->update(dt);
92
[4293]93      // Draw the scene
[4330]94      framework->draw(dt);
[4331]95
[4293]96    }
97}
98
[4297]99bool Framework::draw(float dt)
[4293]100{
[2748]101  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
102  glLoadIdentity(); // Reset the view
[4554]103
[4349]104  this->moduleDraw();
[4554]105
[4297]106  camera->apply();
[4554]107
[2748]108  SDL_GL_SwapBuffers(); // Swap the buffers
109}
[4300]110
111
[4297]112float Framework::tick()
[2748]113{
[4293]114  currFrame = SDL_GetTicks();
[4300]115  float dt = (float)(currFrame - lastFrame) / 1000.0;
116  lastFrame = currFrame;
[3398]117
[4333]118  this->moduleTick(dt);
[2963]119
[4294]120  return dt;
[4293]121}
[2952]122
[2931]123
[4334]124bool Framework::eventHandler()
[4293]125{
[2748]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;
[4305]130    while(SDL_PollEvent(&event))
[4303]131    {
[4334]132      moduleEventHandler(&event);
133
[2748]134      switch (event.type) {
[2931]135      case SDL_MOUSEMOTION:
[4554]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();
[4306]145
[4554]146          Vector newCameraPos = camera->getAbsCoor();
147          Vector newTargetPos = camera->getTarget()->getAbsCoor();
148          int changed = 0;
[4358]149
[4554]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;
[2931]175      case SDL_MOUSEBUTTONDOWN:
[4554]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;
[2952]194      case SDL_MOUSEBUTTONUP:
[4554]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;
[4302]204      case SDL_VIDEORESIZE:
[4785]205        GraphicsEngine::getInstance()->resolutionChanged(event.resize);
[4554]206        break;
[3211]207      case SDL_KEYDOWN:
[4554]208        switch (event.key.keysym.sym)
209          {
210          case SDLK_q:
211          case SDLK_ESCAPE:
[4343]212#ifdef GUI_MODULE
[4554]213            quitGui(NULL, NULL);
[4343]214#else
[4554]215            this->quit();
[4343]216#endif
[4554]217            break;
[4724]218          case SDLK_a:
[4554]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;
[4647]231          case SDLK_c:
[4554]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;
[4647]240          case SDLK_x:
[4554]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
[2748]252        // If a quit event was recieved
[2863]253      case SDL_QUIT:
[4554]254        // then we're done and we'll end this program
[4343]255#ifdef GUI_MODULE
[4554]256            quitGui(NULL, NULL);
[4343]257#else
[4554]258            this->quit();
[4343]259#endif
[4554]260        break;
[2863]261      default:
[4554]262        break;
[2748]263      }
[2952]264
[2748]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
[4293]271    if(keys[SDLK_ESCAPE]) return false;
[2748]272  }
[4293]273  return true;
274}
[2748]275
[4331]276void Framework::quit(void)
277{
278  this->isFinished = true;
279}
280
[4317]281Framework* Framework::singletonRef = NULL;
[4293]282
[4300]283Framework::Framework()
[4293]284{
[4650]285  this->init();
286
[4785]287  camera = new Camera();
[4903]288  State::setCamera(camera, camera->getTarget());
[4785]289  camera->setAbsCoor(Vector(10, 10, 10));
290
[4331]291  this->isFinished = false;
292
[4304]293  this->lastFrame = 0;
[4293]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
[4374]300  for (int i = 0; i < MOUSE_BUTTON_COUNT; i++)
[4306]301    mouseDown[i] = false;
[4374]302  for (int i = 0; i < 4; i++)
303    backgroundColor[i] = 0;
[4297]304}
305
306Framework::~Framework()
307{
308  delete GraphicsEngine::getInstance();
309
310}
311
[4307]312
313
314void Framework::printHelp(void) const
315{
[4309]316  PRINT(0)(" Help for the frameWork\n");
317  PRINT(0)("========================\n");
[4374]318  PRINT(0)("h - print this Help\n");
[4359]319  PRINT(0)("a - zoom in\n");
320  PRINT(0)("z - zoom out\n");
[4360]321  PRINT(0)("r - reset camera position\n");
[4647]322  PRINT(0)("x - background color darker\n");
323  PRINT(0)("c - background color brighter\n");
[4307]324
[4374]325
[4360]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");
[4554]331
[4333]332  this->moduleHelp();
[4307]333
334}
335
[4343]336#ifdef GUI_MODULE
[4330]337int quitGui(GtkWidget* widget, void* data)
338{
339#ifdef HAVE_GTK2
340  while(gtk_events_pending()) gtk_main_iteration();
[4331]341  Framework::getInstance()->quit();
[4330]342#endif /* HAVE_GTK2 */
343}
[4343]344#endif
[4330]345
[4297]346int main(int argc, char *argv[])
347{
348  verbose = 3;
[4554]349
[4317]350  Framework* framework = Framework::getInstance();
[4316]351
[4343]352  framework->moduleInit(argc, argv);
353#ifdef GUI_MODULE
354  framework->moduleInitGui(argc, argv);
355#endif
[4331]356  framework->mainLoop(NULL);
[4297]357
358  delete framework;
[2748]359  // Kill the GL & SDL screens
360  // And quit
361  return 0;
362}
Note: See TracBrowser for help on using the repository browser.