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