Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 12.3 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  this->videoFlags = 0;
54  this->screen = NULL;
55}
56
57/**
58 *  The Pointer to this GraphicsEngine
59*/
60GraphicsEngine* GraphicsEngine::singletonRef = NULL;
61
62/**
63 *  destructs the graphicsEngine.
64*/
65GraphicsEngine::~GraphicsEngine ()
66{
67  // delete what has to be deleted here
68  delete this->geTextCFPS;
69  delete this->geTextMaxFPS;
70  delete this->geTextMinFPS;
71
72  delete Render2D::getInstance();
73
74  SDL_QuitSubSystem(SDL_INIT_VIDEO);
75  GraphicsEngine::singletonRef = NULL;
76}
77
78/**
79 * initializes the GraphicsEngine with default settings.
80 */
81int GraphicsEngine::init()
82{
83  if (this->isInit)
84    return -1;
85  this->initVideo(640, 480, 16);
86}
87
88/**
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
111  SubString resolution(iniParser->getVar(CONFIG_NAME_RESOLUTION, CONFIG_SECTION_VIDEO, "640x480"), 'x');
112  //resolution.debug();
113
114  this->initVideo(atoi(resolution.getString(0)), atoi(resolution.getString(1)), 16);
115}
116
117
118
119/**
120 *  initializes the Video for openGL.
121
122   This has to be done only once when starting orxonox.
123*/
124int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp)
125{
126  if (this->isInit)
127    return -1;
128  //   initialize SDL_VIDEO
129  if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
130  {
131    PRINTF(1)("could not initialize SDL Video\n");
132      //      return -1;
133  }
134  // initialize SDL_GL-settings
135  this->setGLattribs();
136
137  // setting the Video Flags.
138  this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF | SDL_GL_DOUBLEBUFFER;
139
140  /* query SDL for information about our video hardware */
141  const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
142  if( videoInfo == NULL)
143    {
144      PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError());
145      SDL_Quit ();
146    }
147  if( videoInfo->hw_available)
148    this->videoFlags |= SDL_HWSURFACE;
149  else
150    this->videoFlags |= SDL_SWSURFACE;
151  /*
152  if(VideoInfo -> blit_hw)
153    VideoFlags |= SDL_HWACCEL;
154  */
155    // setting up the Resolution
156  this->setResolution(resX, resY, bbp);
157
158  // Enable default GL stuff
159  glEnable(GL_DEPTH_TEST);
160
161  Render2D::getInstance();
162
163  this->isInit = true;
164}
165
166/**
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{
173  SDL_Surface* iconSurf = SDL_LoadBMP(icon);
174
175  SDL_WM_SetIcon(iconSurf, NULL);
176  SDL_WM_SetCaption (windowName, icon);
177
178  delete iconSurf;
179}
180
181
182/**
183 *  Sets the GL-attributes
184*/
185int GraphicsEngine::setGLattribs()
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
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);
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
204/**
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
209*/
210int GraphicsEngine::setResolution(int width, int height, int bpp)
211{
212  this->resolutionX = width;
213  this->resolutionY = height;
214  this->bitsPerPixel = bpp;
215
216  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL)
217    {
218      PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
219      //    SDL_Quit();
220      //    return -1;
221    }
222}
223
224/**
225 *  sets Fullscreen mode
226 * @param fullscreen true if fullscreen, false if windowed
227*/
228void GraphicsEngine::setFullscreen(bool fullscreen)
229{
230  if (fullscreen)
231    fullscreenFlag = SDL_FULLSCREEN;
232  else
233    fullscreenFlag = 0;
234  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
235}
236
237/**
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
243*/
244void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
245{
246  glClearColor(red, green, blue, alpha);
247}
248
249
250/**
251 *  Signalhandler, for when the resolution has changed
252 * @param resizeInfo SDL information about the size of the new screen size
253*/
254int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo)
255{
256  this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel);
257}
258
259/**
260 * if Textures should be enabled
261*/
262bool GraphicsEngine::texturesEnabled = true;
263
264
265
266/**
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);
303   else
304     SDL_WM_GrabInput(SDL_GRAB_OFF);
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/**
322 *  entering 2D Mode
323
324   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
325*/
326void GraphicsEngine::enter2DMode()
327{
328  //GraphicsEngine::storeMatrices();
329  SDL_Surface *screen = SDL_GetVideoSurface();
330
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
338
339  glViewport(0, 0, screen->w, screen->h);
340
341  glMatrixMode(GL_PROJECTION);
342  glPushMatrix();
343  glLoadIdentity();
344
345  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
346
347  glMatrixMode(GL_MODELVIEW);
348  glPushMatrix();
349  glLoadIdentity();
350
351  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
352}
353
354/**
355 *  leaves the 2DMode again also \see Font::enter2DMode()
356*/
357void GraphicsEngine::leave2DMode()
358{
359  glMatrixMode(GL_PROJECTION);
360  glPopMatrix();
361
362  glMatrixMode(GL_MODELVIEW);
363  glPopMatrix();
364
365  glPopAttrib();
366}
367
368/**
369 *  stores the GL_matrices
370*/
371void GraphicsEngine::storeMatrices()
372{
373  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
374  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
375  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
376}
377
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};
384
385
386
387/**
388 *  outputs all the Fullscreen modes.
389*/
390void GraphicsEngine::listModes()
391{
392  /* Get available fullscreen/hardware modes */
393  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
394
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  }
400
401  /* Check if our resolution is restricted */
402  if(this->videoModes == (SDL_Rect **)-1){
403    PRINTF(2)("All resolutions available.\n");
404  }
405  else{
406    /* Print valid modes */
407    PRINT(0)("Available Resoulution Modes are\n");
408    for(int i = 0; this->videoModes[i]; ++i)
409      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
410  }
411}
412
413/**
414 * updates everything that is to be updated in the GraphicsEngine
415 */
416void GraphicsEngine::update(float dt)
417{
418  NullElement2D::getInstance()->update2D(dt);
419}
420
421
422/**
423 *  ticks the Text
424 * @param dt the time passed
425*/
426  void GraphicsEngine::tick(float dt)
427{
428  if( unlikely(this->bDisplayFPS))
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;
433
434#ifndef NO_TEXT
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);
444#endif /* NO_TEXT */
445
446
447  }
448  Render2D::getInstance()->tick(dt);
449}
450
451
452void GraphicsEngine::draw() const
453{
454  GraphicsEngine::storeMatrices();
455  Render2D::getInstance()->draw(E2D_ALL_LAYERS);
456  LightManager::getInstance()->draw();
457}
458
459/**
460 *  displays the Frames per second
461 * @param display if the text should be displayed
462
463   @todo this is dangerous
464*/
465void GraphicsEngine::displayFPS(bool display)
466{
467  if( display)
468    {
469#ifndef NO_TEXT
470if (this->geTextCFPS == NULL)
471{
472  this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
473  this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
474  this->geTextCFPS->setAbsCoor2D(5, 15);
475}
476if (this->geTextMaxFPS == NULL)
477{
478      this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
479      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
480      this->geTextMaxFPS->setAbsCoor2D(5, 40);
481}
482if (this->geTextMinFPS == NULL)
483{
484      this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC);
485      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
486      this->geTextMinFPS->setAbsCoor2D(5, 65);
487}
488#endif /* NO_TEXT */
489    }
490  this->bDisplayFPS = display;
491}
492
493
494/**
495  \brief processes the events for orxonox main class
496* @param the event to handle
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.