Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6324 was 6074, checked in by bensch, 19 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
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 "state.h"
[4272]21#include "debug.h"
[4300]22#include "light.h"
[4295]23#include "resource_manager.h"
[4297]24#include "camera.h"
[5944]25#include "parser/ini_parser/ini_parser.h"
[5485]26#include "globals.h"
[3657]27
[3398]28int verbose;
[4293]29
[4650]30void Framework::init(void)
31{
32  // create parser
[4785]33
[5030]34  char* configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
[4785]35
[5030]36  IniParser iniParser (configFileName);
37  delete configFileName;
38
39  GraphicsEngine::getInstance()->initFromIniFile(&iniParser);
40
[4785]41  LightManager::getInstance();
42
[5030]43  const char* dataPath;
44  if ((dataPath = iniParser.getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
[4650]45  {
[5030]46    if (!ResourceManager::getInstance()->setDataDir(dataPath))
[4650]47    {
[5030]48      PRINTF(1)("Data Could not be located\n");
49      exit(-1);
[4650]50    }
51  }
52
[5484]53  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4650]54  {
[5030]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",
[4650]57    ResourceManager::getInstance()->getDataDir(),
58    DEFAULT_CONFIG_FILE,
59    CONFIG_SECTION_DATA,
60    CONFIG_NAME_DATADIR);
61    exit(-1);
62  }
63}
64
65
[4330]66void* Framework::mainLoop(void* tmp)
[2748]67{
[4330]68  Framework* framework = Framework::getInstance();
[4331]69  while(!framework->isFinished)
[4293]70    {
[4343]71#ifdef GUI_MODULE
[4358]72      while(gtk_events_pending())
[4554]73        gtk_main_iteration();
[4343]74#endif
[4297]75      // keyhandler returns false if sdl gets quit by some event
[4334]76      framework->eventHandler();
[2939]77
[4297]78      // tick the scene
[4330]79      float dt = framework->tick();
[4297]80
[6074]81      PNode::getNullParent()->updateNode(dt);
[4333]82
[4293]83      // Draw the scene
[4330]84      framework->draw(dt);
[4331]85
[4293]86    }
87}
88
[4297]89bool Framework::draw(float dt)
[4293]90{
[2748]91  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
92  glLoadIdentity(); // Reset the view
[4554]93
[4349]94  this->moduleDraw();
[4554]95
[4297]96  camera->apply();
[4554]97
[2748]98  SDL_GL_SwapBuffers(); // Swap the buffers
99}
[4300]100
101
[4297]102float Framework::tick()
[2748]103{
[4293]104  currFrame = SDL_GetTicks();
[4300]105  float dt = (float)(currFrame - lastFrame) / 1000.0;
106  lastFrame = currFrame;
[3398]107
[4333]108  this->moduleTick(dt);
[2963]109
[4294]110  return dt;
[4293]111}
[2952]112
[2931]113
[4334]114bool Framework::eventHandler()
[4293]115{
[2748]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;
[4305]120    while(SDL_PollEvent(&event))
[4303]121    {
[4334]122      moduleEventHandler(&event);
123
[2748]124      switch (event.type) {
[2931]125      case SDL_MOUSEMOTION:
[4554]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();
[4306]135
[4554]136          Vector newCameraPos = camera->getAbsCoor();
137          Vector newTargetPos = camera->getTarget()->getAbsCoor();
138          int changed = 0;
[4358]139
[4554]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;
[2931]165      case SDL_MOUSEBUTTONDOWN:
[4554]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;
[2952]184      case SDL_MOUSEBUTTONUP:
[4554]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;
[4302]194      case SDL_VIDEORESIZE:
[4785]195        GraphicsEngine::getInstance()->resolutionChanged(event.resize);
[4554]196        break;
[3211]197      case SDL_KEYDOWN:
[4554]198        switch (event.key.keysym.sym)
199          {
200          case SDLK_q:
201          case SDLK_ESCAPE:
[4343]202#ifdef GUI_MODULE
[4554]203            quitGui(NULL, NULL);
[4343]204#else
[4554]205            this->quit();
[4343]206#endif
[4554]207            break;
[4724]208          case SDLK_a:
[4554]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;
[4647]221          case SDLK_c:
[4554]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;
[4647]230          case SDLK_x:
[4554]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
[2748]242        // If a quit event was recieved
[2863]243      case SDL_QUIT:
[4554]244        // then we're done and we'll end this program
[4343]245#ifdef GUI_MODULE
[4554]246            quitGui(NULL, NULL);
[4343]247#else
[4554]248            this->quit();
[4343]249#endif
[4554]250        break;
[2863]251      default:
[4554]252        break;
[2748]253      }
[2952]254
[2748]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
[4293]261    if(keys[SDLK_ESCAPE]) return false;
[2748]262  }
[4293]263  return true;
264}
[2748]265
[4331]266void Framework::quit(void)
267{
268  this->isFinished = true;
269}
270
[4317]271Framework* Framework::singletonRef = NULL;
[4293]272
[4300]273Framework::Framework()
[4293]274{
[4650]275  this->init();
276
[4785]277  camera = new Camera();
[4903]278  State::setCamera(camera, camera->getTarget());
[4785]279  camera->setAbsCoor(Vector(10, 10, 10));
280
[4331]281  this->isFinished = false;
282
[4304]283  this->lastFrame = 0;
[4293]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
[4374]290  for (int i = 0; i < MOUSE_BUTTON_COUNT; i++)
[4306]291    mouseDown[i] = false;
[4374]292  for (int i = 0; i < 4; i++)
293    backgroundColor[i] = 0;
[4297]294}
295
296Framework::~Framework()
297{
298  delete GraphicsEngine::getInstance();
299
300}
301
[4307]302
303
304void Framework::printHelp(void) const
305{
[4309]306  PRINT(0)(" Help for the frameWork\n");
307  PRINT(0)("========================\n");
[4374]308  PRINT(0)("h - print this Help\n");
[4359]309  PRINT(0)("a - zoom in\n");
310  PRINT(0)("z - zoom out\n");
[4360]311  PRINT(0)("r - reset camera position\n");
[4647]312  PRINT(0)("x - background color darker\n");
313  PRINT(0)("c - background color brighter\n");
[4307]314
[4374]315
[4360]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");
[4554]321
[4333]322  this->moduleHelp();
[4307]323
324}
325
[4343]326#ifdef GUI_MODULE
[4330]327int quitGui(GtkWidget* widget, void* data)
328{
329#ifdef HAVE_GTK2
330  while(gtk_events_pending()) gtk_main_iteration();
[4331]331  Framework::getInstance()->quit();
[4330]332#endif /* HAVE_GTK2 */
333}
[4343]334#endif
[4330]335
[4297]336int main(int argc, char *argv[])
337{
338  verbose = 3;
[4554]339
[4317]340  Framework* framework = Framework::getInstance();
[4316]341
[4343]342  framework->moduleInit(argc, argv);
343#ifdef GUI_MODULE
344  framework->moduleInitGui(argc, argv);
345#endif
[4331]346  framework->mainLoop(NULL);
[4297]347
348  delete framework;
[2748]349  // Kill the GL & SDL screens
350  // And quit
351  return 0;
352}
Note: See TracBrowser for help on using the repository browser.