SFML community forums

Help => Graphics => Topic started by: 7vape on June 20, 2024, 01:56:25 pm

Title: SFML Bitmap Masking
Post by: 7vape on June 20, 2024, 01:56:25 pm
Edit: Thanks, my problem is solved
Title: Re: SFML Bitmap Masking
Post by: Me-Myself-And-I on June 21, 2024, 03:22:48 am
I don't know what all that code does. :o
I'll just give a simple example.

There are two methods to have transparent sprites.
1: Use an image editor that supports exporting transparency. I use transparent png's.
2: (Hardly very reasonable but usable) Use sf::Image and two for loops to remove the specified color. Then load that image into the texture. I find this useful for removing a color that image editors can't completely remove correctly.

How to use sf::image for that:

sf::Image image;
image.loadFromFile("image.bmp");

for(int x=0;x<image.getSize().x;x++)
{
     for(int y=0;y<image.getSize().y;y++)
     {
          if(image.getPixel(x,y)==sf::Color::Yellow)
          {
                image.setPixel(x,y,sf::Color::Transparent);
          }
     }
}

sf::Texture texture;
texture.loadFromImage(image);
Sprite sprite;
sprite.setTexture(texture);

 
Title: Re: SFML Bitmap Masking
Post by: 7vape on June 21, 2024, 03:32:13 am
@Me-Myself-And-I, Thanks I will try the codes later and update the outcome here.

Code: [Select]
if(image.getPixel(x,y)==sf::Color::Yellow)
About this line, not all the spritesheet background are in orange color. Some are in pink, green, etc. Do I need to specify those other colors too?
Title: Re: SFML Bitmap Masking
Post by: 7vape on June 21, 2024, 04:07:01 am
This does not work
Code: [Select]
if(image.getPixel(x,y)==sf::Color::Yellow)
I changed to this and it worked
Code: [Select]
if (_image.getPixel(x, y) == sf::Color(255, 132, 66))
Do i need to specify all background colors from all my spritesheets?
Title: Re: SFML Bitmap Masking
Post by: eXpl0it3r on June 21, 2024, 08:12:36 am
If you have a single color for transparency, you can actually use SFML's built-in createMaskFromColor function (https://www.sfml-dev.org/documentation/2.6.1/classsf_1_1Image.php#a22f13f8c242a6b38eb73cc176b37ae34) on sf::Image.

sf::Image image;
if (!image.loadFromFile("image.bmp"))
{
    return -1;
}
image.createMaskFromColor(sf::Color(255, 132, 66));

Yes, you'd have to do that for every image that has a colored background.
Alternatively, what Me-Myself-And-I suggested and what I'd do as well, is to use an image program to export a true transparent background stored in a PNG.
Title: Re: SFML Bitmap Masking
Post by: 7vape on June 21, 2024, 01:09:57 pm
Alrights thanks @eXpl0it3r and @Me-Myself-And-I, this really helps alot ;)