Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5789 was 5789, checked in by bensch, 18 years ago

orxonox/trunk: windows-compile fix (stl-list)

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