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

Author Topic: custom text  (Read 1478 times)

0 Members and 1 Guest are viewing this topic.

andrewkoro105

  • Newbie
  • *
  • Posts: 5
    • View Profile
custom text
« 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.



#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;
}
« Last Edit: June 14, 2022, 12:00:18 pm by andrewkoro105 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: custom text
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

andrewkoro105

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: custom text
« Reply #2 on: June 14, 2022, 11:35:45 am »
I need to find bearing


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: custom text
« Reply #3 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything