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 position on screen, the string that holds the message, and, of course, associating it with our font object:

// Assign the actual message 
myText.setString("Press Enter to start!"); 
 
// assign a size 
myText.setCharacterSize(75); 
 
// Choose a color 
myText.setFillColor(Color::White); 
 
// Set the font to our Text object 
myText.setFont(font); 
 

Let's add a HUD to Timber!!!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.0.85