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;
121 GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
122 GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
124 GLuint th = gltexture->get_handle();
125 if (th != last_texture) {
127 glBindTexture(GL_TEXTURE_2D, th);
129 intern_draw(request.pos.x, request.pos.y,
130 request.pos.x + surface->get_width(),
131 request.pos.y + surface->get_height(),
132 surface_data->get_uv_left(),
133 surface_data->get_uv_top(),
134 surface_data->get_uv_right(),
135 surface_data->get_uv_bottom(),
140 request.drawing_effect);
144 GLRenderer::draw_surface_part(const DrawingRequest& request)
146 const SurfacePartRequest* surfacepartrequest
147 = (SurfacePartRequest*) request.request_data;
148 const Surface* surface = surfacepartrequest->surface;
149 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
150 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
152 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
153 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
155 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
156 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
157 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
158 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
160 GLuint th = gltexture->get_handle();
161 if (th != last_texture) {
163 glBindTexture(GL_TEXTURE_2D, th);
165 intern_draw(request.pos.x, request.pos.y,
166 request.pos.x + surfacepartrequest->size.x,
167 request.pos.y + surfacepartrequest->size.y,
176 request.drawing_effect);
180 GLRenderer::draw_gradient(const DrawingRequest& request)
182 const GradientRequest* gradientrequest
183 = (GradientRequest*) request.request_data;
184 const Color& top = gradientrequest->top;
185 const Color& bottom = gradientrequest->bottom;
187 glDisable(GL_TEXTURE_2D);
188 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
189 glEnableClientState(GL_COLOR_ARRAY);
193 float(SCREEN_WIDTH), 0,
194 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
195 0, float(SCREEN_HEIGHT)
197 glVertexPointer(2, GL_FLOAT, 0, vertices);
200 top.red, top.green, top.blue, top.alpha,
201 top.red, top.green, top.blue, top.alpha,
202 bottom.red, bottom.green, bottom.blue, bottom.alpha,
203 bottom.red, bottom.green, bottom.blue, bottom.alpha,
205 glColorPointer(4, GL_FLOAT, 0, colors);
207 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
209 glDisableClientState(GL_COLOR_ARRAY);
210 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
212 glEnable(GL_TEXTURE_2D);
213 glColor4f(1, 1, 1, 1);
217 GLRenderer::draw_filled_rect(const DrawingRequest& request)
219 const FillRectRequest* fillrectrequest
220 = (FillRectRequest*) request.request_data;
222 glDisable(GL_TEXTURE_2D);
223 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
224 fillrectrequest->color.blue, fillrectrequest->color.alpha);
225 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
227 if (fillrectrequest->radius != 0.0f)
230 // Keep radius in the limits, so that we get a circle instead of
232 float radius = std::min(fillrectrequest->radius,
233 std::min(fillrectrequest->size.x/2,
234 fillrectrequest->size.y/2));
237 Rectf irect(request.pos.x + radius,
238 request.pos.y + radius,
239 request.pos.x + fillrectrequest->size.x - radius,
240 request.pos.y + fillrectrequest->size.y - radius);
244 std::vector<float> vertices((n+1) * 4 * 2);
246 for(int i = 0; i <= n; ++i)
248 float x = sinf(i * (M_PI/2) / n) * radius;
249 float y = cosf(i * (M_PI/2) / n) * radius;
251 vertices[p++] = irect.get_left() - x;
252 vertices[p++] = irect.get_top() - y;
254 vertices[p++] = irect.get_right() + x;
255 vertices[p++] = irect.get_top() - y;
258 for(int i = 0; i <= n; ++i)
260 float x = cosf(i * (M_PI/2) / n) * radius;
261 float y = sinf(i * (M_PI/2) / n) * radius;
263 vertices[p++] = irect.get_left() - x;
264 vertices[p++] = irect.get_bottom() + y;
266 vertices[p++] = irect.get_right() + x;
267 vertices[p++] = irect.get_bottom() + y;
270 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
271 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
275 float x = request.pos.x;
276 float y = request.pos.y;
277 float w = fillrectrequest->size.x;
278 float h = fillrectrequest->size.y;
286 glVertexPointer(2, GL_FLOAT, 0, vertices);
288 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
291 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
292 glEnable(GL_TEXTURE_2D);
293 glColor4f(1, 1, 1, 1);
297 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
299 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
301 glDisable(GL_TEXTURE_2D);
302 glColor4f(ellipse->color.red, ellipse->color.green,
303 ellipse->color.blue, ellipse->color.alpha);
305 float x = request.pos.x;
306 float y = request.pos.y;
307 float w = ellipse->size.x/2.0f;
308 float h = ellipse->size.y/2.0f;
310 static const int slices = 16;
311 static const int points = (slices+1) * 12;
313 float vertices[points * 2];
317 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
318 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
319 vertices[p++] = x; vertices[p++] = y+h;
322 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
323 vertices[p++] = 0; vertices[p++] = 0;
324 vertices[p++] = x; vertices[p++] = y-h;
327 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
328 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
329 vertices[p++] = x+w; vertices[p++] = y;
332 vertices[p++] = 0; vertices[p++] = 0;
333 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
334 vertices[p++] = x-w; vertices[p++] = y;
336 for(int i = 0; i < slices; ++i)
338 float ex1 = sinf(M_PI/2 / slices * i) * w;
339 float ey1 = cosf(M_PI/2 / slices * i) * h;
341 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
342 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
345 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
346 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
347 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
350 vertices[p++] = 0; vertices[p++] = 0;
351 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
352 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
355 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
356 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
357 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
360 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
361 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
362 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
365 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
366 glVertexPointer(2, GL_FLOAT, 0, vertices);
368 glDrawArrays(GL_TRIANGLES, 0, points);
370 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
372 glEnable(GL_TEXTURE_2D);
373 glColor4f(1, 1, 1, 1);
377 GLRenderer::do_take_screenshot()
379 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
381 SDL_Surface *shot_surf;
382 // create surface to hold screenshot
383 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
384 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
386 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
389 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
393 // read pixels into array
394 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
396 log_warning << "Could not allocate memory to store screenshot" << std::endl;
397 SDL_FreeSurface(shot_surf);
400 glPixelStorei(GL_PACK_ALIGNMENT, 1);
401 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
403 // copy array line-by-line
404 for (int i = 0; i < SCREEN_HEIGHT; i++) {
405 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
406 if(SDL_MUSTLOCK(shot_surf))
408 SDL_LockSurface(shot_surf);
410 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
411 memcpy(dst, src, 3 * SCREEN_WIDTH);
412 if(SDL_MUSTLOCK(shot_surf))
414 SDL_UnlockSurface(shot_surf);
422 static const std::string writeDir = PHYSFS_getWriteDir();
423 static const std::string dirSep = PHYSFS_getDirSeparator();
424 static const std::string baseName = "screenshot";
425 static const std::string fileExt = ".bmp";
426 std::string fullFilename;
427 for (int num = 0; num < 1000; num++) {
428 std::ostringstream oss;
430 oss << std::setw(3) << std::setfill('0') << num;
432 std::string fileName = oss.str();
433 fullFilename = writeDir + dirSep + fileName;
434 if (!PHYSFS_exists(fileName.c_str())) {
435 SDL_SaveBMP(shot_surf, fullFilename.c_str());
436 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
437 SDL_FreeSurface(shot_surf);
441 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
442 SDL_FreeSurface(shot_surf);
448 assert_gl("drawing");
449 SDL_GL_SwapWindow(screen)();
453 GLRenderer::resize(int w, int h)
455 // This causes the screen to go black, which is annoying, but seems
456 // unavoidable with SDL at the moment
457 SDL_CreateWindow(SDL_GL_CreateContext(w, h, 0, SDL_OPENGL | SDL_RESIZABLE));
459 g_config->window_size = Size(w, h);
465 GLRenderer::apply_config()
469 log_info << "Applying Config:"
470 << "\n Desktop: " << desktop_size.width << "x" << desktop_size.height
471 << "\n Window: " << g_config->window_size
472 << "\n FullRes: " << g_config->fullscreen_size
473 << "\n Aspect: " << g_config->aspect_size
474 << "\n Magnif: " << g_config->magnification
478 float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
479 if (g_config->aspect_size != Size(0, 0))
481 target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
484 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
485 if (desktop_size.width != -1 && desktop_size.height != -1)
487 desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
492 // Get the screen width
493 if (g_config->use_fullscreen)
495 screen_size = g_config->fullscreen_size;
496 desktop_aspect = float(screen_size.width) / float(screen_size.height);
500 screen_size = g_config->window_size;
503 apply_video_mode(screen_size, g_config->use_fullscreen);
505 if (target_aspect > 1.0f)
507 SCREEN_WIDTH = static_cast<int>(screen_size.width * (target_aspect / desktop_aspect));
508 SCREEN_HEIGHT = static_cast<int>(screen_size.height);
512 SCREEN_WIDTH = static_cast<int>(screen_size.width);
513 SCREEN_HEIGHT = static_cast<int>(screen_size.height * (target_aspect / desktop_aspect));
516 Size max_size(1280, 800);
517 Size min_size(640, 480);
519 if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
521 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
522 // max_size.width/max_size.height resp. min_size.width/min_size.height
523 if (SCREEN_WIDTH > max_size.width || SCREEN_HEIGHT > max_size.height)
525 float scale1 = float(max_size.width)/SCREEN_WIDTH;
526 float scale2 = float(max_size.height)/SCREEN_HEIGHT;
527 float scale = (scale1 < scale2) ? scale1 : scale2;
528 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
529 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
531 else if (SCREEN_WIDTH < min_size.width || SCREEN_HEIGHT < min_size.height)
533 float scale1 = float(min_size.width)/SCREEN_WIDTH;
534 float scale2 = float(min_size.height)/SCREEN_HEIGHT;
535 float scale = (scale1 < scale2) ? scale1 : scale2;
536 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
537 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
541 glViewport(0, 0, screen_size.width, screen_size.height);
545 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / g_config->magnification);
546 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
548 // This works by adding black borders around the screen to limit
549 // SCREEN_WIDTH/SCREEN_HEIGHT to max_size.width/max_size.height
550 Size new_size = screen_size;
552 if (SCREEN_WIDTH > max_size.width)
554 new_size.width = static_cast<int>((float) new_size.width * float(max_size.width)/SCREEN_WIDTH);
555 SCREEN_WIDTH = static_cast<int>(max_size.width);
558 if (SCREEN_HEIGHT > max_size.height)
560 new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
561 SCREEN_HEIGHT = static_cast<int>(max_size.height);
564 // Clear both buffers so that we get a clean black border without junk
565 glClear(GL_COLOR_BUFFER_BIT);
566 SDL_GL_SwapWindow(screen);
567 glClear(GL_COLOR_BUFFER_BIT);
568 SDL_GL_SwapWindow(screen);
570 glViewport(std::max(0, (screen_size.width - new_size.width) / 2),
571 std::max(0, (screen_size.height - new_size.height) / 2),
572 std::min(new_size.width, screen_size.width),
573 std::min(new_size.height, screen_size.height));
576 glMatrixMode(GL_PROJECTION);
579 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
581 glMatrixMode(GL_MODELVIEW);
583 glTranslatef(0, 0, 0);
584 check_gl_error("Setting up view matrices");
588 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
590 // Only change video mode when its different from the current one
591 if (screen_size != size || fullscreen_active != fullscreen)
593 int flags = SDL_OPENGL;
597 flags |= SDL_FULLSCREEN;
601 flags |= SDL_RESIZABLE;
604 if (SDL_Surface *screen = SDL_CreateWindow(SDL_GL_CreateContext( size.width, size.height, 0, flags)))
606 screen_size = Size(screen->w, screen->h);
607 fullscreen_active = fullscreen;
611 std::ostringstream msg;
612 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
613 throw std::runtime_error(msg.str());