Let's write a small program which will open a window, and write "Hello World" on the screen.
#include <SFML\Graphics.hpp>
#include <cassert>
int main() {
sf::RenderWindow sfmlWin(sf::VideoMode(600, 360), "Hello World SFML Window");
sf::Font font;
//You need to pass the font file location
if (!font.loadFromFile(/*
Put the filename that identify the font file you want to load*/"myfont.ttf")) {
return -1;
}
sf::Text message("Hello, World !", font);
while (sfmlWin.isOpen()) {
sf::Event e;
while (sfmlWin.pollEvent(e)) {
switch (e.type) {
case sf::Event::EventType::Closed:
sfmlWin.close();
break;
}
}
sfmlWin.clear();
sfmlWin.draw(message);
sfmlWin.display();
}
return 0;
}
Let's explain what we did there.
First, we created a sf::Font
object. We need this object to store the font data that we will use to display the text. After that, we called the loadFromFile
method, used to load the font in the memory. We should note that SFML don't know about your system fonts, so you need to provide a filename, not a font name
After that, we created a sf::Text
object. We call a 3 parameter constructor taking :
Since the sf::Text
object is ready, we just need to draw it in the main sfml loop, by calling the draw method on the sfmlWin
render window object that we created before