SFML community forums

Help => Graphics => Topic started by: SeriousITGuy on October 28, 2015, 11:29:26 am

Title: [Solved] Problem with calculating global bounds
Post by: SeriousITGuy on October 28, 2015, 11:29:26 am
Hi all,

I'm currently developing a class which represents a text menu for applications (like a main menu for a game). While doing so, I oriented myself at the sf::Text - class, which is used internally for displaying individual meny entries. It works as intended but when calculation the global bounds of the complete menu, the top-left coordinate of the menu is 0.f, 0.f. Width and height should be the same like the local bounds, which is correct (209, 150 in my example below). The local bounds are correctly calculated and stored as a member inside the class.

I used the same method for calculating the global bounds like sf::Text or sf::Sprite do:
sf::FloatRect TextMenu::getGlobalBounds() const
{
  return getTransform().transformRect( getLocalBounds() );  
}
 

... which should return the global coordinates of the menu with all transformations applied.

I have no idea why the global bounds are not correct.   :-\
The complete code with minimal example can be found here: https://github.com/SeriousITGuy/SFML-TextMenu

It is not a deal-breaker as the rest works like intended and is ready to use in my other projects, just wondering why this happens  ;)
I plan on posting this class in the wiki once it's completed and therefor it would be good to get rid of this minor issue.
Title: Re: Problem with calculating global bounds
Post by: Nexus on October 28, 2015, 11:46:27 am
Does getTransform() return what you expect?

The complete code with minimal example can be found here: https://github.com/SeriousITGuy/SFML-TextMenu
That's not minimal. It would be nice to embed a complete code with only a main() function in a single file, and without all the stuff specific to your project (e.g. other menu functionality).

And you should do it inline in the post, not link to external sources. Many people (e.g. me) don't always have the time to skim through multiple files in external repositories :) Furthermore, this has the advantage that it's persistent, so the question is still useful when your repository changes.
Title: Re: Problem with calculating global bounds
Post by: Laurent on October 28, 2015, 11:48:14 am
Quote
I have no idea why the global bounds are not correct.
What is "not correct"? Do you get an empty rectangle? The same as the local bounds? Is its position wrong? Its size? ...

In your example, you retrieve the global bounds before applying your transformations, is it intended?
Title: Re: Problem with calculating global bounds
Post by: SeriousITGuy on October 28, 2015, 01:28:09 pm
In your example, you retrieve the global bounds before applying your transformations, is it intended?

Oh my god, that was the problem. Now I feel dumb!  :o
Now it works. Thanks!
Title: Re: Problem with calculating global bounds
Post by: Hapax on October 28, 2015, 08:23:55 pm
Now it works.
It would be nice if you posted what you did that actually works  ;)
Title: Re: [Solved] Problem with calculating global bounds
Post by: Laurent on October 28, 2015, 11:25:05 pm
Quote
It would be nice if you posted what you did that actually works
He now gets the global bounds after applying transformations, obviously.
Title: Re: [Solved] Problem with calculating global bounds
Post by: Hapax on October 29, 2015, 03:58:51 am
He now gets the global bounds after applying transformations, obviously.
He gets global bounds?
Title: Re: [Solved] Problem with calculating global bounds
Post by: SeriousITGuy on October 29, 2015, 09:07:10 am
Yes Laurent is correct. In my example program I instantiated TextMenu and saved the global bounds of it into a variable before even doing a transformation to it (menu.setPosition). I then used the saved bounds instead of calling getGlobalBounds again, therefore the transformation did not apply. Obvious mistake is obvious  :D