Implemented set_gamma()
[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 //#include "SDL/SDL.h"
25 //#include "SDL/SDL_opengl.h"
26
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
33
34 #ifdef GL_VERSION_ES_CM_1_0
35 #  define glOrtho glOrthof
36 #endif
37
38 GLRenderer::GLRenderer() :
39   desktop_size(-1, -1),
40   screen_size(-1, -1),
41   fullscreen_active(false),
42   last_texture(static_cast<GLuint> (-1))
43 {
44   Renderer::instance_ = this;
45
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 #ifdef OLD_SDL1
51   const SDL_VideoInfo *info = SDL_GetVideoInfo();
52   if (info)
53   {
54       desktop_size = Size(info->current_w, info->current_h);
55   }
56 #else
57   desktop_size = Size(1920, 1080);
58 #endif
59 #endif
60
61   if(texture_manager != 0)
62     texture_manager->save_textures();
63
64 #ifdef SDL_GL_SWAP_CONTROL
65   if(config->try_vsync) {
66     /* we want vsync for smooth scrolling */
67      SDL_GL_SetSwapInterval(1);
68   }
69 #endif
70
71 #ifdef OLD_SDL1
72   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
73
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);
78 #endif
79
80   if(g_config->use_fullscreen)
81   {
82     apply_video_mode(g_config->fullscreen_size, true);
83   }
84   else
85   {
86     apply_video_mode(g_config->window_size, false);
87   }
88
89   // setup opengl state and transform
90   glDisable(GL_DEPTH_TEST);
91   glDisable(GL_CULL_FACE);
92   glEnable(GL_TEXTURE_2D);
93   glEnable(GL_BLEND);
94   glEnableClientState(GL_VERTEX_ARRAY);
95   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
96   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
97
98   // Init the projection matrix, viewport and stuff
99   apply_config();
100   
101   if(texture_manager == 0)
102     texture_manager = new TextureManager();
103   else
104     texture_manager->reload_textures();
105   
106 #ifndef GL_VERSION_ES_CM_1_0
107   GLenum err = glewInit();
108   if (GLEW_OK != err)
109   {
110     std::ostringstream out;
111     out << "GLRenderer: " << glewGetErrorString(err);
112     throw std::runtime_error(out.str());
113   }
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;
116 #endif
117 }
118
119 GLRenderer::~GLRenderer()
120 {
121   SDL_GL_DeleteContext(glcontext);
122   SDL_DestroyWindow(window);
123 }
124
125 void
126 GLRenderer::draw_surface(const DrawingRequest& request)
127 {
128   const Surface* surface = (const Surface*) request.request_data;
129   if(surface == NULL)
130   {
131     return;
132   }
133   GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
134   if(gltexture == NULL)
135   {
136     return;
137   }
138   GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
139   if(surface_data == NULL)
140   {
141     return;
142   }
143
144   GLuint th = gltexture->get_handle();
145   if (th != last_texture) {
146     last_texture = th;
147     glBindTexture(GL_TEXTURE_2D, th);
148   }
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(),
156               request.angle,
157               request.alpha,
158               request.color,
159               request.blend,
160               request.drawing_effect);
161 }
162
163 void
164 GLRenderer::draw_surface_part(const DrawingRequest& request)
165 {
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());
171
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();
174
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();
179
180   GLuint th = gltexture->get_handle();
181   if (th != last_texture) {
182     last_texture = th;
183     glBindTexture(GL_TEXTURE_2D, th);
184   }
185   intern_draw(request.pos.x, request.pos.y,
186               request.pos.x + surfacepartrequest->size.x,
187               request.pos.y + surfacepartrequest->size.y,
188               uv_left,
189               uv_top,
190               uv_right,
191               uv_bottom,
192               0.0,
193               request.alpha,
194               request.color,
195               Blend(),
196               request.drawing_effect);
197 }
198
199 void
200 GLRenderer::draw_gradient(const DrawingRequest& request)
201 {
202   const GradientRequest* gradientrequest 
203     = (GradientRequest*) request.request_data;
204   const Color& top = gradientrequest->top;
205   const Color& bottom = gradientrequest->bottom;
206
207   glDisable(GL_TEXTURE_2D);
208   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
209   glEnableClientState(GL_COLOR_ARRAY);
210
211   float vertices[] = {
212     0, 0,
213     float(SCREEN_WIDTH), 0,
214     float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
215     0, float(SCREEN_HEIGHT)
216   };
217   glVertexPointer(2, GL_FLOAT, 0, vertices);
218
219   float colors[] = {
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,
224   };
225   glColorPointer(4, GL_FLOAT, 0, colors);
226
227   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
228
229   glDisableClientState(GL_COLOR_ARRAY);
230   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
231
232   glEnable(GL_TEXTURE_2D);
233   glColor4f(1, 1, 1, 1);
234 }
235
236 void
237 GLRenderer::draw_filled_rect(const DrawingRequest& request)
238 {
239   const FillRectRequest* fillrectrequest
240     = (FillRectRequest*) request.request_data;
241
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);
246   
247   if (fillrectrequest->radius != 0.0f)
248   {
249     // draw round rect
250     // Keep radius in the limits, so that we get a circle instead of
251     // just graphic junk
252     float radius = std::min(fillrectrequest->radius,
253                             std::min(fillrectrequest->size.x/2,
254                                      fillrectrequest->size.y/2));
255
256     // inner rectangle
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);
261
262     int n = 8;
263     int p = 0;
264     std::vector<float> vertices((n+1) * 4 * 2);
265
266     for(int i = 0; i <= n; ++i)
267     {
268       float x = sinf(i * (M_PI/2) / n) * radius;
269       float y = cosf(i * (M_PI/2) / n) * radius;
270
271       vertices[p++] = irect.get_left() - x;
272       vertices[p++] = irect.get_top()  - y;
273
274       vertices[p++] = irect.get_right() + x;
275       vertices[p++] = irect.get_top()   - y;
276     }
277
278     for(int i = 0; i <= n; ++i)
279     {
280       float x = cosf(i * (M_PI/2) / n) * radius;
281       float y = sinf(i * (M_PI/2) / n) * radius;
282
283       vertices[p++] = irect.get_left()   - x;
284       vertices[p++] = irect.get_bottom() + y;
285
286       vertices[p++] = irect.get_right()  + x;
287       vertices[p++] = irect.get_bottom() + y;
288     }
289
290     glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
291     glDrawArrays(GL_TRIANGLE_STRIP, 0,  vertices.size()/2);
292   }
293   else
294   {
295     float x = request.pos.x;
296     float y = request.pos.y;
297     float w = fillrectrequest->size.x;
298     float h = fillrectrequest->size.y;
299
300     float vertices[] = {
301       x,   y,
302       x+w, y,
303       x+w, y+h,
304       x,   y+h
305     };
306     glVertexPointer(2, GL_FLOAT, 0, vertices);
307
308     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
309   }
310
311   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
312   glEnable(GL_TEXTURE_2D);
313   glColor4f(1, 1, 1, 1);
314 }
315
316 void
317 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
318 {
319   const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
320
321   glDisable(GL_TEXTURE_2D);
322   glColor4f(ellipse->color.red,  ellipse->color.green,
323             ellipse->color.blue, ellipse->color.alpha);
324     
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;
329
330   static const int slices = 16;
331   static const int points = (slices+1) * 12;
332
333   float vertices[points * 2];
334   int   p = 0;
335
336   // Bottom
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;
340
341   // Top
342   vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
343   vertices[p++] = 0;            vertices[p++] = 0;
344   vertices[p++] = x;            vertices[p++] = y-h;
345
346   // Left
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;
350
351   // Right
352   vertices[p++] = 0;            vertices[p++] = 0;
353   vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
354   vertices[p++] = x-w;          vertices[p++] = y;
355
356   for(int i = 0; i < slices; ++i)
357   {
358     float ex1 = sinf(M_PI/2 / slices * i) * w;
359     float ey1 = cosf(M_PI/2 / slices * i) * h;
360
361     float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
362     float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
363
364     // Bottom/Right
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;
368
369     // Top/Left
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;
373
374     // Top/Right
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;
378
379     // Bottom/Left
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;
383   }
384
385   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
386   glVertexPointer(2, GL_FLOAT, 0, vertices);
387
388   glDrawArrays(GL_TRIANGLES, 0, points);
389
390   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
391
392   glEnable(GL_TEXTURE_2D);
393   glColor4f(1, 1, 1, 1);    
394 }
395
396 void 
397 GLRenderer::do_take_screenshot()
398 {
399   // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
400
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);
405 #else
406   shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
407 #endif
408   if (!shot_surf) {
409     log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
410     return;
411   }
412
413   // read pixels into array
414   char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
415   if (!pixels) {
416     log_warning << "Could not allocate memory to store screenshot" << std::endl;
417     SDL_FreeSurface(shot_surf);
418     return;
419   }
420   glPixelStorei(GL_PACK_ALIGNMENT, 1);
421   glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
422
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))
427     {
428       SDL_LockSurface(shot_surf);
429     }
430     char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
431     memcpy(dst, src, 3 * SCREEN_WIDTH);
432     if(SDL_MUSTLOCK(shot_surf))
433     {
434       SDL_UnlockSurface(shot_surf);
435     }
436   }
437
438   // free array
439   delete[](pixels);
440
441   // save screenshot
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;
449     oss << baseName;
450     oss << std::setw(3) << std::setfill('0') << num;
451     oss << fileExt;
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);
458       return;
459     }
460   }
461   log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
462   SDL_FreeSurface(shot_surf);
463 }
464
465 void
466 GLRenderer::flip()
467 {
468   assert_gl("drawing");
469   SDL_GL_SwapWindow(window);
470 }
471
472 void
473 GLRenderer::resize(int w, int h)
474 {
475 #ifdef OLD_SDL1
476   SDL_CreateWindow(SDL_GL_CreateContext(w, h, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE));
477 #endif
478
479   g_config->window_size = Size(w, 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   // Only change video mode when its different from the current one
611   if (screen_size != size || fullscreen_active != fullscreen)
612   {
613     int flags = SDL_WINDOW_OPENGL;
614
615     if (fullscreen)
616     {
617       flags |= SDL_WINDOW_FULLSCREEN;
618     }
619     else
620     {
621       flags |= SDL_WINDOW_RESIZABLE;
622     }
623
624     window = SDL_CreateWindow("SuperTux",
625                               SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
626                               size.width, size.height,
627                               flags);
628     if (!window)
629     {
630       std::ostringstream msg;
631       msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
632       throw std::runtime_error(msg.str());
633     }
634     else
635     {
636       glcontext = SDL_GL_CreateContext(window);
637       screen_size = size;
638       fullscreen_active = fullscreen;
639     }
640   }
641 }
642
643 void
644 GLRenderer::set_gamma(float gamma)
645 {
646   Uint16 ramp[256];
647   SDL_CalculateGammaRamp(gamma, ramp);
648   SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
649 }
650
651 /* EOF */