Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cd_merge/src/lib/graphics/graphics_engine.cc @ 6782

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

orxonox/trunk: merged the network-branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6500:HEAD
minor conflicts in texture and one Makefile resolved to the trunk

also made a small patch to texture, so it Modulates with GL_REPEAT

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