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