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