Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Shaders now work :)

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