Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: GraphicsEngine now parses the INI-file

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