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

Author Topic: Text.GetGlobalBounds() is lying to me!  (Read 7838 times)

0 Members and 1 Guest are viewing this topic.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Text.GetGlobalBounds() is lying to me!
« on: September 18, 2015, 10:20:10 am »
Hello. Getting the width and height isn't acting like I thought it would.

            font = new Font("arial.ttf");

            text = new Text("Text!", font, 90);
            text.Position = new Vector2f(300, 300);

            RectangleShape shape = new RectangleShape();
            shape.Size = new Vector2f(text.GetGlobalBounds().Width, text.GetGlobalBounds().Height);
            shape.Position = text.Position;
            shape.FillColor = Color.Transparent;
            shape.OutlineColor = Color.White;
            shape.OutlineThickness = 1;

What it produces: http://puu.sh/kflYc/d0f958ab90.png

There is probably something obvious that I'm missing.


FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #1 on: September 18, 2015, 10:33:37 am »
The text bounds left and top are usually non zero so you must use them too.
Back to C++ gamedev with SFML in May 2023

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #2 on: September 18, 2015, 10:59:43 am »
How do you get how much they're offset by 0? Also, why are is text left and top bounds non zero?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #3 on: September 18, 2015, 11:12:30 am »
Quote
How do you get how much they're offset by 0?
GetGlobalBounds().Left and GetGlobalBounds().Top...

Quote
Also, why are is text left and top bounds non zero?
Because text is aligned on its baseline, not on its top-left corner. So there's potential empty space on top of it, which matches the height of the tallest character of the font (accentuated upper case letters, for exemple).
Laurent Gomila - SFML developer

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #4 on: September 18, 2015, 11:25:39 am »
Sorry, what I meant to ask was, "How do I calculate the spacing between the absolute position/size to the intended position/size?" Which you indirectly answered with your second explanation: I have to find the tallest character and subtract it from the absolute position. Right? If so, how? I'm not sure how to get the size of a letter with the given char size and char type.
« Last Edit: September 18, 2015, 11:27:17 am by asusralis »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #5 on: September 18, 2015, 11:38:09 am »
Quote
How do I calculate the spacing between the absolute position/size to the intended position/size?
GetGlobalBounds().Top. Really. It's not a joke.
Laurent Gomila - SFML developer

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #6 on: September 18, 2015, 11:42:44 am »
Sigh. I meant the difference between the absolute position and the top of the tallest letter. But I figured it out:

int highest = 0;

            for (int i = 0; i < text.DisplayedString.Length; i++)
            {
                Glyph glyph = font.GetGlyph((uint)text.DisplayedString[i], 150, false);

                if (glyph.Bounds.Height > highest)
                    highest = (int)glyph.Bounds.Height;
            }

Thanks for the help. It's kind of lame you have to do this but I guess everything can't be done for us ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #7 on: September 18, 2015, 11:58:31 am »
Why do you keep thinking that GetGlobalBounds().Top is not what you want? If it doesn't work as expected then I'd like to get your feedback.
Laurent Gomila - SFML developer

Raptor2277

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Text.GetGlobalBounds() is lying to me!
« Reply #8 on: October 06, 2015, 12:14:05 am »
It's kinda weird how it works. It is implemented weirdly. If you do it normally.
t.CharacterSize = 100;
t.Position = new Vector2f(200, 200);
...
...
Draw.drawRectangle(w, gBounds, Color.Yellow);
w.Draw(new Vertex[] { new Vertex(new Vector2f(0, 200), Color.White), new Vertex(new Vector2f(900, 200), Color.White) }, PrimitiveType.Lines);
w.Draw(t);
 

And here is what it looks like. As you can see there is some white space at the top. And the getGlobalBounds() returns not 200, 200 but 201, 224. That's because of the empty space at the top. If you notice the getLocanBounds() x and y are 1, 24. So you can use that to position it properly. IDK why the it doesn't do it right the first time.


Here is what I use to position it properly.
t.CharacterSize = 100;
setPosition(t, new Vector2f(200, 200));
...
...
public void setPosition(Text text, Vector2f pos)
{
     Vector2f offsets = new Vector2f(text.GetLocalBounds().Left, text.GetLocalBounds().Top);

     text.Position = new Vector2f(pos.X - offsets.X, pos.Y - offsets.Y);
}
 

And here is what it looks like.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Text.GetGlobalBounds() is lying to me!
« Reply #9 on: October 06, 2015, 12:51:24 am »
Quote
IDK why the it doesn't do it right the first time.

I believe this was already answered by the previous posts in this thread, right?

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Text.GetGlobalBounds() is lying to me!
« Reply #10 on: October 06, 2015, 12:08:57 pm »
Another method would be:
text.setOrigin(text.getLocalBounds().left, text.getLocalBounds().top);
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Text.GetGlobalBounds() is lying to me!
« Reply #11 on: October 12, 2015, 06:35:22 pm »
Another method would be:
text.setOrigin(text.getLocalBounds().left, text.getLocalBounds().top);
This must be updated whenever the string is altered.

I'm not sure what the results of getLocalBounds().left and getLocalBounds().top are when the string is empty.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything