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;
45 #if SDL_MAJOR_VERSION > 1 || SDL_MINOR_VERSION > 2 || (SDL_MINOR_VERSION == 2 && SDL_PATCHLEVEL >= 10)
46 // unfortunately only newer SDLs have these infos.
47 // This must be called before SDL_SetVideoMode() or it will return
48 // the window size instead of the desktop size.
50 const SDL_VideoInfo *info = SDL_GetVideoInfo();
53 desktop_size = Size(info->current_w, info->current_h);
56 desktop_size = Size(1920, 1080);
60 if(texture_manager != 0)
61 texture_manager->save_textures();
63 if(g_config->try_vsync) {
64 /* we want vsync for smooth scrolling */
65 if (SDL_GL_SetSwapInterval(-1) != 0)
67 log_info << "no support for late swap tearing vsync: " << SDL_GetError() << std::endl;
68 if (SDL_GL_SetSwapInterval(1))
70 log_info << "no support for vsync: " << SDL_GetError() << std::endl;
75 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
77 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
78 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
79 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
81 if(g_config->use_fullscreen)
83 apply_video_mode(g_config->fullscreen_size, true);
87 apply_video_mode(g_config->window_size, false);
90 // setup opengl state and transform
91 glDisable(GL_DEPTH_TEST);
92 glDisable(GL_CULL_FACE);
93 glEnable(GL_TEXTURE_2D);
95 glEnableClientState(GL_VERTEX_ARRAY);
96 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
97 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99 // Init the projection matrix, viewport and stuff
102 if(texture_manager == 0)
103 texture_manager = new TextureManager();
105 texture_manager->reload_textures();
107 #ifndef GL_VERSION_ES_CM_1_0
108 GLenum err = glewInit();
111 std::ostringstream out;
112 out << "GLRenderer: " << glewGetErrorString(err);
113 throw std::runtime_error(out.str());
115 log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
116 log_info << "GLEW_ARB_texture_non_power_of_two: " << static_cast<int>(GLEW_ARB_texture_non_power_of_two) << std::endl;
120 GLRenderer::~GLRenderer()
122 SDL_GL_DeleteContext(glcontext);
123 SDL_DestroyWindow(window);
127 GLRenderer::draw_surface(const DrawingRequest& request)
129 const Surface* surface = (const Surface*) request.request_data;
134 GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
135 if(gltexture == NULL)
139 GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
140 if(surface_data == NULL)
145 GLuint th = gltexture->get_handle();
146 if (th != last_texture) {
148 glBindTexture(GL_TEXTURE_2D, th);
150 intern_draw(request.pos.x, request.pos.y,
151 request.pos.x + surface->get_width(),
152 request.pos.y + surface->get_height(),
153 surface_data->get_uv_left(),
154 surface_data->get_uv_top(),
155 surface_data->get_uv_right(),
156 surface_data->get_uv_bottom(),
161 request.drawing_effect);
165 GLRenderer::draw_surface_part(const DrawingRequest& request)
167 const SurfacePartRequest* surfacepartrequest
168 = (SurfacePartRequest*) request.request_data;
169 const Surface* surface = surfacepartrequest->surface;
170 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
171 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
173 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
174 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
176 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
177 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
178 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
179 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
181 GLuint th = gltexture->get_handle();
182 if (th != last_texture) {
184 glBindTexture(GL_TEXTURE_2D, th);
186 intern_draw(request.pos.x, request.pos.y,
187 request.pos.x + surfacepartrequest->size.x,
188 request.pos.y + surfacepartrequest->size.y,
197 request.drawing_effect);
201 GLRenderer::draw_gradient(const DrawingRequest& request)
203 const GradientRequest* gradientrequest
204 = (GradientRequest*) request.request_data;
205 const Color& top = gradientrequest->top;
206 const Color& bottom = gradientrequest->bottom;
208 glDisable(GL_TEXTURE_2D);
209 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
210 glEnableClientState(GL_COLOR_ARRAY);
214 float(SCREEN_WIDTH), 0,
215 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
216 0, float(SCREEN_HEIGHT)
218 glVertexPointer(2, GL_FLOAT, 0, vertices);
221 top.red, top.green, top.blue, top.alpha,
222 top.red, top.green, top.blue, top.alpha,
223 bottom.red, bottom.green, bottom.blue, bottom.alpha,
224 bottom.red, bottom.green, bottom.blue, bottom.alpha,
226 glColorPointer(4, GL_FLOAT, 0, colors);
228 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
230 glDisableClientState(GL_COLOR_ARRAY);
231 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
233 glEnable(GL_TEXTURE_2D);
234 glColor4f(1, 1, 1, 1);
238 GLRenderer::draw_filled_rect(const DrawingRequest& request)
240 const FillRectRequest* fillrectrequest
241 = (FillRectRequest*) request.request_data;
243 glDisable(GL_TEXTURE_2D);
244 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
245 fillrectrequest->color.blue, fillrectrequest->color.alpha);
246 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
248 if (fillrectrequest->radius != 0.0f)
251 // Keep radius in the limits, so that we get a circle instead of
253 float radius = std::min(fillrectrequest->radius,
254 std::min(fillrectrequest->size.x/2,
255 fillrectrequest->size.y/2));
258 Rectf irect(request.pos.x + radius,
259 request.pos.y + radius,
260 request.pos.x + fillrectrequest->size.x - radius,
261 request.pos.y + fillrectrequest->size.y - radius);
265 std::vector<float> vertices((n+1) * 4 * 2);
267 for(int i = 0; i <= n; ++i)
269 float x = sinf(i * (M_PI/2) / n) * radius;
270 float y = cosf(i * (M_PI/2) / n) * radius;
272 vertices[p++] = irect.get_left() - x;
273 vertices[p++] = irect.get_top() - y;
275 vertices[p++] = irect.get_right() + x;
276 vertices[p++] = irect.get_top() - y;
279 for(int i = 0; i <= n; ++i)
281 float x = cosf(i * (M_PI/2) / n) * radius;
282 float y = sinf(i * (M_PI/2) / n) * radius;
284 vertices[p++] = irect.get_left() - x;
285 vertices[p++] = irect.get_bottom() + y;
287 vertices[p++] = irect.get_right() + x;
288 vertices[p++] = irect.get_bottom() + y;
291 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
292 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
296 float x = request.pos.x;
297 float y = request.pos.y;
298 float w = fillrectrequest->size.x;
299 float h = fillrectrequest->size.y;
307 glVertexPointer(2, GL_FLOAT, 0, vertices);
309 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
312 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
313 glEnable(GL_TEXTURE_2D);
314 glColor4f(1, 1, 1, 1);
318 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
320 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
322 glDisable(GL_TEXTURE_2D);
323 glColor4f(ellipse->color.red, ellipse->color.green,
324 ellipse->color.blue, ellipse->color.alpha);
326 float x = request.pos.x;
327 float y = request.pos.y;
328 float w = ellipse->size.x/2.0f;
329 float h = ellipse->size.y/2.0f;
331 static const int slices = 16;
332 static const int points = (slices+1) * 12;
334 float vertices[points * 2];
338 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
339 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
340 vertices[p++] = x; vertices[p++] = y+h;
343 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
344 vertices[p++] = 0; vertices[p++] = 0;
345 vertices[p++] = x; vertices[p++] = y-h;
348 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
349 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
350 vertices[p++] = x+w; vertices[p++] = y;
353 vertices[p++] = 0; vertices[p++] = 0;
354 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
355 vertices[p++] = x-w; vertices[p++] = y;
357 for(int i = 0; i < slices; ++i)
359 float ex1 = sinf(M_PI/2 / slices * i) * w;
360 float ey1 = cosf(M_PI/2 / slices * i) * h;
362 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
363 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
366 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
367 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
368 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
371 vertices[p++] = 0; vertices[p++] = 0;
372 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
373 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
376 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
377 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
378 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
381 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
382 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
383 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
386 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
387 glVertexPointer(2, GL_FLOAT, 0, vertices);
389 glDrawArrays(GL_TRIANGLES, 0, points);
391 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
393 glEnable(GL_TEXTURE_2D);
394 glColor4f(1, 1, 1, 1);
398 GLRenderer::do_take_screenshot()
400 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
402 SDL_Surface *shot_surf;
403 // create surface to hold screenshot
404 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
405 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
407 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
410 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
414 // read pixels into array
415 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
417 log_warning << "Could not allocate memory to store screenshot" << std::endl;
418 SDL_FreeSurface(shot_surf);
421 glPixelStorei(GL_PACK_ALIGNMENT, 1);
422 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
424 // copy array line-by-line
425 for (int i = 0; i < SCREEN_HEIGHT; i++) {
426 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
427 if(SDL_MUSTLOCK(shot_surf))
429 SDL_LockSurface(shot_surf);
431 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
432 memcpy(dst, src, 3 * SCREEN_WIDTH);
433 if(SDL_MUSTLOCK(shot_surf))
435 SDL_UnlockSurface(shot_surf);
443 static const std::string writeDir = PHYSFS_getWriteDir();
444 static const std::string dirSep = PHYSFS_getDirSeparator();
445 static const std::string baseName = "screenshot";
446 static const std::string fileExt = ".bmp";
447 std::string fullFilename;
448 for (int num = 0; num < 1000; num++) {
449 std::ostringstream oss;
451 oss << std::setw(3) << std::setfill('0') << num;
453 std::string fileName = oss.str();
454 fullFilename = writeDir + dirSep + fileName;
455 if (!PHYSFS_exists(fileName.c_str())) {
456 SDL_SaveBMP(shot_surf, fullFilename.c_str());
457 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
458 SDL_FreeSurface(shot_surf);
462 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
463 SDL_FreeSurface(shot_surf);
469 assert_gl("drawing");
470 SDL_GL_SwapWindow(window);
474 GLRenderer::resize(int w, int h)
476 g_config->window_size = Size(w, h);
478 PHYSICAL_SCREEN_WIDTH = w;
479 PHYSICAL_SCREEN_HEIGHT = h;
485 GLRenderer::apply_config()
489 log_info << "Applying Config:"
490 << "\n Desktop: " << desktop_size.width << "x" << desktop_size.height
491 << "\n Window: " << g_config->window_size
492 << "\n FullRes: " << g_config->fullscreen_size
493 << "\n Aspect: " << g_config->aspect_size
494 << "\n Magnif: " << g_config->magnification
498 float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
499 if (g_config->aspect_size != Size(0, 0))
501 target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
504 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
505 if (desktop_size.width != -1 && desktop_size.height != -1)
507 desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
512 // Get the screen width
513 if (g_config->use_fullscreen)
515 screen_size = g_config->fullscreen_size;
516 desktop_aspect = float(screen_size.width) / float(screen_size.height);
520 screen_size = g_config->window_size;
523 apply_video_mode(screen_size, g_config->use_fullscreen);
525 if (target_aspect > 1.0f)
527 SCREEN_WIDTH = static_cast<int>(screen_size.width * (target_aspect / desktop_aspect));
528 SCREEN_HEIGHT = static_cast<int>(screen_size.height);
532 SCREEN_WIDTH = static_cast<int>(screen_size.width);
533 SCREEN_HEIGHT = static_cast<int>(screen_size.height * (target_aspect / desktop_aspect));
536 Size max_size(1280, 800);
537 Size min_size(640, 480);
539 if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
541 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
542 // max_size.width/max_size.height resp. min_size.width/min_size.height
543 if (SCREEN_WIDTH > max_size.width || SCREEN_HEIGHT > max_size.height)
545 float scale1 = float(max_size.width)/SCREEN_WIDTH;
546 float scale2 = float(max_size.height)/SCREEN_HEIGHT;
547 float scale = (scale1 < scale2) ? scale1 : scale2;
548 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
549 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
551 else if (SCREEN_WIDTH < min_size.width || SCREEN_HEIGHT < min_size.height)
553 float scale1 = float(min_size.width)/SCREEN_WIDTH;
554 float scale2 = float(min_size.height)/SCREEN_HEIGHT;
555 float scale = (scale1 < scale2) ? scale1 : scale2;
556 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
557 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
561 glViewport(0, 0, screen_size.width, screen_size.height);
565 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / g_config->magnification);
566 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
568 // This works by adding black borders around the screen to limit
569 // SCREEN_WIDTH/SCREEN_HEIGHT to max_size.width/max_size.height
570 Size new_size = screen_size;
572 if (SCREEN_WIDTH > max_size.width)
574 new_size.width = static_cast<int>((float) new_size.width * float(max_size.width)/SCREEN_WIDTH);
575 SCREEN_WIDTH = static_cast<int>(max_size.width);
578 if (SCREEN_HEIGHT > max_size.height)
580 new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
581 SCREEN_HEIGHT = static_cast<int>(max_size.height);
584 // Clear both buffers so that we get a clean black border without junk
585 glClear(GL_COLOR_BUFFER_BIT);
586 SDL_GL_SwapWindow(window);
587 glClear(GL_COLOR_BUFFER_BIT);
588 SDL_GL_SwapWindow(window);
590 glViewport(std::max(0, (screen_size.width - new_size.width) / 2),
591 std::max(0, (screen_size.height - new_size.height) / 2),
592 std::min(new_size.width, screen_size.width),
593 std::min(new_size.height, screen_size.height));
596 glMatrixMode(GL_PROJECTION);
599 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
601 glMatrixMode(GL_MODELVIEW);
603 glTranslatef(0, 0, 0);
604 check_gl_error("Setting up view matrices");
608 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
612 SDL_SetWindowSize(window, size.width, size.height);
616 int fullscreen_flags = SDL_WINDOW_FULLSCREEN; // SDL_WINDOW_FULLSCREEN_DESKTOP or 0
617 SDL_SetWindowDisplayMode(window, NULL);
618 SDL_SetWindowFullscreen(window, fullscreen_flags);
622 SDL_SetWindowFullscreen(window, 0);
627 // Only change video mode when its different from the current one
628 if (screen_size != size || fullscreen_active != fullscreen)
630 int flags = SDL_WINDOW_OPENGL;
634 flags |= SDL_WINDOW_FULLSCREEN;
638 flags |= SDL_WINDOW_RESIZABLE;
641 window = SDL_CreateWindow("SuperTux",
642 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
643 size.width, size.height,
647 std::ostringstream msg;
648 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
649 throw std::runtime_error(msg.str());
653 glcontext = SDL_GL_CreateContext(window);
656 PHYSICAL_SCREEN_WIDTH = size.width;
657 PHYSICAL_SCREEN_HEIGHT = size.height;
659 SCREEN_WIDTH = size.width;
660 SCREEN_HEIGHT = size.height;
662 fullscreen_active = fullscreen;
669 GLRenderer::set_gamma(float gamma)
672 SDL_CalculateGammaRamp(gamma, ramp);
673 SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);