I'm trying to write some text to the screen, but it feels like no matter what I do i always get:
Failed to load font "../assets/Font.ttf" (failed to create the font face)
A very simple example of the code.
#include<SFML/Graphics.hpp>
int main()
{
sf::Font font;
font.loadFromFile("../assets/Font.ttf");
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
sf::Text text("Hello World", font, 50);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(text);
window.display();
window.clear();
}
return 0;
}
I'm building using Cmake
and I expect that the problem lies in my CMakeLists
files. I have one main and a sub-directory like: (Also very simple)
#Main
cmake_minimum_required(VERSION 3.0)
project(Sorting_Visualization)
add_subdirectory(src)
# Sub Directory
add_executable(${PROJECT_NAME} main.cpp)
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-audio)
File structure:
.
├── assets
│ └── Font.ttf
├── CMakeLists.txt
└── src
├── CMakeLists.txt
└── main.cpp
I read that similar problems occurred when people using Visual Studio linked release libs to their debug builds. There was suggested to link with ex. sfml-graphics-d
for debug libs. However this doesn't seem to work for me and using dpkg libsfml-dev
I don't see any files matching sfml-graphics-d
. Should I maybe install these in some way?. Or is that some Visual Studio specific issue?
Further I think I was missing the Freetype
dependecy at the time of installing SFML
however running ldd
on my sfml-graphics.so
file it seem that freetype is found.
libfreetype.so.6 => /lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f26fdca0000)
Lastly I was thinking that maybe the font I'm using is simply not compatible. But surely any true type font should be possible to use. Here is a link the font.
I think I've read nearly everything there is online and I'm still completely stumped. The only thing left is my linking in Cmake which I found no clear resources on and I'm pretty new to working with libs. Some guidence would be greatly appreciated on this : )
I solved the problem and it was apparently the order in which I was finding the packages and linking them. I also have to always make all
This is working!
set(SOURCES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
find_package(SFML 2.5 COMPONENTS REQUIRED audio graphics )
target_link_libraries(${PROJECT_NAME} sfml-audio sfml-graphics)