Optimized gradient when top color is the same as the bottom one.
[supertux.git] / src / screen / texture.cpp
index 6813296..e92ad61 100644 (file)
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
 
-#include <assert.h>
+#include <cassert>
 #include <iostream>
 #include <algorithm>
+
 #include "SDL.h"
 #include "SDL_image.h"
+
 #include "texture.h"
 #include "globals.h"
 #include "setup.h"
@@ -56,6 +58,14 @@ SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h
     x(x_), y(y_), w(w_), h(h_)
 {}
 
+SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_)
+    : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
+{
+top_gradient = top_gradient_;
+bottom_gradient = bottom_gradient_;
+}
+
+
 SurfaceData::~SurfaceData()
 {
   SDL_FreeSurface(surface);
@@ -85,6 +95,8 @@ SurfaceData::create_SurfaceSDL()
     return new SurfaceSDL(file, x, y, w, h, use_alpha);
   case SURFACE:
     return new SurfaceSDL(surface, use_alpha);
+  case GRADIENT:
+    return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
   }
   assert(0);
 }
@@ -101,6 +113,8 @@ SurfaceData::create_SurfaceOpenGL()
     return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
   case SURFACE:
     return new SurfaceOpenGL(surface, use_alpha);
+  case GRADIENT:
+    return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
   }
 #endif
   assert(0);
@@ -156,6 +170,18 @@ Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_al
   surfaces.push_back(this);
 }
 
+Surface::Surface(Color top_background, Color bottom_background, int w, int h)
+    : data(top_background, bottom_background, w, h), w(0), h(0)
+{
+  impl = data.create();
+  if (impl)
+  {
+    w = impl->w;
+    h = impl->h;
+  }
+  surfaces.push_back(this);
+}
+
 void
 Surface::reload()
 {
@@ -335,6 +361,46 @@ sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
   return sdl_surface;
 }
 
+SDL_Surface*
+sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
+{
+  SDL_Surface* sdl_surface;
+
+  sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
+                    screen->format->BitsPerPixel, screen->format->Rmask,
+                    screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
+
+  if(sdl_surface == NULL)
+      st_abort("Cannot create surface for the gradient", "SURFACE");
+
+  if(top == bottom)
+    {
+    SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
+        top.red, top.green, top.blue));
+    }
+  else
+    {
+    float redstep = (float(bottom.red)-float(top.red)) / float(h);
+    float greenstep = (float(bottom.green)-float(top.green)) / float(h);
+    float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
+
+    SDL_Rect rect;
+    rect.x = 0;
+    rect.w = w;
+    rect.h = 1;
+    for(float y = 0; y < h; y++)
+      {
+      rect.y = (int)y;
+      SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
+          int(float(top.red) + redstep * y),
+          int(float(top.green) + greenstep * y),
+          int(float(top.blue) + bluestep * y)));
+      }
+    }
+
+  return sdl_surface;
+}
+
 //---------------------------------------------------------------------------
 
 SurfaceImpl::SurfaceImpl()
@@ -392,6 +458,15 @@ SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h
   h = sdl_surface->h;
 }
 
+SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
+{
+  sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
+  create_gl(sdl_surface, &gl_texture);
+
+  w = sdl_surface->w;
+  h = sdl_surface->h;
+}
+
 SurfaceOpenGL::~SurfaceOpenGL()
 {
   glDeleteTextures(1, &gl_texture);
@@ -448,11 +523,14 @@ SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
 }
 
 int
-SurfaceOpenGL::draw(float x, float y, Uint8 alpha)
+SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
 {
   float pw = power_of_two(w);
   float ph = power_of_two(h);
 
+  if(effect & SEMI_TRANSPARENT)
+    alpha = 128;
+
   glEnable(GL_TEXTURE_2D);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -462,13 +540,35 @@ SurfaceOpenGL::draw(float x, float y, Uint8 alpha)
   glBindTexture(GL_TEXTURE_2D, gl_texture);
 
   glBegin(GL_QUADS);
-  glTexCoord2f(0, 0);
-  glVertex2f(x, y);
-  glTexCoord2f((float)w / pw, 0);
-  glVertex2f((float)w+x, y);
-  glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)w+x, (float)h+y);
-  glTexCoord2f(0, (float)h / ph);
-  glVertex2f(x, (float)h+y);
+
+  if(effect & VERTICAL_FLIP)
+    {
+    glTexCoord2f(0, 0);
+    glVertex2f(x, (float)h+y);
+
+    glTexCoord2f((float)w / pw, 0);
+    glVertex2f((float)w+x, (float)h+y);
+
+    glTexCoord2f((float)w / pw, (float)h / ph);
+    glVertex2f((float)w+x, y);
+    
+    glTexCoord2f(0, (float)h / ph);
+    glVertex2f(x, y);
+    }
+  else
+    {
+    glTexCoord2f(0, 0);
+    glVertex2f(x, y);
+
+    glTexCoord2f((float)w / pw, 0);
+    glVertex2f((float)w+x, y);
+
+    glTexCoord2f((float)w / pw, (float)h / ph);
+    glVertex2f((float)w+x, (float)h+y);
+
+    glTexCoord2f(0, (float)h / ph);
+    glVertex2f(x, (float)h+y);
+    }
   glEnd();
 
   glDisable(GL_TEXTURE_2D);
@@ -478,11 +578,14 @@ SurfaceOpenGL::draw(float x, float y, Uint8 alpha)
 }
 
 int
-SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha)
+SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
 {
   float pw = power_of_two(int(this->w));
   float ph = power_of_two(int(this->h));
 
+  if(effect & SEMI_TRANSPARENT)
+    alpha = 128;
+
   glBindTexture(GL_TEXTURE_2D, gl_texture);
 
   glEnable(GL_BLEND);
@@ -494,14 +597,36 @@ SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h,
 
 
   glBegin(GL_QUADS);
-  glTexCoord2f(sx / pw, sy / ph);
-  glVertex2f(x, y);
-  glTexCoord2f((float)(sx + w) / pw, sy / ph);
-  glVertex2f(w+x, y);
-  glTexCoord2f((sx+w) / pw, (sy+h) / ph);
-  glVertex2f(w +x, h+y);
-  glTexCoord2f(sx / pw, (float)(sy+h) / ph);
-  glVertex2f(x, h+y);
+
+  if(effect & VERTICAL_FLIP)
+    {
+    glTexCoord2f(sx / pw, sy / ph);
+    glVertex2f(x, y);
+
+    glTexCoord2f((float)(sx + w) / pw, sy / ph);
+    glVertex2f(w+x, y);
+
+    glTexCoord2f((sx+w) / pw, (sy+h) / ph);
+    glVertex2f(w +x, h+y);
+
+    glTexCoord2f(sx / pw, (float)(sy+h) / ph);
+    glVertex2f(x, h+y);
+    }
+  else
+    {
+    glTexCoord2f(sx / pw, (float)(sy+h) / ph);
+    glVertex2f(x, h+y);
+
+    glTexCoord2f((sx+w) / pw, (sy+h) / ph);
+    glVertex2f(w +x, h+y);
+
+    glTexCoord2f((float)(sx + w) / pw, sy / ph);
+    glVertex2f(w+x, y);
+
+    glTexCoord2f(sx / pw, sy / ph);
+    glVertex2f(x, y);
+    }
+
   glEnd();
 
   glDisable(GL_TEXTURE_2D);
@@ -568,8 +693,15 @@ SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  int
   h = sdl_surface->h;
 }
 
+SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
+{
+  sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
+  w = sdl_surface->w;
+  h = sdl_surface->h;
+}
+
 int
-SurfaceSDL::draw(float x, float y, Uint8 alpha)
+SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
 {
   SDL_Rect dest;
 
@@ -578,6 +710,17 @@ SurfaceSDL::draw(float x, float y, Uint8 alpha)
   dest.w = w;
   dest.h = h;
 
+  if(effect & SEMI_TRANSPARENT)
+    alpha = 128;
+
+  if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
+    {
+    for(float sy = 0; sy < h; sy++)
+      if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
+        return -2;
+    return 0;
+    }
+
   if(alpha != 255)
     {
     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
@@ -610,7 +753,7 @@ SurfaceSDL::draw(float x, float y, Uint8 alpha)
 }
 
 int
-SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha)
+SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
 {
   SDL_Rect src, dest;
 
@@ -624,6 +767,17 @@ SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Ui
   dest.w = (int)w;
   dest.h = (int)h;
 
+  if(effect & SEMI_TRANSPARENT)
+    alpha = 128;
+
+  if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
+    {
+    for(float sy_ = sy; sy_ < h; sy_++)
+      if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
+        return -2;
+    return 0;
+    }
+
   if(alpha != 255)
     {
     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha