Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - downtimes

Pages: [1]
1
Graphics / Semi-Transparent image
« on: January 20, 2011, 02:35:23 pm »
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

Code: [Select]

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

Pages: [1]
anything