How to add text to 2D graphics
The Font class is no longer in libGDX and BitmapFont class is now in force.
Sample code from SVN:
!TextureAtlas textureAtlas = new !TextureAtlas("data/main"); font = new !BitmapFont(Gdx.files.internal("data/calibri.fnt"), textureAtlas.findRegion("calibri"), false);
Q: How can i write some text? Do I need font.png and font.fnt?
A: There is an embedded font "arial-15", so you can directly write using a BitmapFont. For custom text instantiate a BitmapFont, and use
font.draw(spritebatch, "my-string", x, y);
A: you need a .png file containing every symbol you need your font to contain (e.g. alphanumeric characters). The .fnt file houses the locations of every character in the .png file.
One of the simplest ways to procure these files is through a piece of software called Hiero, which generates bitmap fonts of multiple sizes and styles based on your computer's TTF fonts.
A nice tutorial for the user-interface can be found here
Once you have made all the fonts you need, you need to add them to your project's /data folder.
At minimum, you need to declare two objects: a SpriteBatch to extract the characters from the .png file and your BitmapFont object.
SpriteBatch spriteBatch; BitmapFont font; CharSequence str = "Hello World!"
BitmapFonts can only draw text using CharSequence objects, so I'm providing one in the example.
It's best practice to use Gdx.files.internal(Str relativePath) to retrieve your .png and .fnt files.
spriteBatch = new SpriteBatch(); font = new BitmapFont(Gdx.files.internal("data/nameOfFont.fnt"), Gdx.files.internal("data/nameOfFont.png"), false);
The first line in the example simply instantiates a new SpriteBatch, which you can learn about elsewhere in this wiki (TODO: link to SpriteBatch tutorial and/or API doc).
The second line instantiates our font.
[TODO: provide examples for each constructor method in the API docs]
Text is written on-screen in the class's draw method, but the SpriteBatch object must be called first.
spriteBatch.begin(); font.setColor(1.0f, 1.0f, 1.0f, 1.0f); font.draw(spriteBatch, str, 25, 160); spriteBatch.end();
The first and last lines in the code are essential, without which you will generate lots of errors. What you really should be concerned with are the font.setColor() and font.draw() commands.
font.setColor() is used here as a precautionary measure. Hypothetically speaking, if the characters in your font's .png file are colored black and your screen's background is also colored black, you will never see your text when it is drawn. font.setColor() can change the color of your font to anything within the visible spectrum, and can even take an alpha variable if you want to add transparency to it.
font.draw() is where everything happens: The first argument passed is the SpriteBatch object. The second argument passed is the CharSquence object we defined at the beginning of this example. The third and fourth arguments are ints in the x and y position, respectively - in integer values - that indicate the text's starting position.
There are several methods included in the BitmapFont class that allow you to format your text.
font.setColor(0.0f, 0.0f, 1.0f, 1.0f); // tint font blue spriteBatch.begin(); font.draw(spriteBatch, charSeq, xPos, yPos); spriteBatch.end();
TODO: write about scaling fonts to different sizes.
Another approach is to have the text made up from bitmaps as described in this String builder tutorial: