SFML community forums
Help => Graphics => Topic started by: cheeseboy on December 16, 2014, 01:38:10 am
-
I want to change color in image to alpha. I can do this with sf::image's createMaskFromColor however what's the best way to reverse this? Do I need to store two copies? or do I loop through all the pixels? or is there a better way?
-
It's not clear what you're trying to ask. Do you have a texture that you want to draw on the window semi-transparently? Are you actually editing images within your program? When you say "image", are you talking about a .png file, an sf::Image, an sf::Texture, an sf::Sprite, an array of Uint8s or something else entirely?
For the typical use case of a game that draws a texture to the window with some transparency, you shouldn't be using sf::Image at all. Use an sf::Texture so the pixels live on the GPU, and draw() an sf::Sprite with that texture. The sprite has a color (which includes alpha) which the GPU will efficiently apply to the pixels at draw() time. The relevant tutorial is: http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php
-
I think (Correct me if I'm wrong), he knows that he can create a color mask with a sprite, but he wants to know if he can create a mask from alpha. I'm not sure how to go about it though.
-
If your image initially had no alpha at all, then you know that every pixel with alpha == 0 was originally of the masked color. So reverting is easy, just go through all pixels and do the replacement.