Hi there.
I'm using SFML 1.6 and want to have Semi Transparent images.
One way to do this is to save the images with an alpha value directly from Gimp or
some other picture manipulating programm.
However this is not a valid solution if you want to "Fade out" an image over Time.
So i looked through the documentation but didn't find a function to set the Images alpha
value directly. (Or is there Something ?).
I now wrote this little helper function
void setImageAlpha(sf::Image& img, const sf::Uint8& alpha) {
const sf::Uint8* ptr = img.GetPixelsPtr();
std::vector<sf::Uint8> pixels(ptr, ptr + img.GetWidth() * img.GetHeight() * 4);
for (std::vector<sf::Uint8>::size_type x = 3; x < pixels.size(); x += 4) {
pixels[x] = alpha;
}
img.LoadFromPixels(img.GetWidth(), img.GetHeight(), &pixels[0]);
}
This is way faster then setting every Pixel directly with the SetPixel() function of sf::Image
but if i am not mistaken it is not garantueed that Sprites using this image are valid
after calling this function? (because they only store a pointer).
Am i mistaken?
Inherit from Image and writing a function is not an option either because the
members needed for this are private.
Writing my own image class seemed a bit oversized for this Problem.
So now my Question. How would you do it? Is there a faster/easier way?
Thanks for any help.
MFG downtimes