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

Author Topic: make part of the spritesheet transparent?  (Read 4456 times)

0 Members and 1 Guest are viewing this topic.

Matan

  • Newbie
  • *
  • Posts: 4
    • View Profile
make part of the spritesheet transparent?
« on: June 08, 2021, 02:52:16 pm »
Hello!
I'm trying to make only part of my spritesheet transparent.
I'm using this spritesheet for animation, however I don't want to change the sf::Color everytime I'm changing the rect.
How can I change it once so it will stay for the whole run?
Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11002
    • View Profile
    • development blog
    • Email
Re: make part of the spritesheet transparent?
« Reply #1 on: June 08, 2021, 04:13:17 pm »
How can I change it once so it will stay for the whole run?
What do you mean "it" exactly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: make part of the spritesheet transparent?
« Reply #2 on: June 08, 2021, 04:32:06 pm »
are you trying to replace a 'default' color with transparent (like old RPG Maker versions)?
can't you simply use PNGs that have transparent backgrounds by default?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Matan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: make part of the spritesheet transparent?
« Reply #3 on: June 09, 2021, 09:06:26 am »
How can I change it once so it will stay for the whole run?
What do you mean "it" exactly?
only part of my sprite.
the black part of the "tiles" picture (attached)
when i display, i want the grid to be less dominant (attached, too)

are you trying to replace a 'default' color with transparent (like old RPG Maker versions)?
can't you simply use PNGs that have transparent backgrounds by default?

i'm not trying to make the background trasnparent - i want the color to be half transparent
(something like mySprite.setFillColor(sf::Color(255, 255, 255, 128)) ; )
however, i don't know how to do so, and i don't want to set the color on each loop everytime over and over :)

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: make part of the spritesheet transparent?
« Reply #4 on: June 09, 2021, 01:27:03 pm »
you don't set it every frame, you just set it once.

#include <SFML/Graphics.hpp>
int main(){
    sf::RenderWindow window(sf::VideoMode(200, 150), "SFML window");

    sf::Texture texture;
    if (!texture.loadFromFile("tileset.png"))
        return 1;
    sf::Sprite sprite(texture);
    sprite.setColor(sf::Color(255, 255, 255, 128));

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        window.draw(sprite);
        window.display();
    }
    return 0;
}
 






BUT if you need to use SOME PARTS of the same sprite as half transparent, and other parts as opaque, you have these options (ranked from best to worst IMO):
  • use an image that is already colored half-transparent in the parts it should be
  • use two different sprites, one being for transparent parts; two sprites can share the same texture, and you then set the color of one as half transparent, as in the code above.
  • change the transparency every time you draw a rect with transparency different from the last one. you don't need to change transparency every frame; if in this loop you are moving from a rect half transparent to another half transparent, just dont touch the colors.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Matan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: make part of the spritesheet transparent?
« Reply #5 on: June 09, 2021, 07:44:11 pm »
you don't set it every frame, you just set it once.

#include <SFML/Graphics.hpp>
int main(){
    sf::RenderWindow window(sf::VideoMode(200, 150), "SFML window");

    sf::Texture texture;
    if (!texture.loadFromFile("tileset.png"))
        return 1;
    sf::Sprite sprite(texture);
    sprite.setColor(sf::Color(255, 255, 255, 128));

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        window.draw(sprite);
        window.display();
    }
    return 0;
}
 






BUT if you need to use SOME PARTS of the same sprite as half transparent, and other parts as opaque, you have these options (ranked from best to worst IMO):
  • use an image that is already colored half-transparent in the parts it should be
  • use two different sprites, one being for transparent parts; two sprites can share the same texture, and you then set the color of one as half transparent, as in the code above.
  • change the transparency every time you draw a rect with transparency different from the last one. you don't need to change transparency every frame; if in this loop you are moving from a rect half transparent to another half transparent, just dont touch the colors.

Thank you very much!
Yes, the last case is what I had:  some parts of the same sprite as half transparent.
I followed your first tip, and changed it original sprite.

 

anything