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

Author Topic: Getting bounds for sf::View  (Read 16810 times)

0 Members and 1 Guest are viewing this topic.

Aster

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Getting bounds for sf::View
« on: September 02, 2013, 09:38:38 am »
Hiya,

In most of my games involving an sf::View, I write a little function that looks somewhat like:
sf::FloatRect getViewBounds(const sf::View &view)
{
        sf::FloatRect rt;
        rt.left = view.getCenter().x - view.getSize().x/2.f;
        rt.top  = view.getCenter().y - view.getSize().y/2.f;
        rt.width  = view.getSize().x;
        rt.height = view.getSize().y;
        return rt;
}
 

As you can guess, it returns an sf::FloatRect containing information on the bounds of a given sf::View. This function has many uses, such as checking if something is on-screen or not using sf::Rect's very useful .contains() and .intersects() functions.

My feature request is to ask if such a thing could be stuck into sf::View by default, as it has many uses and it's pretty essential for game development, in my opinion.

Thanks,
Aster

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Getting bounds for sf::View
« Reply #1 on: September 02, 2013, 09:53:37 am »
It is impossible: if the view is rotated you cannot construct such a rectangle (SFML has no way of representing rotated rectangles).
Laurent Gomila - SFML developer

Aster

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Re: Getting bounds for sf::View
« Reply #2 on: September 02, 2013, 09:57:34 am »
Ah, I see.

Could you not apply the rotation then get the topmost value, leftmost value, etc?
I don't think it's necessary to have it at 100% precision, covering exactly the bounds of the view.

EDIT: I now feel stupid for not thinking about rotations, as I'm currently making a game closely bound to rotations.
« Last Edit: September 02, 2013, 10:04:11 am by Aster »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Getting bounds for sf::View
« Reply #3 on: September 02, 2013, 10:10:51 am »
Not using FloatRect.  That class simply doesn't support rotation (nor should it).  What you describe is entirely possible, but it's outside SFML's scope so you'd have to write a function to do it yourself (which isn't too hard, so feel free to ask about that).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Getting bounds for sf::View
« Reply #4 on: September 02, 2013, 10:15:57 am »
Well, returning the axis-aligned bounding rectangle of the rotated view would be possible (that's what getGlobalBounds() does for Sprite, Text and Shape). And since most people don't use rotated views anyway, the function would return the exact bounds of the view in most cases.

So I'm not saying no, but I need to think about it ;)
Laurent Gomila - SFML developer

Aster

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Re: Getting bounds for sf::View
« Reply #5 on: September 02, 2013, 10:18:55 am »
Alright. It would be really cool and useful in any case.

Thanks for your time. :)

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Getting bounds for sf::View
« Reply #6 on: September 02, 2013, 10:28:50 am »
Actually it could be usefull to me too, I stored the "view" (scene, part of world, whatever) bounds in a separated variable.

I've to admit I do not use rotated views so much, but sometimes I need to rotate the scene 90° or 180°.Still that's a powerfull feature.

RyanPaul87

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Getting bounds for sf::View
« Reply #7 on: December 30, 2021, 04:30:43 am »
Did this ever get added?