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

Author Topic: Basic questions  (Read 3228 times)

0 Members and 1 Guest are viewing this topic.

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Basic questions
« 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.
« Last Edit: July 21, 2014, 03:28:49 pm by Law »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Basic questions
« Reply #1 on: July 21, 2014, 03:36:18 pm »
Just make your PNG 50% transparent.

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Basic questions
« Reply #2 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%)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Basic questions
« Reply #3 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?
Laurent Gomila - SFML developer

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Basic questions
« Reply #4 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 :-)

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Basic questions
« Reply #5 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!

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Basic questions
« Reply #6 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);
    }
}
Back to C++ gamedev with SFML in May 2023

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Basic questions
« Reply #7 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Basic questions
« Reply #8 on: July 21, 2014, 06:33:13 pm »
No, you have to use the scale factors.
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Basic questions
« Reply #9 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...  :-\

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Basic questions
« Reply #10 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Basic questions
« Reply #11 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!