To get started with making games it's often a good idea to be able to write out debug messages while you're working on your game even if that's just the current FPS. MonoGame doesn't allow you to use the system fonts on the computer the game is running on you need to provide your own. To achieve this you need to add a font to your game to be used for writing strings to the screen.
To be able to use a TrueType font, MonoGame requires the TrueType font file and a .spritefont file.
<FontName>Arial</FontName>
private SpriteFont systemFont;
// TODO: use this.Content to load your game content here
systemFont = Content.Load<spritefont>("SystemFont");
// TODO: Add your drawing code here
_spriteBatch.Begin();
_spriteBatch.DrawString(systemFont, "Hello World.", new Vector2(100.0f, 100.0f), Color.Green);
_spriteBatch.End();
base.Draw(gameTime);
If like me you're using Linux Mint(any Debian based Distro) you can install the Microsoft TTF fonts by running the following from the terminal.
sudo apt-get install ttf-mscorefonts-installer
This will give you access to fonts like:-
Another option is to download the Redistributable Font Pack from XNAGameStudio by downloading the zip file from the Archived GitHub site.
Once you download the zip file unarchive the contents of the zip file to your user font folder (~/.local/share/fonts).
when you add new fonts to this folder then it's a good idea to log out and back in again or refresh your font cache with the command:-
sudo fc-cache -fv
But remember you need to be sure that you are allowed to use a given font in your game, it's always a good idea to only use fonts that you know have a Creative Commons license that allows commercial use.
If you want to write something on the screen without getting sued? These sites contain, besides other stuff, many free-for-commercial-use fonts (you'll have to read the licenses though).
If you don't like the fact you currently need a windows application to add content to your games in Linux here is an extremely quick overview of how I add spriteFontDescription files to my project without needing to use the MGCB Editor.
ext install bam.vscode-file-templates
"Sprite Font Description": {
"prefix": "font",
"body": [
"#begin ${1:fileName}.spritefont",
"/importer:FontDescriptionImporter",
"/processor:FontDescriptionProcessor",
"/processorParam:PremultiplyAlpha=True",
"/processorParam:TextureFormat=Compressed",
"/build:${1:fileName}.spritefont",
"$0"
],
"description": "A sprite font description block."
}
Now when you want to add a new font to the project.
davehenry.blog
by
Dave Henry
is licensed under
CC BY-NC-SA 4.0