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