diff --git a/src/font.cpp b/src/font.cpp index 2de101f4ecf..1629a7d30c3 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -880,12 +880,8 @@ surface floating_label::create_surface() } else { // background is blurred shadow of the text - // we don't parse markup here to guarantee a black shadow - // FIXME: using color mark-up will cause colored shadow - surface background = font::render_text(text_, font_size_, font::BLACK_COLOUR, 0, true); //TODO add a little extra space for the blur of letters with a lower part - background = blur_alpha_surface(background, 2, false); - background = adjust_surface_alpha(background, ftofxp(4.0), false); + surface background = shadow_image(foreground, false); if (background == NULL) { ERR_FT << "could not create floating label's shadow" << std::endl; diff --git a/src/sdl_utils.cpp b/src/sdl_utils.cpp index 68870155b90..7adfbb2cd3c 100644 --- a/src/sdl_utils.cpp +++ b/src/sdl_utils.cpp @@ -635,6 +635,44 @@ surface darken_image(surface const &surf, bool optimize) return optimize ? create_optimized_surface(nsurf) : nsurf; } +surface shadow_image(surface const &surf, bool optimize) +{ + if(surf == NULL) + return NULL; + + // we blur it, and reuse the neutral surface created by the blur function (optimized = false) + surface nsurf (blur_alpha_surface(surf, 2, false)); + + if(nsurf == NULL) { + std::cerr << "failed to blur the shadow surface\n"; + return NULL; + } + + { + surface_lock lock(nsurf); + Uint32* beg = lock.pixels(); + Uint32* end = beg + nsurf->w*surf->h; + + while(beg != end) { + Uint8 alpha = (*beg) >> 24; + + if(alpha) { + // increase alpha and color in black (RGB=0) + // with some stupid optimization for handling maximum values + if (alpha < 255/4) + *beg = (alpha*4) << 24; + else + *beg = 0xFF000000; // we hit the maximum + } + + ++beg; + } + } + + return optimize ? create_optimized_surface(nsurf) : nsurf; +} + + surface recolor_image(surface surf, const std::map& map_rgb, bool optimize){ if(map_rgb.size()){ if(surf == NULL) diff --git a/src/sdl_utils.hpp b/src/sdl_utils.hpp index ffa8efcd6ef..03b85cb0c46 100644 --- a/src/sdl_utils.hpp +++ b/src/sdl_utils.hpp @@ -170,6 +170,8 @@ surface scale_surface_blended(surface const &surf, int w, int h, bool optimize=t surface adjust_surface_colour(surface const &surf, int r, int g, int b, bool optimize=true); surface greyscale_image(surface const &surf, bool optimize=true); surface darken_image(surface const &surf, bool optimize=true); +/** create an heavy shadow of the image, by blurring, increasing alpha and darkening */ +surface shadow_image(surface const &surf, bool optimize=true); surface recolor_image(surface surf, const std::map& map_rgb, bool optimize=true); surface brighten_image(surface const &surf, fixed_t amount, bool optimize=true);