From 65135b4b8646e6cc9c46a56e29f8f7d1c5403c6a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 19 Jul 2013 14:06:10 +1000 Subject: [PATCH] Codebase update: Added transparency control to Image --- common/image.cpp | 15 +++++++++++++++ common/image.hpp | 3 +++ 2 files changed, 18 insertions(+) diff --git a/common/image.cpp b/common/image.cpp index 9d8669c..b0da00f 100644 --- a/common/image.cpp +++ b/common/image.cpp @@ -29,6 +29,7 @@ SDL_Surface* Image::LoadSurface(std::string fname) { throw(std::runtime_error(std::string() + "Failed to load file: " + fname)); } surface = p; + SetTransparentColor(255, 0, 255); //default clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; local = true; return surface; @@ -60,3 +61,17 @@ void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { SDL_Rect sclip = clip, dclip = {x,y}; SDL_BlitSurface(surface, &sclip, dest, &dclip); } + +void Image::SetTransparentColor(Uint8 r, Uint8 g, Uint8 b) { + if (!surface) { + throw(std::logic_error("Failed to set the transparent color")); + } + SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b)); +} + +void Image::ClearTransparentColor() { + if (!surface) { + throw(std::logic_error("Failed to clear the transparent color")); + } + SDL_SetColorKey(surface, 0, 0); +} diff --git a/common/image.hpp b/common/image.hpp index 463e382..ca3980b 100644 --- a/common/image.hpp +++ b/common/image.hpp @@ -54,6 +54,9 @@ public: Uint16 GetClipH() const { return clip.h; } bool GetLocal() { return local; } + + void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b); + void ClearTransparentColor(); protected: SDL_Surface* surface = nullptr; SDL_Rect clip = {0, 0, 0, 0};