Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

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