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