Implemented desktop_size discovery in GLRenderer
[supertux.git] / src / video / gl / gl_renderer.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //      Updated by GiBy 2013 for SDL2 <giby_the_kid@yahoo.fr>
4 //
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.
9 //
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.
14 //
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/>.
17
18 #include "video/gl/gl_renderer.hpp"
19
20 #include <iomanip>
21 #include <iostream>
22 #include <physfs.h>
23 #include "SDL.h"
24
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 #define LIGHTMAP_DIV 5
31
32 #ifdef GL_VERSION_ES_CM_1_0
33 #  define glOrtho glOrthof
34 #endif
35
36 GLRenderer::GLRenderer() :
37   window(),
38   desktop_size(-1, -1),
39   screen_size(-1, -1),
40   fullscreen_active(false),
41   last_texture(static_cast<GLuint> (-1))
42 {
43   Renderer::instance_ = this;
44
45   SDL_DisplayMode mode;
46   SDL_GetCurrentDisplayMode(0, &mode);
47   desktop_size = Size(mode.w, mode.h);
48
49   if(texture_manager != 0)
50     texture_manager->save_textures();
51
52   if(g_config->try_vsync) {
53     /* we want vsync for smooth scrolling */
54     if (SDL_GL_SetSwapInterval(-1) != 0)
55     {
56       log_info << "no support for late swap tearing vsync: " << SDL_GetError() << std::endl;
57       if (SDL_GL_SetSwapInterval(1))
58       {
59         log_info << "no support for vsync: " << SDL_GetError() << std::endl;
60       }
61     }
62   }
63
64   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
65
66   SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   5);
67   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
68   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  5);
69
70   if(g_config->use_fullscreen)
71   {
72     apply_video_mode(g_config->fullscreen_size, true);
73   }
74   else
75   {
76     apply_video_mode(g_config->window_size, false);
77   }
78
79   // setup opengl state and transform
80   glDisable(GL_DEPTH_TEST);
81   glDisable(GL_CULL_FACE);
82   glEnable(GL_TEXTURE_2D);
83   glEnable(GL_BLEND);
84   glEnableClientState(GL_VERTEX_ARRAY);
85   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
86   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
87
88   // Init the projection matrix, viewport and stuff
89   apply_config();
90   
91   if(texture_manager == 0)
92     texture_manager = new TextureManager();
93   else
94     texture_manager->reload_textures();
95   
96 #ifndef GL_VERSION_ES_CM_1_0
97   GLenum err = glewInit();
98   if (GLEW_OK != err)
99   {
100     std::ostringstream out;
101     out << "GLRenderer: " << glewGetErrorString(err);
102     throw std::runtime_error(out.str());
103   }
104   log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
105   log_info << "GLEW_ARB_texture_non_power_of_two: " << static_cast<int>(GLEW_ARB_texture_non_power_of_two) << std::endl;
106 #endif
107 }
108
109 GLRenderer::~GLRenderer()
110 {
111   SDL_GL_DeleteContext(glcontext);
112   SDL_DestroyWindow(window);
113 }
114
115 void
116 GLRenderer::draw_surface(const DrawingRequest& request)
117 {
118   const Surface* surface = (const Surface*) request.request_data;
119   if(surface == NULL)
120   {
121     return;
122   }
123   GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
124   if(gltexture == NULL)
125   {
126     return;
127   }
128   GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
129   if(surface_data == NULL)
130   {
131     return;
132   }
133
134   GLuint th = gltexture->get_handle();
135   if (th != last_texture) {
136     last_texture = th;
137     glBindTexture(GL_TEXTURE_2D, th);
138   }
139   intern_draw(request.pos.x, request.pos.y,
140               request.pos.x + surface->get_width(),
141               request.pos.y + surface->get_height(),
142               surface_data->get_uv_left(),
143               surface_data->get_uv_top(),
144               surface_data->get_uv_right(),
145               surface_data->get_uv_bottom(),
146               request.angle,
147               request.alpha,
148               request.color,
149               request.blend,
150               request.drawing_effect);
151 }
152
153 void
154 GLRenderer::draw_surface_part(const DrawingRequest& request)
155 {
156   const SurfacePartRequest* surfacepartrequest
157     = (SurfacePartRequest*) request.request_data;
158   const Surface* surface = surfacepartrequest->surface;
159   boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
160   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
161
162   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
163   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
164
165   float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
166   float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
167   float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
168   float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
169
170   GLuint th = gltexture->get_handle();
171   if (th != last_texture) {
172     last_texture = th;
173     glBindTexture(GL_TEXTURE_2D, th);
174   }
175   intern_draw(request.pos.x, request.pos.y,
176               request.pos.x + surfacepartrequest->size.x,
177               request.pos.y + surfacepartrequest->size.y,
178               uv_left,
179               uv_top,
180               uv_right,
181               uv_bottom,
182               0.0,
183               request.alpha,
184               request.color,
185               Blend(),
186               request.drawing_effect);
187 }
188
189 void
190 GLRenderer::draw_gradient(const DrawingRequest& request)
191 {
192   const GradientRequest* gradientrequest 
193     = (GradientRequest*) request.request_data;
194   const Color& top = gradientrequest->top;
195   const Color& bottom = gradientrequest->bottom;
196
197   glDisable(GL_TEXTURE_2D);
198   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
199   glEnableClientState(GL_COLOR_ARRAY);
200
201   float vertices[] = {
202     0, 0,
203     float(SCREEN_WIDTH), 0,
204     float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
205     0, float(SCREEN_HEIGHT)
206   };
207   glVertexPointer(2, GL_FLOAT, 0, vertices);
208
209   float colors[] = {
210     top.red, top.green, top.blue, top.alpha,
211     top.red, top.green, top.blue, top.alpha,
212     bottom.red, bottom.green, bottom.blue, bottom.alpha,
213     bottom.red, bottom.green, bottom.blue, bottom.alpha,
214   };
215   glColorPointer(4, GL_FLOAT, 0, colors);
216
217   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
218
219   glDisableClientState(GL_COLOR_ARRAY);
220   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
221
222   glEnable(GL_TEXTURE_2D);
223   glColor4f(1, 1, 1, 1);
224 }
225
226 void
227 GLRenderer::draw_filled_rect(const DrawingRequest& request)
228 {
229   const FillRectRequest* fillrectrequest
230     = (FillRectRequest*) request.request_data;
231
232   glDisable(GL_TEXTURE_2D);
233   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
234             fillrectrequest->color.blue, fillrectrequest->color.alpha);
235   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
236   
237   if (fillrectrequest->radius != 0.0f)
238   {
239     // draw round rect
240     // Keep radius in the limits, so that we get a circle instead of
241     // just graphic junk
242     float radius = std::min(fillrectrequest->radius,
243                             std::min(fillrectrequest->size.x/2,
244                                      fillrectrequest->size.y/2));
245
246     // inner rectangle
247     Rectf irect(request.pos.x    + radius,
248                request.pos.y    + radius,
249                request.pos.x + fillrectrequest->size.x - radius,
250                request.pos.y + fillrectrequest->size.y - radius);
251
252     int n = 8;
253     int p = 0;
254     std::vector<float> vertices((n+1) * 4 * 2);
255
256     for(int i = 0; i <= n; ++i)
257     {
258       float x = sinf(i * (M_PI/2) / n) * radius;
259       float y = cosf(i * (M_PI/2) / n) * radius;
260
261       vertices[p++] = irect.get_left() - x;
262       vertices[p++] = irect.get_top()  - y;
263
264       vertices[p++] = irect.get_right() + x;
265       vertices[p++] = irect.get_top()   - y;
266     }
267
268     for(int i = 0; i <= n; ++i)
269     {
270       float x = cosf(i * (M_PI/2) / n) * radius;
271       float y = sinf(i * (M_PI/2) / n) * radius;
272
273       vertices[p++] = irect.get_left()   - x;
274       vertices[p++] = irect.get_bottom() + y;
275
276       vertices[p++] = irect.get_right()  + x;
277       vertices[p++] = irect.get_bottom() + y;
278     }
279
280     glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
281     glDrawArrays(GL_TRIANGLE_STRIP, 0,  vertices.size()/2);
282   }
283   else
284   {
285     float x = request.pos.x;
286     float y = request.pos.y;
287     float w = fillrectrequest->size.x;
288     float h = fillrectrequest->size.y;
289
290     float vertices[] = {
291       x,   y,
292       x+w, y,
293       x+w, y+h,
294       x,   y+h
295     };
296     glVertexPointer(2, GL_FLOAT, 0, vertices);
297
298     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
299   }
300
301   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
302   glEnable(GL_TEXTURE_2D);
303   glColor4f(1, 1, 1, 1);
304 }
305
306 void
307 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
308 {
309   const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
310
311   glDisable(GL_TEXTURE_2D);
312   glColor4f(ellipse->color.red,  ellipse->color.green,
313             ellipse->color.blue, ellipse->color.alpha);
314     
315   float x = request.pos.x;
316   float y = request.pos.y;
317   float w = ellipse->size.x/2.0f;
318   float h = ellipse->size.y/2.0f;
319
320   static const int slices = 16;
321   static const int points = (slices+1) * 12;
322
323   float vertices[points * 2];
324   int   p = 0;
325
326   // Bottom
327   vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
328   vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
329   vertices[p++] = x;            vertices[p++] = y+h;
330
331   // Top
332   vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
333   vertices[p++] = 0;            vertices[p++] = 0;
334   vertices[p++] = x;            vertices[p++] = y-h;
335
336   // Left
337   vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
338   vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
339   vertices[p++] = x+w;          vertices[p++] = y;
340
341   // Right
342   vertices[p++] = 0;            vertices[p++] = 0;
343   vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
344   vertices[p++] = x-w;          vertices[p++] = y;
345
346   for(int i = 0; i < slices; ++i)
347   {
348     float ex1 = sinf(M_PI/2 / slices * i) * w;
349     float ey1 = cosf(M_PI/2 / slices * i) * h;
350
351     float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
352     float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
353
354     // Bottom/Right
355     vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
356     vertices[p++] = x + ex1;      vertices[p++] = y + ey1;
357     vertices[p++] = x + ex2;      vertices[p++] = y + ey2;
358
359     // Top/Left
360     vertices[p++] = 0;            vertices[p++] = 0;
361     vertices[p++] = x - ex1;      vertices[p++] = y - ey1;
362     vertices[p++] = x - ex2;      vertices[p++] = y - ey2;
363
364     // Top/Right
365     vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
366     vertices[p++] = x + ex1;      vertices[p++] = y - ey1;
367     vertices[p++] = x + ex2;      vertices[p++] = y - ey2;
368
369     // Bottom/Left
370     vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
371     vertices[p++] = x - ex1;      vertices[p++] = y + ey1;
372     vertices[p++] = x - ex2;      vertices[p++] = y + ey2;
373   }
374
375   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
376   glVertexPointer(2, GL_FLOAT, 0, vertices);
377
378   glDrawArrays(GL_TRIANGLES, 0, points);
379
380   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
381
382   glEnable(GL_TEXTURE_2D);
383   glColor4f(1, 1, 1, 1);    
384 }
385
386 void 
387 GLRenderer::do_take_screenshot()
388 {
389   // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
390
391   SDL_Surface *shot_surf;
392   // create surface to hold screenshot
393 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
394   shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
395 #else
396   shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
397 #endif
398   if (!shot_surf) {
399     log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
400     return;
401   }
402
403   // read pixels into array
404   char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
405   if (!pixels) {
406     log_warning << "Could not allocate memory to store screenshot" << std::endl;
407     SDL_FreeSurface(shot_surf);
408     return;
409   }
410   glPixelStorei(GL_PACK_ALIGNMENT, 1);
411   glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
412
413   // copy array line-by-line
414   for (int i = 0; i < SCREEN_HEIGHT; i++) {
415     char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
416     if(SDL_MUSTLOCK(shot_surf))
417     {
418       SDL_LockSurface(shot_surf);
419     }
420     char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
421     memcpy(dst, src, 3 * SCREEN_WIDTH);
422     if(SDL_MUSTLOCK(shot_surf))
423     {
424       SDL_UnlockSurface(shot_surf);
425     }
426   }
427
428   // free array
429   delete[](pixels);
430
431   // save screenshot
432   static const std::string writeDir = PHYSFS_getWriteDir();
433   static const std::string dirSep = PHYSFS_getDirSeparator();
434   static const std::string baseName = "screenshot";
435   static const std::string fileExt = ".bmp";
436   std::string fullFilename;
437   for (int num = 0; num < 1000; num++) {
438     std::ostringstream oss;
439     oss << baseName;
440     oss << std::setw(3) << std::setfill('0') << num;
441     oss << fileExt;
442     std::string fileName = oss.str();
443     fullFilename = writeDir + dirSep + fileName;
444     if (!PHYSFS_exists(fileName.c_str())) {
445       SDL_SaveBMP(shot_surf, fullFilename.c_str());
446       log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
447       SDL_FreeSurface(shot_surf);
448       return;
449     }
450   }
451   log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
452   SDL_FreeSurface(shot_surf);
453 }
454
455 void
456 GLRenderer::flip()
457 {
458   assert_gl("drawing");
459   SDL_GL_SwapWindow(window);
460 }
461
462 void
463 GLRenderer::resize(int w, int h)
464 {
465   g_config->window_size = Size(w, h);
466
467   PHYSICAL_SCREEN_WIDTH = w;
468   PHYSICAL_SCREEN_HEIGHT = h;
469
470   apply_config();
471 }
472
473 void
474 GLRenderer::apply_config()
475 {    
476   if (false)
477   {
478     log_info << "Applying Config:" 
479              << "\n  Desktop: " << desktop_size.width << "x" << desktop_size.height
480              << "\n  Window:  " << g_config->window_size
481              << "\n  FullRes: " << g_config->fullscreen_size
482              << "\n  Aspect:  " << g_config->aspect_size
483              << "\n  Magnif:  " << g_config->magnification
484              << std::endl;
485   }
486
487   float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
488   if (g_config->aspect_size != Size(0, 0))
489   {
490     target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
491   }
492
493   float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
494   if (desktop_size.width != -1 && desktop_size.height != -1)
495   {
496     desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
497   }
498
499   Size screen_size;
500
501   // Get the screen width
502   if (g_config->use_fullscreen)
503   {
504     screen_size = g_config->fullscreen_size;
505     desktop_aspect = float(screen_size.width) / float(screen_size.height);
506   }
507   else
508   {
509     screen_size = g_config->window_size;
510   }
511
512   apply_video_mode(screen_size, g_config->use_fullscreen);
513
514   if (target_aspect > 1.0f)
515   {
516     SCREEN_WIDTH  = static_cast<int>(screen_size.width * (target_aspect / desktop_aspect));
517     SCREEN_HEIGHT = static_cast<int>(screen_size.height);
518   }
519   else
520   {
521     SCREEN_WIDTH  = static_cast<int>(screen_size.width);
522     SCREEN_HEIGHT = static_cast<int>(screen_size.height  * (target_aspect / desktop_aspect));
523   }
524
525   Size max_size(1280, 800);
526   Size min_size(640, 480);
527
528   if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
529   {
530     // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
531     // max_size.width/max_size.height resp. min_size.width/min_size.height
532     if (SCREEN_WIDTH > max_size.width || SCREEN_HEIGHT > max_size.height)
533     {
534       float scale1  = float(max_size.width)/SCREEN_WIDTH;
535       float scale2  = float(max_size.height)/SCREEN_HEIGHT;
536       float scale   = (scale1 < scale2) ? scale1 : scale2;
537       SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  * scale);
538       SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
539     } 
540     else if (SCREEN_WIDTH < min_size.width || SCREEN_HEIGHT < min_size.height)
541     {
542       float scale1  = float(min_size.width)/SCREEN_WIDTH;
543       float scale2  = float(min_size.height)/SCREEN_HEIGHT;
544       float scale   = (scale1 < scale2) ? scale1 : scale2;
545       SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  * scale);
546       SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
547     }
548    
549
550     glViewport(0, 0, screen_size.width, screen_size.height);
551   }
552   else
553   {
554     SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  / g_config->magnification);
555     SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
556
557     // This works by adding black borders around the screen to limit
558     // SCREEN_WIDTH/SCREEN_HEIGHT to max_size.width/max_size.height
559     Size new_size = screen_size;
560
561     if (SCREEN_WIDTH > max_size.width)
562     {
563       new_size.width = static_cast<int>((float) new_size.width * float(max_size.width)/SCREEN_WIDTH);
564       SCREEN_WIDTH = static_cast<int>(max_size.width);
565     }
566
567     if (SCREEN_HEIGHT > max_size.height)
568     {
569       new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
570       SCREEN_HEIGHT = static_cast<int>(max_size.height);
571     }
572
573     // Clear both buffers so that we get a clean black border without junk
574     glClear(GL_COLOR_BUFFER_BIT);
575     SDL_GL_SwapWindow(window);
576     glClear(GL_COLOR_BUFFER_BIT);
577     SDL_GL_SwapWindow(window);
578
579     glViewport(std::max(0, (screen_size.width  - new_size.width)  / 2),
580                std::max(0, (screen_size.height - new_size.height) / 2),
581                std::min(new_size.width,  screen_size.width),
582                std::min(new_size.height, screen_size.height));
583   }
584
585   glMatrixMode(GL_PROJECTION);
586   glLoadIdentity();
587
588   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
589
590   glMatrixMode(GL_MODELVIEW);
591   glLoadIdentity();
592   glTranslatef(0, 0, 0);
593   check_gl_error("Setting up view matrices");
594 }
595
596 void
597 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
598 {
599   if (window)
600   {
601     SDL_SetWindowSize(window, size.width, size.height);
602
603     if (fullscreen)
604     {
605       int fullscreen_flags = SDL_WINDOW_FULLSCREEN; // SDL_WINDOW_FULLSCREEN_DESKTOP or 0
606       SDL_SetWindowDisplayMode(window, NULL);
607
608       SDL_DisplayMode mode;
609       mode.format = SDL_PIXELFORMAT_RGB888;
610       mode.w = g_config->fullscreen_size.width;
611       mode.h = g_config->fullscreen_size.height;
612       mode.refresh_rate = g_config->fullscreen_refresh_rate;
613       mode.driverdata = 0;
614
615       if (SDL_SetWindowDisplayMode(window, &mode) != 0)
616       {
617         log_warning << "failed to set display mode: "
618                     << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
619                     << SDL_GetError() << std::endl;
620       }
621       else
622       {
623         SDL_SetWindowFullscreen(window, fullscreen_flags);
624       }
625     }
626     else
627     {
628       SDL_SetWindowFullscreen(window, 0);
629     }
630   }
631   else
632   {
633     // Only change video mode when its different from the current one
634     if (screen_size != size || fullscreen_active != fullscreen)
635     {
636       int flags = SDL_WINDOW_OPENGL;
637
638       if (fullscreen)
639       {
640         flags |= SDL_WINDOW_FULLSCREEN;
641       }
642       else
643       {
644         flags |= SDL_WINDOW_RESIZABLE;
645       }
646
647       window = SDL_CreateWindow("SuperTux",
648                                 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
649                                 size.width, size.height,
650                                 flags);
651       if (!window)
652       {
653         std::ostringstream msg;
654         msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
655         throw std::runtime_error(msg.str());
656       }
657       else
658       {
659         glcontext = SDL_GL_CreateContext(window);
660         screen_size = size;
661         
662         PHYSICAL_SCREEN_WIDTH = size.width;
663         PHYSICAL_SCREEN_HEIGHT = size.height;
664
665         SCREEN_WIDTH = size.width;
666         SCREEN_HEIGHT = size.height;
667         
668         fullscreen_active = fullscreen;
669       }
670     }
671   }
672 }
673
674 void
675 GLRenderer::set_gamma(float gamma)
676 {
677   Uint16 ramp[256];
678   SDL_CalculateGammaRamp(gamma, ramp);
679   SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
680 }
681
682 /* EOF */