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