SFML Text and Font

Let's briefly discuss the Text and Font classes with some hypothetical code, before we actually go ahead and add code to our game.

The first step to drawing text on the screen is to have a font. In the first chapter we added a font file to the project folder. Now we can load the font, ready for use, into an SFML Font object.

The code to do so looks like this:

Font font; 
font.loadFromFile("myfont.ttf"); 

In the previous code we first declare the Font object and then load an actual font file. Note that myfont.ttf is a hypothetical font and we could use any font that is in the project folder.

Once we have loaded a font we need an SFML Text object:

Text myText; 

Now we can configure our Text object. This includes the size, the color, the ...

Get Beginning C++ Game Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.