Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/graphics_engine.cc @ 5217

Last change on this file since 5217 was 5216, checked in by bensch, 20 years ago

orxonox/trunk: more fixes due to valgrind

File size: 12.2 KB
RevLine 
[4597]1/*
[1853]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.
[1855]10
11   ### File Specific:
[3610]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3610]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]17
[3610]18#include "graphics_engine.h"
[4094]19#include "resource_manager.h"
[4817]20#include "event_handler.h"
[1853]21
[4849]22#include "render_2d.h"
[4850]23#include "light.h"
[3611]24#include "debug.h"
[4245]25#include "text_engine.h"
[3611]26
[4770]27#include "ini_parser.h"
28#include "substring.h"
29
[1856]30using namespace std;
[1853]31
[1856]32
[3245]33/**
[4836]34 *  standard constructor
35   @todo this constructor is not jet implemented - do it
[3245]36*/
[4597]37GraphicsEngine::GraphicsEngine ()
[3365]38{
[4597]39  this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine");
40  this->setName("GraphicsEngine");
[4784]41
42  this->isInit = false;
43
[4245]44  this->bDisplayFPS = false;
45  this->minFPS = 9999;
46  this->maxFPS = 0;
[4135]47
[5079]48  this->geTextCFPS = NULL;
49  this->geTextMaxFPS = NULL;
50  this->geTextMinFPS = NULL;
51
[4768]52  this->fullscreenFlag = 0;
[3611]53}
[3610]54
[3621]55/**
[4836]56 *  The Pointer to this GraphicsEngine
[3621]57*/
[3611]58GraphicsEngine* GraphicsEngine::singletonRef = NULL;
[3610]59
[3621]60/**
[4836]61 *  destructs the graphicsEngine.
[3611]62*/
[4597]63GraphicsEngine::~GraphicsEngine ()
[3611]64{
65  // delete what has to be deleted here
[5079]66  delete this->geTextCFPS;
67  delete this->geTextMaxFPS;
68  delete this->geTextMinFPS;
[4849]69
[5079]70
[4849]71  delete Render2D::getInstance();
72  GraphicsEngine::singletonRef = NULL;
[3611]73}
74
[4830]75/**
76 * initializes the GraphicsEngine with default settings.
77 */
[4784]78int GraphicsEngine::init()
79{
[4830]80  if (this->isInit)
81    return -1;
82  this->initVideo(640, 480, 16);
[4784]83}
84
[3611]85/**
[4830]86 * loads the GraphicsEngine's settings from a given ini-file and section
87 * @param iniParser the iniParser to load from
88 * @param section the Section in the ini-file to load from
89 * @returns nothing usefull
90 */
91int GraphicsEngine::initFromIniFile(IniParser* iniParser)
92{
93  // looking if we are in fullscreen-mode
94  const char* fullscreen = iniParser->getVar(CONFIG_NAME_FULLSCREEN, CONFIG_SECTION_VIDEO, "0");
95  if (strchr(fullscreen, '1'))
96    this->fullscreenFlag = SDL_FULLSCREEN;
97
98
99
100  // looking if we are in fullscreen-mode
101  const char* textures = iniParser->getVar(CONFIG_NAME_TEXTURES, CONFIG_SECTION_VIDEO_ADVANCED, "0");
102  if (strchr(textures, '1'))
103    this->texturesEnabled = true;
104  else
105    this->texturesEnabled = false;
106
107  // searching for a usefull resolution
[4835]108  SubString resolution(iniParser->getVar(CONFIG_NAME_RESOLUTION, CONFIG_SECTION_VIDEO, "640x480"), 'x');
[4833]109  //resolution.debug();
110
[4835]111  this->initVideo(atoi(resolution.getString(0)), atoi(resolution.getString(1)), 16);
[4830]112}
113
114
115
116/**
[4836]117 *  initializes the Video for openGL.
[3611]118
119   This has to be done only once when starting orxonox.
120*/
[4784]121int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp)
[3611]122{
[4784]123  if (this->isInit)
124    return -1;
[5024]125  //   initialize SDL_VIDEO
[3610]126  if (SDL_Init(SDL_INIT_VIDEO) == -1)
[5024]127  {
128    PRINTF(1)("could not initialize SDL Video\n");
[3610]129      //      return -1;
[5024]130  }
[3617]131  // initialize SDL_GL-settings
132  this->setGLattribs();
[3610]133
[3617]134  // setting the Video Flags.
[3619]135  this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF;
[3610]136
137  /* query SDL for information about our video hardware */
138  const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
139  if( videoInfo == NULL)
140    {
[4597]141      PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError());
[3610]142      SDL_Quit ();
143    }
144  if( videoInfo->hw_available)
[3611]145    this->videoFlags |= SDL_HWSURFACE;
[4597]146  else
[3611]147    this->videoFlags |= SDL_SWSURFACE;
[3610]148  /*
[3619]149  if(VideoInfo -> blit_hw)
[3610]150    VideoFlags |= SDL_HWACCEL;
151  */
[4739]152    // setting up the Resolution
[4784]153  this->setResolution(resX, resY, bbp);
[3611]154
[3621]155  // Enable default GL stuff
156  glEnable(GL_DEPTH_TEST);
[4784]157
[4849]158  Render2D::getInstance();
[4833]159
[4784]160  this->isInit = true;
[3365]161}
[1853]162
[4770]163/**
[4619]164 * sets the Window Captions and the Name of the icon.
165 * @param windowName The name of the Window
166 * @param icon The name of the Icon on the Disc
167 */
168void GraphicsEngine::setWindowName(const char* windowName, const char* icon)
169{
[5216]170  SDL_Surface* iconSurf = SDL_LoadBMP(icon);
[5024]171
[5216]172  SDL_WM_SetIcon(iconSurf, NULL);
[4619]173  SDL_WM_SetCaption (windowName, icon);
[5216]174
175  delete iconSurf;
[4619]176}
177
178
179/**
[4836]180 *  Sets the GL-attributes
[3621]181*/
[4746]182int GraphicsEngine::setGLattribs()
[3617]183{
184  // Set video mode
185  // TO DO: parse arguments for settings
186  //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
187  //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
188  //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
189  //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
190
[4597]191
192  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
193  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
194  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);
[3617]195  SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
196  SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
197  SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
198  SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
199}
200
[3621]201/**
[4836]202 *  sets the Resolution of the Screen to display the Graphics to.
203 * @param width The width of the window
204 * @param height The height of the window
205 * @param bpp bits per pixel
[3621]206*/
[3619]207int GraphicsEngine::setResolution(int width, int height, int bpp)
[3610]208{
[3611]209  this->resolutionX = width;
210  this->resolutionY = height;
211  this->bitsPerPixel = bpp;
[4739]212
[4769]213  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
[3611]214    {
215      PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
[5216]216      //    SDL_Quit();
[3611]217      //    return -1;
218    }
[4135]219}
[3610]220
[4458]221/**
[4836]222 *  sets Fullscreen mode
223 * @param fullscreen true if fullscreen, false if windowed
[4458]224*/
[4135]225void GraphicsEngine::setFullscreen(bool fullscreen)
226{
[4768]227  if (fullscreen)
228    fullscreenFlag = SDL_FULLSCREEN;
229  else
230    fullscreenFlag = 0;
[4135]231  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
[3543]232}
[3617]233
[4458]234/**
[4836]235 *  sets the background color
236 * @param red the red part of the background
237 * @param blue the blue part of the background
238 * @param green the green part of the background
239 * @param alpha the alpha part of the background
[4458]240*/
[4374]241void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
242{
243  glClearColor(red, green, blue, alpha);
244}
245
246
[3621]247/**
[4836]248 *  Signalhandler, for when the resolution has changed
249 * @param resizeInfo SDL information about the size of the new screen size
[3621]250*/
[4782]251int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
[3619]252{
[4782]253  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
[3619]254}
[3617]255
[3622]256/**
[4833]257 * if Textures should be enabled
[3622]258*/
259bool GraphicsEngine::texturesEnabled = true;
[3617]260
261
262
[3790]263/**
[4833]264 *
265 * @param show if The mouse-cursor should be visible
266 */
267void GraphicsEngine::showMouse(bool show)
268{
269  if (show)
270    SDL_ShowCursor(SDL_ENABLE);
271  else
272    SDL_ShowCursor(SDL_DISABLE);
273}
274
275/**
276 *
277 * @returns The Visinility of the mouse-cursor (true if visible, false if it is invisible)
278 */
279bool GraphicsEngine::isMouseVisible()
280{
281  if (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE)
282    return true;
283  else
284    return false;
285}
286
287/**
288 *
289 * @param steal If the Winodow-Managers Events should be stolen to this app
290 * (steals the mouse, and all WM-clicks)
291 *
292 * This only happens, if the HARD-Debug-level is set to 0,1,2, because otherwise a Segfault could
293 * result in the loss of System-controll
294 */
295void GraphicsEngine::stealWMEvents(bool steal)
296{
297#if DEBUG < 3
298   if (steal)
299     SDL_WM_GrabInput(SDL_GRAB_ON);
[4864]300   else
301     SDL_WM_GrabInput(SDL_GRAB_OFF);
[4833]302#endif
303}
304
305/**
306 *
307 * @returns true if Events are stolen from the WM, false if not.
308 */
309bool GraphicsEngine::isStealingEvents()
310{
311   if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
312     return true;
313   else
314     return false;
315};
316
317
318/**
[4836]319 *  entering 2D Mode
[4597]320
[3790]321   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
322*/
[4746]323void GraphicsEngine::enter2DMode()
[3790]324{
[4955]325  //GraphicsEngine::storeMatrices();
[3790]326  SDL_Surface *screen = SDL_GetVideoSurface();
[4597]327
[3790]328  /* Note, there may be other things you need to change,
329     depending on how you have your OpenGL state set up.
330  */
331  glPushAttrib(GL_ENABLE_BIT);
332  glDisable(GL_DEPTH_TEST);
333  glDisable(GL_CULL_FACE);
334  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
[3617]335
[3790]336  glViewport(0, 0, screen->w, screen->h);
[4597]337
[3790]338  glMatrixMode(GL_PROJECTION);
339  glPushMatrix();
340  glLoadIdentity();
[4597]341
[3790]342  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
[4597]343
[3790]344  glMatrixMode(GL_MODELVIEW);
345  glPushMatrix();
346  glLoadIdentity();
[4597]347
[3790]348  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
349}
[3617]350
[3790]351/**
[4836]352 *  leaves the 2DMode again also \see Font::enter2DMode()
[3790]353*/
[4746]354void GraphicsEngine::leave2DMode()
[3790]355{
[4834]356  glMatrixMode(GL_PROJECTION);
[3790]357  glPopMatrix();
[4597]358
[4834]359  glMatrixMode(GL_MODELVIEW);
[3790]360  glPopMatrix();
[4597]361
[3790]362  glPopAttrib();
363}
[3622]364
[3844]365/**
[4836]366 *  stores the GL_matrices
[3844]367*/
[4746]368void GraphicsEngine::storeMatrices()
[3844]369{
370  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
371  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
372  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
373}
[3622]374
[3844]375//! the stored ModelView Matrix.
376GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
377//! the stored Projection Matrix
378GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
379//! The ViewPort
380GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
[3790]381
382
[3844]383
[3617]384/**
[4836]385 *  outputs all the Fullscreen modes.
[3617]386*/
[4746]387void GraphicsEngine::listModes()
[3617]388{
389  /* Get available fullscreen/hardware modes */
390  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
[4597]391
[3617]392  /* Check is there are any modes available */
393  if(this->videoModes == (SDL_Rect **)0){
394    PRINTF(1)("No modes available!\n");
395    exit(-1);
396  }
[4597]397
[3617]398  /* Check if our resolution is restricted */
399  if(this->videoModes == (SDL_Rect **)-1){
[4135]400    PRINTF(2)("All resolutions available.\n");
[3617]401  }
402  else{
403    /* Print valid modes */
404    PRINT(0)("Available Resoulution Modes are\n");
405    for(int i = 0; this->videoModes[i]; ++i)
[4135]406      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
[3617]407  }
[4245]408}
409
[4458]410/**
[5084]411 * updates everything that is to be updated in the GraphicsEngine
412 */
413void GraphicsEngine::update(float dt)
414{
[5089]415  NullElement2D::getInstance()->update2D(dt);
[5084]416}
417
418
419/**
[4836]420 *  ticks the Text
421 * @param dt the time passed
[4458]422*/
[4849]423  void GraphicsEngine::tick(float dt)
[4245]424{
[4458]425  if( unlikely(this->bDisplayFPS))
[4849]426  {
427    this->currentFPS = 1.0/dt;
428    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
429    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
[4597]430
[4536]431#ifndef NO_TEXT
[4458]432      char tmpChar1[20];
433      sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
434      this->geTextCFPS->setText(tmpChar1);
435      char tmpChar2[20];
436      sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
437      this->geTextMaxFPS->setText(tmpChar2);
438      char tmpChar3[20];
439      sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
440      this->geTextMinFPS->setText(tmpChar3);
[4536]441#endif /* NO_TEXT */
[4849]442
443
444  }
445  Render2D::getInstance()->tick(dt);
[4245]446}
[4597]447
[4849]448
449void GraphicsEngine::draw() const
450{
[4955]451  GraphicsEngine::storeMatrices();
[4862]452  Render2D::getInstance()->draw(E2D_ALL_LAYERS);
[4850]453  LightManager::getInstance()->draw();
[4849]454}
455
[4458]456/**
[4836]457 *  displays the Frames per second
458 * @param display if the text should be displayed
[4458]459
[4836]460   @todo this is dangerous
[4458]461*/
[4245]462void GraphicsEngine::displayFPS(bool display)
463{
[4458]464  if( display)
465    {
[4536]466#ifndef NO_TEXT
[5079]467if (this->geTextCFPS == NULL)
468{
[5122]469  this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
[5079]470  this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
[5124]471  this->geTextCFPS->setAbsCoor2D(5, 15);
[5079]472}
473if (this->geTextMaxFPS == NULL)
474{
[5122]475      this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
[4458]476      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
[5124]477      this->geTextMaxFPS->setAbsCoor2D(5, 40);
[5079]478}
479if (this->geTextMinFPS == NULL)
480{
[5122]481      this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
[4458]482      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
[5089]483      this->geTextMinFPS->setAbsCoor2D(5, 65);
[5079]484}
[4536]485#endif /* NO_TEXT */
[4458]486    }
487  this->bDisplayFPS = display;
[3617]488}
[4245]489
[4597]490
[4817]491/**
492  \brief processes the events for orxonox main class
[4836]493* @param the event to handle
[4817]494 */
495void GraphicsEngine::process(const Event &event)
496{
497  switch (event.type)
498  {
499    case EV_VIDEO_RESIZE:
500      this->resolutionChanged(event.resize);
501      break;
502  }
503
504}
505
Note: See TracBrowser for help on using the repository browser.