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 #include "video/util.hpp"
32 #define LIGHTMAP_DIV 5
34 #ifdef GL_VERSION_ES_CM_1_0
35 # define glOrtho glOrthof
38 GLRenderer::GLRenderer() :
43 fullscreen_active(false),
44 last_texture(static_cast<GLuint> (-1))
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::draw_surface(const DrawingRequest& request)
114 const Surface* surface = (const Surface*) request.request_data;
119 GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
120 if(gltexture == NULL)
124 GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
125 if(surface_data == NULL)
130 GLuint th = gltexture->get_handle();
131 if (th != last_texture) {
133 glBindTexture(GL_TEXTURE_2D, th);
135 intern_draw(request.pos.x, request.pos.y,
136 request.pos.x + surface->get_width(),
137 request.pos.y + surface->get_height(),
138 surface_data->get_uv_left(),
139 surface_data->get_uv_top(),
140 surface_data->get_uv_right(),
141 surface_data->get_uv_bottom(),
146 request.drawing_effect);
150 GLRenderer::draw_surface_part(const DrawingRequest& request)
152 const SurfacePartRequest* surfacepartrequest
153 = (SurfacePartRequest*) request.request_data;
154 const Surface* surface = surfacepartrequest->surface;
155 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
156 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
158 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
159 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
161 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
162 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
163 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
164 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
166 GLuint th = gltexture->get_handle();
167 if (th != last_texture) {
169 glBindTexture(GL_TEXTURE_2D, th);
171 intern_draw(request.pos.x, request.pos.y,
172 request.pos.x + surfacepartrequest->size.x,
173 request.pos.y + surfacepartrequest->size.y,
182 request.drawing_effect);
186 GLRenderer::draw_gradient(const DrawingRequest& request)
188 const GradientRequest* gradientrequest
189 = (GradientRequest*) request.request_data;
190 const Color& top = gradientrequest->top;
191 const Color& bottom = gradientrequest->bottom;
193 glDisable(GL_TEXTURE_2D);
194 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
195 glEnableClientState(GL_COLOR_ARRAY);
199 float(SCREEN_WIDTH), 0,
200 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
201 0, float(SCREEN_HEIGHT)
203 glVertexPointer(2, GL_FLOAT, 0, vertices);
206 top.red, top.green, top.blue, top.alpha,
207 top.red, top.green, top.blue, top.alpha,
208 bottom.red, bottom.green, bottom.blue, bottom.alpha,
209 bottom.red, bottom.green, bottom.blue, bottom.alpha,
211 glColorPointer(4, GL_FLOAT, 0, colors);
213 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
215 glDisableClientState(GL_COLOR_ARRAY);
216 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
218 glEnable(GL_TEXTURE_2D);
219 glColor4f(1, 1, 1, 1);
223 GLRenderer::draw_filled_rect(const DrawingRequest& request)
225 const FillRectRequest* fillrectrequest
226 = (FillRectRequest*) request.request_data;
228 glDisable(GL_TEXTURE_2D);
229 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
230 fillrectrequest->color.blue, fillrectrequest->color.alpha);
231 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
233 if (fillrectrequest->radius != 0.0f)
236 // Keep radius in the limits, so that we get a circle instead of
238 float radius = std::min(fillrectrequest->radius,
239 std::min(fillrectrequest->size.x/2,
240 fillrectrequest->size.y/2));
243 Rectf irect(request.pos.x + radius,
244 request.pos.y + radius,
245 request.pos.x + fillrectrequest->size.x - radius,
246 request.pos.y + fillrectrequest->size.y - radius);
250 std::vector<float> vertices((n+1) * 4 * 2);
252 for(int i = 0; i <= n; ++i)
254 float x = sinf(i * (M_PI/2) / n) * radius;
255 float y = cosf(i * (M_PI/2) / n) * radius;
257 vertices[p++] = irect.get_left() - x;
258 vertices[p++] = irect.get_top() - y;
260 vertices[p++] = irect.get_right() + x;
261 vertices[p++] = irect.get_top() - y;
264 for(int i = 0; i <= n; ++i)
266 float x = cosf(i * (M_PI/2) / n) * radius;
267 float y = sinf(i * (M_PI/2) / n) * radius;
269 vertices[p++] = irect.get_left() - x;
270 vertices[p++] = irect.get_bottom() + y;
272 vertices[p++] = irect.get_right() + x;
273 vertices[p++] = irect.get_bottom() + y;
276 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
277 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
281 float x = request.pos.x;
282 float y = request.pos.y;
283 float w = fillrectrequest->size.x;
284 float h = fillrectrequest->size.y;
292 glVertexPointer(2, GL_FLOAT, 0, vertices);
294 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
297 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
298 glEnable(GL_TEXTURE_2D);
299 glColor4f(1, 1, 1, 1);
303 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
305 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
307 glDisable(GL_TEXTURE_2D);
308 glColor4f(ellipse->color.red, ellipse->color.green,
309 ellipse->color.blue, ellipse->color.alpha);
311 float x = request.pos.x;
312 float y = request.pos.y;
313 float w = ellipse->size.x/2.0f;
314 float h = ellipse->size.y/2.0f;
316 static const int slices = 16;
317 static const int points = (slices+1) * 12;
319 float vertices[points * 2];
323 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
324 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
325 vertices[p++] = x; vertices[p++] = y+h;
328 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
329 vertices[p++] = 0; vertices[p++] = 0;
330 vertices[p++] = x; vertices[p++] = y-h;
333 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
334 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
335 vertices[p++] = x+w; vertices[p++] = y;
338 vertices[p++] = 0; vertices[p++] = 0;
339 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
340 vertices[p++] = x-w; vertices[p++] = y;
342 for(int i = 0; i < slices; ++i)
344 float ex1 = sinf(M_PI/2 / slices * i) * w;
345 float ey1 = cosf(M_PI/2 / slices * i) * h;
347 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
348 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
351 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
352 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
353 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
356 vertices[p++] = 0; vertices[p++] = 0;
357 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
358 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
361 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
362 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
363 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
366 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
367 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
368 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
371 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
372 glVertexPointer(2, GL_FLOAT, 0, vertices);
374 glDrawArrays(GL_TRIANGLES, 0, points);
376 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
378 glEnable(GL_TEXTURE_2D);
379 glColor4f(1, 1, 1, 1);
383 GLRenderer::do_take_screenshot()
385 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
387 SDL_Surface *shot_surf;
388 // create surface to hold screenshot
389 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
390 shot_surf = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
392 shot_surf = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
395 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
399 // read pixels into array
400 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
402 log_warning << "Could not allocate memory to store screenshot" << std::endl;
403 SDL_FreeSurface(shot_surf);
406 glPixelStorei(GL_PACK_ALIGNMENT, 1);
407 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
409 // copy array line-by-line
410 for (int i = 0; i < SCREEN_HEIGHT; i++) {
411 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
412 if(SDL_MUSTLOCK(shot_surf))
414 SDL_LockSurface(shot_surf);
416 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
417 memcpy(dst, src, 3 * SCREEN_WIDTH);
418 if(SDL_MUSTLOCK(shot_surf))
420 SDL_UnlockSurface(shot_surf);
428 static const std::string writeDir = PHYSFS_getWriteDir();
429 static const std::string dirSep = PHYSFS_getDirSeparator();
430 static const std::string baseName = "screenshot";
431 static const std::string fileExt = ".bmp";
432 std::string fullFilename;
433 for (int num = 0; num < 1000; num++) {
434 std::ostringstream oss;
436 oss << std::setw(3) << std::setfill('0') << num;
438 std::string fileName = oss.str();
439 fullFilename = writeDir + dirSep + fileName;
440 if (!PHYSFS_exists(fileName.c_str())) {
441 SDL_SaveBMP(shot_surf, fullFilename.c_str());
442 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
443 SDL_FreeSurface(shot_surf);
447 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
448 SDL_FreeSurface(shot_surf);
454 assert_gl("drawing");
455 SDL_GL_SwapWindow(window);
459 GLRenderer::resize(int w, int h)
461 g_config->window_size = Size(w, h);
467 GLRenderer::apply_config()
471 Size target_size = g_config->use_fullscreen ?
472 ((g_config->fullscreen_size == Size(0, 0)) ? desktop_size : g_config->fullscreen_size) :
473 g_config->window_size;
475 float pixel_aspect_ratio = 1.0f;
476 if (g_config->aspect_size != Size(0, 0))
478 pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
479 g_config->aspect_size);
481 else if (g_config->use_fullscreen)
483 pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
487 Size max_size(1280, 800);
488 Size min_size(640, 480);
492 calculate_viewport(min_size, max_size, target_size,
494 g_config->magnification,
499 SCREEN_WIDTH = logical_size.width;
500 SCREEN_HEIGHT = logical_size.height;
502 if (viewport.x != 0 || viewport.y != 0)
504 // Clear both buffers so that we get a clean black border without junk
505 glClear(GL_COLOR_BUFFER_BIT);
506 SDL_GL_SwapWindow(window);
507 glClear(GL_COLOR_BUFFER_BIT);
508 SDL_GL_SwapWindow(window);
511 glViewport(viewport.x, viewport.y, viewport.w, viewport.h);
513 glMatrixMode(GL_PROJECTION);
516 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
518 glMatrixMode(GL_MODELVIEW);
520 glTranslatef(0, 0, 0);
521 check_gl_error("Setting up view matrices");
525 GLRenderer::apply_video_mode()
529 if (!g_config->use_fullscreen)
531 SDL_SetWindowFullscreen(window, 0);
535 if (g_config->fullscreen_size.width == 0 &&
536 g_config->fullscreen_size.height == 0)
538 if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP) != 0)
540 log_warning << "failed to switch to desktop fullscreen mode: "
541 << SDL_GetError() << std::endl;
545 log_info << "switched to desktop fullscreen mode" << std::endl;
550 SDL_DisplayMode mode;
551 mode.format = SDL_PIXELFORMAT_RGB888;
552 mode.w = g_config->fullscreen_size.width;
553 mode.h = g_config->fullscreen_size.height;
554 mode.refresh_rate = g_config->fullscreen_refresh_rate;
557 if (SDL_SetWindowDisplayMode(window, &mode) != 0)
559 log_warning << "failed to set display mode: "
560 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
561 << SDL_GetError() << std::endl;
565 if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) != 0)
567 log_warning << "failed to switch to fullscreen mode: "
568 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
569 << SDL_GetError() << std::endl;
573 log_info << "switched to fullscreen mode: "
574 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << std::endl;
582 int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
584 if (g_config->use_fullscreen)
586 if (g_config->fullscreen_size == Size(0, 0))
588 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
593 flags |= SDL_WINDOW_FULLSCREEN;
594 size.width = g_config->fullscreen_size.width;
595 size.height = g_config->fullscreen_size.height;
600 size = g_config->window_size;
603 window = SDL_CreateWindow("SuperTux",
604 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
605 size.width, size.height,
609 std::ostringstream msg;
610 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
611 throw std::runtime_error(msg.str());
615 glcontext = SDL_GL_CreateContext(window);
617 SCREEN_WIDTH = size.width;
618 SCREEN_HEIGHT = size.height;
620 fullscreen_active = g_config->use_fullscreen;
626 GLRenderer::to_logical(int physical_x, int physical_y)
628 return Vector(static_cast<float>(physical_x - viewport.x) * SCREEN_WIDTH / viewport.w,
629 static_cast<float>(physical_y - viewport.y) * SCREEN_HEIGHT / viewport.h);
633 GLRenderer::set_gamma(float gamma)
636 SDL_CalculateGammaRamp(gamma, ramp);
637 SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);