sfml - drawing text

Hello,

I am having difficulty drawing text to a window using SFML.

My code is below :

#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>


int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);

sf::Font font;
if (!font.loadFromFile("arial.ttf"))
{
// error
}
sf::Text text;
text.setFont(font);
text.setString("Hello World");
text.setCharacterSize(24);
text.setColor(sf::Color::Red);
text.setStyle(sf::Text::Bold);

shape.setPosition(150, 0);
text.setPosition(150, 400);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}

window.clear(sf::Color::White);
window.draw(shape);
window.draw(text);
window.display();
}

return 0;
}


When compiled (using the Microsoft visual studio 2015 x64 compiler) I only get the green circle drawn. I have put the arial.ttf file in all possible folders and it still wont print.

Anybody know exactly which folder the .ttf file should go in ? If that's not the issue could you point (the possibly obvious) mistake?

Thanks
Presumably the debugging working directory. Note that by default Visual Studio puts the generated executables in $(SolutionDir)\$(Configuration), but during debugging sets the working directory to $(SolutionDir)\$(ProjectName).
This is IMO a brain-dead default. I always set both to $(SolutionDir)\bin (or bin64 when targeting x86-64).
Thanks, but still doesn't work. I have copy-pasted arial.tff in every single folder of my project and still doesn't work.
More then likely it isn't loading because you don't have the font inside of your working directory's path.

Now the working directory can change depending on what IDE you are using and how you are running the program. For VS the working directory when running your program inside of the IDE is the folder where your VS project file is (Not to be confused with your solution file).

Though when you are running the application outside of your IDE (Meaning you go straight to the executable folder and run it from there) your working directory will be the folder where that executable is located.

So with that in mind you will have separate paths where your resources go depending on how you are running your SFML program. So if you are running the program inside of Visual Studio you need to place the font file in the same folder as your project file (.vcxproj). Otherwise if you are running it manually through either browsing to the folder of you .exe or through a non VS cmd then you need to place the font in your .exe's folder.

And if even all that fails try loading it with an absolute path instead of a relative one. For example if lets say you have the font in your projects folder which is located on the C drive you can do something like this.

1
2
3
4
if (!font.loadFromFile("C:/MyProject/arial.ttf"))
{
    // error
}


And finally it can be quite a pain to have all your assets (Fonts, Textures, Sounds ,etc) in the same folder as your either your .exe or project file. So usually what most people end up doing is adding an "Assets" folder to the folder where their project file is. So for example

MyProjectFolder
|
|- MyProject.vcxproj
|- Assets
     |
     |- Fonts
         |
         |-arial.ttf
     |
     |- Textures

etc. etc.


And then doing your loading call like so

1
2
3
4
if (!font.loadFromFile("Assets/Fonts/arial.ttf"))
{
    // error
}


Anyways hopefully that helps you out a little bit with VS's working directories and how SFML handles asset loading. If you have any more questions regarding working with assets in SFML would be glad to help.
Last edited on
Topic archived. No new replies allowed.