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

Author Topic: Get rectangle of a sprite or image  (Read 4553 times)

0 Members and 1 Guest are viewing this topic.

Deftwun

  • Newbie
  • *
  • Posts: 27
    • View Profile
Get rectangle of a sprite or image
« on: April 25, 2011, 03:37:21 am »
Just wondering if there was a function like sprite.getrect or image.getrect. similar to the method a view has. Well, I know there is no function but I guess is there a simple implementation? I'm trying to set a boundary for my camera by using the limits of a large level background sprite.

Isn't a sprite just a rectangle with an image?

heres what I've been trying to do currently with little success:

Pseudo code:

Here I try to create my own rectangle from the dimensions of a sprite. It will not compile though and says there is no matching function for call to sf::shape::rectangle. I assume its because an sf::shape is not at all an sf::rect.
Code: [Select]
Engine_Class{
    sf::Rect <float> CameraBounds = sf::Shape::Rectangle(0,sprite_width,0,sprite_height);  
    Cam1.SetBounds(CameraBounds);
}



This next part should (theoretically) work if I could just come up with a rectangle lol!
Code: [Select]

Camera_Class{
    sf::Rect <float> myBoundary;
   
    void Camera::SetBounds(sf::Rect <float> CameraBounds){
        myBoundary = CameraBounds;
    }

    bool Camera::Within_Bounds(){

        if (ViewPort.GetRect().Left < myBoundary.Left) return false;
        if (ViewPort.GetRect().Right > myBoundary.Right) return false;
        if (ViewPort.GetRect().Top < myBoundary.Top) return false;
        if (ViewPort.GetRect().Bottom > myBoundary.Bottom) return false;

        return true;
    }

}


any suggestions/thoughts/advice?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get rectangle of a sprite or image
« Reply #1 on: April 25, 2011, 09:52:47 am »
This is a little bit tricky.

The sprite's rectangle is (0, 0, sprite.GetSubRect().GetWidth(), sprite.GetSubRect().GetHeight()). But this is in local coordinates. Now you have to convert this rectangle to global coordinates, which means applying the same translation/rotation/scale that you applied to the sprite. This can be done by calling sprite.TransformToGlobal on every corner of the rectangle. Be careful, if you applied a rotation the result won't be an aligned rectangle anymore.

If you have not rotation, there's a simpler formula:
Code: [Select]
sf::FloatRect rect;
rect.Left = sprite.GetPosition().x;
rect.Top = sprite.GetPosition().y;
rect.Right = rect.Left + sprite.GetSize().x;
rect.Bottom = rect.Top + sprite.GetSize().y;
Laurent Gomila - SFML developer

Deftwun

  • Newbie
  • *
  • Posts: 27
    • View Profile
Get rectangle of a sprite or image
« Reply #2 on: April 27, 2011, 05:49:22 am »
Thank you so much. sorry for the delayed response. I did not know about sf::floatrect, that is exactly what i needed. Still not working yet but its something simple in my code that I'm not doing I think. I've got past the hard part. Thanks again Laurent.

P.S
It amazes me how many questions are answered by the Developer of this software. I've learned a ton about C++ and programming in general with SFML. Keep up the great work!