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

Author Topic: positioning sf::Text  (Read 6032 times)

0 Members and 1 Guest are viewing this topic.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
positioning sf::Text
« on: May 14, 2018, 01:16:27 pm »
hello everybody
i want to align a text box with the bottom edge of the screen. but the way i'm doing it is not working correctly. this is a minimal code example:

#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    sf::Font font;
    font.loadFromFile("automati.ttf");
    sf::Text text("One\nTwo\nThree\nFour", font, 30);
    text.setPosition(window.getSize().x*0.02, window.getSize().y - text.getGlobalBounds().height);
    text.setColor(sf::Color::Black);

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }
        }
        window.clear(sf::Color::White);
        window.draw(text);
        window.display();
    }

    return 0;
}

and this is the result:



i searched it, but haven't found anything about...
i'm using SFML 2.4 from the oficial Debian Testing repositories.

thanks in advance :D
Visit my game site (and hopefully help funding it? )
Website | IndieDB

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: positioning sf::Text
« Reply #1 on: May 14, 2018, 01:37:35 pm »
Can you provide the values that text.getGlobalBounds() returns?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: positioning sf::Text
« Reply #2 on: May 14, 2018, 02:01:13 pm »
text.getGlobalBounds().height is not enough, you must take text.getGlobalBounds().y in account as well. Unlike other drawables, for sf::Text it is not zero (because text is aligned on the baseline, not on the top of the entity).
Laurent Gomila - SFML developer

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: positioning sf::Text
« Reply #3 on: May 15, 2018, 12:54:53 pm »
did you mean text.getGlobalBounds().top?  ;D

thanks, it works now

#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    sf::Font font;
    font.loadFromFile("automati.ttf");
    sf::Text text("One\nTwo\nThree\nFour", font, 30);
    text.setPosition(window.getSize().x*0.02, window.getSize().y - text.getGlobalBounds().height - text.getGlobalBounds().top);
    text.setColor(sf::Color::Black);

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }
        }
        window.clear(sf::Color::White);
        window.draw(text);
        window.display();
    }

    return 0;
}
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything