Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: framework update

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