make function more robust
[supertux.git] / lib / video / surface.cpp
index 6b8b773..8385766 100644 (file)
@@ -18,6 +18,8 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
 
+#include <config.h>
+
 #include <cassert>
 #include <iostream>
 #include <algorithm>
@@ -56,16 +58,18 @@ SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
     : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
 {}
 
-SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
+SurfaceData::SurfaceData(const std::string& file_, int x_, int y_,
+    int w_, int h_, bool use_alpha_)
     : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
     x(x_), y(y_), w(w_), h(h_)
 {}
 
-SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int 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_;
+  top_gradient = top_gradient_;
+  bottom_gradient = bottom_gradient_;
 }
 
 
@@ -194,12 +198,21 @@ Surface::reload()
   {
     w = impl->w;
     h = impl->h;
+    for(std::vector<SurfaceData::Filter>::iterator i =
+        data.applied_filters.begin(); i != data.applied_filters.end();
+        i++)
+      impl->apply_filter(i->type, i->color);
   }
 }
 
-void Surface::apply_mask(Color color)
+void Surface::apply_filter(int filter, Color color)
 {
-impl->apply_mask(color);
+  impl->apply_filter(filter, color);
+
+  SurfaceData::Filter apply_filter;
+  apply_filter.type = filter;
+  apply_filter.color = color;
+  data.applied_filters.push_back(apply_filter);
 }
 
 Surface::~Surface()
@@ -255,7 +268,25 @@ Surface::resize(int w_, int h_)
 void
 apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
 {
-if(filter == MASK_FILTER)
+if(filter == HORIZONTAL_FLIP_FILTER)
+  {
+  SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
+  SDL_BlitSurface(surface, NULL, sur_copy, NULL);
+  SDL_SetAlpha(sur_copy,0,0);
+
+  SDL_Rect src, dst;
+  src.y = dst.y = 0;
+  src.w = dst.w = 1;
+  src.h = dst.h = sur_copy->h;
+  for(int x = 0; x < sur_copy->w; x++)
+    {
+    src.x = x; dst.x = sur_copy->w-1 - x;
+    SDL_BlitSurface(sur_copy, &src, surface, &dst);
+    }
+
+  SDL_FreeSurface(sur_copy);
+  }
+else if(filter == MASK_FILTER)
   {
   SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
 
@@ -402,22 +433,18 @@ SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
 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,
+  SDL_Surface* sdl_surface
+    = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
                     screen->format->BitsPerPixel, screen->format->Rmask,
                     screen->format->Gmask, screen->format->Bmask, 0);
 
-  if(sdl_surface == NULL)
+  if(sdl_surface == 0)
       Termination::abort("Cannot create surface for the gradient", "SURFACE");
 
-  if(top == bottom)
-    {
+  if(top == bottom) {
     SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
         top.red, top.green, top.blue));
-    }
-  else
-    {
+  } 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);
@@ -426,15 +453,14 @@ sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
     rect.x = 0;
     rect.w = w;
     rect.h = 1;
-    for(float y = 0; y < h; y++)
-      {
+    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)));
-      }
+            int(float(top.red) + redstep * y),
+            int(float(top.green) + greenstep * y),
+            int(float(top.blue) + bluestep * y)));
     }
+  }
 
   return sdl_surface;
 }
@@ -487,23 +513,23 @@ SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
   h = sdl_surface->h;
 }
 
-SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
+SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_,
+    int w_, int h_, bool use_alpha_)
 {
   sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
   
   create_gl(sdl_surface, &gl_texture);
-
   w = sdl_surface->w;
-  h = sdl_surface->h;
+  h = sdl_surface->h;  
 }
 
-SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int 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);
+  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;
+  h = sdl_surface->h;  
 }
 
 SurfaceOpenGL::~SurfaceOpenGL()
@@ -757,9 +783,6 @@ SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h,
 int
 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
 {
-  if(effect & SEMI_TRANSPARENT)
-    alpha = 128;
-
   float pw = power_of_two(sw);
   float ph = power_of_two(sh);
 
@@ -841,9 +864,9 @@ SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uin
 }
 
 void
-SurfaceOpenGL::apply_mask(Color color)
+SurfaceOpenGL::apply_filter(int filter, Color color)
 {
-  ::apply_filter_to_surface(sdl_surface, MASK_FILTER, color);
+  ::apply_filter_to_surface(sdl_surface, filter, color);
   create_gl(sdl_surface,&gl_texture);
 
   w = sdl_surface->w;
@@ -866,18 +889,20 @@ SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
   h = sdl_surface->h;
 }
 
-SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  bool use_alpha)
+SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int _w, int _h,
+    bool use_alpha)
 {
-  sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
+  sdl_surface = sdl_surface_part_from_file(file, x, y, _w, _h, use_alpha);
   w = sdl_surface->w;
-  h = sdl_surface->h;
+  h = sdl_surface->h;  
 }
 
-SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int 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);
+  sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
   w = sdl_surface->w;
-  h = sdl_surface->h;
+  h = sdl_surface->h;  
 }
 
 int
@@ -995,10 +1020,10 @@ SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Ui
     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
       to temp sur, blit the temp into the screen */
     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
-      already have an alpha mask yet... */
+      already have an alpha mask, yet... */
 
     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
-                                    sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
+                                    (int)w, (int)h, sdl_surface->format->BitsPerPixel,
                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
                                     sdl_surface->format->Bmask,
                                     0);
@@ -1007,7 +1032,7 @@ SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Ui
     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
 
 
-    SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
+    SDL_BlitSurface(sdl_surface, &src, sdl_surface_copy, NULL);
     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
 
     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
@@ -1053,9 +1078,9 @@ SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32
 }
 
 void
-SurfaceSDL::apply_mask(Color color)
+SurfaceSDL::apply_filter(int filter, Color color)
 {
-  ::apply_filter_to_surface(sdl_surface, MASK_FILTER, color);
+  ::apply_filter_to_surface(sdl_surface, filter, color);
 
   w = sdl_surface->w;
   h = sdl_surface->h;