SFML community forums

Help => Graphics => Topic started by: game_maker on June 30, 2012, 08:10:56 pm

Title: getSize in version 2.0
Post by: game_maker on June 30, 2012, 08:10:56 pm
I created a method:

sf::Vector2f getSize()
{
        sf::Rect _temp = this->GetGlobalBounds();

        return sf::Vector2f(_temp.width, _temp.height);
}

I used:
sf::Vector2f size = Object.getSize();

Is this correct?

There are some errors in general, so I need to know if this is wrong.

Thanks in advance.
Title: Re: getSize in version 2.0
Post by: Laurent on June 30, 2012, 09:14:57 pm
Quote
Is this correct?
Yes, but remember that getGlobalBounds() returns the axis-aligned bounding box of the sprite after it has been transformed. So the size may change if you rotate the sprite.
Title: Re: getSize in version 2.0
Post by: eXpl0it3r on June 30, 2012, 11:23:54 pm
Although Laurent already kind of pointed it out, in SFML 2.0 the function nameing is now camelCase, thus GetGlobalBounds() is now getGlobalBounds(). ;)
Title: Re: getSize in version 2.0
Post by: game_maker on July 01, 2012, 02:15:56 am
Yes. I remember.
It was a fail...

Goodbye!
Title: Re: getSize in version 2.0
Post by: Haze on July 02, 2012, 02:20:30 pm
If you just want to retrieve the width/height of your sprite, I suggest to use the texture rect instead of the global bounding box (see Laurent's comment about transformations):

float width  = sprite.getTextureRect().width;
float height = sprite.getTextureRect().height;

This code is much faster but it won't work if you have applied transformations on your sprite (such as rotation, scale, etc.).

If you only care about the scale transformation, you can still get the correct size without using the global bounding box:

float width  = sprite.getTextureRect().width  * sprite.getScale().x;
float height = sprite.getTextureRect().height * sprite.getScale().y;
(By the way, this is the exact implementation of sf::Sprite::GetSize in SFML 1.6).

Pick the solution which best fits your needs.
Title: Re: getSize in version 2.0
Post by: game_maker on July 09, 2012, 04:57:40 pm
My code is for any transformation, right?
I relied on other code, so I'm not sure.