Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: structure of the Shader-lib

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