Codebase update: Added transparency control to Image

This commit is contained in:
Kayne Ruse
2013-07-19 14:06:10 +10:00
parent 51a0974d25
commit 65135b4b86
2 changed files with 18 additions and 0 deletions
+15
View File
@@ -29,6 +29,7 @@ SDL_Surface* Image::LoadSurface(std::string fname) {
throw(std::runtime_error(std::string() + "Failed to load file: " + fname)); throw(std::runtime_error(std::string() + "Failed to load file: " + fname));
} }
surface = p; surface = p;
SetTransparentColor(255, 0, 255); //default
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
local = true; local = true;
return surface; return surface;
@@ -60,3 +61,17 @@ void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) {
SDL_Rect sclip = clip, dclip = {x,y}; SDL_Rect sclip = clip, dclip = {x,y};
SDL_BlitSurface(surface, &sclip, dest, &dclip); 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);
}
+3
View File
@@ -54,6 +54,9 @@ public:
Uint16 GetClipH() const { return clip.h; } Uint16 GetClipH() const { return clip.h; }
bool GetLocal() { return local; } bool GetLocal() { return local; }
void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b);
void ClearTransparentColor();
protected: protected:
SDL_Surface* surface = nullptr; SDL_Surface* surface = nullptr;
SDL_Rect clip = {0, 0, 0, 0}; SDL_Rect clip = {0, 0, 0, 0};