Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merging minor nice fixes from laptop

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