SFML community forums

Help => Graphics => Topic started by: andrewkoro105 on June 14, 2022, 11:10:56 am

Title: custom text
Post by: andrewkoro105 on June 14, 2022, 11:10:56 am
I'm trying to create custom text and I can't get origin or bearing for the character.

(https://hsto.org/r/w1560/webt/ux/2y/kh/ux2ykhrgznafueqwujrjdqgun9q.png)

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::Sprite sprite, sprite1;


    sf::Font font;
    font.loadFromFile("segoeui.ttf");
    sf::Glyph glyph0 = font.getGlyph('a', 20, false);
    sf::Glyph glyph1 = font.getGlyph('F', 20, false);

    sprite.setTexture(font.getTexture(20));
    sprite.setTextureRect(glyph0.textureRect);
    sprite.setPosition(0, 20);

    sprite1.setTexture(font.getTexture(20));
    sprite1.setTextureRect(glyph1.textureRect);
    sprite1.setPosition(glyph1.advance, 20);

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

        window.clear();
        window.draw(sprite);
        window.draw(sprite1);
        window.display();
    }

    return 0;
}
Title: Re: custom text
Post by: eXpl0it3r on June 14, 2022, 11:16:05 am
Might want to be a bit more specific what you want to achieve, "can't get origin or bearing" doesn't really mean much without providing additional context. ;)
Title: Re: custom text
Post by: andrewkoro105 on June 14, 2022, 11:35:45 am
I need to find bearing

(https://hsto.org/r/w1560/webt/ux/2y/kh/ux2ykhrgznafueqwujrjdqgun9q.png)
Title: Re: custom text
Post by: eXpl0it3r on June 14, 2022, 01:16:43 pm
The glphy returns a bounding rectangle that's relative to the baseline, so glyph.bounds.left should give you the bearingX and glyph.bounds.top should give you the bearingY.
The origin would then be (0, 0).

Note this is all in local coordinates.

Please make sure you double check in what forum thread and sub-forum you're posting. ;)