Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: removing some initialisation-stuff… i hope this works

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