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