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

Author Topic: sf::Text appearing with an odd offset  (Read 2143 times)

0 Members and 1 Guest are viewing this topic.

Sam42

  • Newbie
  • *
  • Posts: 16
    • View Profile
sf::Text appearing with an odd offset
« on: October 31, 2012, 08:27:47 pm »
Using latest SFML2 from giit (LaurentGomila-SFML-c02e375)

I seem to be unable to centre sf::Text within a box without manually tuning it. Setting the origin of the text to the centre of its bounds results in the text (and its bounding box) showing with an offset from the expected position.



Minimal code:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Text Test");
    window.setFramerateLimit(60);

    sf::RectangleShape shape(sf::Vector2f(50, 50));
    shape.setPosition(100, 100);
    shape.setFillColor(sf::Color::Blue);

    sf::Font font;
    font.loadFromFile("arial.ttf");

    sf::Text text("A", font, 20);
    text.setOrigin(text.getLocalBounds().width/2, text.getLocalBounds().height/2);
    text.setColor(sf::Color::White);
    text.setPosition(125,125);

    sf::RectangleShape textbox(sf::Vector2f(text.getLocalBounds().width, text.getLocalBounds().height));
    textbox.setPosition(text.getGlobalBounds().left, text.getGlobalBounds().top);
    textbox.setFillColor(sf::Color::Transparent);
    textbox.setOutlineColor(sf::Color::White);
    textbox.setOutlineThickness(1);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();

        window.draw(shape);
        window.draw(textbox);
        window.draw(text);

        window.display();
    }

    return EXIT_SUCCESS;
}

Unless I've missed something, i'd expect the above code to result in the letter (or at least its bounding box) being centred in the bigger blue box. Whether or not the origin for text is set, there still seems to be an odd offset from the expected position.

Having searched, this thread - http://en.sfml-dev.org/forums/index.php?topic=9235.0 - appears to be related, though I can't see a resolution for it.

Any thoughts on why it's happening, or if it is indeed a bug?

Thanks!
« Last Edit: October 31, 2012, 08:33:09 pm by Sam42 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10827
    • View Profile
    • development blog
    • Email
Re: sf::Text appearing with an odd offset
« Reply #1 on: October 31, 2012, 08:35:16 pm »
You should've probably also searched on the issue tracker: #216
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sam42

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: sf::Text appearing with an odd offset
« Reply #2 on: October 31, 2012, 08:54:47 pm »
I had seen that, but thought it was to do with whitespaces not counting properly, and the fact that the bounding box changes size vertically depending on the characters contained in the text.

My problem here is that it appears to be impossible to centre even the reported bounding box. Even without setting the origin of the text to half its size, the topleft of the bounding box will still not be drawn at the centre of the blue rectangle.

Apologies if I've misunderstood.