Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more fixes due to valgrind

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