2

As I was creating a program that would print text, that I input, out onto the window I have stumbled on a problem. My font always fails to load. I tried putting it in multiple directories, even putting in the whole path to the file yet it does not work. The text printing will not work without the font, therefore I can't progress

Thx for your answers

Here is the code

#include <SFML/Graphics.hpp>
#include <iostream>

int main() {

    sf::String playerInput;
    sf::Font font;
    sf::Text playerText;

    if (!font.loadFromFile("arial.ttf")) {
        printf("Failed to load font.\n");
    }

    playerText.setFont(font);
    playerText.setFillColor(sf::Color::Red);

    sf::RenderWindow window(sf::VideoMode(600, 600), "Window", sf::Style::Close | sf::Style::Resize);
    while (window.isOpen()) {

        sf::Event evt;
        while (window.pollEvent(evt)) {
            switch (evt.type) {
                case sf::Event::Closed:
                    window.close();
                    break;
                case sf::Event::TextEntered:
                        playerInput += evt.text.unicode;
                        playerText.setString(playerInput);
                    break;
                
                default:
                    break;
            }
        }

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

    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    Can you check the location of the `arial.ttf` file against the working directory of the process running your program? – JFMR Oct 26 '20 at 14:40
  • It depends on where you put that file. It's highly unlikely that that the working directory will be the place in your OS that contains the true type fonts. More likely the default working directory is the location of the executable or if you use Visual Studio (I mean the full IDE + compiler not VS Code) the location of your project file. – drescherjm Oct 26 '20 at 15:20
  • @果歩プライit is in the project folder – Martin Rosa Oct 26 '20 at 15:57
  • 1
    Since you now have mentioned that you are using Visual Studio 2019. Make sure you use debug libraries for debug configuration and release libraries for release. – drescherjm Oct 26 '20 at 18:22
  • @drescherjmyep it does work now thx – Martin Rosa Oct 31 '20 at 12:16
  • put font file in directory with your main code – luk_chesnok_xren Nov 01 '20 at 11:38

1 Answers1

0

Edit: I forgot to mention that I am using SFML 2.5.1 for Visual Studio 2017 64bit, while using Visual Studio 2019

  • 1
    I don't believe this solves the problem. Please edit the question with the additional information instead of providing a solution that does not solve the problem. – drescherjm Oct 26 '20 at 18:19