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