- rest of 64bit fix.. missed some vars
[supertux.git] / lib / video / surface.cpp
index 1adff4f..718fbeb 100644 (file)
@@ -26,6 +26,7 @@
 #include "SDL_image.h"
 
 #include "../video/surface.h"
+#include "../video/screen.h"
 #include "../app/globals.h"
 #include "../app/setup.h"
 
@@ -196,6 +197,11 @@ Surface::reload()
   }
 }
 
+void Surface::apply_mask(Color color)
+{
+impl->apply_mask(color);
+}
+
 Surface::~Surface()
 {
 #ifdef DEBUG
@@ -246,6 +252,32 @@ Surface::resize(int w_, int h_)
   }
 }
 
+void
+apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
+{
+if(filter == MASK_FILTER)
+  {
+  SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
+
+  Uint8 r,g,b,a;
+
+  SDL_LockSurface(sur_copy);
+  for(int x = 0; x < sur_copy->w; x++)
+    for(int y = 0; y < sur_copy->h; y++)
+      {
+      SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
+      if(a != 0)
+        {
+        putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
+        }
+      }
+  SDL_UnlockSurface(sur_copy);
+
+  SDL_BlitSurface(sur_copy, NULL, surface, NULL);
+  SDL_FreeSurface(sur_copy);
+  }
+}
+
 SDL_Surface*
 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h,  bool use_alpha)
 {
@@ -330,6 +362,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;
 
@@ -341,18 +374,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");
@@ -804,6 +840,16 @@ SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uin
   return 0;
 }
 
+void
+SurfaceOpenGL::apply_mask(Color color)
+{
+  ::apply_filter_to_surface(sdl_surface, MASK_FILTER, color);
+  create_gl(sdl_surface,&gl_texture);
+
+  w = sdl_surface->w;
+  h = sdl_surface->h;
+}
+
 #endif
 
 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
@@ -949,10 +995,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);
@@ -961,7 +1007,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);
@@ -1006,6 +1052,15 @@ SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32
   return ret;
 }
 
+void
+SurfaceSDL::apply_mask(Color color)
+{
+  ::apply_filter_to_surface(sdl_surface, MASK_FILTER, color);
+
+  w = sdl_surface->w;
+  h = sdl_surface->h;
+}
+
 SurfaceSDL::~SurfaceSDL()
 {}