Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now there is the ability to Fly in fullscreen-mode

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