Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: file-parsing works

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