Bugfix: Fixed the visible black pixel, for instance, when jumping facing left.
[supertux.git] / lib / video / surface.cpp
index 73dc131..c72b5a3 100644 (file)
@@ -194,12 +194,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,26 +264,44 @@ 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);
+
   Uint8 r,g,b,a;
-  SDL_Rect rect;
-  rect.w = rect.h = 1;
-  SDL_LockSurface(surface);
-  for(int x = 0; x < surface->w; x++)
-    for(int y = 0; y < surface->h; y++)
+
+  SDL_LockSurface(sur_copy);
+  for(int x = 0; x < sur_copy->w; x++)
+    for(int y = 0; y < sur_copy->h; y++)
       {
-//    SDL_LockSurface(surface);
-      SDL_GetRGBA(getpixel(surface,x,y), surface->format, &r,&g,&b,&a);
-//    SDL_UnlockSurface(surface);
+      SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
       if(a != 0)
         {
-      putpixel(surface, x,y, color.map_rgba(surface));
-//        rect.x = x; rect.y = y;
-//        SDL_FillRect(surface, &rect, color.map_rgba(surface));
+        putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
         }
       }
-  SDL_UnlockSurface(surface);
+  SDL_UnlockSurface(sur_copy);
+
+  SDL_BlitSurface(sur_copy, NULL, surface, NULL);
+  SDL_FreeSurface(sur_copy);
   }
 }
 
@@ -362,6 +389,7 @@ SDL_Surface*
 SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
 {
   SDL_Surface* sdl_surface;
+#if 0
   Uint32 saved_flags;
   Uint8  saved_alpha;
 
@@ -373,18 +401,21 @@ SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
   {
     SDL_SetAlpha(sdl_surf, 0, 0);
   }
+#endif
 
   if(use_alpha == false && !use_gl)
     sdl_surface = SDL_DisplayFormat(sdl_surf);
   else
     sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
 
+#if 0
   /* Restore the alpha blending attributes */
   if ( (saved_flags & SDL_SRCALPHA)
        == SDL_SRCALPHA )
   {
     SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
   }
+#endif
 
   if (sdl_surface == NULL)
     Termination::abort("Can't covert to display format", "SURFACE");
@@ -753,9 +784,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);
 
@@ -837,9 +865,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;
@@ -991,10 +1019,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);
@@ -1003,7 +1031,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);
@@ -1049,9 +1077,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;