Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Element2D added → will be moved to lib/graphics afterwards
ProtoClass update
EventListeners do not have to be unsubscribed externally, but still one listener won't unsubscribe

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