Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: enableTexture now a property of Texture and not GraphicsEngine → Texture is a Leaf now

@patrick: as you said so long ago, this is the better idea

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