Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: text is now Rendered as expected (in FastMode)

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