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