Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setting the resolution works again (workaround. now i fix the subprojects again.)

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