Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: all subprojects compile and work again

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