University of Wales Trinity Saint David
ACCB5017 - Games Architecture and Mechanics

Sourcing Fonts

Overview

Text in an SFML project always comes from a font file you supply yourself, SFML ships no default font. This lab covers two ways to get one: copying a font that is already sitting on your machine inside the Windows font store, and downloading a font published under an explicit open license from a site built for that purpose. Because a font file that ends up inside a project you submit or share is being redistributed, not just displayed on your own screen, this lab treats license checking as part of the workflow before loading the chosen font into an SFML program and rendering text with it.

Learning Objectives

By the end of this lab you will be able to:

Concept

SFML has no built-in font. Every sf::Text object needs an sf::Font loaded from a real .ttf or .otf file on disk before it can draw a single glyph, so the first practical question on any project is not how to draw text but where the font file itself is allowed to come from.

The fastest source is already on your machine. Windows keeps its own interface fonts in a system folder, and because they are ordinary files, copying one out and into a project folder works without any error. It also usually breaks the font's license. Most fonts Microsoft bundles with Windows are licensed for use while running Windows, not for embedding inside a separate application, game or coursework submission you hand to someone else. The file copies fine. The right to redistribute it does not come with it. That gap between the copy working and the copy being allowed is the part this lab treats as the actual lesson, not the file operation itself.

The safer source is a font explicitly published for reuse. Font libraries built for this, such as Google Fonts, publish the large majority of their catalogue under the SIL Open Font License (OFL), which explicitly permits embedding, modifying and redistributing the font, including inside a shipped or submitted project, as long as the font itself is not sold on its own. Each download includes the license file itself so the terms travel with the font rather than being buried on a website.

Once a font file is chosen, loading it is identical regardless of where it came from. sf::Font::loadFromFile() takes a path to the .ttf or .otf file and returns false if it cannot find or parse it. An sf::Text object then takes a reference to that loaded font via setFont(), plus a string via setString() and a size via setCharacterSize(), before it can be drawn like any other SFML object.

Packages

PackageDescriptionDocs
Google Fonts Font library. Most entries are published under the SIL Open Font License and are explicitly safe to embed and redistribute. fonts.google.com
SIL Open Font License FAQ Plain-language explanation of what the OFL does and does not allow. scripts.sil.org/OFL

Tasks

Task 1 - Inspect the Windows font store

  1. Open a terminal (PowerShell or Command Prompt) and list the font files Windows actually stores on disk.
  2. Pick one file from the list, for example arial.ttf, and open its properties from File Explorer's C:\Windows\Fonts view (right-click the font, then Properties, then the Details tab).
  3. Read the copyright and license fields in that Details tab.

Warning: the file copies without any error or prompt, but that is not the same as being licensed for it. Most fonts listed here are licensed for use while running Windows, not for redistribution inside a separate project. Do not copy a font from this folder into a project you plan to submit, share or ship, unless its license explicitly says redistribution is allowed. Treat this task as inspection only.

Success condition: you can name the exact file format (.ttf or .otf) and quote the copyright or license text shown for the font you inspected.

dir C:\Windows\Fonts\*.ttf

Task 2 - Find and download an openly licensed font

  1. Go to Google Fonts (link in the Resources table above) and search for a font suited to your project.
  2. Open the font's page and check the license it is published under. Most entries use the SIL Open Font License, shown directly on the page.
  3. Download the font family as a .zip, then extract it. The extracted folder contains one .ttf file per weight or style, plus an OFL.txt license file.
  4. Open OFL.txt and confirm it permits embedding and redistribution.

Success condition: you have a .ttf file on disk together with its license file, and can state in one sentence what that license permits.

Task 3 - Load the font in SFML and render text

  1. In your project folder, create an assets/fonts folder if one does not already exist, and copy the .ttf file from Task 2 into it.
  2. Add the code below to your project, adjusting the filename to match the font you downloaded.
  3. Build and run.

If loadFromFile returns false, check the path is relative to the working directory your project actually runs from (in Visual Studio this is usually the project folder, not the solution folder) rather than assuming the filename alone is wrong.

Success condition: a window opens showing your text string drawn in the downloaded font, not a blank window or console error.

main.cpp
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Font Check");

    sf::Font font;
    if (!font.loadFromFile("assets/fonts/YourFont-Regular.ttf"))
    {
        return -1;
    }

    sf::Text text;
    text.setFont(font);
    text.setString("Licensed and loaded");
    text.setCharacterSize(36);
    text.setFillColor(sf::Color::White);
    text.setPosition(40.f, 40.f);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color(30, 30, 30));
        window.draw(text);
        window.display();
    }

    return 0;
}