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

Author Topic: Text.getGlobalBounds returns zero width and height  (Read 2443 times)

0 Members and 1 Guest are viewing this topic.

Nasaman

  • Guest
Text.getGlobalBounds returns zero width and height
« on: April 11, 2017, 03:55:14 pm »
Hey peeps!
Just started to work with SFML(.NET) a few weeks ago by porting my game from MonoGame to it.
So, I tried to port the dynamic text rendering method to SFML and when using getGlobalBounds() for a Text object I get a Rectangle with the right X and Y coordinates but with Zero for the width and height.
Here is the relevant code:
FloatRect rectangle = new FloatRect(SomeXCoordinate, SomeYCoordinate, SomeWidth, SomeHeight);
_text = new Text(text, FontManager.KingthingsFoundation, 50);
_text.Position = new Vector2f(rectangle.Left, rectangle.Top);
uint currentDotSize = 49;
FloatRect stringSize = _text.GetGlobalBounds();
Console.WriteLine(stringSize);
while((stringSize.Width > rectangle.Width || stringSize.Height > rectangle.Height) && currentDotSize >= 0) {
        _text = new Text(text, FontManager.KingthingsFoundation, currentDotSize);
        _text.Position = new Vector2f(rectangle.Left, rectangle.Top);
        currentDotSize--;
        stringSize = _text.GetGlobalBounds();
}
Where "text" is a specific String and the "rectangle" object is a Rectangle the text should fit in.

Now, as i said, on the line "Console.WriteLine(stringSize);" it prints out:
"[FloatRect] Left(SomeXCoordinate) Top(SomeYCoordinate) Width(0) Height(0)"
And therefore it doesn't even enter the while-loop.

I just can't wrap my head around why this is happening. Help would be very appreciated!

EDIT: Might also be worth noting that this is the only code in the method and that "text", the values for "rectangle" and the font are the only outside variables accessed.
"_text" is just a "private Text _text" which hasn't been touched before.
« Last Edit: April 11, 2017, 04:10:54 pm by Nasaman »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Text.getGlobalBounds returns zero width and height
« Reply #1 on: April 11, 2017, 04:00:40 pm »
And text contains a valid string?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nasaman

  • Guest
Re: Text.getGlobalBounds returns zero width and height
« Reply #2 on: April 11, 2017, 04:01:42 pm »
100% sure.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Text.getGlobalBounds returns zero width and height
« Reply #3 on: April 12, 2017, 08:38:47 pm »
FontManager.KingthingsFoundation is a valid font?

I mean, outputting the global bounds immediately after constructing the temporary text object and resulting in a zero-sized object would mean it was definitely the construction of that object. Have you tried replacing the parameters with direct values (hard-coded string or a temporarily loaded font in this function)?

By the way, you should not be entering the while loop if currentDotSize is equal to zero... :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nasaman

  • Guest
Re: Text.getGlobalBounds returns zero width and height
« Reply #4 on: April 13, 2017, 02:01:31 am »
The Font is definitely valid because it does show with the right font on the screen. But at 50 dot size because of the getGlobalBounds. The text is also a valid string because when I try to print it, it shows the correct string. Is there maybe like any restriction for where the getGlobalBounds method can be called?

"By the way, you should not be entering the while loop if currentDotSize is equal to zero... :P"
*cough*typo*cough*