Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/md2_loader/src/lib/graphics/graphics_engine.cc @ 4072

Last change on this file since 4072 was 4072, checked in by patrick, 19 years ago

orxonox/branches/md2_loader: implemented some data formats used by the md2 loader, altered debug levels, and commented out all the debug() functions. This branche won't run on you computer unless you have the fonts and models installed i use temporary for testing purposes

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