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

Author Topic: Sprite getLocalBounds implementation  (Read 3390 times)

0 Members and 1 Guest are viewing this topic.

lid6j86

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Sprite getLocalBounds implementation
« on: July 02, 2014, 09:13:16 am »
I'm writing an entity class which very closely mirrors the sprite class (with some extras), and as I look through the source code, I have a question about getLocalBounds().

I noticed that the textureRect (which is being used as the Source Rectangle and Destination Rectangle at the Position location I'm assuming) is an intRect, which makes perfect sense.  What I don't understand is why getLocalBounds returns a FloatRect, especially considering it simply pulls the textureRect width and height and casts it from an integer to a float.

Quote
    IntRect        m_textureRect; ///< Rectangle defining the area of the source texture to display

Quote
////////////////////////////////////////////////////////////
FloatRect Sprite::getLocalBounds() const
{
    float width = static_cast<float>(std::abs(m_textureRect.width));
    float height = static_cast<float>(std::abs(m_textureRect.height));

    return FloatRect(0.f, 0.f, width, height);
}

Is it common to need the local bounds as float values instead of integer values?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Sprite getLocalBounds implementation
« Reply #1 on: July 02, 2014, 09:22:21 am »
I'm not sure what Laurent's motivation was at the time, but from my perspective having local and global bounds returning different types would be inconsistent. From a user perspective when getting bounds you're operating within the player space/coordinate space which is built on top of floats. That the sprite internally pulls it's information from integer values shouldn't concern the user. Besides that if you'd apply a similar design for other things, where the local bounds wouldn't be pulled form integer values, you'd have to adjust the API again.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lid6j86

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Sprite getLocalBounds implementation
« Reply #2 on: July 02, 2014, 09:55:15 am »
that's sort of what I was suspecting, but wanted to make sure.  Thanks for the clarification

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite getLocalBounds implementation
« Reply #3 on: July 02, 2014, 10:06:49 am »
sf::Sprite is just one class that provides getLocalBounds(). Other drawable objects such as sf::Text or sf::Shape provide this method, too. The latter accesses sf::VertexArray::getBounds(), which computes the bounds from float coordinates.
« Last Edit: July 02, 2014, 10:08:26 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lid6j86

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Sprite getLocalBounds implementation
« Reply #4 on: July 02, 2014, 11:41:03 am »
I am curious about one thing, was there a particular design decision behind just making the destination size the same as the source size for sprite (i.e. simplicity, etc...) over keeping a separate source rectangle and destination rectangle (for stretching, for instance)?

Also, the reason why the texcoords are not bounded between 0-1 is because you are utilizing openGL's subimage?

Trying to get behind the 'why' so that I can understand the classes better and perhaps utilize similar logic in my own designs.  Thanks for the help and clarification
« Last Edit: July 02, 2014, 12:02:38 pm by lid6j86 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite getLocalBounds implementation
« Reply #5 on: July 02, 2014, 12:04:01 pm »
Quote
was there a particular design decision behind just making the destination size the same as the source size for sprite
They are not the same. Like any other sf::Drawable, a sprite can be translated, rotated and stretched with the functions inherited from sf::Transformable. Having a destinationRect property would provide the same functionality, except less generic and more limited (ie. no rotation).

This point of view is typical from people coming from SDL 1 and more generally old graphics APIs, where sprites where just "blitted" (copied pixel per pixel) from an image buffer to the screen buffer. Nowadays, 2D graphics is similar to 3D, you have a world where entities can be transformed freely, the GPU takes care of transforming and rasterizing everything correctly.
« Last Edit: July 02, 2014, 12:06:31 pm by Laurent »
Laurent Gomila - SFML developer

lid6j86

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Sprite getLocalBounds implementation
« Reply #6 on: July 02, 2014, 12:05:36 pm »
i see, so you just implemented the functionality elsewhere while simplifying things