From 37550b59d4c12cd9e1573692a67a92df515ec563 Mon Sep 17 00:00:00 2001 From: Thomas Rohloff Date: Sun, 18 Jul 2021 08:10:57 +0200 Subject: [PATCH 1/2] Add GuiImage:setBlendMode Signed-off-by: Thomas Rohloff --- include/gui-sdl/gui/GuiImage.h | 5 +++++ source/gui/GuiImage.cpp | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/gui-sdl/gui/GuiImage.h b/include/gui-sdl/gui/GuiImage.h index 8a4e54d..923e8fd 100644 --- a/include/gui-sdl/gui/GuiImage.h +++ b/include/gui-sdl/gui/GuiImage.h @@ -41,10 +41,15 @@ public: void setTexture(GuiTextureData *tex); + int setBlendMode(SDL_BlendMode blendMode); + private: GuiTextureData *texture = nullptr; bool freeTextureData = false; // Color of the rect that's drawn if the picture has no texture. SDL_Color color = {0, 0, 0, 0}; + +protected: + SDL_BlendMode blendMode; }; diff --git a/source/gui/GuiImage.cpp b/source/gui/GuiImage.cpp index fad3231..8682c06 100644 --- a/source/gui/GuiImage.cpp +++ b/source/gui/GuiImage.cpp @@ -47,11 +47,24 @@ void GuiImage::draw(Renderer *renderer) { if (texture) { texture->draw(renderer, rect, getAngle()); } else { + // copy the texture to the rendering context + SDL_BlendMode mode; + SDL_GetRenderDrawBlendMode(renderer->getRenderer(), &mode); + + // adjust blend mode + if(blendMode != mode){ + SDL_SetRenderDrawBlendMode(renderer->getRenderer(), blendMode); + } + SDL_SetRenderDrawColor(renderer->getRenderer(), color.r, color.g, color.b, color.a); SDL_RenderFillRect(renderer->getRenderer(), &rect); if(getAngle() != 0.0f){ LG_Log("Drawing a rotated rect is not supported yet"); } + + if(blendMode != mode){ + SDL_SetRenderDrawBlendMode(renderer->getRenderer(), mode); + } } } @@ -64,3 +77,8 @@ void GuiImage::setTexture(GuiTextureData *tex) { this->setSize(tex->getWidth(), tex->getHeight()); } } + +int GuiImage::setBlendMode(SDL_BlendMode blendMode) { + this->blendMode = blendMode; + return this->texture ? this->texture->setBlendMode(blendMode) : 0; +} From e0cc7dfb2e5a873c0c7f674f9aef4ec4f43e0e9a Mon Sep 17 00:00:00 2001 From: Thomas Rohloff Date: Sun, 18 Jul 2021 08:14:48 +0200 Subject: [PATCH 2/2] Set default blend mode Signed-off-by: Thomas Rohloff --- include/gui-sdl/gui/GuiImage.h | 2 +- include/gui-sdl/gui/GuiTextureData.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gui-sdl/gui/GuiImage.h b/include/gui-sdl/gui/GuiImage.h index 923e8fd..ff0fbea 100644 --- a/include/gui-sdl/gui/GuiImage.h +++ b/include/gui-sdl/gui/GuiImage.h @@ -51,5 +51,5 @@ private: SDL_Color color = {0, 0, 0, 0}; protected: - SDL_BlendMode blendMode; + SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; }; diff --git a/include/gui-sdl/gui/GuiTextureData.h b/include/gui-sdl/gui/GuiTextureData.h index 0690b0c..b4bea96 100644 --- a/include/gui-sdl/gui/GuiTextureData.h +++ b/include/gui-sdl/gui/GuiTextureData.h @@ -37,5 +37,5 @@ protected: SDL_Texture *texture = nullptr; int32_t width = 0; int32_t height = 0; - SDL_BlendMode blendMode; + SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; };