New grow and skid sounds from remaxim
[supertux.git] / src / video / gl_renderer.cpp
index e182988..c861595 100644 (file)
@@ -103,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[] = {
@@ -151,10 +151,12 @@ Renderer::Renderer()
   if(texture_manager != 0)
     texture_manager->save_textures();
 
+#ifdef SDL_GL_SWAP_CONTROL
   if(config->try_vsync) {
     /* we want vsync for smooth scrolling */
     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
   }
+#endif
 
   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
@@ -175,7 +177,7 @@ Renderer::Renderer()
     }
   else
     {
-      flags |= SDL_RESIZABLE;
+//      flags |= SDL_RESIZABLE;
       width  = config->window_width;
       height = config->window_height;
     }
@@ -195,8 +197,8 @@ 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);
 
   // Init the projection matrix, viewport and stuff
@@ -261,7 +263,7 @@ Renderer::draw_surface_part(const DrawingRequest& request)
               uv_bottom,
               0.0,
               request.alpha,
-              Color(1.0, 1.0, 1.0),
+              request.color,
               Blend(),
               request.drawing_effect);
 }
@@ -275,8 +277,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,
@@ -296,8 +298,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);
@@ -309,6 +311,11 @@ Renderer::draw_filled_rect(const DrawingRequest& request)
   const FillRectRequest* fillrectrequest
     = (FillRectRequest*) request.request_data;
 
+  glDisable(GL_TEXTURE_2D);
+  glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
+            fillrectrequest->color.blue, fillrectrequest->color.alpha);
+  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+  
   if (fillrectrequest->radius != 0.0f)
     {
       // draw round rect
@@ -324,32 +331,37 @@ Renderer::draw_filled_rect(const DrawingRequest& request)
                  request.pos.x + fillrectrequest->size.x - radius,
                  request.pos.y + fillrectrequest->size.y - radius);
 
-      glDisable(GL_TEXTURE_2D);
-      glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
-                fillrectrequest->color.blue, fillrectrequest->color.alpha);
-
 
       int n = 8;
-      glBegin(GL_QUAD_STRIP);
+      int p = 0;
+      float vertices[(n+1) * 4 * 2];
+
       for(int i = 0; i <= n; ++i)
         {
           float x = sinf(i * (M_PI/2) / n) * radius;
           float y = cosf(i * (M_PI/2) / n) * radius;
 
-          glVertex2f(irect.get_left()  - x, irect.get_top() - y);
-          glVertex2f(irect.get_right() + x, irect.get_top() - y);
+          vertices[p++] = irect.get_left() - x;
+          vertices[p++] = irect.get_top()  - y;
+
+          vertices[p++] = irect.get_right() + x;
+          vertices[p++] = irect.get_top()   - y;
         }
+
       for(int i = 0; i <= n; ++i)
         {
           float x = cosf(i * (M_PI/2) / n) * radius;
           float y = sinf(i * (M_PI/2) / n) * radius;
 
-          glVertex2f(irect.get_left()  - x, irect.get_bottom() + y);
-          glVertex2f(irect.get_right() + x, irect.get_bottom() + y);
+          vertices[p++] = irect.get_left()   - x;
+          vertices[p++] = irect.get_bottom() + y;
+
+          vertices[p++] = irect.get_right()  + x;
+          vertices[p++] = irect.get_bottom() + y;
         }
-      glEnd();
-      glEnable(GL_TEXTURE_2D);
-      glColor4f(1, 1, 1, 1);
+
+      glVertexPointer(2, GL_FLOAT, 0, vertices);
+      glDrawArrays(GL_TRIANGLE_STRIP, 0,  sizeof(vertices)/sizeof(float)/2);
     }
   else
     {
@@ -358,11 +370,6 @@ Renderer::draw_filled_rect(const DrawingRequest& request)
       float w = fillrectrequest->size.x;
       float h = fillrectrequest->size.y;
 
-      glDisable(GL_TEXTURE_2D);
-      glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
-                fillrectrequest->color.blue, fillrectrequest->color.alpha);
-      glDisable(GL_TEXTURE_COORD_ARRAY);
-
       float vertices[] = {
         x,   y,
         x+w, y,
@@ -372,11 +379,11 @@ Renderer::draw_filled_rect(const DrawingRequest& request)
       glVertexPointer(2, GL_FLOAT, 0, vertices);
 
       glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-      glEnable(GL_TEXTURE_COORD_ARRAY);
-      glEnable(GL_TEXTURE_2D);
-      glColor4f(1, 1, 1, 1);
     }
+
+  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+  glEnable(GL_TEXTURE_2D);
+  glColor4f(1, 1, 1, 1);
 }
 
 void
@@ -448,12 +455,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);    
@@ -539,7 +546,7 @@ Renderer::resize(int w, int h)
 {
   // This causes the screen to go black, which is annoying, but seems
   // unavoidable with SDL at the moment
-  SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_RESIZABLE);
+  SDL_SetVideoMode(w, h, 0, SDL_OPENGL /*| SDL_RESIZABLE*/);
 
   config->window_width  = w;
   config->window_height = h;
@@ -562,7 +569,11 @@ Renderer::apply_config()
     }
 
   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)
@@ -594,13 +605,10 @@ Renderer::apply_config()
       SCREEN_HEIGHT = static_cast<int>(h  * (target_aspect / desktop_aspect));
     }
 
-  SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  / config->magnification);
-  SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / config->magnification);
-
   int max_width  = 1600; // FIXME: Maybe 1920 is ok too
   int max_height = 1200;
 
-  if (config->fill_screen)
+  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
@@ -617,6 +625,9 @@ Renderer::apply_config()
     }
   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;
@@ -634,8 +645,11 @@ Renderer::apply_config()
           SCREEN_HEIGHT = static_cast<int>(max_height);
         }
 
-      // Clear so that we get a clean black border without junk
+      // 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 << " "