Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: less output, and shell also outputs to shell if debug-mode ≥3

File size: 12.1 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{
[5024]170  SDL_WM_SetIcon(SDL_LoadBMP(icon), NULL);
171
[4619]172  SDL_WM_SetCaption (windowName, icon);
173}
174
175
176/**
[4836]177 *  Sets the GL-attributes
[3621]178*/
[4746]179int GraphicsEngine::setGLattribs()
[3617]180{
181  // Set video mode
182  // TO DO: parse arguments for settings
183  //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
184  //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
185  //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
186  //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
187
[4597]188
189  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
190  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
191  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);
[3617]192  SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
193  SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
194  SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
195  SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
196}
197
[3621]198/**
[4836]199 *  sets the Resolution of the Screen to display the Graphics to.
200 * @param width The width of the window
201 * @param height The height of the window
202 * @param bpp bits per pixel
[3621]203*/
[3619]204int GraphicsEngine::setResolution(int width, int height, int bpp)
[3610]205{
[3611]206  this->resolutionX = width;
207  this->resolutionY = height;
208  this->bitsPerPixel = bpp;
[4739]209
[4769]210  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
[3611]211    {
212      PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
213      SDL_Quit();
214      //    return -1;
215    }
[4135]216}
[3610]217
[4458]218/**
[4836]219 *  sets Fullscreen mode
220 * @param fullscreen true if fullscreen, false if windowed
[4458]221*/
[4135]222void GraphicsEngine::setFullscreen(bool fullscreen)
223{
[4768]224  if (fullscreen)
225    fullscreenFlag = SDL_FULLSCREEN;
226  else
227    fullscreenFlag = 0;
[4135]228  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
[3543]229}
[3617]230
[4458]231/**
[4836]232 *  sets the background color
233 * @param red the red part of the background
234 * @param blue the blue part of the background
235 * @param green the green part of the background
236 * @param alpha the alpha part of the background
[4458]237*/
[4374]238void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
239{
240  glClearColor(red, green, blue, alpha);
241}
242
243
[3621]244/**
[4836]245 *  Signalhandler, for when the resolution has changed
246 * @param resizeInfo SDL information about the size of the new screen size
[3621]247*/
[4782]248int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
[3619]249{
[4782]250  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
[3619]251}
[3617]252
[3622]253/**
[4833]254 * if Textures should be enabled
[3622]255*/
256bool GraphicsEngine::texturesEnabled = true;
[3617]257
258
259
[3790]260/**
[4833]261 *
262 * @param show if The mouse-cursor should be visible
263 */
264void GraphicsEngine::showMouse(bool show)
265{
266  if (show)
267    SDL_ShowCursor(SDL_ENABLE);
268  else
269    SDL_ShowCursor(SDL_DISABLE);
270}
271
272/**
273 *
274 * @returns The Visinility of the mouse-cursor (true if visible, false if it is invisible)
275 */
276bool GraphicsEngine::isMouseVisible()
277{
278  if (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE)
279    return true;
280  else
281    return false;
282}
283
284/**
285 *
286 * @param steal If the Winodow-Managers Events should be stolen to this app
287 * (steals the mouse, and all WM-clicks)
288 *
289 * This only happens, if the HARD-Debug-level is set to 0,1,2, because otherwise a Segfault could
290 * result in the loss of System-controll
291 */
292void GraphicsEngine::stealWMEvents(bool steal)
293{
294#if DEBUG < 3
295   if (steal)
296     SDL_WM_GrabInput(SDL_GRAB_ON);
[4864]297   else
298     SDL_WM_GrabInput(SDL_GRAB_OFF);
[4833]299#endif
300}
301
302/**
303 *
304 * @returns true if Events are stolen from the WM, false if not.
305 */
306bool GraphicsEngine::isStealingEvents()
307{
308   if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
309     return true;
310   else
311     return false;
312};
313
314
315/**
[4836]316 *  entering 2D Mode
[4597]317
[3790]318   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
319*/
[4746]320void GraphicsEngine::enter2DMode()
[3790]321{
[4955]322  //GraphicsEngine::storeMatrices();
[3790]323  SDL_Surface *screen = SDL_GetVideoSurface();
[4597]324
[3790]325  /* Note, there may be other things you need to change,
326     depending on how you have your OpenGL state set up.
327  */
328  glPushAttrib(GL_ENABLE_BIT);
329  glDisable(GL_DEPTH_TEST);
330  glDisable(GL_CULL_FACE);
331  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
[3617]332
[3790]333  glViewport(0, 0, screen->w, screen->h);
[4597]334
[3790]335  glMatrixMode(GL_PROJECTION);
336  glPushMatrix();
337  glLoadIdentity();
[4597]338
[3790]339  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
[4597]340
[3790]341  glMatrixMode(GL_MODELVIEW);
342  glPushMatrix();
343  glLoadIdentity();
[4597]344
[3790]345  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
346}
[3617]347
[3790]348/**
[4836]349 *  leaves the 2DMode again also \see Font::enter2DMode()
[3790]350*/
[4746]351void GraphicsEngine::leave2DMode()
[3790]352{
[4834]353  glMatrixMode(GL_PROJECTION);
[3790]354  glPopMatrix();
[4597]355
[4834]356  glMatrixMode(GL_MODELVIEW);
[3790]357  glPopMatrix();
[4597]358
[3790]359  glPopAttrib();
360}
[3622]361
[3844]362/**
[4836]363 *  stores the GL_matrices
[3844]364*/
[4746]365void GraphicsEngine::storeMatrices()
[3844]366{
367  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
368  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
369  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
370}
[3622]371
[3844]372//! the stored ModelView Matrix.
373GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
374//! the stored Projection Matrix
375GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
376//! The ViewPort
377GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
[3790]378
379
[3844]380
[3617]381/**
[4836]382 *  outputs all the Fullscreen modes.
[3617]383*/
[4746]384void GraphicsEngine::listModes()
[3617]385{
386  /* Get available fullscreen/hardware modes */
387  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
[4597]388
[3617]389  /* Check is there are any modes available */
390  if(this->videoModes == (SDL_Rect **)0){
391    PRINTF(1)("No modes available!\n");
392    exit(-1);
393  }
[4597]394
[3617]395  /* Check if our resolution is restricted */
396  if(this->videoModes == (SDL_Rect **)-1){
[4135]397    PRINTF(2)("All resolutions available.\n");
[3617]398  }
399  else{
400    /* Print valid modes */
401    PRINT(0)("Available Resoulution Modes are\n");
402    for(int i = 0; this->videoModes[i]; ++i)
[4135]403      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
[3617]404  }
[4245]405}
406
[4458]407/**
[5084]408 * updates everything that is to be updated in the GraphicsEngine
409 */
410void GraphicsEngine::update(float dt)
411{
[5089]412  NullElement2D::getInstance()->update2D(dt);
[5084]413}
414
415
416/**
[4836]417 *  ticks the Text
418 * @param dt the time passed
[4458]419*/
[4849]420  void GraphicsEngine::tick(float dt)
[4245]421{
[4458]422  if( unlikely(this->bDisplayFPS))
[4849]423  {
424    this->currentFPS = 1.0/dt;
425    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
426    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
[4597]427
[4536]428#ifndef NO_TEXT
[4458]429      char tmpChar1[20];
430      sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
431      this->geTextCFPS->setText(tmpChar1);
432      char tmpChar2[20];
433      sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
434      this->geTextMaxFPS->setText(tmpChar2);
435      char tmpChar3[20];
436      sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
437      this->geTextMinFPS->setText(tmpChar3);
[4536]438#endif /* NO_TEXT */
[4849]439
440
441  }
442  Render2D::getInstance()->tick(dt);
[4245]443}
[4597]444
[4849]445
446void GraphicsEngine::draw() const
447{
[4955]448  GraphicsEngine::storeMatrices();
[4862]449  Render2D::getInstance()->draw(E2D_ALL_LAYERS);
[4850]450  LightManager::getInstance()->draw();
[4849]451}
452
[4458]453/**
[4836]454 *  displays the Frames per second
455 * @param display if the text should be displayed
[4458]456
[4836]457   @todo this is dangerous
[4458]458*/
[4245]459void GraphicsEngine::displayFPS(bool display)
460{
[4458]461  if( display)
462    {
[4536]463#ifndef NO_TEXT
[5079]464if (this->geTextCFPS == NULL)
465{
466  this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0);
467  this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
[5089]468  this->geTextCFPS->setAbsCoor2D(5, 5);
[5079]469}
470if (this->geTextMaxFPS == NULL)
471{
[4681]472      this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0);
[4458]473      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
[5089]474      this->geTextMaxFPS->setAbsCoor2D(5, 35);
[5079]475}
476if (this->geTextMinFPS == NULL)
477{
[5078]478      this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0);
[4458]479      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
[5089]480      this->geTextMinFPS->setAbsCoor2D(5, 65);
[5079]481}
[4536]482#endif /* NO_TEXT */
[4458]483    }
484  this->bDisplayFPS = display;
[3617]485}
[4245]486
[4597]487
[4817]488/**
489  \brief processes the events for orxonox main class
[4836]490* @param the event to handle
[4817]491 */
492void GraphicsEngine::process(const Event &event)
493{
494  switch (event.type)
495  {
496    case EV_VIDEO_RESIZE:
497      this->resolutionChanged(event.resize);
498      break;
499  }
500
501}
502
Note: See TracBrowser for help on using the repository browser.