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

Author Topic: Font file isnt found  (Read 42 times)

0 Members and 1 Guest are viewing this topic.

jjasome11

  • Newbie
  • *
  • Posts: 1
    • View Profile
Font file isnt found
« on: March 02, 2026, 05:41:32 pm »
I am using Microsoft Visual Studio version 18.3.2, I am attempting to recreate pong in c++, and everything up until I have tried to use text has worked perfectly fine. I downloaded Arial.ttf and put it into my project directory, but when I use sf::Font font("Arial.ttf"), trying to run it gives me the no such file was found in the directory error. I am on a console application file, using ctrl+f5 in the Visual Studio to build the exe and run it.

The following is the code for the game
#include <iostream>
#include <SFML/Graphics.hpp>
#include <chrono>
#include <thread>
#include <cmath>

int main()
{

    using namespace sf::Literals;

    int score = 0;

    sf::RenderWindow window(sf::VideoMode({ 800, 800 }), "PONG", sf::Style::None);
    sf::RectangleShape PaddleL({ 20.f, 160.f });
    sf::RectangleShape paddleR({ 20.f, 160.f });
        sf::CircleShape ball(10.f);
        sf::Font font("Arial.ttf");
        sf::Text scoretext(font);

        scoretext.setCharacterSize(30);
        scoretext.setFillColor(sf::Color::White);
        scoretext.setPosition({ 400.f, 10.f });
        scoretext.setString(std::to_string(score));

        ball.setOrigin({ 10.f, 10.f });
        ball.setPosition({ 400.f, 400.f });

        PaddleL.setOrigin({ 10.f, 80.f });
        paddleR.setOrigin({ 10.f, 80.f });

        PaddleL.setPosition({ 30.f, 400.f});
        paddleR.setPosition({ 770.f, 400.f });

    PaddleL.setFillColor(sf::Color::White);
        paddleR.setFillColor(sf::Color::White);

    bool PaddleLup = false;
    bool PaddleLdown = false;
    bool paddleRup = false;
    bool paddleRdown = false;
    bool play = false;

    auto right_vertical = paddleR.getPosition().y;
    auto left_vertical = PaddleL.getPosition().y;

    int right_top = right_vertical - 80;
    int right_bottom = right_vertical + 80;
    int left_top = left_vertical - 80;
    int left_bottom = left_vertical + 80;

    std::srand(std::time(nullptr));
    float start_angle = std::rand() % 360;
        start_angle = start_angle * 3.1415 / 180;
    float start_velocity = 0.2f;
       

    while (window.isOpen())
    {

        while (const std::optional event = window.pollEvent())
        {
            if (event->is<sf::Event::Closed>())
                window.close();

            if (const auto* Keypressed = event->getIf<sf::Event::KeyPressed>()) {
                if (Keypressed->scancode == sf::Keyboard::Scan::W)
                    PaddleLup = true;
                else if (Keypressed->scancode == sf::Keyboard::Scan::S)
                    PaddleLdown = true;
                if (Keypressed->scancode == sf::Keyboard::Scan::Up)
                    paddleRup = true;
                else if (Keypressed->scancode == sf::Keyboard::Scan::Down)
                    paddleRdown = true;
                if (Keypressed->scancode == sf::Keyboard::Scan::Space) {
                    std::srand(std::time(nullptr));
                    if (play == false) {
                        play = true;
                    }
                    else {
                        play = false;
                        ball.setPosition({ 400.f, 400.f });
                        start_angle = 3.1415;
                    }
                }
               
            }
            if (const auto* KeyReleased = event->getIf<sf::Event::KeyReleased>()) {
                if (KeyReleased->scancode == sf::Keyboard::Scan::W)
                    PaddleLup = false;
                else if (KeyReleased->scancode == sf::Keyboard::Scan::S)
                    PaddleLdown = false;
                if (KeyReleased->scancode == sf::Keyboard::Scan::Up)
                    paddleRup = false;
                else if (KeyReleased->scancode == sf::Keyboard::Scan::Down)
                    paddleRdown = false;
            }

        }
        if (ball.getPosition().y <= 10 or ball.getPosition().y >= 790)
            start_angle = -start_angle;

        if (play) {
            if (PaddleLup and PaddleL.getPosition().y >= 80)
                PaddleL.move({ 0, -0.5 });
            else if (PaddleLdown and PaddleL.getPosition().y <= 720)
                PaddleL.move({ 0, 0.5 });
            if (paddleRup and paddleR.getPosition().y >= 80)
                paddleR.move({ 0, -0.5 });
            else if (paddleRdown and paddleR.getPosition().y <= 720)
                paddleR.move({ 0, 0.5 });

            right_vertical = paddleR.getPosition().y;
            left_vertical = PaddleL.getPosition().y;
            right_top = right_vertical - 80;
            right_bottom = right_vertical + 80;
            left_top = left_vertical - 80;
            left_bottom = left_vertical + 80;

            if ((ball.getPosition().x - 10 <= 40 and ball.getPosition().x >= 20.f) and (ball.getPosition().y >= left_top and ball.getPosition().y <= left_bottom)) {
               start_angle = 3.1415 - start_angle;
                                start_angle += (std::rand() % 12 - 6) * 3.1415 / 180;
                start_velocity = start_velocity * 1.02f;
                                std::cout << start_velocity << " " << start_angle << std::endl;

            }
            else if ((ball.getPosition().x + 10 >= 760 and ball.getPosition().x <= 780) and (ball.getPosition().y >= right_top and ball.getPosition().y <= right_bottom)) {
                start_angle = 3.1415 - start_angle;
                                start_angle += (std::rand() % 12 - 6) * 3.1415 / 180;
                                start_velocity = start_velocity * 1.02f;
                                std::cout << start_velocity <<  " " << start_angle << std::endl;
            }
           
            float sine = sin(start_angle);
            float cosine = cos(start_angle);
            ball.move({ cosine * start_velocity, sine * start_velocity});
        }

                right_vertical = paddleR.getPosition().y;
                left_vertical = PaddleL.getPosition().y;
                right_top = right_vertical - 80;
                right_bottom = right_vertical + 80;
                left_top = left_vertical - 80;
                left_bottom = left_vertical + 80;

        scoretext.setString(std::to_string(score));

        window.clear();
        window.draw(PaddleL);
                window.draw(paddleR);
                window.draw(ball);
                window.draw(scoretext);
        window.display();
       
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
 

removing lines 18-24, 146, and 152 makes the code run perfectly fine, those are the lines related to text.

vokazoo

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Font file isnt found
« Reply #1 on: March 02, 2026, 10:03:45 pm »
The code itself doesn't cause the error you're getting, this is almost certainly the result of project misconfiguration. Have you tried putting Arial.ttf in the same folder as the executable? What's the project structure like?

Try passing the absolute path of the font file (for example: sf::Font font("C:\\Users\\username\\ProjectName\\Arial2.ttf"); ), if that works the problem is confirmed.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11296
    • View Profile
    • development blog
    • Email
Re: Font file isnt found
« Reply #2 on: March 03, 2026, 07:38:38 am »
The error message on the console should tell you, where sf::Font expected the font to be located.

Paths are loaded relative to the working directory. If you launch it from VS it would be by default the project location. If you run it from the explorer, it would be next to the executable.

Note that Microsoft's license doesn't allow you to redistribute Arial. I suggest you pick an open font such as DejaVuSans or similar.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything