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