Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: introducing Layers

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 SDL_WM_GrabInput(SDL_GRAB_OFF);
294#endif
295}
296
297/**
298 *
299 * @returns true if Events are stolen from the WM, false if not.
300 */
301bool GraphicsEngine::isStealingEvents()
302{
303   if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
304     return true;
305   else
306     return false;
307};
308
309
310/**
311 *  entering 2D Mode
312
313   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
314*/
315void GraphicsEngine::enter2DMode()
316{
317  GraphicsEngine::storeMatrices();
318  SDL_Surface *screen = SDL_GetVideoSurface();
319
320  /* Note, there may be other things you need to change,
321     depending on how you have your OpenGL state set up.
322  */
323  glPushAttrib(GL_ENABLE_BIT);
324  glDisable(GL_DEPTH_TEST);
325  glDisable(GL_CULL_FACE);
326  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
327
328  glViewport(0, 0, screen->w, screen->h);
329
330  glMatrixMode(GL_PROJECTION);
331  glPushMatrix();
332  glLoadIdentity();
333
334  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
335
336  glMatrixMode(GL_MODELVIEW);
337  glPushMatrix();
338  glLoadIdentity();
339
340  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
341}
342
343/**
344 *  leaves the 2DMode again also \see Font::enter2DMode()
345*/
346void GraphicsEngine::leave2DMode()
347{
348  glMatrixMode(GL_PROJECTION);
349  glPopMatrix();
350
351  glMatrixMode(GL_MODELVIEW);
352  glPopMatrix();
353
354  glPopAttrib();
355}
356
357/**
358 *  stores the GL_matrices
359*/
360void GraphicsEngine::storeMatrices()
361{
362  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
363  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
364  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
365}
366
367//! the stored ModelView Matrix.
368GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
369//! the stored Projection Matrix
370GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
371//! The ViewPort
372GLint GraphicsEngine::viewPort[4] = {0,0,0,0};
373
374
375
376/**
377 *  outputs all the Fullscreen modes.
378*/
379void GraphicsEngine::listModes()
380{
381  /* Get available fullscreen/hardware modes */
382  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
383
384  /* Check is there are any modes available */
385  if(this->videoModes == (SDL_Rect **)0){
386    PRINTF(1)("No modes available!\n");
387    exit(-1);
388  }
389
390  /* Check if our resolution is restricted */
391  if(this->videoModes == (SDL_Rect **)-1){
392    PRINTF(2)("All resolutions available.\n");
393  }
394  else{
395    /* Print valid modes */
396    PRINT(0)("Available Resoulution Modes are\n");
397    for(int i = 0; this->videoModes[i]; ++i)
398      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
399  }
400}
401
402/**
403 *  ticks the Text
404 * @param dt the time passed
405*/
406  void GraphicsEngine::tick(float dt)
407{
408  if( unlikely(this->bDisplayFPS))
409  {
410    this->currentFPS = 1.0/dt;
411    if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
412    if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
413
414#ifndef NO_TEXT
415      char tmpChar1[20];
416      sprintf(tmpChar1, "Current:  %4.0f", this->currentFPS);
417      this->geTextCFPS->setText(tmpChar1);
418      char tmpChar2[20];
419      sprintf(tmpChar2, "Max:    %4.0f", this->maxFPS);
420      this->geTextMaxFPS->setText(tmpChar2);
421      char tmpChar3[20];
422      sprintf(tmpChar3, "Min:    %4.0f", this->minFPS);
423      this->geTextMinFPS->setText(tmpChar3);
424#endif /* NO_TEXT */
425
426
427  }
428  Render2D::getInstance()->tick(dt);
429}
430
431
432void GraphicsEngine::draw() const
433{
434  Render2D::getInstance()->draw(E2D_ALL_LAYERS);
435  LightManager::getInstance()->draw();
436}
437
438/**
439 *  displays the Frames per second
440 * @param display if the text should be displayed
441
442   @todo this is dangerous
443*/
444void GraphicsEngine::displayFPS(bool display)
445{
446  if( display)
447    {
448#ifndef NO_TEXT
449      this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0);
450      this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
451      this->geTextCFPS->setPosition2D(5, 5);
452      this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0);
453      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
454      this->geTextMaxFPS->setPosition2D(5, 35);
455      this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 35, TEXT_DYNAMIC, 0, 255, 0);
456      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
457      this->geTextMinFPS->setPosition2D(5, 65);
458#endif /* NO_TEXT */
459    }
460  this->bDisplayFPS = display;
461}
462
463
464/**
465  \brief processes the events for orxonox main class
466* @param the event to handle
467 */
468void GraphicsEngine::process(const Event &event)
469{
470  switch (event.type)
471  {
472    case EV_VIDEO_RESIZE:
473      this->resolutionChanged(event.resize);
474      break;
475  }
476
477}
478
Note: See TracBrowser for help on using the repository browser.