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

Author Topic: Load Font Produces Error If Full Directory Path Not Declared  (Read 13239 times)

0 Members and 1 Guest are viewing this topic.

Stupidmonkey

  • Newbie
  • *
  • Posts: 13
    • View Profile
Load Font Produces Error If Full Directory Path Not Declared
« on: August 09, 2010, 12:35:19 pm »
Hi,

I have modified the font from comic.ttf to arial.ttf in the following example code - please forgive...

When I try to use the following code from the example file graphics-fonts.cpp and I do not show the full directory path in if (!MyFont.LoadFromFile("arial.ttf", 50)) the system advises the following error "Failed to load font "arial.ttf" and exits the program.

If I place "C:/Windows/Fonts/arial.ttf", the program will execute correctly.

2 issues... Firstly, the program executes correctly as per above so I believe all libraries are linked correctly. Secondly, I have the arial.ttf font in the same directory with the executable so the program should be able to find "arial.ttf" without the full path.

I would like to not have the specify the full path for obvious reasons, but I can't figure out where I am going wrong.

My system is Windows 7 and using Netbeans 6.9...

Your help would be graciously received...

Code: [Select]
// Load a font from a file
    sf::Font MyFont;
    if (!MyFont.LoadFromFile("arial.ttf", 50))
        return EXIT_FAILURE;


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


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Fonts");

    // Load a font from a file
    sf::Font MyFont;
    if (!MyFont.LoadFromFile("comic.ttf", 50))
        return EXIT_FAILURE;

    // Create a graphical string
    sf::String Hello;
    Hello.SetText("Hello !\nHow are you ?");
    Hello.SetFont(MyFont);
    Hello.SetColor(sf::Color(0, 128, 128));
    Hello.SetPosition(100.f, 100.f);
    Hello.SetRotation(15.f);
    Hello.SetSize(50.f);

    // You can also use the constructor
    sf::String Bonjour("Salut !\nComment ça va ?", sf::Font::GetDefaultFont(), 30.f);
    Bonjour.SetColor(sf::Color(200, 128, 0));
    Bonjour.SetPosition(200.f, 300.f);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Make the second string rotate
        Bonjour.Rotate(App.GetFrameTime() * 100.f);

        // Clear screen
        App.Clear();

        // Draw our strings
        App.Draw(Hello);
        App.Draw(Bonjour);

        // Finally, display rendered frame on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Load Font Produces Error If Full Directory Path Not Declared
« Reply #1 on: August 09, 2010, 12:42:09 pm »
If the font file is located in the same directory as the executable, and the program fails to find it, it means that the current working directory is wrong.

How do you launch the executable? From the console, the explorer or an IDE?
Laurent Gomila - SFML developer

Stupidmonkey

  • Newbie
  • *
  • Posts: 13
    • View Profile
Load Font Produces Error If Full Directory Path Not Declared
« Reply #2 on: August 10, 2010, 12:18:29 pm »
You are awesome and a bloody legend!!!

I'm just learning at the moment and I had no clue about a working directory.

I never tried to run the program outside of the IDE so I ran the program from the folder that contained the executable and the arial.ttf file, and the program worked, but from the IDE it would not work.

If you have the same problem as I did using Netbeans 6.9 as an IDE on Windows 7, complete the following steps:

1) Right click on the project under the 'Projects' pane and select 'Properties'.
2) In the 'Project Properties' window that is displayed select 'Run' under categories.
3) Next to the the 'Run Directory' option select '...' to add the full directory where the executable and font file exists.
4) The program will find the font.ttf file and run correctly.

No need for hoodoo. You just need some one who can teach a man how to fish. Thanks again Laurent.