From c7ec12584c01a4912c722d519e70ef07b889c3aa Mon Sep 17 00:00:00 2001 From: zv0n Date: Sun, 7 Mar 2021 13:18:58 +0100 Subject: [PATCH] Add texture alpha blending --- sdlpp/sdlpp_renderobject.cpp | 11 ++++++++++- sdlpp/sdlpp_renderobject.hpp | 3 +++ sdlpp/sdlpp_texture.cpp | 5 +++++ sdlpp/sdlpp_texture.hpp | 1 + 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sdlpp/sdlpp_renderobject.cpp b/sdlpp/sdlpp_renderobject.cpp index 0d615a4..b6a5e5c 100644 --- a/sdlpp/sdlpp_renderobject.cpp +++ b/sdlpp/sdlpp_renderobject.cpp @@ -12,7 +12,7 @@ void RenderObject::render() { polygon->render( *renderer ); if ( texture != NULL ) { SDL_Rect *src = NULL; - if(!entireTexture()) + if ( !entireTexture() ) src = &src_rect; SDL_RenderCopy( renderer->getRendererPtr(), texture->getTexturePtr(), src, &rect ); @@ -186,4 +186,13 @@ void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) { } 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 diff --git a/sdlpp/sdlpp_renderobject.hpp b/sdlpp/sdlpp_renderobject.hpp index bf96423..16184d1 100644 --- a/sdlpp/sdlpp_renderobject.hpp +++ b/sdlpp/sdlpp_renderobject.hpp @@ -86,6 +86,9 @@ public: bool isStatic(); void setStatic( bool stat = true ); 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: virtual void copyTo( std::shared_ptr< RenderObject > other ); diff --git a/sdlpp/sdlpp_texture.cpp b/sdlpp/sdlpp_texture.cpp index 70c6e89..b9431ee 100644 --- a/sdlpp/sdlpp_texture.cpp +++ b/sdlpp/sdlpp_texture.cpp @@ -73,4 +73,9 @@ void Texture::setTextureFromSurface( std::shared_ptr< Renderer > &renderer, } SDL_FreeSurface( surface ); } + +void Texture::setAlpha( uint8_t alpha ) { + SDL_SetTextureBlendMode( texture, SDL_BLENDMODE_BLEND ); + SDL_SetTextureAlphaMod( texture, alpha ); +} } // namespace SDLPP diff --git a/sdlpp/sdlpp_texture.hpp b/sdlpp/sdlpp_texture.hpp index 7428ee5..30400b5 100644 --- a/sdlpp/sdlpp_texture.hpp +++ b/sdlpp/sdlpp_texture.hpp @@ -22,6 +22,7 @@ public: const int outline_size = -1 ); virtual ~Texture(); SDL_Texture *getTexturePtr(); + void setAlpha( uint8_t alpha ); private: void setTextureFromSurface( std::shared_ptr< Renderer > &renderer,