Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ini-parser small cleanup char* → const char*

File size: 9.3 KB
RevLine 
[4597]1/*
[1853]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.
[1855]10
11   ### File Specific:
[3610]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3610]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]17
[3610]18#include "graphics_engine.h"
[4094]19#include "resource_manager.h"
[1853]20
[3611]21#include "debug.h"
[4245]22#include "text_engine.h"
[3611]23
[1856]24using namespace std;
[1853]25
[1856]26
[3245]27/**
28   \brief standard constructor
29   \todo this constructor is not jet implemented - do it
30*/
[4597]31GraphicsEngine::GraphicsEngine ()
[3365]32{
[4597]33  this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine");
34  this->setName("GraphicsEngine");
[4245]35  this->bDisplayFPS = false;
36  this->minFPS = 9999;
37  this->maxFPS = 0;
[4135]38
[4768]39  this->fullscreenFlag = 0;
[4135]40
[3611]41  this->initVideo();
[3610]42
[3617]43  this->listModes();
[3611]44}
[3610]45
[3621]46/**
47   \brief The Pointer to this GraphicsEngine
48*/
[3611]49GraphicsEngine* GraphicsEngine::singletonRef = NULL;
[3610]50
[3621]51/**
[3611]52   \brief destructs the graphicsEngine.
53*/
[4597]54GraphicsEngine::~GraphicsEngine ()
[3611]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{
[3617]66  // initialize SDL_VIDEO
[3610]67  if (SDL_Init(SDL_INIT_VIDEO) == -1)
68    {
[3617]69      PRINTF(1)("could not initialize SDL Video\n");
[3610]70      //      return -1;
71    }
[3617]72  // initialize SDL_GL-settings
73  this->setGLattribs();
[3610]74
[3617]75  // setting the Video Flags.
[3619]76  this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF;
[3610]77
78  /* query SDL for information about our video hardware */
79  const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
80  if( videoInfo == NULL)
81    {
[4597]82      PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError());
[3610]83      SDL_Quit ();
84    }
85  if( videoInfo->hw_available)
[3611]86    this->videoFlags |= SDL_HWSURFACE;
[4597]87  else
[3611]88    this->videoFlags |= SDL_SWSURFACE;
[3610]89  /*
[3619]90  if(VideoInfo -> blit_hw)
[3610]91    VideoFlags |= SDL_HWACCEL;
92  */
[4739]93    // setting up the Resolution
[4768]94  this->setResolution(640, 480, 16);
[3611]95
[3610]96  // TO DO: Create a cool icon and use it here
[4094]97  char* loadPic = new char[strlen(ResourceManager::getInstance()->getDataDir())+ 100];
98  sprintf(loadPic, "%s%s", ResourceManager::getInstance()->getDataDir(),  "pictures/orxonox-icon32x32.bmp");
[4597]99  SDL_WM_SetIcon(SDL_LoadBMP(loadPic), NULL);
[4094]100  delete loadPic;
[3621]101  // Enable default GL stuff
102  glEnable(GL_DEPTH_TEST);
[3365]103}
[1853]104
[4619]105
[3621]106/**
[4619]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/**
[3621]119   \brief Sets the GL-attributes
120*/
[4746]121int GraphicsEngine::setGLattribs()
[3617]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
[4597]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);
[3617]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
[3621]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*/
[3619]146int GraphicsEngine::setResolution(int width, int height, int bpp)
[3610]147{
[3611]148  this->resolutionX = width;
149  this->resolutionY = height;
150  this->bitsPerPixel = bpp;
[4739]151
[4768]152  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | SDL_FULLSCREEN)) == NULL)
[3611]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    }
[4135]158}
[3610]159
[4458]160/**
161   \brief sets Fullscreen mode
162   \param fullscreen true if fullscreen, false if windowed
163*/
[4135]164void GraphicsEngine::setFullscreen(bool fullscreen)
165{
[4768]166  if (fullscreen)
167    fullscreenFlag = SDL_FULLSCREEN;
168  else
169    fullscreenFlag = 0;
[4135]170  this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel);
[3543]171}
[3617]172
[4458]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*/
[4374]180void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
181{
182  glClearColor(red, green, blue, alpha);
183}
184
185
[3621]186/**
187   \brief Signalhandler, for when the resolution has changed
188   \param resizeInfo SDL information about the size of the new screen size
189*/
[3619]190int GraphicsEngine::resolutionChanged(SDL_ResizeEvent* resizeInfo)
191{
192  this->setResolution(resizeInfo->w, resizeInfo->h, this->bitsPerPixel);
193}
[3617]194
[3622]195/**
196   \brief if Textures should be enabled
197*/
198bool GraphicsEngine::texturesEnabled = true;
[3617]199
200
201
[3790]202/**
203   \brief entering 2D Mode
[4597]204
[3790]205   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
206*/
[4746]207void GraphicsEngine::enter2DMode()
[3790]208{
[3844]209  GraphicsEngine::storeMatrices();
[3790]210  SDL_Surface *screen = SDL_GetVideoSurface();
[4597]211
[3790]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);
[3617]220
[3790]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);
[4597]224
[3790]225  glViewport(0, 0, screen->w, screen->h);
[4597]226
[3790]227  glMatrixMode(GL_PROJECTION);
228  glPushMatrix();
229  glLoadIdentity();
[4597]230
[3790]231  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
[4597]232
[3790]233  glMatrixMode(GL_MODELVIEW);
234  glPushMatrix();
235  glLoadIdentity();
[4597]236
[3790]237  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
238}
[3617]239
[3790]240/**
[4746]241   \brief leaves the 2DMode again also \see Font::enter2DMode()
[3790]242*/
[4746]243void GraphicsEngine::leave2DMode()
[3790]244{
245  glMatrixMode(GL_MODELVIEW);
246  glPopMatrix();
[4597]247
[3790]248  glMatrixMode(GL_PROJECTION);
249  glPopMatrix();
[4597]250
[3790]251  glPopAttrib();
252}
[3622]253
[3844]254/**
[4597]255   \brief stores the GL_matrices
[3844]256*/
[4746]257void GraphicsEngine::storeMatrices()
[3844]258{
259  glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat);
260  glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat);
261  glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort);
262}
[3622]263
[3844]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};
[3790]270
271
[3844]272
[3617]273/**
274   \brief outputs all the Fullscreen modes.
275*/
[4746]276void GraphicsEngine::listModes()
[3617]277{
278  /* Get available fullscreen/hardware modes */
279  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
[4597]280
[3617]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  }
[4597]286
[3617]287  /* Check if our resolution is restricted */
288  if(this->videoModes == (SDL_Rect **)-1){
[4135]289    PRINTF(2)("All resolutions available.\n");
[3617]290  }
291  else{
292    /* Print valid modes */
293    PRINT(0)("Available Resoulution Modes are\n");
294    for(int i = 0; this->videoModes[i]; ++i)
[4135]295      PRINT(4)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
[3617]296  }
[4245]297}
298
[4458]299/**
300   \brief ticks the Text
301   \param dt the time passed
302*/
[4245]303void GraphicsEngine::tick(float dt)
304{
[4458]305  if( unlikely(this->bDisplayFPS))
306    {
307      this->currentFPS = 1.0/dt;
[4245]308      if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS;
[4458]309      if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS;
[4597]310
[4536]311#ifndef NO_TEXT
[4458]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);
[4536]321#endif /* NO_TEXT */
[4458]322    }
[4245]323}
[4597]324
[4458]325/**
326   \brief displays the Frames per second
327   \param display if the text should be displayed
328
329   \todo this is dangerous
330*/
[4245]331void GraphicsEngine::displayFPS(bool display)
332{
[4458]333  if( display)
334    {
[4536]335#ifndef NO_TEXT
[4681]336      this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0);
[4458]337      this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT);
338      this->geTextCFPS->setPosition(5, 500);
[4681]339      this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0);
[4458]340      this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT);
341      this->geTextMaxFPS->setPosition(5, 530);
[4681]342      this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 35, TEXT_DYNAMIC, 0, 255, 0);
[4458]343      this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT);
[4597]344      this->geTextMinFPS->setPosition(5, 560);
[4536]345#endif /* NO_TEXT */
[4458]346    }
347  this->bDisplayFPS = display;
[3617]348}
[4245]349
[4597]350
Note: See TracBrowser for help on using the repository browser.