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 | |
---|
22 | #include "render_2d.h" |
---|
23 | #include "light.h" |
---|
24 | #include "debug.h" |
---|
25 | #include "text_engine.h" |
---|
26 | |
---|
27 | #include "ini_parser.h" |
---|
28 | #include "substring.h" |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | |
---|
33 | /** |
---|
34 | * standard constructor |
---|
35 | @todo this constructor is not jet implemented - do it |
---|
36 | */ |
---|
37 | GraphicsEngine::GraphicsEngine () |
---|
38 | { |
---|
39 | this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine"); |
---|
40 | this->setName("GraphicsEngine"); |
---|
41 | |
---|
42 | this->isInit = false; |
---|
43 | |
---|
44 | this->bDisplayFPS = false; |
---|
45 | this->minFPS = 9999; |
---|
46 | this->maxFPS = 0; |
---|
47 | |
---|
48 | this->geTextCFPS = NULL; |
---|
49 | this->geTextMaxFPS = NULL; |
---|
50 | this->geTextMinFPS = NULL; |
---|
51 | |
---|
52 | this->fullscreenFlag = 0; |
---|
53 | this->videoFlags = 0; |
---|
54 | this->screen = NULL; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * The Pointer to this GraphicsEngine |
---|
59 | */ |
---|
60 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
---|
61 | |
---|
62 | /** |
---|
63 | * destructs the graphicsEngine. |
---|
64 | */ |
---|
65 | GraphicsEngine::~GraphicsEngine () |
---|
66 | { |
---|
67 | // delete what has to be deleted here |
---|
68 | delete this->geTextCFPS; |
---|
69 | delete this->geTextMaxFPS; |
---|
70 | delete this->geTextMinFPS; |
---|
71 | |
---|
72 | delete Render2D::getInstance(); |
---|
73 | |
---|
74 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
---|
75 | // if (this->screen != NULL) |
---|
76 | // SDL_FreeSurface(this->screen); |
---|
77 | |
---|
78 | GraphicsEngine::singletonRef = NULL; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * initializes the GraphicsEngine with default settings. |
---|
83 | */ |
---|
84 | int GraphicsEngine::init() |
---|
85 | { |
---|
86 | if (this->isInit) |
---|
87 | return -1; |
---|
88 | this->initVideo(640, 480, 16); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * loads the GraphicsEngine's settings from a given ini-file and section |
---|
93 | * @param iniParser the iniParser to load from |
---|
94 | * @param section the Section in the ini-file to load from |
---|
95 | * @returns nothing usefull |
---|
96 | */ |
---|
97 | int GraphicsEngine::initFromIniFile(IniParser* iniParser) |
---|
98 | { |
---|
99 | // looking if we are in fullscreen-mode |
---|
100 | const char* fullscreen = iniParser->getVar(CONFIG_NAME_FULLSCREEN, CONFIG_SECTION_VIDEO, "0"); |
---|
101 | if (strchr(fullscreen, '1')) |
---|
102 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | // looking if we are in fullscreen-mode |
---|
107 | const char* textures = iniParser->getVar(CONFIG_NAME_TEXTURES, CONFIG_SECTION_VIDEO_ADVANCED, "0"); |
---|
108 | if (strchr(textures, '1')) |
---|
109 | this->texturesEnabled = true; |
---|
110 | else |
---|
111 | this->texturesEnabled = false; |
---|
112 | |
---|
113 | // searching for a usefull resolution |
---|
114 | SubString resolution(iniParser->getVar(CONFIG_NAME_RESOLUTION, CONFIG_SECTION_VIDEO, "640x480"), 'x'); |
---|
115 | //resolution.debug(); |
---|
116 | |
---|
117 | this->initVideo(atoi(resolution.getString(0)), atoi(resolution.getString(1)), 16); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | * initializes the Video for openGL. |
---|
124 | |
---|
125 | This has to be done only once when starting orxonox. |
---|
126 | */ |
---|
127 | int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp) |
---|
128 | { |
---|
129 | if (this->isInit) |
---|
130 | return -1; |
---|
131 | // initialize SDL_VIDEO |
---|
132 | if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1) |
---|
133 | { |
---|
134 | PRINTF(1)("could not initialize SDL Video\n"); |
---|
135 | // return -1; |
---|
136 | } |
---|
137 | // initialize SDL_GL-settings |
---|
138 | this->setGLattribs(); |
---|
139 | |
---|
140 | // setting the Video Flags. |
---|
141 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF | SDL_GL_DOUBLEBUFFER; |
---|
142 | |
---|
143 | /* query SDL for information about our video hardware */ |
---|
144 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
---|
145 | if( videoInfo == NULL) |
---|
146 | { |
---|
147 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
---|
148 | SDL_Quit (); |
---|
149 | } |
---|
150 | if( videoInfo->hw_available) |
---|
151 | this->videoFlags |= SDL_HWSURFACE; |
---|
152 | else |
---|
153 | this->videoFlags |= SDL_SWSURFACE; |
---|
154 | /* |
---|
155 | if(VideoInfo -> blit_hw) |
---|
156 | VideoFlags |= SDL_HWACCEL; |
---|
157 | */ |
---|
158 | // setting up the Resolution |
---|
159 | this->setResolution(resX, resY, bbp); |
---|
160 | |
---|
161 | // Enable default GL stuff |
---|
162 | glEnable(GL_DEPTH_TEST); |
---|
163 | |
---|
164 | Render2D::getInstance(); |
---|
165 | |
---|
166 | this->isInit = true; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * sets the Window Captions and the Name of the icon. |
---|
171 | * @param windowName The name of the Window |
---|
172 | * @param icon The name of the Icon on the Disc |
---|
173 | */ |
---|
174 | void GraphicsEngine::setWindowName(const char* windowName, const char* icon) |
---|
175 | { |
---|
176 | SDL_Surface* iconSurf = SDL_LoadBMP(icon); |
---|
177 | if (iconSurf != NULL) |
---|
178 | { |
---|
179 | Uint32 colorkey = SDL_MapRGB(iconSurf->format, 0, 0, 0); |
---|
180 | SDL_SetColorKey(iconSurf, SDL_SRCCOLORKEY, colorkey); |
---|
181 | SDL_WM_SetIcon(iconSurf,NULL); |
---|
182 | SDL_FreeSurface(iconSurf); |
---|
183 | } |
---|
184 | |
---|
185 | SDL_WM_SetCaption (windowName, icon); |
---|
186 | |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /** |
---|
191 | * Sets the GL-attributes |
---|
192 | */ |
---|
193 | int GraphicsEngine::setGLattribs() |
---|
194 | { |
---|
195 | // Set video mode |
---|
196 | // TO DO: parse arguments for settings |
---|
197 | //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
---|
198 | //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); |
---|
199 | //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
---|
200 | //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
---|
201 | |
---|
202 | |
---|
203 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
---|
204 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16); |
---|
205 | SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0); |
---|
206 | SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0); |
---|
207 | SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0); |
---|
208 | SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0); |
---|
209 | SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * sets the Resolution of the Screen to display the Graphics to. |
---|
214 | * @param width The width of the window |
---|
215 | * @param height The height of the window |
---|
216 | * @param bpp bits per pixel |
---|
217 | */ |
---|
218 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
---|
219 | { |
---|
220 | this->resolutionX = width; |
---|
221 | this->resolutionY = height; |
---|
222 | this->bitsPerPixel = bpp; |
---|
223 | |
---|
224 | if (this->screen) |
---|
225 | SDL_FreeSurface(screen); |
---|
226 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL) |
---|
227 | { |
---|
228 | PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError()); |
---|
229 | // SDL_Quit(); |
---|
230 | // return -1; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * sets Fullscreen mode |
---|
236 | * @param fullscreen true if fullscreen, false if windowed |
---|
237 | */ |
---|
238 | void GraphicsEngine::setFullscreen(bool fullscreen) |
---|
239 | { |
---|
240 | if (fullscreen) |
---|
241 | fullscreenFlag = SDL_FULLSCREEN; |
---|
242 | else |
---|
243 | fullscreenFlag = 0; |
---|
244 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * sets the background color |
---|
249 | * @param red the red part of the background |
---|
250 | * @param blue the blue part of the background |
---|
251 | * @param green the green part of the background |
---|
252 | * @param alpha the alpha part of the background |
---|
253 | */ |
---|
254 | void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) |
---|
255 | { |
---|
256 | glClearColor(red, green, blue, alpha); |
---|
257 | } |
---|
258 | |
---|
259 | |
---|
260 | /** |
---|
261 | * Signalhandler, for when the resolution has changed |
---|
262 | * @param resizeInfo SDL information about the size of the new screen size |
---|
263 | */ |
---|
264 | int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo) |
---|
265 | { |
---|
266 | this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel); |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * if Textures should be enabled |
---|
271 | */ |
---|
272 | bool GraphicsEngine::texturesEnabled = true; |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | /** |
---|
277 | * |
---|
278 | * @param show if The mouse-cursor should be visible |
---|
279 | */ |
---|
280 | void GraphicsEngine::showMouse(bool show) |
---|
281 | { |
---|
282 | if (show) |
---|
283 | SDL_ShowCursor(SDL_ENABLE); |
---|
284 | else |
---|
285 | SDL_ShowCursor(SDL_DISABLE); |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * |
---|
290 | * @returns The Visinility of the mouse-cursor (true if visible, false if it is invisible) |
---|
291 | */ |
---|
292 | bool GraphicsEngine::isMouseVisible() |
---|
293 | { |
---|
294 | if (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE) |
---|
295 | return true; |
---|
296 | else |
---|
297 | return false; |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * |
---|
302 | * @param steal If the Winodow-Managers Events should be stolen to this app |
---|
303 | * (steals the mouse, and all WM-clicks) |
---|
304 | * |
---|
305 | * This only happens, if the HARD-Debug-level is set to 0,1,2, because otherwise a Segfault could |
---|
306 | * result in the loss of System-controll |
---|
307 | */ |
---|
308 | void GraphicsEngine::stealWMEvents(bool steal) |
---|
309 | { |
---|
310 | #if DEBUG < 3 |
---|
311 | if (steal) |
---|
312 | SDL_WM_GrabInput(SDL_GRAB_ON); |
---|
313 | else |
---|
314 | SDL_WM_GrabInput(SDL_GRAB_OFF); |
---|
315 | #endif |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * |
---|
320 | * @returns true if Events are stolen from the WM, false if not. |
---|
321 | */ |
---|
322 | bool GraphicsEngine::isStealingEvents() |
---|
323 | { |
---|
324 | if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON) |
---|
325 | return true; |
---|
326 | else |
---|
327 | return false; |
---|
328 | }; |
---|
329 | |
---|
330 | |
---|
331 | /** |
---|
332 | * entering 2D Mode |
---|
333 | |
---|
334 | this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else |
---|
335 | */ |
---|
336 | void GraphicsEngine::enter2DMode() |
---|
337 | { |
---|
338 | //GraphicsEngine::storeMatrices(); |
---|
339 | SDL_Surface *screen = SDL_GetVideoSurface(); |
---|
340 | |
---|
341 | /* Note, there may be other things you need to change, |
---|
342 | depending on how you have your OpenGL state set up. |
---|
343 | */ |
---|
344 | glPushAttrib(GL_ENABLE_BIT); |
---|
345 | glDisable(GL_DEPTH_TEST); |
---|
346 | glDisable(GL_CULL_FACE); |
---|
347 | glDisable(GL_LIGHTING); // will be set back when leaving 2D-mode |
---|
348 | |
---|
349 | glViewport(0, 0, screen->w, screen->h); |
---|
350 | |
---|
351 | glMatrixMode(GL_PROJECTION); |
---|
352 | glPushMatrix(); |
---|
353 | glLoadIdentity(); |
---|
354 | |
---|
355 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
---|
356 | |
---|
357 | glMatrixMode(GL_MODELVIEW); |
---|
358 | glPushMatrix(); |
---|
359 | glLoadIdentity(); |
---|
360 | |
---|
361 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | * leaves the 2DMode again also \see Font::enter2DMode() |
---|
366 | */ |
---|
367 | void GraphicsEngine::leave2DMode() |
---|
368 | { |
---|
369 | glMatrixMode(GL_PROJECTION); |
---|
370 | glPopMatrix(); |
---|
371 | |
---|
372 | glMatrixMode(GL_MODELVIEW); |
---|
373 | glPopMatrix(); |
---|
374 | |
---|
375 | glPopAttrib(); |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | * stores the GL_matrices |
---|
380 | */ |
---|
381 | void GraphicsEngine::storeMatrices() |
---|
382 | { |
---|
383 | glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat); |
---|
384 | glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat); |
---|
385 | glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort); |
---|
386 | } |
---|
387 | |
---|
388 | //! the stored ModelView Matrix. |
---|
389 | GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
390 | //! the stored Projection Matrix |
---|
391 | GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
392 | //! The ViewPort |
---|
393 | GLint GraphicsEngine::viewPort[4] = {0,0,0,0}; |
---|
394 | |
---|
395 | |
---|
396 | |
---|
397 | /** |
---|
398 | * outputs all the Fullscreen modes. |
---|
399 | */ |
---|
400 | void GraphicsEngine::listModes() |
---|
401 | { |
---|
402 | /* Get available fullscreen/hardware modes */ |
---|
403 | this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); |
---|
404 | |
---|
405 | /* Check is there are any modes available */ |
---|
406 | if(this->videoModes == (SDL_Rect **)0){ |
---|
407 | PRINTF(1)("No modes available!\n"); |
---|
408 | exit(-1); |
---|
409 | } |
---|
410 | |
---|
411 | /* Check if our resolution is restricted */ |
---|
412 | if(this->videoModes == (SDL_Rect **)-1){ |
---|
413 | PRINTF(2)("All resolutions available.\n"); |
---|
414 | } |
---|
415 | else{ |
---|
416 | /* Print valid modes */ |
---|
417 | PRINT(0)("Available Resoulution Modes are\n"); |
---|
418 | for(int i = 0; this->videoModes[i]; ++i) |
---|
419 | PRINT(4)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | * updates everything that is to be updated in the GraphicsEngine |
---|
425 | */ |
---|
426 | void GraphicsEngine::update(float dt) |
---|
427 | { |
---|
428 | NullElement2D::getInstance()->update2D(dt); |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | /** |
---|
433 | * ticks the Text |
---|
434 | * @param dt the time passed |
---|
435 | */ |
---|
436 | void GraphicsEngine::tick(float dt) |
---|
437 | { |
---|
438 | if( unlikely(this->bDisplayFPS)) |
---|
439 | { |
---|
440 | this->currentFPS = 1.0/dt; |
---|
441 | if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS; |
---|
442 | if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS; |
---|
443 | |
---|
444 | #ifndef NO_TEXT |
---|
445 | char tmpChar1[20]; |
---|
446 | sprintf(tmpChar1, "Current: %4.0f", this->currentFPS); |
---|
447 | this->geTextCFPS->setText(tmpChar1); |
---|
448 | char tmpChar2[20]; |
---|
449 | sprintf(tmpChar2, "Max: %4.0f", this->maxFPS); |
---|
450 | this->geTextMaxFPS->setText(tmpChar2); |
---|
451 | char tmpChar3[20]; |
---|
452 | sprintf(tmpChar3, "Min: %4.0f", this->minFPS); |
---|
453 | this->geTextMinFPS->setText(tmpChar3); |
---|
454 | #endif /* NO_TEXT */ |
---|
455 | |
---|
456 | |
---|
457 | } |
---|
458 | Render2D::getInstance()->tick(dt); |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | void GraphicsEngine::draw() const |
---|
463 | { |
---|
464 | GraphicsEngine::storeMatrices(); |
---|
465 | Render2D::getInstance()->draw(E2D_ALL_LAYERS); |
---|
466 | LightManager::getInstance()->draw(); |
---|
467 | } |
---|
468 | |
---|
469 | /** |
---|
470 | * displays the Frames per second |
---|
471 | * @param display if the text should be displayed |
---|
472 | |
---|
473 | @todo this is dangerous |
---|
474 | */ |
---|
475 | void GraphicsEngine::displayFPS(bool display) |
---|
476 | { |
---|
477 | if( display) |
---|
478 | { |
---|
479 | #ifndef NO_TEXT |
---|
480 | if (this->geTextCFPS == NULL) |
---|
481 | { |
---|
482 | this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC); |
---|
483 | this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
484 | this->geTextCFPS->setAbsCoor2D(5, 15); |
---|
485 | } |
---|
486 | if (this->geTextMaxFPS == NULL) |
---|
487 | { |
---|
488 | this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC); |
---|
489 | this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
490 | this->geTextMaxFPS->setAbsCoor2D(5, 40); |
---|
491 | } |
---|
492 | if (this->geTextMinFPS == NULL) |
---|
493 | { |
---|
494 | this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_RENDER_DYNAMIC); |
---|
495 | this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
496 | this->geTextMinFPS->setAbsCoor2D(5, 65); |
---|
497 | } |
---|
498 | #endif /* NO_TEXT */ |
---|
499 | } |
---|
500 | this->bDisplayFPS = display; |
---|
501 | } |
---|
502 | |
---|
503 | |
---|
504 | /** |
---|
505 | \brief processes the events for orxonox main class |
---|
506 | * @param the event to handle |
---|
507 | */ |
---|
508 | void GraphicsEngine::process(const Event &event) |
---|
509 | { |
---|
510 | switch (event.type) |
---|
511 | { |
---|
512 | case EV_VIDEO_RESIZE: |
---|
513 | this->resolutionChanged(event.resize); |
---|
514 | break; |
---|
515 | } |
---|
516 | |
---|
517 | } |
---|
518 | |
---|