SFML community forums

Help => Graphics => Topic started by: Law on July 21, 2014, 03:26:33 pm

Title: Basic questions
Post by: Law on July 21, 2014, 03:26:33 pm
Hello,

1. I created a texture and loaded a .png file on it. Then I created a sprite, using the same texture. Now I would like to make my sprite n% transparent, with n between 0 and 100. How do I do?

2. If I want to create a sprite without any file, like a 100-px square, filled in red, how do I do? The only possibility is to actually use a sf::RectangleShape instance?

Thank you.
Title: Re: Basic questions
Post by: Jesper Juhl on July 21, 2014, 03:36:18 pm
Just make your PNG 50% transparent.
Title: Re: Basic questions
Post by: Law on July 21, 2014, 03:38:03 pm
No, because I would like the transparency of the sprite changing. (going progressively from 100% to 0%)
Title: Re: Basic questions
Post by: Laurent on July 21, 2014, 03:52:51 pm
1.
sprite.setColor(sf::Color(255, 255, 255, n));

2.
sf::Image image;
image.create(width, height, sf::Color::Red);

sf::Texture texture;
texture.loadFromImage(image);

sf::Sprite sprite(texture);

I don't remember if we still support texture-less sprites, you can try it:
sf::Sprite sprite;
sprite.setTextureRect(sf::IntRect(0, 0, width, height));
sprite.setColor(sf::Color::Red);

But what's wrong with sf::RectangleShape?
Title: Re: Basic questions
Post by: Peteck on July 21, 2014, 04:00:24 pm
1.
sprite.setColor(sf::Color(255, 255, 255, n));

Please notice that n should go from 0 to 255 in the Laurent example :-)
Title: Re: Basic questions
Post by: Law on July 21, 2014, 04:08:50 pm
1.
sprite.setColor(sf::Color(255, 255, 255, n));

2.
sf::Image image;
image.create(width, height, sf::Color::Red);

sf::Texture texture;
texture.loadFromImage(image);

sf::Sprite sprite(texture);

I don't remember if we still support texture-less sprites, you can try it:
sf::Sprite sprite;
sprite.setTextureRect(sf::IntRect(0, 0, width, height));
sprite.setColor(sf::Color::Red);

But what's wrong with sf::RectangleShape?
Thanks a lot!

Nothing in particular, I was just checking my options.

Please notice that n should go from 0 to 255 in the Laurent example :-)
Figured, but thanks anyway!
Title: Re: Basic questions
Post by: FRex on July 21, 2014, 04:11:50 pm
Quote
I don't remember if we still support texture-less sprites, you can try it:
You don't.
////////////////////////////////////////////////////////////
void Sprite::draw(RenderTarget& target, RenderStates states) const
{
    if (m_texture)
    {
        states.transform *= getTransform();
        states.texture = m_texture;
        target.draw(m_vertices, 4, TrianglesStrip, states);
    }
}
Title: Re: Basic questions
Post by: Law on July 21, 2014, 04:55:57 pm
Well I would have one more question.

I am making sprites via textures, which are loaded from files. The width and height of those sprites depend on the width and height of the window (let's assume they are random).

It is likely that the sprites i am making are never at the right size when I want to draw them on the window. I know I can change the sizes via scale, but instead I would like the height to be precisely this, and the width to be precisely that.

Do we have something for that, or I must use scale still (and calculate the right factors beforehand) ? I am asking this because there might be a problem of rounding, and I would hate to have my sprite at the correct width + 1, or the correct height - 1.
Title: Re: Basic questions
Post by: Laurent on July 21, 2014, 06:33:13 pm
No, you have to use the scale factors.
Title: Re: Basic questions
Post by: Law on July 21, 2014, 11:11:38 pm
No, you have to use the scale factors.
Well, Sprite is the only class that doesn't have getSize(). I wanted to make some sort of while loop that would scale the sprite until it reaches the exact size I want, but that is not possible since Sprites have no sizes...  :-\
Title: Re: Basic questions
Post by: Nexus on July 21, 2014, 11:17:07 pm
You can get the sprite's local or global bounds, and extract the size from them. And no, it's not the only drawable class without getSize ;)

And that while loop seems pointless, why not set the correct size directly? That is, using setScale.
Title: Re: Basic questions
Post by: Law on July 21, 2014, 11:29:55 pm
You can get the sprite's local or global bounds, and extract the size from them. And no, it's not the only drawable class without getSize ;)

And that while loop seems pointless, why not set the correct size directly? That is, using setScale.
Thanks!