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 "util/loading/resource_manager.h" |
---|
20 | #include "state.h" |
---|
21 | |
---|
22 | #include "world_entity.h" |
---|
23 | |
---|
24 | #include "render_2d.h" |
---|
25 | #include "text_engine.h" |
---|
26 | #include "light.h" |
---|
27 | #include "shader.h" |
---|
28 | #include "debug.h" |
---|
29 | |
---|
30 | #include "util/preferences.h" |
---|
31 | #include "substring.h" |
---|
32 | #include "text.h" |
---|
33 | |
---|
34 | #include "globals.h" |
---|
35 | #include "texture.h" |
---|
36 | |
---|
37 | #include "effects/graphics_effect.h" |
---|
38 | #include "effects/fog_effect.h" |
---|
39 | #include "effects/lense_flare.h" |
---|
40 | |
---|
41 | #include "shell_command.h" |
---|
42 | |
---|
43 | |
---|
44 | #include "parser/tinyxml/tinyxml.h" |
---|
45 | #include "util/loading/load_param.h" |
---|
46 | #include "util/loading/factory.h" |
---|
47 | #include "class_list.h" |
---|
48 | |
---|
49 | #ifdef __WIN32__ |
---|
50 | #include "static_model.h" |
---|
51 | #endif |
---|
52 | |
---|
53 | SHELL_COMMAND(wireframe, GraphicsEngine, wireframe); |
---|
54 | SHELL_COMMAND(fps, GraphicsEngine, toggleFPSdisplay); |
---|
55 | |
---|
56 | /** |
---|
57 | * @brief standard constructor |
---|
58 | */ |
---|
59 | GraphicsEngine::GraphicsEngine () |
---|
60 | { |
---|
61 | this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine"); |
---|
62 | this->setName("GraphicsEngine"); |
---|
63 | |
---|
64 | this->isInit = false; |
---|
65 | |
---|
66 | this->bDisplayFPS = false; |
---|
67 | this->bAntialiasing = false; |
---|
68 | this->minFPS = 9999; |
---|
69 | this->maxFPS = 0; |
---|
70 | |
---|
71 | this->geTextCFPS = NULL; |
---|
72 | this->geTextMaxFPS = NULL; |
---|
73 | this->geTextMinFPS = NULL; |
---|
74 | |
---|
75 | this->fullscreenFlag = 0; |
---|
76 | this->videoFlags = 0; |
---|
77 | this->screen = NULL; |
---|
78 | |
---|
79 | // initialize the Modules |
---|
80 | TextEngine::getInstance(); |
---|
81 | this->graphicsEffects = NULL; |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * @brief The Pointer to this GraphicsEngine |
---|
87 | */ |
---|
88 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
---|
89 | |
---|
90 | /** |
---|
91 | * @brief destructs the graphicsEngine. |
---|
92 | */ |
---|
93 | GraphicsEngine::~GraphicsEngine () |
---|
94 | { |
---|
95 | // delete what has to be deleted here |
---|
96 | this->displayFPS( false ); |
---|
97 | |
---|
98 | //TextEngine |
---|
99 | delete TextEngine::getInstance(); |
---|
100 | // render 2D |
---|
101 | delete Render2D::getInstance(); |
---|
102 | |
---|
103 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
---|
104 | // if (this->screen != NULL) |
---|
105 | // SDL_FreeSurface(this->screen); |
---|
106 | |
---|
107 | GraphicsEngine::singletonRef = NULL; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | /** |
---|
112 | * @brief loads the GraphicsEngine Specific Parameters. |
---|
113 | * @param root: the XML-Element to load the Data From |
---|
114 | */ |
---|
115 | void GraphicsEngine::loadParams(const TiXmlElement* root) |
---|
116 | { |
---|
117 | LoadParamXML(root, "GraphicsEffect", this, GraphicsEngine, loadGraphicsEffects) |
---|
118 | .describe("loads a graphics effect"); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * @param root The XML-element to load GraphicsEffects from |
---|
126 | */ |
---|
127 | void GraphicsEngine::loadGraphicsEffects(const TiXmlElement* root) |
---|
128 | { |
---|
129 | LOAD_PARAM_START_CYCLE(root, element); |
---|
130 | { |
---|
131 | PRINTF(4)("element is: %s\n", element->Value()); |
---|
132 | Factory::fabricate(element); |
---|
133 | } |
---|
134 | LOAD_PARAM_END_CYCLE(element); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | /** |
---|
140 | * @brief initializes the GraphicsEngine with default settings. |
---|
141 | */ |
---|
142 | int GraphicsEngine::init() |
---|
143 | { |
---|
144 | if (this->isInit) |
---|
145 | return -1; |
---|
146 | this->initVideo(640, 480, 16); |
---|
147 | return 1; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * @brief loads the GraphicsEngine's settings from a given ini-file and section |
---|
152 | * @returns nothing usefull |
---|
153 | */ |
---|
154 | int GraphicsEngine::initFromPreferences() |
---|
155 | { |
---|
156 | // looking if we are in fullscreen-mode |
---|
157 | MultiType fullscreen = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_FULLSCREEN, "0"); |
---|
158 | |
---|
159 | if (fullscreen.getBool()) |
---|
160 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
161 | |
---|
162 | // looking if we are in fullscreen-mode |
---|
163 | MultiType textures = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO_ADVANCED, CONFIG_NAME_TEXTURES, "1"); |
---|
164 | Texture::setTextureEnableState(textures.getBool()); |
---|
165 | |
---|
166 | // searching for a usefull resolution |
---|
167 | SubString resolution(Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_RESOLUTION, "640x480").c_str(), 'x'); ///FIXME |
---|
168 | //resolution.debug(); |
---|
169 | MultiType x = resolution.getString(0), y = resolution.getString(1); |
---|
170 | return this->initVideo(x.getInt(), y.getInt(), 16); |
---|
171 | |
---|
172 | // GraphicsEffect* fe = new FogEffect(NULL); |
---|
173 | // this->loadGraphicsEffect(fe); |
---|
174 | // fe->activate(); |
---|
175 | // PRINTF(0)("--------------------------------------------------------------\n"); |
---|
176 | |
---|
177 | //LenseFlare* ge = new LenseFlare(); |
---|
178 | //this->loadGraphicsEffect(ge); |
---|
179 | |
---|
180 | //ge->addFlare("pictures/lense_flare/sun.png"); //sun |
---|
181 | //ge->addFlare("pictures/lense_flare/lens2.png"); //first halo |
---|
182 | //ge->addFlare("pictures/lense_flare/lens1.png"); //small birst |
---|
183 | //ge->addFlare("pictures/lense_flare/lens3.png"); //second halo |
---|
184 | //ge->addFlare("pictures/lense_flare/lens4.png"); |
---|
185 | //ge->addFlare("pictures/lense_flare/lens1.png"); |
---|
186 | //ge->addFlare("pictures/lense_flare/lens3.png"); |
---|
187 | |
---|
188 | //ge->activate(); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | /** |
---|
194 | * @brief initializes the Video for openGL. |
---|
195 | * |
---|
196 | * This has to be done only once when starting orxonox. |
---|
197 | */ |
---|
198 | int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp) |
---|
199 | { |
---|
200 | if (this->isInit) |
---|
201 | return -1; |
---|
202 | // initialize SDL_VIDEO |
---|
203 | if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1) |
---|
204 | { |
---|
205 | PRINTF(1)("could not initialize SDL Video\n"); |
---|
206 | return -1; |
---|
207 | } |
---|
208 | // initialize SDL_GL-settings |
---|
209 | this->setGLattribs(); |
---|
210 | |
---|
211 | // setting the Video Flags. |
---|
212 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE ; |
---|
213 | |
---|
214 | /* query SDL for information about our video hardware */ |
---|
215 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
---|
216 | if( videoInfo == NULL) |
---|
217 | { |
---|
218 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
---|
219 | SDL_Quit (); |
---|
220 | } |
---|
221 | if( videoInfo->hw_available) |
---|
222 | this->videoFlags |= SDL_HWSURFACE; |
---|
223 | else |
---|
224 | this->videoFlags |= SDL_SWSURFACE; |
---|
225 | /* |
---|
226 | if(VideoInfo -> blit_hw) |
---|
227 | VideoFlags |= SDL_HWACCEL; |
---|
228 | */ |
---|
229 | // setting up the Resolution |
---|
230 | this->setResolution(resX, resY, bbp); |
---|
231 | |
---|
232 | // GRABBING ALL GL-extensions |
---|
233 | this->grabHardwareSettings(); |
---|
234 | |
---|
235 | // Enable default GL stuff |
---|
236 | glEnable(GL_DEPTH_TEST); |
---|
237 | |
---|
238 | Render2D::getInstance(); |
---|
239 | |
---|
240 | this->isInit = true; |
---|
241 | return 1; |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * @brief sets the Window Captions and the Name of the icon. |
---|
246 | * @param windowName The name of the Window |
---|
247 | * @param icon The name of the Icon on the Disc |
---|
248 | */ |
---|
249 | void GraphicsEngine::setWindowName(const std::string& windowName, const std::string& icon) |
---|
250 | { |
---|
251 | SDL_Surface* iconSurf = SDL_LoadBMP(icon.c_str()); |
---|
252 | if (iconSurf != NULL) |
---|
253 | { |
---|
254 | Uint32 colorkey = SDL_MapRGB(iconSurf->format, 0, 0, 0); |
---|
255 | SDL_SetColorKey(iconSurf, SDL_SRCCOLORKEY, colorkey); |
---|
256 | SDL_WM_SetIcon(iconSurf, NULL); |
---|
257 | SDL_FreeSurface(iconSurf); |
---|
258 | } |
---|
259 | |
---|
260 | SDL_WM_SetCaption (windowName.c_str(), icon.c_str()); |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | /** |
---|
265 | * @brief Sets the GL-attributes |
---|
266 | */ |
---|
267 | void GraphicsEngine::setGLattribs() |
---|
268 | { |
---|
269 | // Set video mode |
---|
270 | // TO DO: parse arguments for settings |
---|
271 | //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
---|
272 | //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); |
---|
273 | //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
---|
274 | //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
---|
275 | |
---|
276 | |
---|
277 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
---|
278 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16); |
---|
279 | SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0); |
---|
280 | SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0); |
---|
281 | SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0); |
---|
282 | SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0); |
---|
283 | SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0); |
---|
284 | |
---|
285 | SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); //Use at least 5 bits of Red |
---|
286 | SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); //Use at least 5 bits of Green |
---|
287 | SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); //Use at least 5 bits of Blue |
---|
288 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); //Use at least 16 bits for the depth buffer |
---|
289 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //Enable double buffering |
---|
290 | |
---|
291 | // enable antialiasing? |
---|
292 | if( this->bAntialiasing) |
---|
293 | { |
---|
294 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,4); |
---|
295 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); |
---|
296 | } |
---|
297 | |
---|
298 | glEnable(GL_CULL_FACE); |
---|
299 | glCullFace(GL_FRONT); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * @brief grabs the Hardware Specifics |
---|
304 | * |
---|
305 | * checks for all the different HW-types |
---|
306 | */ |
---|
307 | void GraphicsEngine::grabHardwareSettings() |
---|
308 | { |
---|
309 | const char* renderer = (const char*) glGetString(GL_RENDERER); |
---|
310 | const char* vendor = (const char*) glGetString(GL_VENDOR); |
---|
311 | const char* version = (const char*) glGetString(GL_VERSION); |
---|
312 | const char* extensions = (const char*) glGetString(GL_EXTENSIONS); |
---|
313 | |
---|
314 | // printf("%s %s %s\n %s", renderer, vendor, version, extensions); |
---|
315 | |
---|
316 | if (renderer != NULL) |
---|
317 | { |
---|
318 | this->hwRenderer == renderer; |
---|
319 | } |
---|
320 | if (vendor != NULL) |
---|
321 | { |
---|
322 | this->hwVendor == vendor; |
---|
323 | } |
---|
324 | if (version != NULL) |
---|
325 | { |
---|
326 | this->hwVersion == version; |
---|
327 | } |
---|
328 | |
---|
329 | if (extensions != NULL) |
---|
330 | this->hwExtensions.split(extensions, " \n\t,"); |
---|
331 | |
---|
332 | PRINT(4)("Running on : vendor: %s, renderer: %s, version:%s\n", vendor, renderer, version); |
---|
333 | PRINT(4)("Extensions:\n"); |
---|
334 | for (unsigned int i = 0; i < this->hwExtensions.size(); i++) |
---|
335 | PRINT(4)("%d: %s\n", i, this->hwExtensions[i].c_str()); |
---|
336 | |
---|
337 | |
---|
338 | // inizializing GLEW |
---|
339 | GLenum err = glewInit(); |
---|
340 | if (GLEW_OK != err) |
---|
341 | { |
---|
342 | /* Problem: glewInit failed, something is seriously wrong. */ |
---|
343 | PRINTF(1)("%s\n", glewGetErrorString(err)); |
---|
344 | } |
---|
345 | PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | /** |
---|
350 | * @brief sets the Resolution of the Screen to display the Graphics to. |
---|
351 | * @param width The width of the window |
---|
352 | * @param height The height of the window |
---|
353 | * @param bpp bits per pixel |
---|
354 | */ |
---|
355 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
---|
356 | { |
---|
357 | this->resolutionX = width; |
---|
358 | this->resolutionY = height; |
---|
359 | this->bitsPerPixel = bpp; |
---|
360 | State::setResolution( width, height); |
---|
361 | |
---|
362 | if (this->screen != NULL) |
---|
363 | SDL_FreeSurface(screen); |
---|
364 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL) |
---|
365 | { |
---|
366 | PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError()); |
---|
367 | // SDL_Quit(); |
---|
368 | // return -1; |
---|
369 | return -1; |
---|
370 | } |
---|
371 | glViewport(0, 0, width, height); // Reset The Current Viewport |
---|
372 | |
---|
373 | #ifdef __WIN32__ |
---|
374 | // REBUILDING TEXTURES (ON WINDOWS CONTEXT SWITCH) |
---|
375 | const std::list<BaseObject*>* texList = ClassList::getList(CL_TEXTURE); |
---|
376 | if (texList != NULL) |
---|
377 | { |
---|
378 | std::list<BaseObject*>::const_iterator reTex; |
---|
379 | for (reTex = texList->begin(); reTex != texList->end(); reTex++) |
---|
380 | dynamic_cast<Texture*>(*reTex)->rebuild(); |
---|
381 | } |
---|
382 | // REBUILDING MODELS |
---|
383 | const std::list<BaseObject*>* modelList = ClassList::getList(CL_STATIC_MODEL); |
---|
384 | if (texList != NULL) |
---|
385 | { |
---|
386 | std::list<BaseObject*>::const_iterator reModel; |
---|
387 | for (reModel = modelList->begin(); reModel != modelList->end(); reModel++) |
---|
388 | dynamic_cast<StaticModel*>(*reModel)->rebuild(); |
---|
389 | } |
---|
390 | #endif /* __WIN32__ */ |
---|
391 | return 1; |
---|
392 | } |
---|
393 | |
---|
394 | /** |
---|
395 | * @brief sets Fullscreen mode |
---|
396 | * @param fullscreen true if fullscreen, false if windowed |
---|
397 | */ |
---|
398 | void GraphicsEngine::setFullscreen(bool fullscreen) |
---|
399 | { |
---|
400 | if (fullscreen) |
---|
401 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
402 | else |
---|
403 | this->fullscreenFlag = 0; |
---|
404 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
---|
405 | } |
---|
406 | |
---|
407 | void GraphicsEngine::toggleFullscreen() |
---|
408 | { |
---|
409 | if (this->fullscreenFlag == SDL_FULLSCREEN) |
---|
410 | this->fullscreenFlag = 0; |
---|
411 | else |
---|
412 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
413 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | /** |
---|
418 | * @brief sets the background color |
---|
419 | * @param red the red part of the background |
---|
420 | * @param blue the blue part of the background |
---|
421 | * @param green the green part of the background |
---|
422 | * @param alpha the alpha part of the background |
---|
423 | */ |
---|
424 | void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) |
---|
425 | { |
---|
426 | glClearColor(red, green, blue, alpha); |
---|
427 | } |
---|
428 | |
---|
429 | /** |
---|
430 | * @brief Signalhandler, for when the resolution has changed |
---|
431 | * @param resizeInfo SDL information about the size of the new screen size |
---|
432 | */ |
---|
433 | void GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo) |
---|
434 | { |
---|
435 | this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel); |
---|
436 | } |
---|
437 | |
---|
438 | /** |
---|
439 | * @brief entering 2D Mode |
---|
440 | * this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else |
---|
441 | */ |
---|
442 | void GraphicsEngine::enter2DMode() |
---|
443 | { |
---|
444 | //GraphicsEngine::storeMatrices(); |
---|
445 | SDL_Surface *screen = SDL_GetVideoSurface(); |
---|
446 | |
---|
447 | /* Note, there may be other things you need to change, |
---|
448 | depending on how you have your OpenGL state set up. |
---|
449 | */ |
---|
450 | glPushAttrib(GL_ENABLE_BIT); |
---|
451 | glDisable(GL_DEPTH_TEST); |
---|
452 | glDisable(GL_CULL_FACE); |
---|
453 | glDisable(GL_LIGHTING); // will be set back when leaving 2D-mode |
---|
454 | |
---|
455 | glMatrixMode(GL_PROJECTION); |
---|
456 | glPushMatrix(); |
---|
457 | glLoadIdentity(); |
---|
458 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
---|
459 | |
---|
460 | glMatrixMode(GL_MODELVIEW); |
---|
461 | glPushMatrix(); |
---|
462 | glLoadIdentity(); |
---|
463 | } |
---|
464 | |
---|
465 | /** |
---|
466 | * @brief leaves the 2DMode again also @see Font::enter2DMode() |
---|
467 | */ |
---|
468 | void GraphicsEngine::leave2DMode() |
---|
469 | { |
---|
470 | |
---|
471 | glMatrixMode(GL_MODELVIEW); |
---|
472 | glPopMatrix(); |
---|
473 | |
---|
474 | glMatrixMode(GL_PROJECTION); |
---|
475 | glPopMatrix(); |
---|
476 | |
---|
477 | glPopAttrib(); |
---|
478 | } |
---|
479 | |
---|
480 | /** |
---|
481 | * @brief changes to wireframe-mode. |
---|
482 | */ |
---|
483 | void GraphicsEngine::wireframe() |
---|
484 | { |
---|
485 | glPolygonMode(GL_FRONT, GL_LINE); |
---|
486 | } |
---|
487 | |
---|
488 | /** |
---|
489 | * @brief stores the GL_matrices |
---|
490 | */ |
---|
491 | void GraphicsEngine::storeMatrices() |
---|
492 | { |
---|
493 | glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat); |
---|
494 | glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat); |
---|
495 | glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort); |
---|
496 | } |
---|
497 | |
---|
498 | //! the stored ModelView Matrix. |
---|
499 | GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
500 | //! the stored Projection Matrix |
---|
501 | GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
502 | //! The ViewPort |
---|
503 | GLint GraphicsEngine::viewPort[4] = {0,0,0,0}; |
---|
504 | |
---|
505 | |
---|
506 | |
---|
507 | /** |
---|
508 | * @brief outputs all the Fullscreen modes. |
---|
509 | */ |
---|
510 | void GraphicsEngine::listModes() |
---|
511 | { |
---|
512 | /* Get available fullscreen/hardware modes */ |
---|
513 | this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); |
---|
514 | |
---|
515 | /* Check is there are any modes available */ |
---|
516 | if(this->videoModes == (SDL_Rect **)0) |
---|
517 | { |
---|
518 | PRINTF(1)("No modes available!\n"); |
---|
519 | exit(-1); |
---|
520 | } |
---|
521 | |
---|
522 | /* Check if our resolution is restricted */ |
---|
523 | if(this->videoModes == (SDL_Rect **)-1) |
---|
524 | { |
---|
525 | PRINTF(2)("All resolutions available.\n"); |
---|
526 | } |
---|
527 | else |
---|
528 | { |
---|
529 | /* Print valid modes */ |
---|
530 | PRINT(0)("Available Resoulution Modes are\n"); |
---|
531 | for(int i = 0; this->videoModes[i]; ++i) |
---|
532 | PRINT(4)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
---|
533 | } |
---|
534 | } |
---|
535 | |
---|
536 | /** |
---|
537 | * @brief checks wether a certain extension is availiable |
---|
538 | * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3) |
---|
539 | * @return true if it is, false otherwise |
---|
540 | */ |
---|
541 | bool GraphicsEngine::hwSupportsEXT(const std::string& extension) |
---|
542 | { |
---|
543 | for (unsigned int i = 0; i < this->hwExtensions.size(); i++) |
---|
544 | if ( this->hwExtensions.getString(i) == extension) |
---|
545 | return true; |
---|
546 | return false; |
---|
547 | } |
---|
548 | |
---|
549 | /** |
---|
550 | * @brief updates everything that is to be updated in the GraphicsEngine |
---|
551 | */ |
---|
552 | void GraphicsEngine::update(float dt) |
---|
553 | { |
---|
554 | Render2D::getInstance()->update(dt); |
---|
555 | } |
---|
556 | |
---|
557 | |
---|
558 | /** |
---|
559 | * @brief ticks the Text |
---|
560 | * @param dt the time passed |
---|
561 | */ |
---|
562 | void GraphicsEngine::tick(float dt) |
---|
563 | { |
---|
564 | if( unlikely(this->bDisplayFPS)) |
---|
565 | { |
---|
566 | this->currentFPS = 1.0/dt; |
---|
567 | if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS; |
---|
568 | if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS; |
---|
569 | |
---|
570 | #ifndef NO_TEXT |
---|
571 | char tmpChar1[20]; |
---|
572 | sprintf(tmpChar1, "Current: %4.0f", this->currentFPS); |
---|
573 | this->geTextCFPS->setText(tmpChar1); |
---|
574 | char tmpChar2[20]; |
---|
575 | sprintf(tmpChar2, "Max: %4.0f", this->maxFPS); |
---|
576 | this->geTextMaxFPS->setText(tmpChar2); |
---|
577 | char tmpChar3[20]; |
---|
578 | sprintf(tmpChar3, "Min: %4.0f", this->minFPS); |
---|
579 | this->geTextMinFPS->setText(tmpChar3); |
---|
580 | #endif /* NO_TEXT */ |
---|
581 | |
---|
582 | } |
---|
583 | |
---|
584 | Render2D::getInstance()->tick(dt); |
---|
585 | |
---|
586 | // tick the graphics effects |
---|
587 | if (this->graphicsEffects != NULL || (this->graphicsEffects = ClassList::getList(CL_GRAPHICS_EFFECT)) != NULL) |
---|
588 | { |
---|
589 | std::list<BaseObject*>::const_iterator it; |
---|
590 | for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++) |
---|
591 | dynamic_cast<GraphicsEffect*>(*it)->tick(dt); |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | /** |
---|
596 | * @brief draws all Elements that should be displayed on the Background. |
---|
597 | */ |
---|
598 | void GraphicsEngine::drawBackgroundElements() const |
---|
599 | { |
---|
600 | GraphicsEngine::storeMatrices(); |
---|
601 | |
---|
602 | Render2D::getInstance()->draw(E2D_LAYER_BELOW_ALL, E2D_LAYER_BELOW_ALL); |
---|
603 | } |
---|
604 | |
---|
605 | void GraphicsEngine::draw() const |
---|
606 | { |
---|
607 | // LightManager::getInstance()->draw(); |
---|
608 | |
---|
609 | if (this->graphicsEffects != NULL) |
---|
610 | { |
---|
611 | //draw the graphics effects |
---|
612 | std::list<BaseObject*>::const_iterator it; |
---|
613 | for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++) |
---|
614 | dynamic_cast<GraphicsEffect*>(*it)->draw(); |
---|
615 | } |
---|
616 | Shader::suspendShader(); |
---|
617 | Render2D::getInstance()->draw(E2D_LAYER_BOTTOM, E2D_LAYER_ABOVE_ALL); |
---|
618 | Shader::restoreShader(); |
---|
619 | } |
---|
620 | |
---|
621 | |
---|
622 | void GraphicsEngine::toggleFPSdisplay() |
---|
623 | { |
---|
624 | this->displayFPS(!this->bDisplayFPS); |
---|
625 | } |
---|
626 | |
---|
627 | |
---|
628 | /** |
---|
629 | * @brief displays the Frames per second |
---|
630 | * @param display if the text should be displayed |
---|
631 | */ |
---|
632 | void GraphicsEngine::displayFPS(bool display) |
---|
633 | { |
---|
634 | #ifndef NO_TEXT |
---|
635 | if( display ) |
---|
636 | { |
---|
637 | if (this->geTextCFPS == NULL) |
---|
638 | { |
---|
639 | this->geTextCFPS = new Text("fonts/arial_black.ttf", 15); |
---|
640 | this->geTextCFPS->setName("curFPS"); |
---|
641 | this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
642 | this->geTextCFPS->setAbsCoor2D(5, 0); |
---|
643 | } |
---|
644 | if (this->geTextMaxFPS == NULL) |
---|
645 | { |
---|
646 | this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15); |
---|
647 | this->geTextMaxFPS->setName("MaxFPS"); |
---|
648 | this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
649 | this->geTextMaxFPS->setAbsCoor2D(5, 20); |
---|
650 | } |
---|
651 | if (this->geTextMinFPS == NULL) |
---|
652 | { |
---|
653 | this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15); |
---|
654 | this->geTextMinFPS->setName("MinFPS"); |
---|
655 | this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
656 | this->geTextMinFPS->setAbsCoor2D(5, 40); |
---|
657 | } |
---|
658 | } |
---|
659 | else |
---|
660 | { |
---|
661 | delete this->geTextCFPS; |
---|
662 | this->geTextCFPS = NULL; |
---|
663 | delete this->geTextMaxFPS; |
---|
664 | this->geTextMaxFPS = NULL; |
---|
665 | delete this->geTextMinFPS; |
---|
666 | this->geTextMinFPS = NULL; |
---|
667 | } |
---|
668 | this->bDisplayFPS = display; |
---|
669 | #else |
---|
670 | this->bDisplayFPS = false; |
---|
671 | #endif /* NO_TEXT */ |
---|
672 | } |
---|
673 | |
---|
674 | |
---|
675 | /** |
---|
676 | * @brief processes the events for the GraphicsEngine class |
---|
677 | * @param the event to handle |
---|
678 | */ |
---|
679 | void GraphicsEngine::process(const Event &event) |
---|
680 | { |
---|
681 | switch (event.type) |
---|
682 | { |
---|
683 | case EV_VIDEO_RESIZE: |
---|
684 | this->resolutionChanged(event.resize); |
---|
685 | break; |
---|
686 | } |
---|
687 | } |
---|