Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/lib/graphics/graphics_engine.cc @ 6474

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

orxonox/trunk: the Hud now scales the Widgets itself

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