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

Author Topic: getSize in version 2.0  (Read 2271 times)

0 Members and 1 Guest are viewing this topic.

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
getSize in version 2.0
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: getSize in version 2.0
« Reply #1 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.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: getSize in version 2.0
« Reply #2 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(). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: getSize in version 2.0
« Reply #3 on: July 01, 2012, 02:15:56 am »
Yes. I remember.
It was a fail...

Goodbye!

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: getSize in version 2.0
« Reply #4 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.
« Last Edit: July 02, 2012, 02:31:56 pm by Haze »

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: getSize in version 2.0
« Reply #5 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.