Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the textEngine back into the trunk.
merged with command:
svn merge -r 3681:HEAD branches/textEngine/ trunk/

conflicts in:
world.cc/h orxonox.cc NEWS
changed in favor of the trunk

File size: 6.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
22using namespace std;
23
24
25/**
26   \brief standard constructor
27   \todo this constructor is not jet implemented - do it
28*/
29GraphicsEngine::GraphicsEngine () 
30{
31  this->setClassName ("GraphicsEngine");
32  this->initVideo();
33
34  this->listModes();
35}
36
37/**
38   \brief The Pointer to this GraphicsEngine
39*/
40GraphicsEngine* GraphicsEngine::singletonRef = NULL;
41
42/**
43   \returns A pointer to this GraphicsEngine
44*/
45GraphicsEngine* GraphicsEngine::getInstance()
46{
47  if (!GraphicsEngine::singletonRef)
48    GraphicsEngine::singletonRef = new GraphicsEngine();
49  return GraphicsEngine::singletonRef;
50}
51
52
53/**
54   \brief destructs the graphicsEngine.
55*/
56GraphicsEngine::~GraphicsEngine () 
57{
58  // delete what has to be deleted here
59}
60
61/**
62   \brief initializes the Video for openGL.
63
64   This has to be done only once when starting orxonox.
65*/
66int GraphicsEngine::initVideo()
67{
68  // initialize SDL_VIDEO
69  if (SDL_Init(SDL_INIT_VIDEO) == -1)
70    {
71      PRINTF(1)("could not initialize SDL Video\n");
72      //      return -1;
73    }
74  // initialize SDL_GL-settings
75  this->setGLattribs();
76
77  // setting the Video Flags.
78  this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF;
79
80  /* query SDL for information about our video hardware */
81  const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
82  if( videoInfo == NULL)
83    {
84      PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); 
85      SDL_Quit ();
86    }
87  if( videoInfo->hw_available)
88    this->videoFlags |= SDL_HWSURFACE;
89  else 
90    this->videoFlags |= SDL_SWSURFACE;
91  /*
92  if(VideoInfo -> blit_hw)
93    VideoFlags |= SDL_HWACCEL;
94  */
95
96  // setting up the Resolution
97  this->setResolution(800, 600, 16);
98 
99  // Set window labeling
100  SDL_WM_SetCaption ("Orxonox " PACKAGE_VERSION, "Orxonox " PACKAGE_VERSION);
101 
102  // TO DO: Create a cool icon and use it here
103  SDL_WM_SetIcon(SDL_LoadBMP("../data/pictures/orxonox-icon32x32.bmp"), NULL); 
104
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  this->resolutionX = width;
140  this->resolutionY = height;
141  this->bitsPerPixel = bpp;
142 
143  printf ("ok\n");
144  if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags)) == NULL)
145    {
146      PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError());
147      SDL_Quit();
148      //    return -1;
149    }
150
151}
152
153/**
154   \brief Signalhandler, for when the resolution has changed
155   \param resizeInfo SDL information about the size of the new screen size
156*/
157int GraphicsEngine::resolutionChanged(SDL_ResizeEvent* resizeInfo)
158{
159  this->setResolution(resizeInfo->w, resizeInfo->h, this->bitsPerPixel);
160}
161
162/**
163   \brief if Textures should be enabled
164*/
165bool GraphicsEngine::texturesEnabled = true;
166
167
168
169/**
170   \brief entering 2D Mode
171   
172   this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else
173*/
174void GraphicsEngine::enter2DMode(void)
175{
176  SDL_Surface *screen = SDL_GetVideoSurface();
177 
178  /* Note, there may be other things you need to change,
179     depending on how you have your OpenGL state set up.
180  */
181  glPushAttrib(GL_ENABLE_BIT);
182  glDisable(GL_DEPTH_TEST);
183  glDisable(GL_CULL_FACE);
184  glDisable(GL_LIGHTING);  // will be set back when leaving 2D-mode
185  glEnable(GL_TEXTURE_2D);
186
187  /* This allows alpha blending of 2D textures with the scene */
188  glEnable(GL_BLEND);
189  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
190 
191  glViewport(0, 0, screen->w, screen->h);
192 
193  glMatrixMode(GL_PROJECTION);
194  glPushMatrix();
195  glLoadIdentity();
196 
197  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
198 
199  glMatrixMode(GL_MODELVIEW);
200  glPushMatrix();
201  glLoadIdentity();
202 
203  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
204}
205
206/**
207   \brief leaves the 2DMode again also \see Font::enter2DMode(void)
208*/
209void GraphicsEngine::leave2DMode(void)
210{
211  glMatrixMode(GL_MODELVIEW);
212  glPopMatrix();
213 
214  glMatrixMode(GL_PROJECTION);
215  glPopMatrix();
216 
217  glPopAttrib();
218}
219
220
221
222
223/**
224   \brief outputs all the Fullscreen modes.
225*/
226void GraphicsEngine::listModes(void)
227{
228  /* Get available fullscreen/hardware modes */
229  this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
230 
231  /* Check is there are any modes available */
232  if(this->videoModes == (SDL_Rect **)0){
233    PRINTF(1)("No modes available!\n");
234    exit(-1);
235  }
236 
237  /* Check if our resolution is restricted */
238  if(this->videoModes == (SDL_Rect **)-1){
239    PRINTF(1)("All resolutions available.\n");
240  }
241  else{
242    /* Print valid modes */
243    PRINT(0)("Available Resoulution Modes are\n");
244    for(int i = 0; this->videoModes[i]; ++i)
245      PRINT(0)(" |  %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h);
246  }
247 
248}
Note: See TracBrowser for help on using the repository browser.