Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: using glew, and Shaders are now read

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