SFML Text not Displaying?

closed account (j1CpDjzh)
I'm new to SFML (and external libraries in general - I messed around with OpenGL and GLUT for awhile, but it was long ago and I only ever messed around with shapes), so maybe I just have an error that I can't see, but my compiler (Xcode 5) isn't picking up any errors. Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "ResourcePath.hpp"

using namespace sf;

int main()
{
    RenderWindow window(VideoMode(800, 600), "Name here", Style::Default);
    
    window.setVerticalSyncEnabled(true);
    
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
            {
                window.close();
            }
        }
        
        window.clear(Color::Black);
        
        //Draw everything here
        Font font;
        if (!font.loadFromFile("Arial.ttf"))
        {
            //Nothing goes here...?
        }
        
        Text text;
        text.setFont(font);
        text.setString("Hello World");
        text.setCharacterSize(24);
        text.setColor(Color::White);
        
        window.draw(text);
        
        window.display();
    }

    return 0;
}


I tried Googling the question, but 90% of what came up were answers like "You didn't set a font, that's why the text isn't being displayed." But I set a font, and I included it in my Resources folder. Maybe I've just defined everything in the wrong place??
Hello, I tested the code and it works fine. Check that you have the font in the correct directory. Is your line 29 returning true?
closed account (j1CpDjzh)
I just checked the console and found this error:

Failed to load font "Arial.ttf" (failed to create the font face)

I don't understand why it's failing though? It says the font is in the Resources folder ( http://prntscr.com/48te54 ).
The program doesn't know to look in your resources folder. Whenever you give a filename without a full path, it looks first in the current directory.

This directory is usually the same directory as the exe. Though in some IDEs (like VS), when you run from the IDE the directory is configurable, and it'll default to the project directory.


If you're on Windows... you can do this to see the current directory:

1
2
3
4
// be sure to #include <Windows.h> .. at least temporarily
    char path[MAX_PATH];
    GetCurrentDirectoryA(MAX_PATH,path);
    MessageBoxA(NULL,path,"Current Directory",MB_OK);



Make sure you put Arial.ttf in that directory.
closed account (j1CpDjzh)
Thank you so much! I'm on a Mac, so I had to do it manually, but the code is working just fine now.
Topic archived. No new replies allowed.