Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed a bug in the deletion Process of NullElement2D. deleted list before entities that were stored in the lists… stupid me…

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