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_painter.hpp"
29 #include "video/gl/gl_surface_data.hpp"
30 #include "video/gl/gl_texture.hpp"
31 #include "video/util.hpp"
33 #define LIGHTMAP_DIV 5
35 #ifdef GL_VERSION_ES_CM_1_0
36 # define glOrtho glOrthof
39 GLRenderer::GLRenderer() :
44 fullscreen_active(false)
46 Renderer::instance_ = this;
49 SDL_GetCurrentDisplayMode(0, &mode);
50 desktop_size = Size(mode.w, mode.h);
52 if(texture_manager != 0)
53 texture_manager->save_textures();
55 if(g_config->try_vsync) {
56 /* we want vsync for smooth scrolling */
57 if (SDL_GL_SetSwapInterval(-1) != 0)
59 log_info << "no support for late swap tearing vsync: " << SDL_GetError() << std::endl;
60 if (SDL_GL_SetSwapInterval(1))
62 log_info << "no support for vsync: " << SDL_GetError() << std::endl;
67 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
69 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
70 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
71 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
75 // setup opengl state and transform
76 glDisable(GL_DEPTH_TEST);
77 glDisable(GL_CULL_FACE);
78 glEnable(GL_TEXTURE_2D);
80 glEnableClientState(GL_VERTEX_ARRAY);
81 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
82 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
84 // Init the projection matrix, viewport and stuff
87 if(texture_manager == 0)
88 texture_manager = new TextureManager();
90 texture_manager->reload_textures();
92 #ifndef GL_VERSION_ES_CM_1_0
93 GLenum err = glewInit();
96 std::ostringstream out;
97 out << "GLRenderer: " << glewGetErrorString(err);
98 throw std::runtime_error(out.str());
100 log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
101 log_info << "GLEW_ARB_texture_non_power_of_two: " << static_cast<int>(GLEW_ARB_texture_non_power_of_two) << std::endl;
105 GLRenderer::~GLRenderer()
107 SDL_GL_DeleteContext(glcontext);
108 SDL_DestroyWindow(window);
112 GLRenderer::do_take_screenshot()
114 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
116 SDL_Surface *shot_surf;
117 // create surface to hold screenshot
118 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
119 shot_surf = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
121 shot_surf = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
124 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
128 // read pixels into array
129 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
131 log_warning << "Could not allocate memory to store screenshot" << std::endl;
132 SDL_FreeSurface(shot_surf);
135 glPixelStorei(GL_PACK_ALIGNMENT, 1);
136 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
138 // copy array line-by-line
139 for (int i = 0; i < SCREEN_HEIGHT; i++) {
140 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
141 if(SDL_MUSTLOCK(shot_surf))
143 SDL_LockSurface(shot_surf);
145 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
146 memcpy(dst, src, 3 * SCREEN_WIDTH);
147 if(SDL_MUSTLOCK(shot_surf))
149 SDL_UnlockSurface(shot_surf);
157 static const std::string writeDir = PHYSFS_getWriteDir();
158 static const std::string dirSep = PHYSFS_getDirSeparator();
159 static const std::string baseName = "screenshot";
160 static const std::string fileExt = ".bmp";
161 std::string fullFilename;
162 for (int num = 0; num < 1000; num++) {
163 std::ostringstream oss;
165 oss << std::setw(3) << std::setfill('0') << num;
167 std::string fileName = oss.str();
168 fullFilename = writeDir + dirSep + fileName;
169 if (!PHYSFS_exists(fileName.c_str())) {
170 SDL_SaveBMP(shot_surf, fullFilename.c_str());
171 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
172 SDL_FreeSurface(shot_surf);
176 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
177 SDL_FreeSurface(shot_surf);
183 assert_gl("drawing");
184 SDL_GL_SwapWindow(window);
188 GLRenderer::resize(int w, int h)
190 g_config->window_size = Size(w, h);
196 GLRenderer::apply_config()
200 Size target_size = g_config->use_fullscreen ?
201 ((g_config->fullscreen_size == Size(0, 0)) ? desktop_size : g_config->fullscreen_size) :
202 g_config->window_size;
204 float pixel_aspect_ratio = 1.0f;
205 if (g_config->aspect_size != Size(0, 0))
207 pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
208 g_config->aspect_size);
210 else if (g_config->use_fullscreen)
212 pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
216 Size max_size(1280, 800);
217 Size min_size(640, 480);
221 calculate_viewport(min_size, max_size, target_size,
223 g_config->magnification,
228 SCREEN_WIDTH = logical_size.width;
229 SCREEN_HEIGHT = logical_size.height;
231 if (viewport.x != 0 || viewport.y != 0)
233 // Clear both buffers so that we get a clean black border without junk
234 glClear(GL_COLOR_BUFFER_BIT);
235 SDL_GL_SwapWindow(window);
236 glClear(GL_COLOR_BUFFER_BIT);
237 SDL_GL_SwapWindow(window);
240 glViewport(viewport.x, viewport.y, viewport.w, viewport.h);
242 glMatrixMode(GL_PROJECTION);
245 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
247 glMatrixMode(GL_MODELVIEW);
249 glTranslatef(0, 0, 0);
250 check_gl_error("Setting up view matrices");
254 GLRenderer::apply_video_mode()
258 if (!g_config->use_fullscreen)
260 SDL_SetWindowFullscreen(window, 0);
264 if (g_config->fullscreen_size.width == 0 &&
265 g_config->fullscreen_size.height == 0)
267 if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP) != 0)
269 log_warning << "failed to switch to desktop fullscreen mode: "
270 << SDL_GetError() << std::endl;
274 log_info << "switched to desktop fullscreen mode" << std::endl;
279 SDL_DisplayMode mode;
280 mode.format = SDL_PIXELFORMAT_RGB888;
281 mode.w = g_config->fullscreen_size.width;
282 mode.h = g_config->fullscreen_size.height;
283 mode.refresh_rate = g_config->fullscreen_refresh_rate;
286 if (SDL_SetWindowDisplayMode(window, &mode) != 0)
288 log_warning << "failed to set display mode: "
289 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
290 << SDL_GetError() << std::endl;
294 if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) != 0)
296 log_warning << "failed to switch to fullscreen mode: "
297 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
298 << SDL_GetError() << std::endl;
302 log_info << "switched to fullscreen mode: "
303 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << std::endl;
311 int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
313 if (g_config->use_fullscreen)
315 if (g_config->fullscreen_size == Size(0, 0))
317 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
322 flags |= SDL_WINDOW_FULLSCREEN;
323 size.width = g_config->fullscreen_size.width;
324 size.height = g_config->fullscreen_size.height;
329 size = g_config->window_size;
332 window = SDL_CreateWindow("SuperTux",
333 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
334 size.width, size.height,
338 std::ostringstream msg;
339 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
340 throw std::runtime_error(msg.str());
344 glcontext = SDL_GL_CreateContext(window);
346 SCREEN_WIDTH = size.width;
347 SCREEN_HEIGHT = size.height;
349 fullscreen_active = g_config->use_fullscreen;
355 GLRenderer::start_draw()
360 GLRenderer::end_draw()
365 GLRenderer::draw_surface(const DrawingRequest& request)
367 GLPainter::draw_surface(request);
371 GLRenderer::draw_surface_part(const DrawingRequest& request)
373 GLPainter::draw_surface_part(request);
377 GLRenderer::draw_gradient(const DrawingRequest& request)
379 GLPainter::draw_gradient(request);
383 GLRenderer::draw_filled_rect(const DrawingRequest& request)
385 GLPainter::draw_filled_rect(request);
389 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
391 GLPainter::draw_inverse_ellipse(request);
395 GLRenderer::to_logical(int physical_x, int physical_y)
397 return Vector(static_cast<float>(physical_x - viewport.x) * SCREEN_WIDTH / viewport.w,
398 static_cast<float>(physical_y - viewport.y) * SCREEN_HEIGHT / viewport.h);
402 GLRenderer::set_gamma(float gamma)
405 SDL_CalculateGammaRamp(gamma, ramp);
406 SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);