Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: fixed most -Wall warnings… but there are still many missing :/

File size: 17.4 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
17
18#include "graphics_engine.h"
19#include "util/loading/resource_manager.h"
20#include "state.h"
21
22#include "world_entity.h"
23
24#include "render_2d.h"
25#include "text_engine.h"
26#include "light.h"
27#include "shader.h"
28#include "debug.h"
29
30#include "util/preferences.h"
31#include "substring.h"
32#include "text.h"
33
34#include "globals.h"
35#include "texture.h"
36
37#include "effects/graphics_effect.h"
38#include "effects/fog_effect.h"
39#include "effects/lense_flare.h"
40
41#include "shell_command.h"
42
43
44#include "parser/tinyxml/tinyxml.h"
45#include "util/loading/load_param.h"
46#include "util/loading/factory.h"
47#include "class_list.h"
48
49#ifdef __WIN32__
50 #include "static_model.h"
51#endif
52using namespace std;
53
54SHELL_COMMAND(wireframe, GraphicsEngine, wireframe);
55
56
57/**
58 * @brief standard constructor
59 */
60GraphicsEngine::GraphicsEngine ()
61{
62  this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine");
63  this->setName("GraphicsEngine");
64
65  this->isInit = false;
66
67  this->bDisplayFPS = false;
68  this->minFPS = 9999;
69  this->maxFPS = 0;
70
71  this->geTextCFPS = NULL;
72  this->geTextMaxFPS = NULL;
73  this->geTextMinFPS = NULL;
74
75  this->fullscreenFlag = 0;
76  this->videoFlags = 0;
77  this->screen = NULL;
78
79  // initialize the Modules
80  TextEngine::getInstance();
81  this->graphicsEffects = NULL;
82
83}
84
85/**
86 * @brief The Pointer to this GraphicsEngine
87 */
88GraphicsEngine* GraphicsEngine::singletonRef = NULL;
89
90/**
91 * @brief destructs the graphicsEngine.
92*/
93GraphicsEngine::~GraphicsEngine ()
94{
95  // delete what has to be deleted here
96  this->displayFPS( false );
97
98  //TextEngine
99  delete TextEngine::getInstance();
100  // render 2D
101  delete Render2D::getInstance();
102
103  SDL_QuitSubSystem(SDL_INIT_VIDEO);
104  //   if (this->screen != NULL)
105  //     SDL_FreeSurface(this->screen);
106
107  GraphicsEngine::singletonRef = NULL;
108}
109
110
111/**
112 * @brief loads the GraphicsEngine Specific Parameters.
113 * @param root: the XML-Element to load the Data From
114 */
115void GraphicsEngine::loadParams(const TiXmlElement* root)
116{
117  LoadParamXML(root, "GraphicsEffect", this, GraphicsEngine, loadGraphicsEffects)
118  .describe("loads a graphics effect");
119}
120
121
122
123
124/**
125 * @param root The XML-element to load GraphicsEffects from
126 */
127void GraphicsEngine::loadGraphicsEffects(const TiXmlElement* root)
128{
129  LOAD_PARAM_START_CYCLE(root, element);
130  {
131    PRINTF(4)("element is: %s\n", element->Value());
132    Factory::fabricate(element);
133  }
134  LOAD_PARAM_END_CYCLE(element);
135}
136
137
138
139/**
140 * @brief initializes the GraphicsEngine with default settings.
141 */
142int GraphicsEngine::init()
143{
144  if (this->isInit)
145    return -1;
146  this->initVideo(640, 480, 16);
147  return 1;
148}
149
150/**
151 * @brief loads the GraphicsEngine's settings from a given ini-file and section
152 * @returns nothing usefull
153 */
154int GraphicsEngine::initFromPreferences()
155{
156  // looking if we are in fullscreen-mode
157  MultiType fullscreen = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_FULLSCREEN, "0");
158
159  if (fullscreen.getBool())
160    this->fullscreenFlag = SDL_FULLSCREEN;
161
162  // looking if we are in fullscreen-mode
163  MultiType textures = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO_ADVANCED, CONFIG_NAME_TEXTURES, "1");
164  Texture::setTextureEnableState(textures.getBool());
165
166  // searching for a usefull resolution
167  SubString resolution(Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_RESOLUTION, "640x480").c_str(), 'x'); ///FIXME
168  //resolution.debug();
169  MultiType x = resolution.getString(0), y = resolution.getString(1);
170  return this->initVideo(x.getInt(), y.getInt(), 16);
171
172  //   GraphicsEffect* fe = new FogEffect(NULL);
173  //   this->loadGraphicsEffect(fe);
174  //   fe->activate();
175  //   PRINTF(0)("--------------------------------------------------------------\n");
176
177  //LenseFlare* ge = new LenseFlare();
178  //this->loadGraphicsEffect(ge);
179
180  //ge->addFlare("pictures/lense_flare/sun.png"); //sun
181  //ge->addFlare("pictures/lense_flare/lens2.png"); //first halo
182  //ge->addFlare("pictures/lense_flare/lens1.png"); //small birst
183  //ge->addFlare("pictures/lense_flare/lens3.png"); //second halo
184  //ge->addFlare("pictures/lense_flare/lens4.png");
185  //ge->addFlare("pictures/lense_flare/lens1.png");
186  //ge->addFlare("pictures/lense_flare/lens3.png");
187
188  //ge->activate();
189}
190
191
192
193/**
194 * @brief initializes the Video for openGL.
195 *
196 * This has to be done only once when starting orxonox.
197 */
198int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp)
199{
200  if (this->isInit)
201    return -1;
202  //   initialize SDL_VIDEO
203  if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
204  {
205    PRINTF(1)("could not initialize SDL Video\n");
206    return -1;
207  }
208  // initialize SDL_GL-settings
209  this->setGLattribs();
210
211  // setting the Video Flags.
212  this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE ;
213
214  /* query SDL for information about our video hardware */
215  const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
216  if( videoInfo == NULL)
217  {
218    PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError());
219    SDL_Quit ();
220  }
221  if( videoInfo->hw_available)
222    this->videoFlags |= SDL_HWSURFACE;
223  else
224    this->videoFlags |= SDL_SWSURFACE;
225  /*
226  if(VideoInfo -> blit_hw)
227    VideoFlags |= SDL_HWACCEL;
228  */
229  // setting up the Resolution
230  this->setResolution(resX, resY, bbp);
231
232  // GRABBING ALL GL-extensions
233  this->grabHardwareSettings();
234
235  // Enable default GL stuff
236  glEnable(GL_DEPTH_TEST);
237
238  Render2D::getInstance();
239
240  this->isInit = true;
241  return 1;
242}
243
244/**
245 * @brief sets the Window Captions and the Name of the icon.
246 * @param windowName The name of the Window
247 * @param icon The name of the Icon on the Disc
248 */
249void GraphicsEngine::setWindowName(const std::string& windowName, const std::string& icon)
250{
251  SDL_Surface* iconSurf = SDL_LoadBMP(icon.c_str());
252  if (iconSurf != NULL)
253  {
254    Uint32 colorkey = SDL_MapRGB(iconSurf->format, 0, 0, 0);
255    SDL_SetColorKey(iconSurf, SDL_SRCCOLORKEY, colorkey);
256    SDL_WM_SetIcon(iconSurf, NULL);
257    SDL_FreeSurface(iconSurf);
258  }
259
260  SDL_WM_SetCaption (windowName.c_str(), icon.c_str());
261}
262
263
264/**
265 * @brief Sets the GL-attributes
266 */
267void GraphicsEngine::setGLattribs()
268{
269  // Set video mode
270  // TO DO: parse arguments for settings
271  //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
272  //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
273  //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
274  //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
275
276
277  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
278  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
279  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);
280  SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
281  SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
282  SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
283  SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
284
285  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);      //Use at least 5 bits of Red
286  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);    //Use at least 5 bits of Green
287  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);     //Use at least 5 bits of Blue
288  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);   //Use at least 16 bits for the depth buffer
289  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);  //Enable double buffering
290
291  glEnable(GL_CULL_FACE);
292  glCullFace(GL_FRONT);
293}
294
295/**
296 * @brief grabs the Hardware Specifics
297 *
298 * checks for all the different HW-types
299 */
300void GraphicsEngine::grabHardwareSettings()
301{
302  const char* renderer = (const char*) glGetString(GL_RENDERER);
303  const char* vendor   = (const char*) glGetString(GL_VENDOR);
304  const char* version  = (const char*) glGetString(GL_VERSION);
305  const char* extensions = (const char*) glGetString(GL_EXTENSIONS);
306
307  //  printf("%s %s %s\n %s", renderer, vendor, version, extensions);
308
309  if (renderer != NULL)
310  {
311    this->hwRenderer == renderer;
312  }
313  if (vendor != NULL)
314  {
315    this->hwVendor == vendor;
316  }
317  if (version != NULL)
318  {
319    this->hwVersion == version;
320  }
321
322  if (extensions != NULL)
323    this->hwExtensions.split(extensions, " \n\t,");
324
325  PRINT(4)("Running on : vendor: %s,  renderer: %s,  version:%s\n", vendor, renderer, version);
326  PRINT(4)("Extensions:\n");
327  for (unsigned int i = 0; i < this->hwExtensions.size(); i++)
328    PRINT(4)("%d: %s\n", i, this->hwExtensions[i].c_str());
329
330
331  // inizializing GLEW
332  GLenum err = glewInit();
333  if (GLEW_OK != err)
334  {
335    /* Problem: glewInit failed, something is seriously wrong. */
336    PRINTF(1)("%s\n", glewGetErrorString(err));
337  }
338  PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
339}
340
341
342/**
343 * @brief sets the Resolution of the Screen to display the Graphics to.
344 * @param width The width of the window
345 * @param height The height of the window
346 * @param bpp bits per pixel
347 */
348int GraphicsEngine::setResolution(int width, int height, int bpp)
349{
350  this->resolutionX = width;
351  this->resolutionY = height;
352  this->bitsPerPixel = bpp;
353  State::setResolution( width, height);
354
355  if (this->screen != NULL)
356    SDL_FreeSurface(screen);
357  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
358  {
359    PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
360    //    SDL_Quit();
361    //    return -1;
362    return -1;
363  }
364  glViewport(0, 0, width, height);                     // Reset The Current Viewport
365
366#ifdef __WIN32__
367  // REBUILDING TEXTURES (ON WINDOWS CONTEXT SWITCH)
368  const std::list<BaseObject*>* texList = ClassList::getList(CL_TEXTURE);
369  if (texList != NULL)
370  {
371    std::list<BaseObject*>::const_iterator reTex;
372    for (reTex = texList->begin(); reTex != texList->end(); reTex++)
373      dynamic_cast<Texture*>(*reTex)->rebuild();
374  }
375  // REBUILDING MODELS
376  const std::list<BaseObject*>* modelList = ClassList::getList(CL_STATIC_MODEL);
377  if (texList != NULL)
378  {
379    std::list<BaseObject*>::const_iterator reModel;
380    for (reModel = modelList->begin(); reModel != modelList->end(); reModel++)
381      dynamic_cast<StaticModel*>(*reModel)->rebuild();
382  }
383#endif /* __WIN32__ */
384  return 1;
385}
386
387/**
388 * @brief sets Fullscreen mode
389 * @param fullscreen true if fullscreen, false if windowed
390*/
391void GraphicsEngine::setFullscreen(bool fullscreen)
392{
393  if (fullscreen)
394    this->fullscreenFlag = SDL_FULLSCREEN;
395  else
396    this->fullscreenFlag = 0;
397  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
398}
399
400/**
401 * @brief sets the background color
402 * @param red the red part of the background
403 * @param blue the blue part of the background
404 * @param green the green part of the background
405 * @param alpha the alpha part of the background
406 */
407void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
408{
409  glClearColor(red, green, blue, alpha);
410}
411
412/**
413 * @brief Signalhandler, for when the resolution has changed
414 * @param resizeInfo SDL information about the size of the new screen size
415 */
416void GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
417{
418  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
419}
420
421/**
422 * @brief entering 2D Mode
423 * this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
424 */
425void GraphicsEngine::enter2DMode()
426{
427  //GraphicsEngine::storeMatrices();
428  SDL_Surface *screen = SDL_GetVideoSurface();
429
430  /* Note, there may be other things you need to change,
431     depending on how you have your OpenGL state set up.
432  */
433  glPushAttrib(GL_ENABLE_BIT);
434  glDisable(GL_DEPTH_TEST);
435  glDisable(GL_CULL_FACE);
436  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
437
438  glMatrixMode(GL_PROJECTION);
439  glPushMatrix();
440  glLoadIdentity();
441  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
442
443  glMatrixMode(GL_MODELVIEW);
444  glPushMatrix();
445  glLoadIdentity();
446}
447
448/**
449 * @brief leaves the 2DMode again also @see Font::enter2DMode()
450 */
451void GraphicsEngine::leave2DMode()
452{
453
454  glMatrixMode(GL_MODELVIEW);
455  glPopMatrix();
456
457  glMatrixMode(GL_PROJECTION);
458  glPopMatrix();
459
460  glPopAttrib();
461}
462
463/**
464 * @brief changes to wireframe-mode.
465 */
466void GraphicsEngine::wireframe()
467{
468  glPolygonMode(GL_FRONT, GL_LINE);
469}
470
471/**
472 * @brief stores the GL_matrices
473 */
474void GraphicsEngine::storeMatrices()
475{
476  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
477  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
478  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
479}
480
481//! the stored ModelView Matrix.
482GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
483//! the stored Projection Matrix
484GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
485//! The ViewPort
486GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
487
488
489
490/**
491 * @brief outputs all the Fullscreen modes.
492 */
493void GraphicsEngine::listModes()
494{
495  /* Get available fullscreen/hardware modes */
496  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
497
498  /* Check is there are any modes available */
499  if(this->videoModes == (SDL_Rect **)0)
500  {
501    PRINTF(1)("No modes available!\n");
502    exit(-1);
503  }
504
505  /* Check if our resolution is restricted */
506  if(this->videoModes == (SDL_Rect **)-1)
507  {
508    PRINTF(2)("All resolutions available.\n");
509  }
510  else
511  {
512    /* Print valid modes */
513    PRINT(0)("Available Resoulution Modes are\n");
514    for(int i = 0; this->videoModes[i]; ++i)
515      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
516  }
517}
518
519/**
520 * @brief checks wether a certain extension is availiable
521 * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3)
522 * @return true if it is, false otherwise
523 */
524bool GraphicsEngine::hwSupportsEXT(const std::string& extension)
525{
526  for (unsigned int i = 0; i < this->hwExtensions.size(); i++)
527    if ( this->hwExtensions.getString(i) == extension)
528      return true;
529  return false;
530}
531
532/**
533 * @brief updates everything that is to be updated in the GraphicsEngine
534 */
535void GraphicsEngine::update(float dt)
536{
537  Render2D::getInstance()->update(dt);
538}
539
540
541/**
542 * @brief ticks the Text
543 * @param dt the time passed
544 */
545void GraphicsEngine::tick(float dt)
546{
547  if( unlikely(this->bDisplayFPS))
548  {
549    this->currentFPS = 1.0/dt;
550    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
551    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
552
553#ifndef NO_TEXT
554    char tmpChar1[20];
555    sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
556    this->geTextCFPS->setText(tmpChar1);
557    char tmpChar2[20];
558    sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
559    this->geTextMaxFPS->setText(tmpChar2);
560    char tmpChar3[20];
561    sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
562    this->geTextMinFPS->setText(tmpChar3);
563#endif /* NO_TEXT */
564
565  }
566
567  Render2D::getInstance()->tick(dt);
568
569  // tick the graphics effects
570  if (this->graphicsEffects != NULL || (this->graphicsEffects = ClassList::getList(CL_GRAPHICS_EFFECT)) != NULL)
571  {
572    std::list<BaseObject*>::const_iterator it;
573    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
574      dynamic_cast<GraphicsEffect*>(*it)->tick(dt);
575  }
576}
577
578/**
579 * @brief draws all Elements that should be displayed on the Background.
580 */
581void GraphicsEngine::drawBackgroundElements() const
582{
583  GraphicsEngine::storeMatrices();
584
585  Render2D::getInstance()->draw(E2D_LAYER_BELOW_ALL, E2D_LAYER_BELOW_ALL);
586}
587
588void GraphicsEngine::draw() const
589{
590  //  LightManager::getInstance()->draw();
591
592  if (this->graphicsEffects != NULL)
593  {
594    //draw the graphics effects
595    list<BaseObject*>::const_iterator it;
596    for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++)
597      dynamic_cast<GraphicsEffect*>(*it)->draw();
598  }
599  GraphicsEngine::storeMatrices();
600  Shader::suspendShader();
601  Render2D::getInstance()->draw(E2D_LAYER_BOTTOM, E2D_LAYER_ABOVE_ALL);
602  Shader::restoreShader();
603}
604
605
606/**
607 * @brief displays the Frames per second
608 * @param display if the text should be displayed
609*/
610void GraphicsEngine::displayFPS(bool display)
611{
612#ifndef NO_TEXT
613  if( display )
614  {
615    if (this->geTextCFPS == NULL)
616    {
617      this->geTextCFPS = new Text("fonts/arial_black.ttf", 15);
618      this->geTextCFPS->setName("curFPS");
619      this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
620      this->geTextCFPS->setAbsCoor2D(5, 0);
621    }
622    if (this->geTextMaxFPS == NULL)
623    {
624      this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15);
625      this->geTextMaxFPS->setName("MaxFPS");
626      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
627      this->geTextMaxFPS->setAbsCoor2D(5, 20);
628    }
629    if (this->geTextMinFPS == NULL)
630    {
631      this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15);
632      this->geTextMinFPS->setName("MinFPS");
633      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
634      this->geTextMinFPS->setAbsCoor2D(5, 40);
635    }
636  }
637  else
638  {
639    delete this->geTextCFPS;
640    this->geTextCFPS = NULL;
641    delete this->geTextMaxFPS;
642    this->geTextMaxFPS = NULL;
643    delete this->geTextMinFPS;
644    this->geTextMinFPS = NULL;
645  }
646  this->bDisplayFPS = display;
647#else
648  this->bDisplayFPS = false;
649#endif /* NO_TEXT */
650}
651
652
653/**
654 * @brief processes the events for the GraphicsEngine class
655 * @param the event to handle
656 */
657void GraphicsEngine::process(const Event &event)
658{
659  switch (event.type)
660  {
661    case EV_VIDEO_RESIZE:
662      this->resolutionChanged(event.resize);
663      break;
664  }
665}
Note: See TracBrowser for help on using the repository browser.