Fonts

As SadConsole mainly focuses on ASCII, command-prompt style graphics it makes use of fonts. The library only comes with an IBM 8×16 font and an extended version of it.

How is a font defined?

A font uses the “Codepage 437” or also sometimes called “Extended ASCII” layout.
In SadConsole, we define these fonts as an image, with a .font metadata file.
For example, here is the metadata of the default SadConsole font:

{
    "$type": "SadConsole.SadFont, SadConsole",
    "Name": "IBM_8x16",
    "FilePath": "IBM8x16.png",
    "GlyphHeight": 16,
    "GlyphPadding": 1,
    "GlyphWidth": 8,
    "SolidGlyphIndex": 219,
    "Columns": 16
}

Note: This font has a padding of 1 between each glyph cell, some fonts may not have a padding, and this must be specified in the .font file on the “GlyphPadding” property.
This is important so SadConsole can properly render the font.

The font image:
(Note: The background should be transparent, not black. I made it black here so you can see the actual tileset.)

Loading a custom font

To load a custom font into SadConsole we can do this during game creation or at game runtime.

Defining it at game creation:

Builder configuration = new Builder()
	.SetScreenSize(90, 30)
	.OnStart(OnStart)
	.IsStartingScreenFocused(false)
	.ConfigureFonts((fConfig, game) => 
	{
		// Default font for all new consoles
		fConfig.UseCustomFont("fonts/tutorialFont.font");
		// Load other fonts that we can use later
		fConfig.AddExtraFonts("fonts/tutorialFont2.font", "fonts/tutorialFont3.font");
	});

Game.Create(configuration);

This will set “fonts/tutorialFont.font” as the default font for each new console.
Here we also load 2 other fonts that we could use later down the line.

To load a new font during runtime you can use:

var font = Game.Instance.LoadFont("fonts/tutorialFont4.font");

To retrieve a loaded font you can use:

var font = Game.Instance.Fonts["tutorialFont4"];

To apply the font to a console you can simply set a console’s .Font property.

Game.Instance.StartingConsole.Font = Game.Instance.Fonts["tutorialFont2"];

You can even scale the font size like this:

// Scale to a certain magnitude
Game.Instance.StartingConsole.FontSize = Game.Instance.StartingConsole.Font.GetFontSize(IFont.Sizes.Two);

// Scale to a custom value of 1.75
var font = Game.Instance.StartingConsole.Font;
Game.Instance.StartingConsole.FontSize = new Point((int)(font.GlyphWidth * 1.75), (int)(font.GlyphHeight * 1.75f));

Note that as a side effect, this also offsets the position of drawing to the surface.
A fontsize magnitude of two, means you have to divide your positions by two.
Thankfully SadConsole provides a method to easily translate this.

Point position = new Point(30, 10);
Point translated = position.TranslateFont(currentFontSize, newFontSize);

Some resources where you can find a bunch of fonts:

https://dwarffortresswiki.org/Tileset_repository

https://github.com/Thraka/SadConsole/tree/develop/Fonts

Thats it for fonts!

Leave a comment

Design a site like this with WordPress.com
Get started