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

Author Topic: Semi-Transparent image  (Read 2519 times)

0 Members and 1 Guest are viewing this topic.

downtimes

  • Newbie
  • *
  • Posts: 9
    • View Profile
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Semi-Transparent image
« Reply #1 on: January 20, 2011, 02:40:18 pm »
Make your sprites transparent, not the image. This is much faster (almost free actually), and it only requires a single function call (SetColor).
Laurent Gomila - SFML developer

downtimes

  • Newbie
  • *
  • Posts: 9
    • View Profile
Semi-Transparent image
« Reply #2 on: January 21, 2011, 12:26:26 am »
Ah thanks a lot. Didn't think the SetColor function would have any effect after the SetImage call. Thanks a lot for claryfication.