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

Author Topic: How to center sf::Text (SFML2)  (Read 20118 times)

0 Members and 2 Guests are viewing this topic.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
How to center sf::Text (SFML2)
« on: September 05, 2011, 08:15:02 pm »
I am creating a button class for SFML2 and I am trying to center a text in my button (a texture).
The problem is that a text is not draw exactly at the correct coordinates (when drawing e.g. a text on position 0 then it will be drawn a little lower).

This is the code I am using:
Code: [Select]
sf::FloatRect rect = Text.GetRect();
rect.Left = (Texture.GetWidth() / 2.0f) - (rect.Width / 2.0f);
rect.Top = (Texture.GetHeight() / 2.0f) - (rect.Height / 2.0f);
Text.SetPosition(rect.Left, rect.Top);

Is there a way to get the top position of the text right?
At this moment the text is always too low.
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to center sf::Text (SFML2)
« Reply #1 on: September 05, 2011, 08:42:48 pm »
This is because the first line is aligned vertically on the height of the tallest character -- even if it's not in your string. This is to keep the top of the string steady even if you add higher characters on the first line.

The exact computation is not straight-forward: you would have to iterate through all the characters of the first line, compute the highest character size, subtract this from the CharacterSize of your sf::Text, and subtract the result from the height of the bounding rectangle.

:D
Laurent Gomila - SFML developer

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
How to center sf::Text (SFML2)
« Reply #2 on: September 05, 2011, 08:46:44 pm »
Thanks, I will try that tomorrow.
TGUI: C++ SFML GUI

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
How to center sf::Text (SFML2)
« Reply #3 on: September 06, 2011, 05:04:09 pm »
I couldn't find a function that calculates the size of one character, but I finally found my solution in the code from sf::Text::UpdateRect.

I will post my code here in case someone needs the same function.
Code: [Select]
size_t CharacterSize = Text.GetCharacterSize();
sf::Font Font = Text.GetFont();
std::string String = Text.GetString().ToAnsiString();
bool bold = (Text.GetStyle() & sf::Text::Bold);
size_t MaxHeight = 0;

for (size_t x=0; x<Text.GetString().GetSize(); ++x)
{
    sf::Uint32 Character = String.at(x);

    const sf::Glyph& CurrentGlyph = Font.GetGlyph(Character, CharacterSize, bold);

    size_t Height = CurrentGlyph.Bounds.Height;

    if (MaxHeight < Height)
        MaxHeight = Height;
}

sf::FloatRect rect = Text.GetRect();

rect.Left = (TextureNormal.GetWidth() / 2.0f) - (rect.Width / 2.0f);
rect.Top = (TextureNormal.GetHeight() / 2.0f) - (MaxHeight/2.0f) - (rect.Height-MaxHeight) + ((rect.Height-CharacterSize)/2.0f);

Text.SetPosition(rect.Left, rect.Top);


EDIT: The text was drawn too high but I have solved it and changed it in the code.
TGUI: C++ SFML GUI

g@rion

  • Newbie
  • *
  • Posts: 28
    • View Profile
How to center sf::Text (SFML2)
« Reply #4 on: January 31, 2012, 12:07:35 am »
Hello,
I'm trying to do quite the same thing. But when I try that :
Code: [Select]
sf::Text Text;
sf::FloatRect rect = Text.GetRect();


MinGW says that :
class sf::Text' has no member named 'GetRect'

It seems to me that I wrote exactly the same line as you did.
So what's the problem ?

Zinlibs

  • Full Member
  • ***
  • Posts: 127
    • View Profile
How to center sf::Text (SFML2)
« Reply #5 on: January 31, 2012, 12:13:28 am »
Code: [Select]
sf::Text Text;
sf::FloatRect rect = Text.GetGlobalBounds();


Global can be replaced by Local.
Zoost & Zoom libraries : An easy way to create and handle geometric objets, animate and use them for better graphics !

g@rion

  • Newbie
  • *
  • Posts: 28
    • View Profile
How to center sf::Text (SFML2)
« Reply #6 on: January 31, 2012, 09:18:21 am »
This works. Thank you.
But could anyone also explain me why this :
Code: [Select]
sf::Text Text;
sf::FloatRect rect = Text.GetRect();

doesn't work please ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to center sf::Text (SFML2)
« Reply #7 on: January 31, 2012, 09:54:34 am »
This function was removed.
Laurent Gomila - SFML developer

Mecha the Slag

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • http://mechatheslag.net
How to center sf::Text (SFML2)
« Reply #8 on: March 10, 2012, 07:52:35 pm »
Quote from: "Laurent"
This function was removed.


Is there an alternative? I'd really like to center my text :P

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
How to center sf::Text (SFML2)
« Reply #9 on: March 10, 2012, 08:08:06 pm »
I think Zinlib gave you the solution, either GetGlobalBounds() or GetLocalBounds() depending on what you want to do :)