SFML community forums

Help => Graphics => Topic started by: wh1t3crayon on November 13, 2014, 06:22:57 pm

Title: Help drawing sf::Text accurately on screen
Post by: wh1t3crayon on November 13, 2014, 06:22:57 pm
Yes this has been discussed before, but from the hours I've spent searching I've never seen it solved. Basically, I'm trying to position a text object directly in the center of the screen, but it appears to be off by several pixels on both axels. Here is the basic code:
sf::Text text;
text.setString("sf text test");
//set origin to physical center of text
text.setOrigin(text.getGlobalBounds().left + (text.getGlobalBounds().width / 2), text.getGlobalBounds().top + (text.getGlobalBounds().height / 2));
//screen is 500 * 500, so this is the center of screen
text.setPosition(250, 250);
 
Yet the linked image is what happens on screen. Adding 20 pixels on the x value and 4 pixels on the y value seems to account for this shift perfectly. I've concluded that this is because there is a hidden, much larger bounding box around the text that is the result of getGlobalBounds(), so how do I account for this hidden variable?
Title: AW: Help drawing sf::Text accurately on screen
Post by: eXpl0it3r on November 13, 2014, 07:14:30 pm
Are you using the latest version from GitHub?
Title: Re: Help drawing sf::Text accurately on screen
Post by: wh1t3crayon on November 14, 2014, 12:30:23 am
Latest version of what exactly?
Title: Re: Help drawing sf::Text accurately on screen
Post by: Nexus on November 14, 2014, 12:36:09 am
SFML.

Latest version means you checkout the HEAD revision of the master branch of SFML's GitHub repository. That is, the commit containing the newest changes.
Title: Re: Help drawing sf::Text accurately on screen
Post by: wh1t3crayon on November 14, 2014, 04:52:30 am
Yes I'm using 2.1 the most recent stable build.
Title: Re: Help drawing sf::Text accurately on screen
Post by: dabbertorres on November 14, 2014, 06:41:53 am
No, not the version from the website, the version you have to compile yourself from github.
Title: Re: Help drawing sf::Text accurately on screen
Post by: wh1t3crayon on November 14, 2014, 02:42:36 pm
Before I spend time compiling that, does anybody know if the text will be aligned in this version?
Title: Re: Help drawing sf::Text accurately on screen
Post by: Hapax on November 14, 2014, 06:59:20 pm
I believe there have been a lot of alterations to the text part of SFML since version 2.1 so it's likely that the latest master would solve your problem.
My SFML is a couple of months old and shows this:
(http://i.imgur.com/PNSSru1.jpg)
using the code:
        sf::Font font;
        font.loadFromFile("G:/Resource Pool/fonts/arial.ttf");
        sf::Text text;
        text.setFont(font);
        text.setString("sf text test");
        text.setOrigin(text.getGlobalBounds().left + round(text.getGlobalBounds().width / 2), text.getGlobalBounds().top + round(text.getGlobalBounds().height / 2));
        text.setPosition(250, 250);
        text.setColor(sf::Colors::Green);

In case you were wondering why there was some rounding there, it's because the text might be an odd number size and therefore will set the origin to half way between two whole number positions.
This is the difference between using round and not using it:
(http://i.imgur.com/8t2Wvxd.jpg)
Title: Re: Help drawing sf::Text accurately on screen
Post by: wh1t3crayon on November 16, 2014, 05:43:15 am
Thank you for that info and I'll make a note to update soon, but for now I have found an interesting solve. In the line where I set the text's origin, (I'm on mobile so this will be in English) I had to divide the width by 4 instead of 2, yet still divide the height by 2. The text now lines up on its center.