Add texture alpha blending

This commit is contained in:
zv0n 2021-03-07 13:18:58 +01:00
parent fbc1fdd6f7
commit c7ec12584c
4 changed files with 19 additions and 1 deletions

View File

@ -12,7 +12,7 @@ void RenderObject::render() {
polygon->render( *renderer ); polygon->render( *renderer );
if ( texture != NULL ) { if ( texture != NULL ) {
SDL_Rect *src = NULL; SDL_Rect *src = NULL;
if(!entireTexture()) if ( !entireTexture() )
src = &src_rect; src = &src_rect;
SDL_RenderCopy( renderer->getRendererPtr(), SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), src, &rect ); texture->getTexturePtr(), src, &rect );
@ -186,4 +186,13 @@ void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
} }
other->colidedWith.clear(); other->colidedWith.clear();
} }
void RenderObject::setTextureAlpha( uint8_t alpha ) {
texture->setAlpha( alpha );
}
void RenderObject::setTextureSourceRect( SDL_Rect source_rect ) {
src_rect = source_rect;
}
void RenderObject::setTextureSourceRect( int x, int y, int w, int h ) {
setTextureSourceRect( { x, y, w, h } );
}
} // namespace SDLPP } // namespace SDLPP

View File

@ -86,6 +86,9 @@ public:
bool isStatic(); bool isStatic();
void setStatic( bool stat = true ); void setStatic( bool stat = true );
std::shared_ptr< Renderer > getRenderer() const; std::shared_ptr< Renderer > getRenderer() const;
void setTextureAlpha( uint8_t alpha );
void setTextureSourceRect( SDL_Rect source_rect );
void setTextureSourceRect( int x, int y, int w, int h );
protected: protected:
virtual void copyTo( std::shared_ptr< RenderObject > other ); virtual void copyTo( std::shared_ptr< RenderObject > other );

View File

@ -73,4 +73,9 @@ void Texture::setTextureFromSurface( std::shared_ptr< Renderer > &renderer,
} }
SDL_FreeSurface( surface ); SDL_FreeSurface( surface );
} }
void Texture::setAlpha( uint8_t alpha ) {
SDL_SetTextureBlendMode( texture, SDL_BLENDMODE_BLEND );
SDL_SetTextureAlphaMod( texture, alpha );
}
} // namespace SDLPP } // namespace SDLPP

View File

@ -22,6 +22,7 @@ public:
const int outline_size = -1 ); const int outline_size = -1 );
virtual ~Texture(); virtual ~Texture();
SDL_Texture *getTexturePtr(); SDL_Texture *getTexturePtr();
void setAlpha( uint8_t alpha );
private: private:
void setTextureFromSurface( std::shared_ptr< Renderer > &renderer, void setTextureFromSurface( std::shared_ptr< Renderer > &renderer,