Optimized gradient when top color is the same as the bottom one.
[supertux.git] / src / screen / texture.cpp
index f7a50e9..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,7 +523,7 @@ SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
 }
 
 int
-SurfaceOpenGL::draw(float x, float y, Uint8 alpha, uint32_t effect)
+SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
 {
   float pw = power_of_two(w);
   float ph = power_of_two(h);
@@ -503,7 +578,7 @@ SurfaceOpenGL::draw(float x, float y, Uint8 alpha, uint32_t effect)
 }
 
 int
-SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, uint32_t effect)
+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));
@@ -618,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, uint32_t effect)
+SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
 {
   SDL_Rect dest;
 
@@ -671,7 +753,7 @@ SurfaceSDL::draw(float x, float y, Uint8 alpha, uint32_t effect)
 }
 
 int
-SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, uint32_t effect)
+SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
 {
   SDL_Rect src, dest;