SDL_Chomik: Using fonts

Pawel BiernackiPawel Biernacki
3 min read

In our first SDL_Chomik article, we learned how to display an image in every iteration of the sdl loop. We also saw that you cannot directly use the predefined integer variable the created image index in multiple places — its value changes each time you create a new image.
The solution is simple: store the index in your own variable and use that instead.

This time, we’ll build on that technique and introduce fonts.


Fonts in SDL_Chomik

SDL_Chomik currently supports TTF fonts only.
Just like images, fonts must be created (loaded from a file) before they can be used.

When creating a font, you need:

  • The font file name (string)

  • The font size (integer)

Once a font is created, the predefined integer variable the created font index is updated. As with images, it’s common practice to store this value in your own variable — even if you only use one font. This makes your code cleaner and easier to maintain.


Showing Text

To display text in SDL_Chomik, we use the built-in code family called show text.
In Chomik, there are no real “parameters” — all the information (font index, text, X position, Y position) becomes part of the compound name of the code.

For example, a command like:

<show text 1 "hello world!" 200 100>

can be thought of as:

  1. show (identifier)

  2. text (identifier)

  3. font index (1)

  4. text string ("hello world!")

  5. X position (200)

  6. Y position (100)


Complete Example

Below is a minimal program that:
- Loads two images (chomik.png and background.png)
- Loads a font (PlayfairDisplay-Regular.ttf at size 32)
- Stores all indices in non-predefined variables
- Shows both images and the text "hello world!" at position (200, 100) in each iteration of the sdl loop.

<create new image "chomik.png">;
variable chomik image index: integer;
let chomik image index = <the created image index>;

<create new image "background.png">;
variable background image index: integer;
let background image index = <the created image index>;

<create new font "PlayfairDisplay-Regular.ttf" 32>;
variable my font index: integer;
let my font index = <the created font index>;

let sdl loop body = value code
{
<show image <background image index> 0 0>;
<show image <chomik image index> 0 0>;
<show text <my font index> "hello world!" 200 100>;
};

<sdl loop>;


Key Points

  • Always store the created image index and the created font index in your own variables.

  • Fonts in SDL_Chomik work similarly to images — they must be created first.

  • show text uses a compound code name instead of traditional function parameters.


Font License Notice

The file PlayfairDisplay-Regular.ttf (and some other Playfair fonts) are included with the Chomik source code (in the font folder) for convenience only.
I do not own these fonts — they have a separate license:
Copyright (c) 2010-2012 by Claus Eggers Sørensen (es@forthehearts.net), with Reserved Font Name 'Playfair'
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This is available with a FAQ at: http://scripts.sil.org/OFL
The license file is also available here:
github.com/pawelbiernacki/chomik/blob/main/font/OFL.txt

0
Subscribe to my newsletter

Read articles from Pawel Biernacki directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pawel Biernacki
Pawel Biernacki

I was born in 1973, in Cracow, Poland. I am a software engineer.