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

Author Topic: Problem with fonts  (Read 3794 times)

0 Members and 2 Guests are viewing this topic.

TopMux

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with fonts
« on: July 25, 2024, 05:17:09 pm »
I'm just starting to learn SFML so I don't understand why this code isn't working. As I understand it, the font simply cannot be loaded, which is why the error occurs. I tried everything I could, absolute path, different fonts, different paths, ran as administrator.
Error:
Failed to load font "AtЇE"
arial.ttf HїO╓▓█ПьУ·┤╟Ў∙FфT}ь< ─Ў∙" (failed to create the font face).
Here is the code:
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Text");

    sf::Font font;
    if (!font.loadFromFile("arial.ttf")) {
        return EXIT_FAILURE;
    }

    sf::Text text("Hello, SFML!", font, 30);
    text.setFillColor(sf::Color::White);
    text.setPosition(200, 300);

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

        window.clear();
        window.draw(text);
        window.display();
    }

    return EXIT_SUCCESS;
}
 
Some additional information: I'm working in Visual Studio 2022 and my operating system is Windows 11

« Last Edit: July 28, 2024, 02:37:16 pm by eXpl0it3r »

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Problem with fonts
« Reply #1 on: July 25, 2024, 07:30:49 pm »
Are you storing the .ttf in the correct directory? Should be the working one with all your other stuff.

TopMux

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem with fonts
« Reply #2 on: July 25, 2024, 08:15:37 pm »
I copied arial.ttf to all project folders.

Martin Sand

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: Problem with fonts
« Reply #3 on: July 28, 2024, 12:00:21 pm »
You have to copy the .ttf file to the folder from where you start the application as you want to access it from there. You load the font without any directory information, so it tries to access it from the same location.

font.loadFromFile("arial.ttf")

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Problem with fonts
« Reply #4 on: July 28, 2024, 02:38:05 pm »
You most likely mixed debug and release, thus ending up with different runtime libs, that can cause odd corruptions around strings, due to different ABIs.

When you're building in debug mode, make sure to use the SFML libraries with the -d suffix.
In release mode, use the ones without the -d suffix.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TopMux

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem with fonts
« Reply #5 on: August 02, 2024, 10:05:34 pm »
Thanks for this answer. It finally worked!