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.
51 const SDL_VideoInfo *info = SDL_GetVideoInfo();
54 desktop_size = Size(info->current_w, info->current_h);
57 desktop_size = Size(1920, 1080);
61 if(texture_manager != 0)
62 texture_manager->save_textures();
64 #ifdef SDL_GL_SWAP_CONTROL
65 if(config->try_vsync) {
66 /* we want vsync for smooth scrolling */
67 SDL_GL_SetSwapInterval(1);
72 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
74 // FIXME: Hu? 16bit rendering?
75 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
76 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
77 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
80 if(g_config->use_fullscreen)
82 apply_video_mode(g_config->fullscreen_size, true);
86 apply_video_mode(g_config->window_size, false);
89 // setup opengl state and transform
90 glDisable(GL_DEPTH_TEST);
91 glDisable(GL_CULL_FACE);
92 glEnable(GL_TEXTURE_2D);
94 glEnableClientState(GL_VERTEX_ARRAY);
95 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
96 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
98 // Init the projection matrix, viewport and stuff
101 if(texture_manager == 0)
102 texture_manager = new TextureManager();
104 texture_manager->reload_textures();
106 #ifndef GL_VERSION_ES_CM_1_0
107 GLenum err = glewInit();
110 std::ostringstream out;
111 out << "GLRenderer: " << glewGetErrorString(err);
112 throw std::runtime_error(out.str());
114 log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
115 log_info << "GLEW_ARB_texture_non_power_of_two: " << static_cast<int>(GLEW_ARB_texture_non_power_of_two) << std::endl;
119 GLRenderer::~GLRenderer()
121 SDL_GL_DeleteContext(glcontext);
122 SDL_DestroyWindow(window);
126 GLRenderer::draw_surface(const DrawingRequest& request)
128 const Surface* surface = (const Surface*) request.request_data;
133 GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
134 if(gltexture == NULL)
138 GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
139 if(surface_data == NULL)
144 GLuint th = gltexture->get_handle();
145 if (th != last_texture) {
147 glBindTexture(GL_TEXTURE_2D, th);
149 intern_draw(request.pos.x, request.pos.y,
150 request.pos.x + surface->get_width(),
151 request.pos.y + surface->get_height(),
152 surface_data->get_uv_left(),
153 surface_data->get_uv_top(),
154 surface_data->get_uv_right(),
155 surface_data->get_uv_bottom(),
160 request.drawing_effect);
164 GLRenderer::draw_surface_part(const DrawingRequest& request)
166 const SurfacePartRequest* surfacepartrequest
167 = (SurfacePartRequest*) request.request_data;
168 const Surface* surface = surfacepartrequest->surface;
169 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
170 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
172 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
173 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
175 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
176 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
177 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
178 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
180 GLuint th = gltexture->get_handle();
181 if (th != last_texture) {
183 glBindTexture(GL_TEXTURE_2D, th);
185 intern_draw(request.pos.x, request.pos.y,
186 request.pos.x + surfacepartrequest->size.x,
187 request.pos.y + surfacepartrequest->size.y,
196 request.drawing_effect);
200 GLRenderer::draw_gradient(const DrawingRequest& request)
202 const GradientRequest* gradientrequest
203 = (GradientRequest*) request.request_data;
204 const Color& top = gradientrequest->top;
205 const Color& bottom = gradientrequest->bottom;
207 glDisable(GL_TEXTURE_2D);
208 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
209 glEnableClientState(GL_COLOR_ARRAY);
213 float(SCREEN_WIDTH), 0,
214 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
215 0, float(SCREEN_HEIGHT)
217 glVertexPointer(2, GL_FLOAT, 0, vertices);
220 top.red, top.green, top.blue, top.alpha,
221 top.red, top.green, top.blue, top.alpha,
222 bottom.red, bottom.green, bottom.blue, bottom.alpha,
223 bottom.red, bottom.green, bottom.blue, bottom.alpha,
225 glColorPointer(4, GL_FLOAT, 0, colors);
227 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
229 glDisableClientState(GL_COLOR_ARRAY);
230 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
232 glEnable(GL_TEXTURE_2D);
233 glColor4f(1, 1, 1, 1);
237 GLRenderer::draw_filled_rect(const DrawingRequest& request)
239 const FillRectRequest* fillrectrequest
240 = (FillRectRequest*) request.request_data;
242 glDisable(GL_TEXTURE_2D);
243 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
244 fillrectrequest->color.blue, fillrectrequest->color.alpha);
245 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
247 if (fillrectrequest->radius != 0.0f)
250 // Keep radius in the limits, so that we get a circle instead of
252 float radius = std::min(fillrectrequest->radius,
253 std::min(fillrectrequest->size.x/2,
254 fillrectrequest->size.y/2));
257 Rectf irect(request.pos.x + radius,
258 request.pos.y + radius,
259 request.pos.x + fillrectrequest->size.x - radius,
260 request.pos.y + fillrectrequest->size.y - radius);
264 std::vector<float> vertices((n+1) * 4 * 2);
266 for(int i = 0; i <= n; ++i)
268 float x = sinf(i * (M_PI/2) / n) * radius;
269 float y = cosf(i * (M_PI/2) / n) * radius;
271 vertices[p++] = irect.get_left() - x;
272 vertices[p++] = irect.get_top() - y;
274 vertices[p++] = irect.get_right() + x;
275 vertices[p++] = irect.get_top() - y;
278 for(int i = 0; i <= n; ++i)
280 float x = cosf(i * (M_PI/2) / n) * radius;
281 float y = sinf(i * (M_PI/2) / n) * radius;
283 vertices[p++] = irect.get_left() - x;
284 vertices[p++] = irect.get_bottom() + y;
286 vertices[p++] = irect.get_right() + x;
287 vertices[p++] = irect.get_bottom() + y;
290 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
291 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
295 float x = request.pos.x;
296 float y = request.pos.y;
297 float w = fillrectrequest->size.x;
298 float h = fillrectrequest->size.y;
306 glVertexPointer(2, GL_FLOAT, 0, vertices);
308 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
311 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
312 glEnable(GL_TEXTURE_2D);
313 glColor4f(1, 1, 1, 1);
317 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
319 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
321 glDisable(GL_TEXTURE_2D);
322 glColor4f(ellipse->color.red, ellipse->color.green,
323 ellipse->color.blue, ellipse->color.alpha);
325 float x = request.pos.x;
326 float y = request.pos.y;
327 float w = ellipse->size.x/2.0f;
328 float h = ellipse->size.y/2.0f;
330 static const int slices = 16;
331 static const int points = (slices+1) * 12;
333 float vertices[points * 2];
337 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
338 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
339 vertices[p++] = x; vertices[p++] = y+h;
342 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
343 vertices[p++] = 0; vertices[p++] = 0;
344 vertices[p++] = x; vertices[p++] = y-h;
347 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
348 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
349 vertices[p++] = x+w; vertices[p++] = y;
352 vertices[p++] = 0; vertices[p++] = 0;
353 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
354 vertices[p++] = x-w; vertices[p++] = y;
356 for(int i = 0; i < slices; ++i)
358 float ex1 = sinf(M_PI/2 / slices * i) * w;
359 float ey1 = cosf(M_PI/2 / slices * i) * h;
361 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
362 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
365 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
366 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
367 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
370 vertices[p++] = 0; vertices[p++] = 0;
371 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
372 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
375 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
376 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
377 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
380 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
381 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
382 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
385 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
386 glVertexPointer(2, GL_FLOAT, 0, vertices);
388 glDrawArrays(GL_TRIANGLES, 0, points);
390 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
392 glEnable(GL_TEXTURE_2D);
393 glColor4f(1, 1, 1, 1);
397 GLRenderer::do_take_screenshot()
399 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
401 SDL_Surface *shot_surf;
402 // create surface to hold screenshot
403 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
404 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
406 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
409 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
413 // read pixels into array
414 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
416 log_warning << "Could not allocate memory to store screenshot" << std::endl;
417 SDL_FreeSurface(shot_surf);
420 glPixelStorei(GL_PACK_ALIGNMENT, 1);
421 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
423 // copy array line-by-line
424 for (int i = 0; i < SCREEN_HEIGHT; i++) {
425 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
426 if(SDL_MUSTLOCK(shot_surf))
428 SDL_LockSurface(shot_surf);
430 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
431 memcpy(dst, src, 3 * SCREEN_WIDTH);
432 if(SDL_MUSTLOCK(shot_surf))
434 SDL_UnlockSurface(shot_surf);
442 static const std::string writeDir = PHYSFS_getWriteDir();
443 static const std::string dirSep = PHYSFS_getDirSeparator();
444 static const std::string baseName = "screenshot";
445 static const std::string fileExt = ".bmp";
446 std::string fullFilename;
447 for (int num = 0; num < 1000; num++) {
448 std::ostringstream oss;
450 oss << std::setw(3) << std::setfill('0') << num;
452 std::string fileName = oss.str();
453 fullFilename = writeDir + dirSep + fileName;
454 if (!PHYSFS_exists(fileName.c_str())) {
455 SDL_SaveBMP(shot_surf, fullFilename.c_str());
456 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
457 SDL_FreeSurface(shot_surf);
461 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
462 SDL_FreeSurface(shot_surf);
468 assert_gl("drawing");
469 SDL_GL_SwapWindow(window);
473 GLRenderer::resize(int w, int h)
476 SDL_CreateWindow(SDL_GL_CreateContext(w, h, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE));
479 g_config->window_size = Size(w, 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);
585 // Clear both buffers so that we get a clean black border without junk
586 glClear(GL_COLOR_BUFFER_BIT);
587 SDL_GL_SwapWindow(screen);
588 glClear(GL_COLOR_BUFFER_BIT);
589 SDL_GL_SwapWindow(screen);
592 glViewport(std::max(0, (screen_size.width - new_size.width) / 2),
593 std::max(0, (screen_size.height - new_size.height) / 2),
594 std::min(new_size.width, screen_size.width),
595 std::min(new_size.height, screen_size.height));
598 glMatrixMode(GL_PROJECTION);
601 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
603 glMatrixMode(GL_MODELVIEW);
605 glTranslatef(0, 0, 0);
606 check_gl_error("Setting up view matrices");
610 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
612 // Only change video mode when its different from the current one
613 if (screen_size != size || fullscreen_active != fullscreen)
615 int flags = SDL_WINDOW_OPENGL;
619 flags |= SDL_WINDOW_FULLSCREEN;
623 flags |= SDL_WINDOW_RESIZABLE;
626 window = SDL_CreateWindow("SuperTux",
627 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
628 size.width, size.height,
632 std::ostringstream msg;
633 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
634 throw std::runtime_error(msg.str());
638 glcontext = SDL_GL_CreateContext(window);
640 fullscreen_active = fullscreen;