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

Author Topic: Help drawing sf::Text accurately on screen  (Read 4745 times)

0 Members and 1 Guest are viewing this topic.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Help drawing sf::Text accurately on screen
« 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?
« Last Edit: November 13, 2014, 06:27:46 pm by wh1t3crayon »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Help drawing sf::Text accurately on screen
« Reply #1 on: November 13, 2014, 07:14:30 pm »
Are you using the latest version from GitHub?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Help drawing sf::Text accurately on screen
« Reply #2 on: November 14, 2014, 12:30:23 am »
Latest version of what exactly?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help drawing sf::Text accurately on screen
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Help drawing sf::Text accurately on screen
« Reply #4 on: November 14, 2014, 04:52:30 am »
Yes I'm using 2.1 the most recent stable build.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Help drawing sf::Text accurately on screen
« Reply #5 on: November 14, 2014, 06:41:53 am »
No, not the version from the website, the version you have to compile yourself from github.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Help drawing sf::Text accurately on screen
« Reply #6 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?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help drawing sf::Text accurately on screen
« Reply #7 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:

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:
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Help drawing sf::Text accurately on screen
« Reply #8 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.

 

anything