Skip to content

Commit

Permalink
Move the blend_rgb() method to sdl/utils and rename it to blend_rgba.
Browse files Browse the repository at this point in the history
Added support for an alpha value.
  • Loading branch information
Fabian Müller committed Dec 13, 2014
1 parent 31201a3 commit 18452f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
18 changes: 4 additions & 14 deletions src/attack_prediction_display.cpp
Expand Up @@ -505,16 +505,16 @@ void battle_prediction_pane::get_hp_distrib_surface(const std::vector<std::pair<
int bar_len = std::max<int>(static_cast<int>((prob * (bar_space - 4)) + 0.5), 2);

SDL_Rect bar_rect_1 = sdl::create_rect(hp_sep + 4, 6 + (fs + 2) * i, bar_len, 8);
sdl::fill_rect(surf, &bar_rect_1, blend_rgb(surf, row_color.r, row_color.g, row_color.b, 100));
sdl::fill_rect(surf, &bar_rect_1, blend_rgba(surf, row_color.r, row_color.g, row_color.b, row_color.unused, 100));

SDL_Rect bar_rect_2 = sdl::create_rect(hp_sep + 4, 7 + (fs + 2) * i, bar_len, 6);
sdl::fill_rect(surf, &bar_rect_2, blend_rgb(surf, row_color.r, row_color.g, row_color.b, 66));
sdl::fill_rect(surf, &bar_rect_2, blend_rgba(surf, row_color.r, row_color.g, row_color.b, row_color.unused, 66));

SDL_Rect bar_rect_3 = sdl::create_rect(hp_sep + 4, 8 + (fs + 2) * i, bar_len, 4);
sdl::fill_rect(surf, &bar_rect_3, blend_rgb(surf, row_color.r, row_color.g, row_color.b, 33));
sdl::fill_rect(surf, &bar_rect_3, blend_rgba(surf, row_color.r, row_color.g, row_color.b, row_color.unused, 33));

SDL_Rect bar_rect_4 = sdl::create_rect(hp_sep + 4, 9 + (fs + 2) * i, bar_len, 2);
sdl::fill_rect(surf, &bar_rect_4, blend_rgb(surf, row_color.r, row_color.g, row_color.b, 0));
sdl::fill_rect(surf, &bar_rect_4, blend_rgba(surf, row_color.r, row_color.g, row_color.b, row_color.unused, 0));

// Draw probability percentage, aligned right.
format_prob(str_buf, prob);
Expand All @@ -524,16 +524,6 @@ void battle_prediction_pane::get_hp_distrib_surface(const std::vector<std::pair<
}
}

Uint32 battle_prediction_pane::blend_rgb(const surface& surf, unsigned char r, unsigned char g, unsigned char b, unsigned char drop)
{
// We simply decrement each component.
if(r < drop) r = 0; else r -= drop;
if(g < drop) g = 0; else g -= drop;
if(b < drop) b = 0; else b -= drop;

return SDL_MapRGB(surf->format, r, g, b);
}

attack_prediction_displayer::RESULT attack_prediction_displayer::button_pressed(int selection)
{
// Get the selected weapon, if any.
Expand Down
7 changes: 0 additions & 7 deletions src/attack_prediction_display.hpp
Expand Up @@ -107,13 +107,6 @@ class battle_prediction_pane : public gui::preview_pane
const battle_context_unit_stats& stats,
const battle_context_unit_stats& opp_stats,
surface& surf, int& width, int& height);

// This method blends a RGB color. The method takes as input a surface,
// the RGB color to blend and a value specifying how much blending to
// apply. The blended color is returned. Caution: if you use a
// transparent color, make sure the resulting color is not equal to the
// transparent color.
Uint32 blend_rgb(const surface& surf, unsigned char r, unsigned char g, unsigned char b, unsigned char drop);
};

// This class is used when the user clicks on the button
Expand Down
10 changes: 10 additions & 0 deletions src/sdl/utils.cpp
Expand Up @@ -374,6 +374,16 @@ scale_surface_down(surface& dst, const surface& src, const int w_dst, const int

#endif

Uint32 blend_rgba(const surface& surf, unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned char drop)
{
// We simply decrement each component.
if(r < drop) r = 0; else r -= drop;
if(g < drop) g = 0; else g -= drop;
if(b < drop) b = 0; else b -= drop;

return SDL_MapRGBA(surf->format, r, g, b, a);
}

surface scale_surface_xbrz(const surface & surf, size_t z)
{
if(surf == NULL)
Expand Down
10 changes: 10 additions & 0 deletions src/sdl/utils.hpp
Expand Up @@ -113,6 +113,16 @@ inline void sdl_blit(const surface& src, SDL_Rect* src_rect, surface& dst, SDL_R
SDL_BlitSurface(src, src_rect, dst, dst_rect);
}

/**
* This method blends a RGBA color. The method takes as input a surface,
* the RGB color to blend and a value specifying how much blending to apply.
* The blended color is returned.
* Caution: if you use a transparent color,
* make sure the resulting color is not equal to the transparent color.
*/
Uint32 blend_rgba(const surface& surf, unsigned char r, unsigned char g, unsigned char b,
unsigned char a, unsigned char drop);

/**
* Check that the surface is neutral bpp 32.
*
Expand Down

0 comments on commit 18452f8

Please sign in to comment.