Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5790 was 5790, checked in by bensch, 18 years ago

orxonox/trunk: rescale of the screen-size should reload Models too on windows

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