Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: checking for HW-extensions

File size: 14.2 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
227void GraphicsEngine::grabHardwareSettings()
228{
229  const char* renderer = (const char*) glGetString(GL_RENDERER);
230  const char* vendor   = (const char*) glGetString(GL_VENDOR);
231  const char* version  = (const char*) glGetString(GL_VERSION);
232  const char* extensions = (const char*) glGetString(GL_EXTENSIONS);
233
234//  printf("%s %s %s\n %s", renderer, vendor, version, extensions);
235
236  if (this->hwRenderer == NULL && renderer != NULL)
237  {
238    this->hwRenderer = new char[strlen(renderer)+1];
239    strcpy(this->hwRenderer, renderer);
240  }
241  if (this->hwVendor == NULL && vendor != NULL)
242  {
243    this->hwVendor = new char[strlen(vendor)+1];
244    strcpy(this->hwVendor, vendor);
245  }
246  if (this->hwVersion == NULL && version != NULL)
247  {
248    this->hwVersion = new char[strlen(version)+11];
249    strcpy(this->hwVersion, version);
250  }
251
252  if (this->hwExtensions == NULL && extensions != NULL)
253    this->hwExtensions = new SubString((char*)glGetString(GL_EXTENSIONS), true);
254
255  PRINT(4)("Running on : %s %s %s\n", vendor, renderer, version);
256  PRINT(4)("Extensions:\n");
257  if (this->hwExtensions != NULL)
258    for (unsigned int i = 0; i < this->hwExtensions->getCount(); i++)
259      PRINT(4)("%d: %s\n", i, this->hwExtensions->getString(i));
260}
261
262/**
263 *  sets the Resolution of the Screen to display the Graphics to.
264 * @param width The width of the window
265 * @param height The height of the window
266 * @param bpp bits per pixel
267*/
268int GraphicsEngine::setResolution(int width, int height, int bpp)
269{
270  this->resolutionX = width;
271  this->resolutionY = height;
272  this->bitsPerPixel = bpp;
273
274  if (this->screen != NULL)
275    SDL_FreeSurface(screen);
276  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
277    {
278      PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
279      //    SDL_Quit();
280      //    return -1;
281    }
282    glViewport(0,0,width,height);                                                   // Reset The Current Viewport
283}
284
285/**
286 *  sets Fullscreen mode
287 * @param fullscreen true if fullscreen, false if windowed
288*/
289void GraphicsEngine::setFullscreen(bool fullscreen)
290{
291  if (fullscreen)
292    fullscreenFlag = SDL_FULLSCREEN;
293  else
294    fullscreenFlag = 0;
295  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
296}
297
298/**
299 *  sets the background color
300 * @param red the red part of the background
301 * @param blue the blue part of the background
302 * @param green the green part of the background
303 * @param alpha the alpha part of the background
304*/
305void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
306{
307  glClearColor(red, green, blue, alpha);
308}
309
310
311/**
312 *  Signalhandler, for when the resolution has changed
313 * @param resizeInfo SDL information about the size of the new screen size
314*/
315int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
316{
317  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
318}
319
320/**
321 * if Textures should be enabled
322*/
323bool GraphicsEngine::texturesEnabled = true;
324
325
326
327/**
328 *
329 * @param show if The mouse-cursor should be visible
330 */
331void GraphicsEngine::showMouse(bool show)
332{
333  if (show)
334    SDL_ShowCursor(SDL_ENABLE);
335  else
336    SDL_ShowCursor(SDL_DISABLE);
337}
338
339/**
340 *
341 * @returns The Visinility of the mouse-cursor (true if visible, false if it is invisible)
342 */
343bool GraphicsEngine::isMouseVisible()
344{
345  if (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE)
346    return true;
347  else
348    return false;
349}
350
351/**
352 *
353 * @param steal If the Winodow-Managers Events should be stolen to this app
354 * (steals the mouse, and all WM-clicks)
355 *
356 * This only happens, if the HARD-Debug-level is set to 0,1,2, because otherwise a Segfault could
357 * result in the loss of System-controll
358 */
359void GraphicsEngine::stealWMEvents(bool steal)
360{
361#if DEBUG < 3
362   if (steal)
363     SDL_WM_GrabInput(SDL_GRAB_ON);
364   else
365     SDL_WM_GrabInput(SDL_GRAB_OFF);
366#endif
367}
368
369/**
370 *
371 * @returns true if Events are stolen from the WM, false if not.
372 */
373bool GraphicsEngine::isStealingEvents()
374{
375   if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
376     return true;
377   else
378     return false;
379};
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 * updates everything that is to be updated in the GraphicsEngine
476 */
477void GraphicsEngine::update(float dt)
478{
479  NullElement2D::getInstance()->update2D(dt);
480}
481
482
483/**
484 *  ticks the Text
485 * @param dt the time passed
486*/
487  void GraphicsEngine::tick(float dt)
488{
489  if( unlikely(this->bDisplayFPS))
490  {
491    this->currentFPS = 1.0/dt;
492    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
493    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
494
495#ifndef NO_TEXT
496      char tmpChar1[20];
497      sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
498      this->geTextCFPS->setText(tmpChar1);
499      char tmpChar2[20];
500      sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
501      this->geTextMaxFPS->setText(tmpChar2);
502      char tmpChar3[20];
503      sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
504      this->geTextMinFPS->setText(tmpChar3);
505#endif /* NO_TEXT */
506
507
508  }
509  Render2D::getInstance()->tick(dt);
510}
511
512
513void GraphicsEngine::draw() const
514{
515  GraphicsEngine::storeMatrices();
516  Render2D::getInstance()->draw(E2D_ALL_LAYERS);
517  LightManager::getInstance()->draw();
518}
519
520/**
521 *  displays the Frames per second
522 * @param display if the text should be displayed
523
524   @todo this is dangerous
525*/
526void GraphicsEngine::displayFPS(bool display)
527{
528  if( display)
529    {
530#ifndef NO_TEXT
531if (this->geTextCFPS == NULL)
532{
533  this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
534  this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
535  this->geTextCFPS->setAbsCoor2D(5, 15);
536}
537if (this->geTextMaxFPS == NULL)
538{
539      this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
540      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
541      this->geTextMaxFPS->setAbsCoor2D(5, 40);
542}
543if (this->geTextMinFPS == NULL)
544{
545      this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
546      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
547      this->geTextMinFPS->setAbsCoor2D(5, 65);
548}
549#endif /* NO_TEXT */
550    }
551  this->bDisplayFPS = display;
552}
553
554
555/**
556  \brief processes the events for orxonox main class
557* @param the event to handle
558 */
559void GraphicsEngine::process(const Event &event)
560{
561  switch (event.type)
562  {
563    case EV_VIDEO_RESIZE:
564      this->resolutionChanged(event.resize);
565      break;
566  }
567
568}
569
Note: See TracBrowser for help on using the repository browser.