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