Getting rid of nasty tabs
[supertux.git] / src / video / gl_renderer.cpp
index b9a2354..c39744a 100644 (file)
 #include "obstack/obstackpp.hpp"
 #define LIGHTMAP_DIV 5
 
+#ifdef GL_VERSION_ES_CM_1_0
+#  define glOrtho glOrthof
+#endif
+
 namespace 
 {
 
@@ -99,11 +103,11 @@ inline void intern_draw(float left, float top, float right, float bottom,
     bottom -= center_y;
 
     float vertices[] = {
-               left*ca - top*sa + center_x, left*sa + top*ca + center_y,
-               right*ca - top*sa + center_x, right*sa + top*ca + center_y,
-               right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
-               left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
-       };
+        left*ca - top*sa + center_x, left*sa + top*ca + center_y,
+        right*ca - top*sa + center_x, right*sa + top*ca + center_y,
+        right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
+        left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
+    };
     glVertexPointer(2, GL_FLOAT, 0, vertices);
 
     float uvs[] = {
@@ -154,7 +158,7 @@ Renderer::Renderer()
 
   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
-  // Hu? 16bit rendering?
+  // FIXME: Hu? 16bit rendering?
   SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   5);
   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  5);
@@ -162,6 +166,7 @@ Renderer::Renderer()
   int flags = SDL_OPENGL;
   int width;
   int height;
+
   if(config->use_fullscreen)
     {
       flags |= SDL_FULLSCREEN;
@@ -174,9 +179,10 @@ Renderer::Renderer()
       width  = config->window_width;
       height = config->window_height;
     }
-  int bpp = 0;
 
+  int bpp = 0;
   SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
+
   if(screen == 0) {
     std::stringstream msg;
     msg << "Couldn't set video mode (" << width << "x" << height
@@ -189,24 +195,13 @@ Renderer::Renderer()
   glDisable(GL_CULL_FACE);
   glEnable(GL_TEXTURE_2D);
   glEnable(GL_BLEND);
-  glEnable(GL_VERTEX_ARRAY);
-  glEnable(GL_TEXTURE_COORD_ARRAY);
+  glEnableClientState(GL_VERTEX_ARRAY);
+  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-  glViewport(0, 0, screen->w, screen->h);
-  glMatrixMode(GL_PROJECTION);
-  glLoadIdentity();
-
-  // logical resolution here not real monitor resolution
-  glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
-
-  glMatrixMode(GL_MODELVIEW);
-  glLoadIdentity();
-  glTranslatef(0, 0, 0);
-
-  check_gl_error("Setting up view matrices");
-
-
+  // Init the projection matrix, viewport and stuff
+  apply_config();
+  
   if(texture_manager == 0)
     texture_manager = new TextureManager();
   else
@@ -280,8 +275,8 @@ Renderer::draw_gradient(const DrawingRequest& request)
   const Color& bottom = gradientrequest->bottom;
 
   glDisable(GL_TEXTURE_2D);
-  glDisable(GL_TEXTURE_COORD_ARRAY);
-  glEnable(GL_COLOR_ARRAY);
+  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+  glEnableClientState(GL_COLOR_ARRAY);
 
   float vertices[] = {
     0, 0,
@@ -301,8 +296,8 @@ Renderer::draw_gradient(const DrawingRequest& request)
 
   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
-  glDisable(GL_COLOR_ARRAY);
-  glEnable(GL_TEXTURE_COORD_ARRAY);
+  glDisableClientState(GL_COLOR_ARRAY);
+  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
   glEnable(GL_TEXTURE_2D);
   glColor4f(1, 1, 1, 1);
@@ -366,7 +361,7 @@ Renderer::draw_filled_rect(const DrawingRequest& request)
       glDisable(GL_TEXTURE_2D);
       glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
                 fillrectrequest->color.blue, fillrectrequest->color.alpha);
-      glDisable(GL_TEXTURE_COORD_ARRAY);
+      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
       float vertices[] = {
         x,   y,
@@ -378,7 +373,7 @@ Renderer::draw_filled_rect(const DrawingRequest& request)
 
       glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
-      glEnable(GL_TEXTURE_COORD_ARRAY);
+      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
       glEnable(GL_TEXTURE_2D);
       glColor4f(1, 1, 1, 1);
     }
@@ -453,12 +448,12 @@ Renderer::draw_inverse_ellipse(const DrawingRequest& request)
       vertices[p++] = x - ex2;      vertices[p++] = y + ey2;
     }
 
-  glDisable(GL_TEXTURE_COORD_ARRAY);
+  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   glVertexPointer(2, GL_FLOAT, 0, vertices);
 
   glDrawArrays(GL_TRIANGLES, 0, points);
 
-  glEnable(GL_TEXTURE_COORD_ARRAY);
+  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
   glEnable(GL_TEXTURE_2D);
   glColor4f(1, 1, 1, 1);    
@@ -555,16 +550,23 @@ Renderer::resize(int w, int h)
 void
 Renderer::apply_config()
 {    
-  std::cout << "Applying Config:" 
-            << "\n  Desktop: " << desktop_width << "x" << desktop_height
-            << "\n  Window:  " << config->window_width << "x" << config->window_height
-            << "\n  FullRes: " << config->fullscreen_width << "x" << config->fullscreen_height
-            << "\n  Aspect:  " << config->aspect_width << ":" << config->aspect_height
-            << "\n  Magnif:  " << config->magnification
-            << std::endl;
+  if (0)
+    {
+      std::cout << "Applying Config:" 
+                << "\n  Desktop: " << desktop_width << "x" << desktop_height
+                << "\n  Window:  " << config->window_width << "x" << config->window_height
+                << "\n  FullRes: " << config->fullscreen_width << "x" << config->fullscreen_height
+                << "\n  Aspect:  " << config->aspect_width << ":" << config->aspect_height
+                << "\n  Magnif:  " << config->magnification
+                << std::endl;
+    }
 
   int w,h;
-  float target_aspect  = float(config->aspect_width) / float(config->aspect_height);
+  float target_aspect = float(desktop_width) / desktop_height;
+  
+  if (config->aspect_width != 0 && config->aspect_height != 0)
+    target_aspect = float(config->aspect_width) / float(config->aspect_height);
+
   float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
   
   if (desktop_width != -1 && desktop_height != -1)
@@ -572,6 +574,7 @@ Renderer::apply_config()
       desktop_aspect = float(desktop_width) / float(desktop_height);
     }
 
+  // Get the screen width
   if (config->use_fullscreen)
     {
       w = config->fullscreen_width;
@@ -586,38 +589,38 @@ Renderer::apply_config()
 
   if (target_aspect > 1.0f)
     {
-      SCREEN_WIDTH  = static_cast<int> (w * (target_aspect / desktop_aspect));
-      SCREEN_HEIGHT = static_cast<int> (h);
+      SCREEN_WIDTH  = static_cast<int>(w * (target_aspect / desktop_aspect));
+      SCREEN_HEIGHT = static_cast<int>(h);
     }
   else
     {
-      SCREEN_WIDTH  = static_cast<int> (w);
-      SCREEN_HEIGHT = static_cast<int> (h  * (target_aspect / desktop_aspect));
+      SCREEN_WIDTH  = static_cast<int>(w);
+      SCREEN_HEIGHT = static_cast<int>(h  * (target_aspect / desktop_aspect));
     }
 
-  SCREEN_WIDTH  = static_cast<int> ((float) SCREEN_WIDTH / config->magnification);
-  SCREEN_HEIGHT = static_cast<int> ((float) SCREEN_HEIGHT / config->magnification);
-
   int max_width  = 1600; // FIXME: Maybe 1920 is ok too
   int max_height = 1200;
 
-  if (0)
+  if (config->magnification == 0.0f) // Magic value that means 'minfill'
     {
       // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
       // max_width/max_height
       if (SCREEN_WIDTH > max_width || SCREEN_HEIGHT > max_height)
         {
-          float scale1 = float(max_width)/SCREEN_WIDTH;
-          float scale2 = float(max_height)/SCREEN_HEIGHT;
-          float scale = scale1 < scale2 ? scale1 : scale2;
-          SCREEN_WIDTH  = static_cast<int> ((float) SCREEN_WIDTH * scale);
-          SCREEN_HEIGHT = static_cast<int> ((float) SCREEN_HEIGHT * scale);
+          float scale1  = float(max_width)/SCREEN_WIDTH;
+          float scale2  = float(max_height)/SCREEN_HEIGHT;
+          float scale   = (scale1 < scale2) ? scale1 : scale2;
+          SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  * scale);
+          SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
         }
 
       glViewport(0, 0, w, h);
     }
   else
     {
+      SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  / config->magnification);
+      SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / config->magnification);
+
       // This works by adding black borders around the screen to limit
       // SCREEN_WIDTH/SCREEN_HEIGHT to max_width/max_height
       int nw = w;
@@ -625,33 +628,45 @@ Renderer::apply_config()
 
       if (SCREEN_WIDTH > max_width)
         {
-          nw = static_cast<int> ((float) nw * float(max_width)/SCREEN_WIDTH);
-          SCREEN_WIDTH = static_cast<int> (max_width);
+          nw = static_cast<int>((float) nw * float(max_width)/SCREEN_WIDTH);
+          SCREEN_WIDTH = static_cast<int>(max_width);
         }
 
       if (SCREEN_HEIGHT > max_height)
         {
-          nh = static_cast<int> ((float) nh * float(max_height)/SCREEN_HEIGHT);
-          SCREEN_HEIGHT = static_cast<int> (max_height);
+          nh = static_cast<int>((float) nh * float(max_height)/SCREEN_HEIGHT);
+          SCREEN_HEIGHT = static_cast<int>(max_height);
         }
 
+      // Clear both buffers so that we get a clean black border without junk
+      glClear(GL_COLOR_BUFFER_BIT);
+      SDL_GL_SwapBuffers();
       glClear(GL_COLOR_BUFFER_BIT);
+      SDL_GL_SwapBuffers();
+
+      if (0)
+        std::cout << (w-nw)/2 << " "
+                  << (h-nh)/2 << " "
+                  << nw << "x" << nh << std::endl;
 
-      std::cout << (w-nw)/2 << " "
-                << (h-nh)/2 << " "
-                << nw << "x" << nh << std::endl;
       glViewport(std::max(0, (w-nw)/2), 
                  std::max(0, (h-nh)/2), 
                  std::min(nw, w),
                  std::min(nh, h));
     }
 
-  std::cout << "  -> " << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << std::endl;
-
+  if (0)
+    std::cout << "  -> " << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << std::endl;
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
-  glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);    
+
+  glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
+
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+  glTranslatef(0, 0, 0);
+  check_gl_error("Setting up view matrices");
 }
 
 } // namespace GL