2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 // Updated by GiBy 2013 for SDL2 <giby_the_kid@yahoo.fr>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "video/gl/gl_renderer.hpp"
25 #include "supertux/gameconfig.hpp"
26 #include "supertux/globals.hpp"
27 #include "video/drawing_request.hpp"
28 #include "video/gl/gl_surface_data.hpp"
29 #include "video/gl/gl_texture.hpp"
30 #define LIGHTMAP_DIV 5
32 #ifdef GL_VERSION_ES_CM_1_0
33 # define glOrtho glOrthof
36 GLRenderer::GLRenderer() :
40 fullscreen_active(false),
41 last_texture(static_cast<GLuint> (-1))
43 Renderer::instance_ = this;
46 SDL_GetCurrentDisplayMode(0, &mode);
47 desktop_size = Size(mode.w, mode.h);
49 if(texture_manager != 0)
50 texture_manager->save_textures();
52 if(g_config->try_vsync) {
53 /* we want vsync for smooth scrolling */
54 if (SDL_GL_SetSwapInterval(-1) != 0)
56 log_info << "no support for late swap tearing vsync: " << SDL_GetError() << std::endl;
57 if (SDL_GL_SetSwapInterval(1))
59 log_info << "no support for vsync: " << SDL_GetError() << std::endl;
64 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
66 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
67 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
68 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
70 if(g_config->use_fullscreen)
72 apply_video_mode(g_config->fullscreen_size, true);
76 apply_video_mode(g_config->window_size, false);
79 // setup opengl state and transform
80 glDisable(GL_DEPTH_TEST);
81 glDisable(GL_CULL_FACE);
82 glEnable(GL_TEXTURE_2D);
84 glEnableClientState(GL_VERTEX_ARRAY);
85 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
86 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
88 // Init the projection matrix, viewport and stuff
91 if(texture_manager == 0)
92 texture_manager = new TextureManager();
94 texture_manager->reload_textures();
96 #ifndef GL_VERSION_ES_CM_1_0
97 GLenum err = glewInit();
100 std::ostringstream out;
101 out << "GLRenderer: " << glewGetErrorString(err);
102 throw std::runtime_error(out.str());
104 log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
105 log_info << "GLEW_ARB_texture_non_power_of_two: " << static_cast<int>(GLEW_ARB_texture_non_power_of_two) << std::endl;
109 GLRenderer::~GLRenderer()
111 SDL_GL_DeleteContext(glcontext);
112 SDL_DestroyWindow(window);
116 GLRenderer::draw_surface(const DrawingRequest& request)
118 const Surface* surface = (const Surface*) request.request_data;
123 GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
124 if(gltexture == NULL)
128 GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
129 if(surface_data == NULL)
134 GLuint th = gltexture->get_handle();
135 if (th != last_texture) {
137 glBindTexture(GL_TEXTURE_2D, th);
139 intern_draw(request.pos.x, request.pos.y,
140 request.pos.x + surface->get_width(),
141 request.pos.y + surface->get_height(),
142 surface_data->get_uv_left(),
143 surface_data->get_uv_top(),
144 surface_data->get_uv_right(),
145 surface_data->get_uv_bottom(),
150 request.drawing_effect);
154 GLRenderer::draw_surface_part(const DrawingRequest& request)
156 const SurfacePartRequest* surfacepartrequest
157 = (SurfacePartRequest*) request.request_data;
158 const Surface* surface = surfacepartrequest->surface;
159 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
160 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
162 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
163 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
165 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
166 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
167 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
168 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
170 GLuint th = gltexture->get_handle();
171 if (th != last_texture) {
173 glBindTexture(GL_TEXTURE_2D, th);
175 intern_draw(request.pos.x, request.pos.y,
176 request.pos.x + surfacepartrequest->size.x,
177 request.pos.y + surfacepartrequest->size.y,
186 request.drawing_effect);
190 GLRenderer::draw_gradient(const DrawingRequest& request)
192 const GradientRequest* gradientrequest
193 = (GradientRequest*) request.request_data;
194 const Color& top = gradientrequest->top;
195 const Color& bottom = gradientrequest->bottom;
197 glDisable(GL_TEXTURE_2D);
198 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
199 glEnableClientState(GL_COLOR_ARRAY);
203 float(SCREEN_WIDTH), 0,
204 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
205 0, float(SCREEN_HEIGHT)
207 glVertexPointer(2, GL_FLOAT, 0, vertices);
210 top.red, top.green, top.blue, top.alpha,
211 top.red, top.green, top.blue, top.alpha,
212 bottom.red, bottom.green, bottom.blue, bottom.alpha,
213 bottom.red, bottom.green, bottom.blue, bottom.alpha,
215 glColorPointer(4, GL_FLOAT, 0, colors);
217 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
219 glDisableClientState(GL_COLOR_ARRAY);
220 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
222 glEnable(GL_TEXTURE_2D);
223 glColor4f(1, 1, 1, 1);
227 GLRenderer::draw_filled_rect(const DrawingRequest& request)
229 const FillRectRequest* fillrectrequest
230 = (FillRectRequest*) request.request_data;
232 glDisable(GL_TEXTURE_2D);
233 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
234 fillrectrequest->color.blue, fillrectrequest->color.alpha);
235 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
237 if (fillrectrequest->radius != 0.0f)
240 // Keep radius in the limits, so that we get a circle instead of
242 float radius = std::min(fillrectrequest->radius,
243 std::min(fillrectrequest->size.x/2,
244 fillrectrequest->size.y/2));
247 Rectf irect(request.pos.x + radius,
248 request.pos.y + radius,
249 request.pos.x + fillrectrequest->size.x - radius,
250 request.pos.y + fillrectrequest->size.y - radius);
254 std::vector<float> vertices((n+1) * 4 * 2);
256 for(int i = 0; i <= n; ++i)
258 float x = sinf(i * (M_PI/2) / n) * radius;
259 float y = cosf(i * (M_PI/2) / n) * radius;
261 vertices[p++] = irect.get_left() - x;
262 vertices[p++] = irect.get_top() - y;
264 vertices[p++] = irect.get_right() + x;
265 vertices[p++] = irect.get_top() - y;
268 for(int i = 0; i <= n; ++i)
270 float x = cosf(i * (M_PI/2) / n) * radius;
271 float y = sinf(i * (M_PI/2) / n) * radius;
273 vertices[p++] = irect.get_left() - x;
274 vertices[p++] = irect.get_bottom() + y;
276 vertices[p++] = irect.get_right() + x;
277 vertices[p++] = irect.get_bottom() + y;
280 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
281 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
285 float x = request.pos.x;
286 float y = request.pos.y;
287 float w = fillrectrequest->size.x;
288 float h = fillrectrequest->size.y;
296 glVertexPointer(2, GL_FLOAT, 0, vertices);
298 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
301 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
302 glEnable(GL_TEXTURE_2D);
303 glColor4f(1, 1, 1, 1);
307 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
309 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
311 glDisable(GL_TEXTURE_2D);
312 glColor4f(ellipse->color.red, ellipse->color.green,
313 ellipse->color.blue, ellipse->color.alpha);
315 float x = request.pos.x;
316 float y = request.pos.y;
317 float w = ellipse->size.x/2.0f;
318 float h = ellipse->size.y/2.0f;
320 static const int slices = 16;
321 static const int points = (slices+1) * 12;
323 float vertices[points * 2];
327 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
328 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
329 vertices[p++] = x; vertices[p++] = y+h;
332 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
333 vertices[p++] = 0; vertices[p++] = 0;
334 vertices[p++] = x; vertices[p++] = y-h;
337 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
338 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
339 vertices[p++] = x+w; vertices[p++] = y;
342 vertices[p++] = 0; vertices[p++] = 0;
343 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
344 vertices[p++] = x-w; vertices[p++] = y;
346 for(int i = 0; i < slices; ++i)
348 float ex1 = sinf(M_PI/2 / slices * i) * w;
349 float ey1 = cosf(M_PI/2 / slices * i) * h;
351 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
352 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
355 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
356 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
357 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
360 vertices[p++] = 0; vertices[p++] = 0;
361 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
362 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
365 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
366 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
367 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
370 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
371 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
372 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
375 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
376 glVertexPointer(2, GL_FLOAT, 0, vertices);
378 glDrawArrays(GL_TRIANGLES, 0, points);
380 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
382 glEnable(GL_TEXTURE_2D);
383 glColor4f(1, 1, 1, 1);
387 GLRenderer::do_take_screenshot()
389 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
391 SDL_Surface *shot_surf;
392 // create surface to hold screenshot
393 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
394 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
396 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
399 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
403 // read pixels into array
404 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
406 log_warning << "Could not allocate memory to store screenshot" << std::endl;
407 SDL_FreeSurface(shot_surf);
410 glPixelStorei(GL_PACK_ALIGNMENT, 1);
411 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
413 // copy array line-by-line
414 for (int i = 0; i < SCREEN_HEIGHT; i++) {
415 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
416 if(SDL_MUSTLOCK(shot_surf))
418 SDL_LockSurface(shot_surf);
420 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
421 memcpy(dst, src, 3 * SCREEN_WIDTH);
422 if(SDL_MUSTLOCK(shot_surf))
424 SDL_UnlockSurface(shot_surf);
432 static const std::string writeDir = PHYSFS_getWriteDir();
433 static const std::string dirSep = PHYSFS_getDirSeparator();
434 static const std::string baseName = "screenshot";
435 static const std::string fileExt = ".bmp";
436 std::string fullFilename;
437 for (int num = 0; num < 1000; num++) {
438 std::ostringstream oss;
440 oss << std::setw(3) << std::setfill('0') << num;
442 std::string fileName = oss.str();
443 fullFilename = writeDir + dirSep + fileName;
444 if (!PHYSFS_exists(fileName.c_str())) {
445 SDL_SaveBMP(shot_surf, fullFilename.c_str());
446 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
447 SDL_FreeSurface(shot_surf);
451 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
452 SDL_FreeSurface(shot_surf);
458 assert_gl("drawing");
459 SDL_GL_SwapWindow(window);
463 GLRenderer::resize(int w, int h)
465 g_config->window_size = Size(w, h);
467 PHYSICAL_SCREEN_WIDTH = w;
468 PHYSICAL_SCREEN_HEIGHT = h;
474 GLRenderer::apply_config()
478 log_info << "Applying Config:"
479 << "\n Desktop: " << desktop_size.width << "x" << desktop_size.height
480 << "\n Window: " << g_config->window_size
481 << "\n FullRes: " << g_config->fullscreen_size
482 << "\n Aspect: " << g_config->aspect_size
483 << "\n Magnif: " << g_config->magnification
487 float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
488 if (g_config->aspect_size != Size(0, 0))
490 target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
493 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
494 if (desktop_size.width != -1 && desktop_size.height != -1)
496 desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
501 // Get the screen width
502 if (g_config->use_fullscreen)
504 screen_size = g_config->fullscreen_size;
505 desktop_aspect = float(screen_size.width) / float(screen_size.height);
509 screen_size = g_config->window_size;
512 apply_video_mode(screen_size, g_config->use_fullscreen);
514 if (target_aspect > 1.0f)
516 SCREEN_WIDTH = static_cast<int>(screen_size.width * (target_aspect / desktop_aspect));
517 SCREEN_HEIGHT = static_cast<int>(screen_size.height);
521 SCREEN_WIDTH = static_cast<int>(screen_size.width);
522 SCREEN_HEIGHT = static_cast<int>(screen_size.height * (target_aspect / desktop_aspect));
525 Size max_size(1280, 800);
526 Size min_size(640, 480);
528 if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
530 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
531 // max_size.width/max_size.height resp. min_size.width/min_size.height
532 if (SCREEN_WIDTH > max_size.width || SCREEN_HEIGHT > max_size.height)
534 float scale1 = float(max_size.width)/SCREEN_WIDTH;
535 float scale2 = float(max_size.height)/SCREEN_HEIGHT;
536 float scale = (scale1 < scale2) ? scale1 : scale2;
537 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
538 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
540 else if (SCREEN_WIDTH < min_size.width || SCREEN_HEIGHT < min_size.height)
542 float scale1 = float(min_size.width)/SCREEN_WIDTH;
543 float scale2 = float(min_size.height)/SCREEN_HEIGHT;
544 float scale = (scale1 < scale2) ? scale1 : scale2;
545 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
546 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
550 glViewport(0, 0, screen_size.width, screen_size.height);
554 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / g_config->magnification);
555 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
557 // This works by adding black borders around the screen to limit
558 // SCREEN_WIDTH/SCREEN_HEIGHT to max_size.width/max_size.height
559 Size new_size = screen_size;
561 if (SCREEN_WIDTH > max_size.width)
563 new_size.width = static_cast<int>((float) new_size.width * float(max_size.width)/SCREEN_WIDTH);
564 SCREEN_WIDTH = static_cast<int>(max_size.width);
567 if (SCREEN_HEIGHT > max_size.height)
569 new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
570 SCREEN_HEIGHT = static_cast<int>(max_size.height);
573 // Clear both buffers so that we get a clean black border without junk
574 glClear(GL_COLOR_BUFFER_BIT);
575 SDL_GL_SwapWindow(window);
576 glClear(GL_COLOR_BUFFER_BIT);
577 SDL_GL_SwapWindow(window);
579 glViewport(std::max(0, (screen_size.width - new_size.width) / 2),
580 std::max(0, (screen_size.height - new_size.height) / 2),
581 std::min(new_size.width, screen_size.width),
582 std::min(new_size.height, screen_size.height));
585 glMatrixMode(GL_PROJECTION);
588 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
590 glMatrixMode(GL_MODELVIEW);
592 glTranslatef(0, 0, 0);
593 check_gl_error("Setting up view matrices");
597 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
601 SDL_SetWindowSize(window, size.width, size.height);
605 int fullscreen_flags = SDL_WINDOW_FULLSCREEN; // SDL_WINDOW_FULLSCREEN_DESKTOP or 0
606 SDL_SetWindowDisplayMode(window, NULL);
608 SDL_DisplayMode mode;
609 mode.format = SDL_PIXELFORMAT_RGB888;
610 mode.w = g_config->fullscreen_size.width;
611 mode.h = g_config->fullscreen_size.height;
612 mode.refresh_rate = g_config->fullscreen_refresh_rate;
615 if (SDL_SetWindowDisplayMode(window, &mode) != 0)
617 log_warning << "failed to set display mode: "
618 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
619 << SDL_GetError() << std::endl;
623 SDL_SetWindowFullscreen(window, fullscreen_flags);
628 SDL_SetWindowFullscreen(window, 0);
633 // Only change video mode when its different from the current one
634 if (screen_size != size || fullscreen_active != fullscreen)
636 int flags = SDL_WINDOW_OPENGL;
640 flags |= SDL_WINDOW_FULLSCREEN;
644 flags |= SDL_WINDOW_RESIZABLE;
647 window = SDL_CreateWindow("SuperTux",
648 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
649 size.width, size.height,
653 std::ostringstream msg;
654 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
655 throw std::runtime_error(msg.str());
659 glcontext = SDL_GL_CreateContext(window);
662 PHYSICAL_SCREEN_WIDTH = size.width;
663 PHYSICAL_SCREEN_HEIGHT = size.height;
665 SCREEN_WIDTH = size.width;
666 SCREEN_HEIGHT = size.height;
668 fullscreen_active = fullscreen;
675 GLRenderer::set_gamma(float gamma)
678 SDL_CalculateGammaRamp(gamma, ramp);
679 SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);