Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Shaders now work :)

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