Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: graphicsEngine and TextEngine documented

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