Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: nicer quit-modi
TextEngine is now deleted by GraphicsEngine
trying to fix errors in the Element2D deletion

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