Skip to content

Commit

Permalink
Store alpha value in ttexture and set it before drawing.
Browse files Browse the repository at this point in the history
Several ttexture instances may share a single SDL_Texture, so having
everyone store their alpha mod directly in the SDL_Texture object is not
viable.
  • Loading branch information
lipk committed Jun 17, 2014
1 parent 752b42c commit 616ab5f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/sdl/texture.cpp
Expand Up @@ -43,6 +43,7 @@ ttexture::ttexture(SDL_Renderer& renderer,
, mod_r_(0)
, mod_g_(0)
, mod_b_(0)
, alpha_(0)
, source_surface_(NULL)
{
if(!texture_) {
Expand All @@ -64,6 +65,7 @@ ttexture::ttexture(SDL_Renderer& renderer,
, mod_r_(0)
, mod_g_(0)
, mod_b_(0)
, alpha_(0)
, source_surface_(IMG_Load(file.c_str()))
{
if(source_surface_ == NULL) {
Expand All @@ -86,6 +88,7 @@ ttexture::ttexture()
, mod_r_(0)
, mod_g_(0)
, mod_b_(0)
, alpha_(0)
, source_surface_(NULL)
{}

Expand Down Expand Up @@ -117,6 +120,7 @@ ttexture::ttexture(const ttexture& texture)
, mod_r_(texture.mod_r_)
, mod_g_(texture.mod_g_)
, mod_b_(texture.mod_b_)
, alpha_(texture.alpha_)
, source_surface_(texture.source_surface_)
{
assert(reference_count_);
Expand All @@ -140,6 +144,7 @@ ttexture::ttexture(SDL_Renderer& renderer,
, mod_r_(0)
, mod_g_(0)
, mod_b_(0)
, alpha_(0)
, source_surface_(source_surface__)
{
if(source_surface_ == NULL) {
Expand All @@ -165,6 +170,7 @@ ttexture::ttexture(SDL_Renderer& renderer,
, mod_r_(0)
, mod_g_(0)
, mod_b_(0)
, alpha_(0)
, source_surface_(
SDL_ConvertSurface(surface, surface->format, surface->flags))
{
Expand All @@ -191,6 +197,7 @@ void ttexture::draw(SDL_Renderer& renderer, const int x, const int y)
{
SDL_Rect dstrect = create_rect(x, y, clip_.w * hscale_, clip_.h * vscale_);

SDL_SetTextureAlphaMod(texture_, alpha_);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, smooth_scaling_ ? "linear" : "nearest");
SDL_RenderCopyEx(&renderer, texture_, &clip_, &dstrect,
rotation_, NULL, flip_);
Expand Down Expand Up @@ -321,14 +328,12 @@ Uint32 ttexture::format() const

void ttexture::set_alpha(Uint8 alpha)
{
SDL_SetTextureAlphaMod(texture_, alpha);
alpha_ = alpha;
}

Uint8 ttexture::alpha() const
{
Uint8 res;
SDL_GetTextureAlphaMod(texture_, &res);
return res;
return alpha_;
}

void ttexture::set_blend_mode(SDL_BlendMode mode)
Expand Down
3 changes: 3 additions & 0 deletions src/sdl/texture.hpp
Expand Up @@ -345,6 +345,9 @@ class ttexture
/** Color modulation. */
Uint8 mod_r_, mod_g_, mod_b_;

/** Alpha. */
Uint8 alpha_;

/**
* The SDL_Surface source of the @ref texture_.
*
Expand Down

0 comments on commit 616ab5f

Please sign in to comment.