Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: saver font: now the implementation of NON-loading font must be done

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