replace some of the glBegin,glEnd calls with glDrawArrays
[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   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
202
203   glMatrixMode(GL_MODELVIEW);
204   glLoadIdentity();
205   glTranslatef(0, 0, 0);
206
207   check_gl_error("Setting up view matrices");
208
209
210   if(texture_manager == 0)
211     texture_manager = new TextureManager();
212   else
213     texture_manager->reload_textures();
214 }
215
216 Renderer::~Renderer()
217 {
218 }
219
220 void
221 Renderer::draw_surface(const DrawingRequest& request)
222 {
223   const Surface* surface = (const Surface*) request.request_data;
224   GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
225   GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
226
227   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
228   intern_draw(request.pos.x, request.pos.y,
229               request.pos.x + surface->get_width(),
230               request.pos.y + surface->get_height(),
231               surface_data->get_uv_left(),
232               surface_data->get_uv_top(),
233               surface_data->get_uv_right(),
234               surface_data->get_uv_bottom(),
235               request.angle,
236               request.alpha,
237               request.color,
238               request.blend,
239               request.drawing_effect);
240 }
241
242 void
243 Renderer::draw_surface_part(const DrawingRequest& request)
244 {
245   const SurfacePartRequest* surfacepartrequest
246     = (SurfacePartRequest*) request.request_data;
247   const Surface *surface = surfacepartrequest->surface;
248   GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
249   GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
250
251   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
252   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
253
254   float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
255   float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
256   float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
257   float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
258
259   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
260   intern_draw(request.pos.x, request.pos.y,
261               request.pos.x + surfacepartrequest->size.x,
262               request.pos.y + surfacepartrequest->size.y,
263               uv_left,
264               uv_top,
265               uv_right,
266               uv_bottom,
267               0.0,
268               request.alpha,
269               Color(1.0, 1.0, 1.0),
270               Blend(),
271               request.drawing_effect);
272 }
273
274 void
275 Renderer::draw_gradient(const DrawingRequest& request)
276 {
277   const GradientRequest* gradientrequest 
278     = (GradientRequest*) request.request_data;
279   const Color& top = gradientrequest->top;
280   const Color& bottom = gradientrequest->bottom;
281
282   glDisable(GL_TEXTURE_2D);
283   glDisable(GL_TEXTURE_COORD_ARRAY);
284   glEnable(GL_COLOR_ARRAY);
285
286   float vertices[] = {
287     0, 0,
288     SCREEN_WIDTH, 0,
289     SCREEN_WIDTH, SCREEN_HEIGHT,
290     0, SCREEN_HEIGHT
291   };
292   glVertexPointer(2, GL_FLOAT, 0, vertices);
293
294   float colors[] = {
295     top.red, top.green, top.blue, top.alpha,
296     top.red, top.green, top.blue, top.alpha,
297     bottom.red, bottom.green, bottom.blue, bottom.alpha,
298     bottom.red, bottom.green, bottom.blue, bottom.alpha,
299   };
300   glColorPointer(4, GL_FLOAT, 0, colors);
301
302   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
303
304   glDisable(GL_COLOR_ARRAY);
305   glEnable(GL_TEXTURE_COORD_ARRAY);
306
307   glEnable(GL_TEXTURE_2D);
308   glColor4f(1, 1, 1, 1);
309 }
310
311 void
312 Renderer::draw_filled_rect(const DrawingRequest& request)
313 {
314   const FillRectRequest* fillrectrequest
315     = (FillRectRequest*) request.request_data;
316
317   if (fillrectrequest->radius != 0.0f)
318     {
319       // draw round rect
320       // Keep radius in the limits, so that we get a circle instead of
321       // just graphic junk
322       float radius = std::min(fillrectrequest->radius,
323                               std::min(fillrectrequest->size.x/2,
324                                        fillrectrequest->size.y/2));
325
326       // inner rectangle
327       Rect irect(request.pos.x    + radius,
328                  request.pos.y    + radius,
329                  request.pos.x + fillrectrequest->size.x - radius,
330                  request.pos.y + fillrectrequest->size.y - radius);
331
332       glDisable(GL_TEXTURE_2D);
333       glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
334                 fillrectrequest->color.blue, fillrectrequest->color.alpha);
335
336
337       int n = 8;
338       glBegin(GL_QUAD_STRIP);
339       for(int i = 0; i <= n; ++i)
340         {
341           float x = sinf(i * (M_PI/2) / n) * radius;
342           float y = cosf(i * (M_PI/2) / n) * radius;
343
344           glVertex2f(irect.get_left()  - x, irect.get_top() - y);
345           glVertex2f(irect.get_right() + x, irect.get_top() - y);
346         }
347       for(int i = 0; i <= n; ++i)
348         {
349           float x = cosf(i * (M_PI/2) / n) * radius;
350           float y = sinf(i * (M_PI/2) / n) * radius;
351
352           glVertex2f(irect.get_left()  - x, irect.get_bottom() + y);
353           glVertex2f(irect.get_right() + x, irect.get_bottom() + y);
354         }
355       glEnd();
356       glEnable(GL_TEXTURE_2D);
357       glColor4f(1, 1, 1, 1);
358     }
359   else
360     {
361       float x = request.pos.x;
362       float y = request.pos.y;
363       float w = fillrectrequest->size.x;
364       float h = fillrectrequest->size.y;
365
366       glDisable(GL_TEXTURE_2D);
367       glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
368                 fillrectrequest->color.blue, fillrectrequest->color.alpha);
369
370       glBegin(GL_QUADS);
371       glVertex2f(x, y);
372       glVertex2f(x+w, y);
373       glVertex2f(x+w, y+h);
374       glVertex2f(x, y+h);
375       glEnd();
376       glEnable(GL_TEXTURE_2D);
377       glColor4f(1, 1, 1, 1);
378     }
379 }
380
381 void
382 Renderer::draw_inverse_ellipse(const DrawingRequest& request)
383 {
384   const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
385
386   glDisable(GL_TEXTURE_2D);
387   glColor4f(ellipse->color.red,  ellipse->color.green,
388             ellipse->color.blue, ellipse->color.alpha);
389     
390   float x = request.pos.x;
391   float y = request.pos.y;
392   float w = ellipse->size.x/2.0f;
393   float h = ellipse->size.y/2.0f;
394
395   static const int slices = 16;
396   static const int points = (slices+1) * 12;
397
398   float vertices[points * 2];
399   int   p = 0;
400
401   // Bottom
402   vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
403   vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
404   vertices[p++] = x;            vertices[p++] = y+h;
405
406   // Top
407   vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
408   vertices[p++] = 0;            vertices[p++] = 0;
409   vertices[p++] = x;            vertices[p++] = y-h;
410
411   // Left
412   vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
413   vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
414   vertices[p++] = x+w;          vertices[p++] = y;
415
416   // Right
417   vertices[p++] = 0;            vertices[p++] = 0;
418   vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
419   vertices[p++] = x-w;          vertices[p++] = y;
420
421   for(int i = 0; i < slices; ++i)
422     {
423       float ex1 = sinf(M_PI/2 / slices * i) * w;
424       float ey1 = cosf(M_PI/2 / slices * i) * h;
425
426       float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
427       float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
428
429       // Bottom/Right
430       vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
431       vertices[p++] = x + ex1;      vertices[p++] = y + ey1;
432       vertices[p++] = x + ex2;      vertices[p++] = y + ey2;
433
434       // Top/Left
435       vertices[p++] = 0;            vertices[p++] = 0;
436       vertices[p++] = x - ex1;      vertices[p++] = y - ey1;
437       vertices[p++] = x - ex2;      vertices[p++] = y - ey2;
438
439       // Top/Right
440       vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
441       vertices[p++] = x + ex1;      vertices[p++] = y - ey1;
442       vertices[p++] = x + ex2;      vertices[p++] = y - ey2;
443
444       // Bottom/Left
445       vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
446       vertices[p++] = x - ex1;      vertices[p++] = y + ey1;
447       vertices[p++] = x - ex2;      vertices[p++] = y + ey2;
448     }
449
450   glDisable(GL_TEXTURE_COORD_ARRAY);
451   glVertexPointer(2, GL_FLOAT, 0, vertices);
452
453   glDrawArrays(GL_TRIANGLES, 0, points);
454
455   glEnable(GL_TEXTURE_COORD_ARRAY);
456
457   glEnable(GL_TEXTURE_2D);
458   glColor4f(1, 1, 1, 1);    
459 }
460
461 void 
462 Renderer::do_take_screenshot()
463 {
464   // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
465
466   SDL_Surface *shot_surf;
467   // create surface to hold screenshot
468 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
469   shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
470 #else
471   shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
472 #endif
473   if (!shot_surf) {
474     log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
475     return;
476   }
477
478   // read pixels into array
479   char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
480   if (!pixels) {
481     log_warning << "Could not allocate memory to store screenshot" << std::endl;
482     SDL_FreeSurface(shot_surf);
483     return;
484   }
485   glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
486
487   // copy array line-by-line
488   for (int i = 0; i < SCREEN_HEIGHT; i++) {
489     char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
490     if(SDL_MUSTLOCK(shot_surf))
491       {
492         SDL_LockSurface(shot_surf);
493       }
494     char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
495     memcpy(dst, src, 3 * SCREEN_WIDTH);
496     if(SDL_MUSTLOCK(shot_surf))
497       {
498         SDL_UnlockSurface(shot_surf);
499       }
500   }
501
502   // free array
503   delete[](pixels);
504
505   // save screenshot
506   static const std::string writeDir = PHYSFS_getWriteDir();
507   static const std::string dirSep = PHYSFS_getDirSeparator();
508   static const std::string baseName = "screenshot";
509   static const std::string fileExt = ".bmp";
510   std::string fullFilename;
511   for (int num = 0; num < 1000; num++) {
512     std::ostringstream oss;
513     oss << baseName;
514     oss << std::setw(3) << std::setfill('0') << num;
515     oss << fileExt;
516     std::string fileName = oss.str();
517     fullFilename = writeDir + dirSep + fileName;
518     if (!PHYSFS_exists(fileName.c_str())) {
519       SDL_SaveBMP(shot_surf, fullFilename.c_str());
520       log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
521       SDL_FreeSurface(shot_surf);
522       return;
523     }
524   }
525   log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
526   SDL_FreeSurface(shot_surf);
527 }
528
529 void
530 Renderer::flip()
531 {
532   assert_gl("drawing");
533   SDL_GL_SwapBuffers();
534 }
535
536 void
537 Renderer::resize(int w, int h)
538 {
539   // This causes the screen to go black, which is annoying, but seems
540   // unavoidable with SDL at the moment
541   SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_RESIZABLE);
542
543   config->window_width  = w;
544   config->window_height = h;
545
546   apply_config();
547 }
548
549 void
550 Renderer::apply_config()
551 {    
552   std::cout << "Applying Config:" 
553             << "\n  Desktop: " << desktop_width << "x" << desktop_height
554             << "\n  Window:  " << config->window_width << "x" << config->window_height
555             << "\n  FullRes: " << config->fullscreen_width << "x" << config->fullscreen_height
556             << "\n  Aspect:  " << config->aspect_width << ":" << config->aspect_height
557             << "\n  Magnif:  " << config->magnification
558             << std::endl;
559
560   int w,h;
561   float target_aspect  = float(config->aspect_width) / float(config->aspect_height);
562   float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
563   
564   if (desktop_width != -1 && desktop_height != -1)
565     {
566       desktop_aspect = float(desktop_width) / float(desktop_height);
567     }
568
569   if (config->use_fullscreen)
570     {
571       w = config->fullscreen_width;
572       h = config->fullscreen_height;
573       desktop_aspect = float(w) / float(h);
574     }
575   else
576     {
577       w = config->window_width;        
578       h = config->window_height;
579     }
580
581   if (target_aspect > 1.0f)
582     {
583       SCREEN_WIDTH  = static_cast<int> (w * (target_aspect / desktop_aspect));
584       SCREEN_HEIGHT = static_cast<int> (h);
585     }
586   else
587     {
588       SCREEN_WIDTH  = static_cast<int> (w);
589       SCREEN_HEIGHT = static_cast<int> (h  * (target_aspect / desktop_aspect));
590     }
591
592   SCREEN_WIDTH  = static_cast<int> ((float) SCREEN_WIDTH / config->magnification);
593   SCREEN_HEIGHT = static_cast<int> ((float) SCREEN_HEIGHT / config->magnification);
594
595   int max_width  = 1600; // FIXME: Maybe 1920 is ok too
596   int max_height = 1200;
597
598   if (0)
599     {
600       // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
601       // max_width/max_height
602       if (SCREEN_WIDTH > max_width || SCREEN_HEIGHT > max_height)
603         {
604           float scale1 = float(max_width)/SCREEN_WIDTH;
605           float scale2 = float(max_height)/SCREEN_HEIGHT;
606           float scale = scale1 < scale2 ? scale1 : scale2;
607           SCREEN_WIDTH  = static_cast<int> ((float) SCREEN_WIDTH * scale);
608           SCREEN_HEIGHT = static_cast<int> ((float) SCREEN_HEIGHT * scale);
609         }
610
611       glViewport(0, 0, w, h);
612     }
613   else
614     {
615       // This works by adding black borders around the screen to limit
616       // SCREEN_WIDTH/SCREEN_HEIGHT to max_width/max_height
617       int nw = w;
618       int nh = h;
619
620       if (SCREEN_WIDTH > max_width)
621         {
622           nw = static_cast<int> ((float) nw * float(max_width)/SCREEN_WIDTH);
623           SCREEN_WIDTH = static_cast<int> (max_width);
624         }
625
626       if (SCREEN_HEIGHT > max_height)
627         {
628           nh = static_cast<int> ((float) nh * float(max_height)/SCREEN_HEIGHT);
629           SCREEN_HEIGHT = static_cast<int> (max_height);
630         }
631
632       glClear(GL_COLOR_BUFFER_BIT);
633
634       std::cout << (w-nw)/2 << " "
635                 << (h-nh)/2 << " "
636                 << nw << "x" << nh << std::endl;
637       glViewport(std::max(0, (w-nw)/2), 
638                  std::max(0, (h-nh)/2), 
639                  std::min(nw, w),
640                  std::min(nh, h));
641     }
642
643   std::cout << "  -> " << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << std::endl;
644
645
646   glMatrixMode(GL_PROJECTION);
647   glLoadIdentity();
648   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);    
649 }
650
651 } // namespace GL
652
653 #endif