Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merge the ObjectManager to the trunk
merged with command:
svn merge -r6082:HEAD objectmanager/ ../trunk/

conflicts resolution was easy this time :)
but specially merged the world to network_world

File size: 16.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 "world_entity.h"
23
24#include "render_2d.h"
25#include "text_engine.h"
26#include "light.h"
27#include "shader.h"
28#include "debug.h"
29
30#include "parser/ini_parser/ini_parser.h"
31#include "substring.h"
32#include "text.h"
33
34#include "globals.h"
35#include "texture.h"
36
37#ifdef __WIN32__
38 #include "class_list.h"
39 #include "list.h"
40 #include "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
302  if (this->screen != NULL)
303    SDL_FreeSurface(screen);
304  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
305    {
306      PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
307      //    SDL_Quit();
308      //    return -1;
309    }
310    glMatrixMode(GL_PROJECTION_MATRIX);
311    glLoadIdentity();
312    glViewport(0, 0, width, height);                     // Reset The Current Viewport
313
314#ifdef __WIN32__
315    // REBUILDING TEXTURES (ON WINDOWS CONTEXT SWITCH)
316    const std::list<BaseObject*>* texList = ClassList::getList(CL_TEXTURE);
317    if (texList != NULL)
318    {
319      std::list<BaseObject*>::const_iterator reTex;
320      for (reTex = texList->begin(); reTex != texList->end(); reTex++)
321        dynamic_cast<Texture*>(*reTex)->rebuild();
322    }
323    // REBUILDING MODELS
324    const std::list<BaseObject*>* modelList = ClassList::getList(CL_MODEL);
325    if (texList != NULL)
326    {
327      std::list<BaseObject*>::const_iterator reModel;
328      for (reModel = modelList->begin(); reModel != modelList->end(); reModel++)
329        dynamic_cast<Model*>(*reModel)->rebuild();
330    }
331#endif /* __WIN32__ */
332}
333
334/**
335 *  sets Fullscreen mode
336 * @param fullscreen true if fullscreen, false if windowed
337*/
338void GraphicsEngine::setFullscreen(bool fullscreen)
339{
340  if (fullscreen)
341    this->fullscreenFlag = SDL_FULLSCREEN;
342  else
343    this->fullscreenFlag = 0;
344  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
345}
346
347/**
348 *  sets the background color
349 * @param red the red part of the background
350 * @param blue the blue part of the background
351 * @param green the green part of the background
352 * @param alpha the alpha part of the background
353 */
354void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
355{
356  glClearColor(red, green, blue, alpha);
357}
358
359/**
360 *  Signalhandler, for when the resolution has changed
361 * @param resizeInfo SDL information about the size of the new screen size
362 */
363int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
364{
365  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
366}
367
368/**
369 *
370 * @param show if The mouse-cursor should be visible
371 */
372void GraphicsEngine::showMouse(bool show)
373{
374  if (show)
375    SDL_ShowCursor(SDL_ENABLE);
376  else
377    SDL_ShowCursor(SDL_DISABLE);
378}
379
380/**
381 *
382 * @returns The Visinility of the mouse-cursor (true if visible, false if it is invisible)
383 */
384bool GraphicsEngine::isMouseVisible()
385{
386  if (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE)
387    return true;
388  else
389    return false;
390}
391
392/**
393 *
394 * @param steal If the Winodow-Managers Events should be stolen to this app
395 * (steals the mouse, and all WM-clicks)
396 *
397 * This only happens, if the HARD-Debug-level is set to 0,1,2, because otherwise a Segfault could
398 * result in the loss of System-controll
399 */
400void GraphicsEngine::stealWMEvents(bool steal)
401{
402#if DEBUG < 3
403   if (steal)
404     SDL_WM_GrabInput(SDL_GRAB_ON);
405   else
406     SDL_WM_GrabInput(SDL_GRAB_OFF);
407#endif
408}
409
410/**
411 *
412 * @returns true if Events are stolen from the WM, false if not.
413 */
414bool GraphicsEngine::isStealingEvents()
415{
416   if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
417     return true;
418   else
419     return false;
420};
421
422/**
423 *  entering 2D Mode
424
425   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
426*/
427void GraphicsEngine::enter2DMode()
428{
429  //GraphicsEngine::storeMatrices();
430  SDL_Surface *screen = SDL_GetVideoSurface();
431
432  /* Note, there may be other things you need to change,
433     depending on how you have your OpenGL state set up.
434  */
435  glPushAttrib(GL_ENABLE_BIT);
436  glDisable(GL_DEPTH_TEST);
437  glDisable(GL_CULL_FACE);
438  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
439
440  glViewport(0, 0, screen->w, screen->h);
441
442  glMatrixMode(GL_PROJECTION);
443  glPushMatrix();
444  glLoadIdentity();
445
446  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
447
448  glMatrixMode(GL_MODELVIEW);
449  glPushMatrix();
450  glLoadIdentity();
451
452  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
453}
454
455/**
456 *  leaves the 2DMode again also @see Font::enter2DMode()
457 */
458void GraphicsEngine::leave2DMode()
459{
460  glMatrixMode(GL_PROJECTION);
461  glPopMatrix();
462
463  glMatrixMode(GL_MODELVIEW);
464  glPopMatrix();
465
466  glPopAttrib();
467}
468
469/**
470 *  stores the GL_matrices
471 */
472void GraphicsEngine::storeMatrices()
473{
474  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
475  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
476  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
477}
478
479//! the stored ModelView Matrix.
480GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
481//! the stored Projection Matrix
482GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
483//! The ViewPort
484GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
485
486
487
488/**
489 *  outputs all the Fullscreen modes.
490 */
491void GraphicsEngine::listModes()
492{
493  /* Get available fullscreen/hardware modes */
494  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
495
496  /* Check is there are any modes available */
497  if(this->videoModes == (SDL_Rect **)0){
498    PRINTF(1)("No modes available!\n");
499    exit(-1);
500  }
501
502  /* Check if our resolution is restricted */
503  if(this->videoModes == (SDL_Rect **)-1){
504    PRINTF(2)("All resolutions available.\n");
505  }
506  else{
507    /* Print valid modes */
508    PRINT(0)("Available Resoulution Modes are\n");
509    for(int i = 0; this->videoModes[i]; ++i)
510      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
511  }
512}
513
514/**
515 * checks wether a certain extension is availiable
516 * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3)
517 * @return true if it is, false otherwise
518 */
519bool GraphicsEngine::hwSupportsEXT(const char* extension)
520{
521  if (this->hwExtensions != NULL)
522    for (unsigned int i = 0; i < this->hwExtensions->getCount(); i++)
523      if (!strcmp(extension, this->hwExtensions->getString(i)))
524        return true;
525  return false;
526}
527
528/**
529 * updates everything that is to be updated in the GraphicsEngine
530 */
531void GraphicsEngine::update(float dt)
532{
533  Render2D::getInstance()->update(dt);
534}
535
536
537/**
538 *  ticks the Text
539 * @param dt the time passed
540 */
541void GraphicsEngine::tick(float dt)
542{
543  if( unlikely(this->bDisplayFPS))
544  {
545    this->currentFPS = 1.0/dt;
546    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
547    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
548
549#ifndef NO_TEXT
550      char tmpChar1[20];
551      sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
552      this->geTextCFPS->setText(tmpChar1);
553      char tmpChar2[20];
554      sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
555      this->geTextMaxFPS->setText(tmpChar2);
556      char tmpChar3[20];
557      sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
558      this->geTextMinFPS->setText(tmpChar3);
559#endif /* NO_TEXT */
560
561
562  }
563  Render2D::getInstance()->tick(dt);
564}
565
566
567void GraphicsEngine::draw() const
568{
569  LightManager::getInstance()->draw();
570  GraphicsEngine::storeMatrices();
571  Shader::suspendShader();
572
573  Render2D::getInstance()->draw(E2D_LAYER_ALL);
574  Shader::restoreShader();
575}
576
577void GraphicsEngine::draw(const std::list<WorldEntity*>& drawList ) const
578{
579  std::list<WorldEntity*>::const_iterator entity;
580  for (entity = drawList.begin(); entity != drawList.end(); entity++)
581    if ((*entity)->isVisible()) 
582      (*entity)->draw(); 
583}
584
585
586/**
587 * displays the Frames per second
588 * @param display if the text should be displayed
589*/
590void GraphicsEngine::displayFPS(bool display)
591{
592#ifndef NO_TEXT
593  if( display )
594{
595  if (this->geTextCFPS == NULL)
596  {
597    this->geTextCFPS = new Text("fonts/arial_black.ttf", 15);
598    this->geTextCFPS->setName("curFPS");
599    this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
600    this->geTextCFPS->setAbsCoor2D(5, 0);
601  }
602  if (this->geTextMaxFPS == NULL)
603  {
604    this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15);
605    this->geTextMaxFPS->setName("MaxFPS");
606    this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
607    this->geTextMaxFPS->setAbsCoor2D(5, 20);
608  }
609  if (this->geTextMinFPS == NULL)
610  {
611    this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15);
612    this->geTextMinFPS->setName("MinFPS");
613    this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
614    this->geTextMinFPS->setAbsCoor2D(5, 40);
615  }
616}
617else
618{
619  delete this->geTextCFPS;
620  this->geTextCFPS = NULL;
621  delete this->geTextMaxFPS;
622  this->geTextMaxFPS = NULL;
623  delete this->geTextMinFPS;
624  this->geTextMinFPS = NULL;
625}
626  this->bDisplayFPS = display;
627#else
628  this->bDisplayFPS = false;
629#endif /* NO_TEXT */
630}
631
632
633/**
634  processes the events for the GraphicsEngine class
635* @param the event to handle
636 */
637void GraphicsEngine::process(const Event &event)
638{
639  switch (event.type)
640  {
641    case EV_VIDEO_RESIZE:
642      this->resolutionChanged(event.resize);
643      break;
644  }
645
646}
Note: See TracBrowser for help on using the repository browser.