Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor gui-fixes…

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