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