Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: code-renice

File size: 15.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 "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#include "shader.h"
27#include "ini_parser.h"
28#include "substring.h"
29#include "text.h"
30
31using namespace std;
32
33/**
34 *  standard constructor
35 */
36GraphicsEngine::GraphicsEngine ()
37{
38  this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine");
39  this->setName("GraphicsEngine");
40
41  this->isInit = false;
42
43  this->bDisplayFPS = false;
44  this->minFPS = 9999;
45  this->maxFPS = 0;
46
47  this->geTextCFPS = NULL;
48  this->geTextMaxFPS = NULL;
49  this->geTextMinFPS = NULL;
50
51  this->fullscreenFlag = 0;
52  this->videoFlags = 0;
53  this->screen = NULL;
54
55
56  // Hardware
57  this->hwRenderer = NULL;
58  this->hwVendor = NULL;
59  this->hwVersion = NULL;
60  this->hwExtensions = NULL;
61
62  // initialize the TextEngine
63  TextEngine::getInstance();
64}
65
66/**
67 *  The Pointer to this GraphicsEngine
68*/
69GraphicsEngine* GraphicsEngine::singletonRef = NULL;
70
71/**
72 *  destructs the graphicsEngine.
73*/
74GraphicsEngine::~GraphicsEngine ()
75{
76  // delete what has to be deleted here
77  delete this->geTextCFPS;
78  delete this->geTextMaxFPS;
79  delete this->geTextMinFPS;
80
81  delete[] this->hwRenderer;
82  delete[] this->hwVendor;
83  delete[] this->hwVersion;
84  delete this->hwExtensions;
85
86  delete Render2D::getInstance();
87  //TextEngine
88  delete TextEngine::getInstance();
89
90  SDL_QuitSubSystem(SDL_INIT_VIDEO);
91//   if (this->screen != NULL)
92//     SDL_FreeSurface(this->screen);
93
94  GraphicsEngine::singletonRef = NULL;
95}
96
97/**
98 * initializes the GraphicsEngine with default settings.
99 */
100int GraphicsEngine::init()
101{
102  if (this->isInit)
103    return -1;
104  this->initVideo(640, 480, 16);
105}
106
107/**
108 * loads the GraphicsEngine's settings from a given ini-file and section
109 * @param iniParser the iniParser to load from
110 * @param section the Section in the ini-file to load from
111 * @returns nothing usefull
112 */
113int GraphicsEngine::initFromIniFile(IniParser* iniParser)
114{
115  // looking if we are in fullscreen-mode
116  const char* fullscreen = iniParser->getVar(CONFIG_NAME_FULLSCREEN, CONFIG_SECTION_VIDEO, "0");
117  if (strchr(fullscreen, '1') || strcasestr(fullscreen, "true"))
118    this->fullscreenFlag = SDL_FULLSCREEN;
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') || strcasestr(textures, "true"))
123    this->texturesEnabled = true;
124  else
125    this->texturesEnabled = false;
126
127  // searching for a usefull resolution
128  SubString resolution(iniParser->getVar(CONFIG_NAME_RESOLUTION, CONFIG_SECTION_VIDEO, "640x480"), 'x');
129  //resolution.debug();
130
131  this->initVideo(atoi(resolution.getString(0)), atoi(resolution.getString(1)), 16);
132}
133
134
135
136/**
137 *  initializes the Video for openGL.
138 *
139 * This has to be done only once when starting orxonox.
140 */
141int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp)
142{
143  if (this->isInit)
144    return -1;
145  //   initialize SDL_VIDEO
146  if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
147  {
148    PRINTF(1)("could not initialize SDL Video\n");
149      //      return -1;
150  }
151  // initialize SDL_GL-settings
152  this->setGLattribs();
153
154  // setting the Video Flags.
155  this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF | SDL_GL_DOUBLEBUFFER;
156
157  /* query SDL for information about our video hardware */
158  const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
159  if( videoInfo == NULL)
160    {
161      PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError());
162      SDL_Quit ();
163    }
164  if( videoInfo->hw_available)
165    this->videoFlags |= SDL_HWSURFACE;
166  else
167    this->videoFlags |= SDL_SWSURFACE;
168  /*
169  if(VideoInfo -> blit_hw)
170    VideoFlags |= SDL_HWACCEL;
171  */
172    // setting up the Resolution
173  this->setResolution(resX, resY, bbp);
174
175  // GRABBING ALL GL-extensions
176  this->grabHardwareSettings();
177
178  // Enable default GL stuff
179  glEnable(GL_DEPTH_TEST);
180
181  Render2D::getInstance();
182
183  this->isInit = true;
184}
185
186/**
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{
193  SDL_Surface* iconSurf = SDL_LoadBMP(icon);
194  if (iconSurf != NULL)
195  {
196    Uint32 colorkey = SDL_MapRGB(iconSurf->format, 0, 0, 0);
197    SDL_SetColorKey(iconSurf, SDL_SRCCOLORKEY, colorkey);
198    SDL_WM_SetIcon(iconSurf,NULL);
199    SDL_FreeSurface(iconSurf);
200  }
201
202  SDL_WM_SetCaption (windowName, icon);
203
204}
205
206
207/**
208 *  Sets the GL-attributes
209 */
210int GraphicsEngine::setGLattribs()
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
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);
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
229/**
230 * grabs the Hardware Specifics
231 * checks for all the different HW-types
232 */
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));
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
277}
278
279/**
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
284 */
285int GraphicsEngine::setResolution(int width, int height, int bpp)
286{
287  this->resolutionX = width;
288  this->resolutionY = height;
289  this->bitsPerPixel = bpp;
290
291  if (this->screen != NULL)
292    SDL_FreeSurface(screen);
293  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
294    {
295      PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
296      //    SDL_Quit();
297      //    return -1;
298    }
299    glMatrixMode(GL_PROJECTION_MATRIX);
300    glLoadIdentity();
301    glViewport(0,0,width,height);                                                   // Reset The Current Viewport
302}
303
304/**
305 *  sets Fullscreen mode
306 * @param fullscreen true if fullscreen, false if windowed
307*/
308void GraphicsEngine::setFullscreen(bool fullscreen)
309{
310  if (fullscreen)
311    fullscreenFlag = SDL_FULLSCREEN;
312  else
313    fullscreenFlag = 0;
314  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
315}
316
317/**
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
323 */
324void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
325{
326  glClearColor(red, green, blue, alpha);
327}
328
329/**
330 *  Signalhandler, for when the resolution has changed
331 * @param resizeInfo SDL information about the size of the new screen size
332 */
333int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
334{
335  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
336}
337
338/**
339 * if Textures should be enabled
340*/
341bool GraphicsEngine::texturesEnabled = true;
342
343/**
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);
380   else
381     SDL_WM_GrabInput(SDL_GRAB_OFF);
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/**
398 *  entering 2D Mode
399
400   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
401*/
402void GraphicsEngine::enter2DMode()
403{
404  //GraphicsEngine::storeMatrices();
405  SDL_Surface *screen = SDL_GetVideoSurface();
406
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
414
415  glViewport(0, 0, screen->w, screen->h);
416
417  glMatrixMode(GL_PROJECTION);
418  glPushMatrix();
419  glLoadIdentity();
420
421  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
422
423  glMatrixMode(GL_MODELVIEW);
424  glPushMatrix();
425  glLoadIdentity();
426
427  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
428}
429
430/**
431 *  leaves the 2DMode again also @see Font::enter2DMode()
432 */
433void GraphicsEngine::leave2DMode()
434{
435  glMatrixMode(GL_PROJECTION);
436  glPopMatrix();
437
438  glMatrixMode(GL_MODELVIEW);
439  glPopMatrix();
440
441  glPopAttrib();
442}
443
444/**
445 *  stores the GL_matrices
446 */
447void GraphicsEngine::storeMatrices()
448{
449  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
450  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
451  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
452}
453
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};
460
461
462
463/**
464 *  outputs all the Fullscreen modes.
465 */
466void GraphicsEngine::listModes()
467{
468  /* Get available fullscreen/hardware modes */
469  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
470
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  }
476
477  /* Check if our resolution is restricted */
478  if(this->videoModes == (SDL_Rect **)-1){
479    PRINTF(2)("All resolutions available.\n");
480  }
481  else{
482    /* Print valid modes */
483    PRINT(0)("Available Resoulution Modes are\n");
484    for(int i = 0; this->videoModes[i]; ++i)
485      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
486  }
487}
488
489/**
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/**
504 * updates everything that is to be updated in the GraphicsEngine
505 */
506void GraphicsEngine::update(float dt)
507{
508  NullElement2D::getInstance()->update2D(dt);
509}
510
511
512/**
513 *  ticks the Text
514 * @param dt the time passed
515 */
516void GraphicsEngine::tick(float dt)
517{
518  if( unlikely(this->bDisplayFPS))
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;
523
524#ifndef NO_TEXT
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);
534#endif /* NO_TEXT */
535
536
537  }
538  Render2D::getInstance()->tick(dt);
539}
540
541
542void GraphicsEngine::draw() const
543{
544  GraphicsEngine::storeMatrices();
545  Shader::suspendShader();
546  Render2D::getInstance()->draw(E2D_ALL_LAYERS);
547  Shader::restoreShader();
548  LightManager::getInstance()->draw();
549}
550
551/**
552 * displays the Frames per second
553 * @param display if the text should be displayed
554*/
555void GraphicsEngine::displayFPS(bool display)
556{
557#ifndef NO_TEXT
558  if( display )
559{
560  if (this->geTextCFPS == NULL)
561  {
562    this->geTextCFPS = new Text("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
563    this->geTextCFPS->setName("curFPS");
564    this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
565    this->geTextCFPS->setAbsCoor2D(5, 15);
566  }
567  if (this->geTextMaxFPS == NULL)
568  {
569    this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
570    this->geTextMaxFPS->setName("MaxFPS");
571    this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
572    this->geTextMaxFPS->setAbsCoor2D(5, 40);
573  }
574  if (this->geTextMinFPS == NULL)
575  {
576    this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
577    this->geTextMinFPS->setName("MinFPS");
578    this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
579    this->geTextMinFPS->setAbsCoor2D(5, 65);
580  }
581}
582else
583{
584  delete this->geTextCFPS;
585  this->geTextCFPS = NULL;
586  delete this->geTextMaxFPS;
587  this->geTextMaxFPS = NULL;
588  delete this->geTextMinFPS;
589  this->geTextMinFPS = NULL;
590}
591  this->bDisplayFPS = display;
592#else
593  this->bDisplayFPS = false;
594#endif /* NO_TEXT */
595}
596
597
598/**
599  processes the events for the GraphicsEngine class
600* @param the event to handle
601 */
602void GraphicsEngine::process(const Event &event)
603{
604  switch (event.type)
605  {
606    case EV_VIDEO_RESIZE:
607      this->resolutionChanged(event.resize);
608      break;
609  }
610
611}
Note: See TracBrowser for help on using the repository browser.