SFML community forums

Help => Graphics => Topic started by: texus on September 05, 2011, 08:15:02 pm

Title: How to center sf::Text (SFML2)
Post by: texus 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.
Title: How to center sf::Text (SFML2)
Post by: Laurent 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
Title: How to center sf::Text (SFML2)
Post by: texus on September 05, 2011, 08:46:44 pm
Thanks, I will try that tomorrow.
Title: How to center sf::Text (SFML2)
Post by: texus 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.
Title: How to center sf::Text (SFML2)
Post by: g@rion 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 ?
Title: How to center sf::Text (SFML2)
Post by: Zinlibs on January 31, 2012, 12:13:28 am
Code: [Select]
sf::Text Text;
sf::FloatRect rect = Text.GetGlobalBounds();


Global can be replaced by Local.
Title: How to center sf::Text (SFML2)
Post by: g@rion 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 ?
Title: How to center sf::Text (SFML2)
Post by: Laurent on January 31, 2012, 09:54:34 am
This function was removed.
Title: How to center sf::Text (SFML2)
Post by: Mecha the Slag 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
Title: How to center sf::Text (SFML2)
Post by: Mjonir 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 :)