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

Author Topic: SFML Bitmap Masking  (Read 3416 times)

0 Members and 1 Guest are viewing this topic.

7vape

  • Guest
SFML Bitmap Masking
« on: June 20, 2024, 01:56:25 pm »
Edit: Thanks, my problem is solved
« Last Edit: June 30, 2024, 11:48:29 pm by 7vape »

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML Bitmap Masking
« Reply #1 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);

 
« Last Edit: June 21, 2024, 03:26:25 am by Me-Myself-And-I »

7vape

  • Guest
Re: SFML Bitmap Masking
« Reply #2 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?

7vape

  • Guest
Re: SFML Bitmap Masking
« Reply #3 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?
« Last Edit: July 01, 2024, 12:12:09 am by 7vape »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML Bitmap Masking
« Reply #4 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 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

7vape

  • Guest
Re: SFML Bitmap Masking
« Reply #5 on: June 21, 2024, 01:09:57 pm »
Alrights thanks @eXpl0it3r and @Me-Myself-And-I, this really helps alot ;)

 

anything