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"
24 //#include "SDL/SDL.h"
25 //#include "SDL/SDL_opengl.h"
27 #include "supertux/gameconfig.hpp"
28 #include "supertux/globals.hpp"
29 #include "video/drawing_request.hpp"
30 #include "video/gl/gl_surface_data.hpp"
31 #include "video/gl/gl_texture.hpp"
32 #define LIGHTMAP_DIV 5
34 #ifdef GL_VERSION_ES_CM_1_0
35 # define glOrtho glOrthof
38 GLRenderer::GLRenderer() :
41 fullscreen_active(false),
42 last_texture(static_cast<GLuint> (-1))
44 Renderer::instance_ = this;
46 #if SDL_MAJOR_VERSION > 1 || SDL_MINOR_VERSION > 2 || (SDL_MINOR_VERSION == 2 && SDL_PATCHLEVEL >= 10)
47 // unfortunately only newer SDLs have these infos.
48 // This must be called before SDL_SetVideoMode() or it will return
49 // 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);
57 if(texture_manager != 0)
58 texture_manager->save_textures();
60 #ifdef SDL_GL_SWAP_CONTROL
61 if(config->try_vsync) {
62 /* we want vsync for smooth scrolling */
63 SDL_GL_SetSwapInterval(1);
67 SDL_GL_SetSwapInterval(SDL_GL_DOUBLEBUFFER, 1);
69 // FIXME: Hu? 16bit rendering?
70 SDL_GL_SetSwapInterval(5);
71 SDL_GL_SetSwapInterval(5);
72 SDL_GL_SetSwapInterval(5);
74 if(g_config->use_fullscreen)
76 apply_video_mode(g_config->fullscreen_size, true);
80 apply_video_mode(g_config->window_size, false);
83 // setup opengl state and transform
84 glDisable(GL_DEPTH_TEST);
85 glDisable(GL_CULL_FACE);
86 glEnable(GL_TEXTURE_2D);
88 glEnableClientState(GL_VERTEX_ARRAY);
89 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
90 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
92 // Init the projection matrix, viewport and stuff
95 if(texture_manager == 0)
96 texture_manager = new TextureManager();
98 texture_manager->reload_textures();
100 #ifndef GL_VERSION_ES_CM_1_0
101 GLenum err = glewInit();
104 std::ostringstream out;
105 out << "GLRenderer: " << glewGetErrorString(err);
106 throw std::runtime_error(out.str());
108 log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
109 log_info << "GLEW_ARB_texture_non_power_of_two: " << static_cast<int>(GLEW_ARB_texture_non_power_of_two) << std::endl;
113 GLRenderer::~GLRenderer()
118 GLRenderer::draw_surface(const DrawingRequest& request)
120 const Surface* surface = (const Surface*) request.request_data;
125 GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
126 if(gltexture == NULL)
130 GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
131 if(surface_data == NULL)
136 GLuint th = gltexture->get_handle();
137 if (th != last_texture) {
139 glBindTexture(GL_TEXTURE_2D, th);
141 intern_draw(request.pos.x, request.pos.y,
142 request.pos.x + surface->get_width(),
143 request.pos.y + surface->get_height(),
144 surface_data->get_uv_left(),
145 surface_data->get_uv_top(),
146 surface_data->get_uv_right(),
147 surface_data->get_uv_bottom(),
152 request.drawing_effect);
156 GLRenderer::draw_surface_part(const DrawingRequest& request)
158 const SurfacePartRequest* surfacepartrequest
159 = (SurfacePartRequest*) request.request_data;
160 const Surface* surface = surfacepartrequest->surface;
161 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
162 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
164 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
165 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
167 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
168 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
169 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
170 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
172 GLuint th = gltexture->get_handle();
173 if (th != last_texture) {
175 glBindTexture(GL_TEXTURE_2D, th);
177 intern_draw(request.pos.x, request.pos.y,
178 request.pos.x + surfacepartrequest->size.x,
179 request.pos.y + surfacepartrequest->size.y,
188 request.drawing_effect);
192 GLRenderer::draw_gradient(const DrawingRequest& request)
194 const GradientRequest* gradientrequest
195 = (GradientRequest*) request.request_data;
196 const Color& top = gradientrequest->top;
197 const Color& bottom = gradientrequest->bottom;
199 glDisable(GL_TEXTURE_2D);
200 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
201 glEnableClientState(GL_COLOR_ARRAY);
205 float(SCREEN_WIDTH), 0,
206 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
207 0, float(SCREEN_HEIGHT)
209 glVertexPointer(2, GL_FLOAT, 0, vertices);
212 top.red, top.green, top.blue, top.alpha,
213 top.red, top.green, top.blue, top.alpha,
214 bottom.red, bottom.green, bottom.blue, bottom.alpha,
215 bottom.red, bottom.green, bottom.blue, bottom.alpha,
217 glColorPointer(4, GL_FLOAT, 0, colors);
219 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
221 glDisableClientState(GL_COLOR_ARRAY);
222 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
224 glEnable(GL_TEXTURE_2D);
225 glColor4f(1, 1, 1, 1);
229 GLRenderer::draw_filled_rect(const DrawingRequest& request)
231 const FillRectRequest* fillrectrequest
232 = (FillRectRequest*) request.request_data;
234 glDisable(GL_TEXTURE_2D);
235 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
236 fillrectrequest->color.blue, fillrectrequest->color.alpha);
237 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
239 if (fillrectrequest->radius != 0.0f)
242 // Keep radius in the limits, so that we get a circle instead of
244 float radius = std::min(fillrectrequest->radius,
245 std::min(fillrectrequest->size.x/2,
246 fillrectrequest->size.y/2));
249 Rectf irect(request.pos.x + radius,
250 request.pos.y + radius,
251 request.pos.x + fillrectrequest->size.x - radius,
252 request.pos.y + fillrectrequest->size.y - radius);
256 std::vector<float> vertices((n+1) * 4 * 2);
258 for(int i = 0; i <= n; ++i)
260 float x = sinf(i * (M_PI/2) / n) * radius;
261 float y = cosf(i * (M_PI/2) / n) * radius;
263 vertices[p++] = irect.get_left() - x;
264 vertices[p++] = irect.get_top() - y;
266 vertices[p++] = irect.get_right() + x;
267 vertices[p++] = irect.get_top() - y;
270 for(int i = 0; i <= n; ++i)
272 float x = cosf(i * (M_PI/2) / n) * radius;
273 float y = sinf(i * (M_PI/2) / n) * radius;
275 vertices[p++] = irect.get_left() - x;
276 vertices[p++] = irect.get_bottom() + y;
278 vertices[p++] = irect.get_right() + x;
279 vertices[p++] = irect.get_bottom() + y;
282 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
283 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
287 float x = request.pos.x;
288 float y = request.pos.y;
289 float w = fillrectrequest->size.x;
290 float h = fillrectrequest->size.y;
298 glVertexPointer(2, GL_FLOAT, 0, vertices);
300 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
303 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
304 glEnable(GL_TEXTURE_2D);
305 glColor4f(1, 1, 1, 1);
309 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
311 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
313 glDisable(GL_TEXTURE_2D);
314 glColor4f(ellipse->color.red, ellipse->color.green,
315 ellipse->color.blue, ellipse->color.alpha);
317 float x = request.pos.x;
318 float y = request.pos.y;
319 float w = ellipse->size.x/2.0f;
320 float h = ellipse->size.y/2.0f;
322 static const int slices = 16;
323 static const int points = (slices+1) * 12;
325 float vertices[points * 2];
329 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
330 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
331 vertices[p++] = x; vertices[p++] = y+h;
334 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
335 vertices[p++] = 0; vertices[p++] = 0;
336 vertices[p++] = x; vertices[p++] = y-h;
339 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
340 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
341 vertices[p++] = x+w; vertices[p++] = y;
344 vertices[p++] = 0; vertices[p++] = 0;
345 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
346 vertices[p++] = x-w; vertices[p++] = y;
348 for(int i = 0; i < slices; ++i)
350 float ex1 = sinf(M_PI/2 / slices * i) * w;
351 float ey1 = cosf(M_PI/2 / slices * i) * h;
353 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
354 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
357 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
358 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
359 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
362 vertices[p++] = 0; vertices[p++] = 0;
363 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
364 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
367 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
368 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
369 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
372 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
373 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
374 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
377 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
378 glVertexPointer(2, GL_FLOAT, 0, vertices);
380 glDrawArrays(GL_TRIANGLES, 0, points);
382 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
384 glEnable(GL_TEXTURE_2D);
385 glColor4f(1, 1, 1, 1);
389 GLRenderer::do_take_screenshot()
391 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
393 SDL_Surface *shot_surf;
394 // create surface to hold screenshot
395 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
396 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
398 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
401 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
405 // read pixels into array
406 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
408 log_warning << "Could not allocate memory to store screenshot" << std::endl;
409 SDL_FreeSurface(shot_surf);
412 glPixelStorei(GL_PACK_ALIGNMENT, 1);
413 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
415 // copy array line-by-line
416 for (int i = 0; i < SCREEN_HEIGHT; i++) {
417 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
418 if(SDL_MUSTLOCK(shot_surf))
420 SDL_LockSurface(shot_surf);
422 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
423 memcpy(dst, src, 3 * SCREEN_WIDTH);
424 if(SDL_MUSTLOCK(shot_surf))
426 SDL_UnlockSurface(shot_surf);
434 static const std::string writeDir = PHYSFS_getWriteDir();
435 static const std::string dirSep = PHYSFS_getDirSeparator();
436 static const std::string baseName = "screenshot";
437 static const std::string fileExt = ".bmp";
438 std::string fullFilename;
439 for (int num = 0; num < 1000; num++) {
440 std::ostringstream oss;
442 oss << std::setw(3) << std::setfill('0') << num;
444 std::string fileName = oss.str();
445 fullFilename = writeDir + dirSep + fileName;
446 if (!PHYSFS_exists(fileName.c_str())) {
447 SDL_SaveBMP(shot_surf, fullFilename.c_str());
448 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
449 SDL_FreeSurface(shot_surf);
453 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
454 SDL_FreeSurface(shot_surf);
460 assert_gl("drawing");
461 SDL_GL_SwapWindow(screen)();
465 GLRenderer::resize(int w, int h)
467 // This causes the screen to go black, which is annoying, but seems
468 // unavoidable with SDL at the moment
469 SDL_CreateWindow(SDL_GL_CreateContext(w, h, 0, SDL_OPENGL | SDL_RESIZABLE));
471 g_config->window_size = Size(w, h);
477 GLRenderer::apply_config()
481 log_info << "Applying Config:"
482 << "\n Desktop: " << desktop_size.width << "x" << desktop_size.height
483 << "\n Window: " << g_config->window_size
484 << "\n FullRes: " << g_config->fullscreen_size
485 << "\n Aspect: " << g_config->aspect_size
486 << "\n Magnif: " << g_config->magnification
490 float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
491 if (g_config->aspect_size != Size(0, 0))
493 target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
496 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
497 if (desktop_size.width != -1 && desktop_size.height != -1)
499 desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
504 // Get the screen width
505 if (g_config->use_fullscreen)
507 screen_size = g_config->fullscreen_size;
508 desktop_aspect = float(screen_size.width) / float(screen_size.height);
512 screen_size = g_config->window_size;
515 apply_video_mode(screen_size, g_config->use_fullscreen);
517 if (target_aspect > 1.0f)
519 SCREEN_WIDTH = static_cast<int>(screen_size.width * (target_aspect / desktop_aspect));
520 SCREEN_HEIGHT = static_cast<int>(screen_size.height);
524 SCREEN_WIDTH = static_cast<int>(screen_size.width);
525 SCREEN_HEIGHT = static_cast<int>(screen_size.height * (target_aspect / desktop_aspect));
528 Size max_size(1280, 800);
529 Size min_size(640, 480);
531 if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
533 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
534 // max_size.width/max_size.height resp. min_size.width/min_size.height
535 if (SCREEN_WIDTH > max_size.width || SCREEN_HEIGHT > max_size.height)
537 float scale1 = float(max_size.width)/SCREEN_WIDTH;
538 float scale2 = float(max_size.height)/SCREEN_HEIGHT;
539 float scale = (scale1 < scale2) ? scale1 : scale2;
540 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
541 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
543 else if (SCREEN_WIDTH < min_size.width || SCREEN_HEIGHT < min_size.height)
545 float scale1 = float(min_size.width)/SCREEN_WIDTH;
546 float scale2 = float(min_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);
553 glViewport(0, 0, screen_size.width, screen_size.height);
557 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / g_config->magnification);
558 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
560 // This works by adding black borders around the screen to limit
561 // SCREEN_WIDTH/SCREEN_HEIGHT to max_size.width/max_size.height
562 Size new_size = screen_size;
564 if (SCREEN_WIDTH > max_size.width)
566 new_size.width = static_cast<int>((float) new_size.width * float(max_size.width)/SCREEN_WIDTH);
567 SCREEN_WIDTH = static_cast<int>(max_size.width);
570 if (SCREEN_HEIGHT > max_size.height)
572 new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
573 SCREEN_HEIGHT = static_cast<int>(max_size.height);
576 // Clear both buffers so that we get a clean black border without junk
577 glClear(GL_COLOR_BUFFER_BIT);
578 SDL_GL_SwapWindow(screen);
579 glClear(GL_COLOR_BUFFER_BIT);
580 SDL_GL_SwapWindow(screen);
582 glViewport(std::max(0, (screen_size.width - new_size.width) / 2),
583 std::max(0, (screen_size.height - new_size.height) / 2),
584 std::min(new_size.width, screen_size.width),
585 std::min(new_size.height, screen_size.height));
588 glMatrixMode(GL_PROJECTION);
591 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
593 glMatrixMode(GL_MODELVIEW);
595 glTranslatef(0, 0, 0);
596 check_gl_error("Setting up view matrices");
600 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
602 // Only change video mode when its different from the current one
603 if (screen_size != size || fullscreen_active != fullscreen)
605 int flags = SDL_OPENGL;
609 flags |= SDL_FULLSCREEN;
613 flags |= SDL_RESIZABLE;
616 if (SDL_Surface *screen = SDL_CreateWindow(SDL_GL_CreateContext( size.width, size.height, 0, flags)))
618 screen_size = Size(screen->w, screen->h);
619 fullscreen_active = fullscreen;
623 std::ostringstream msg;
624 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
625 throw std::runtime_error(msg.str());