« Go back

Load Fonts and draw Text

In the "Limit the Framerate" chapter we limited the FPS and wrote the current FPS into the shell. But now we want to see this output on our window.

Dgame offers two modules for that: Font and Text. Text manages the internal string representation and uses Font to draw it.

The Font constructor takes two parameters: First: the filename of the *.ttf file and second: the size of the text. If you are experienced in CSS, you should have no problems with it.

The Text constructor takes the Font. Text has several helpful methods. One of them can be seen in the code below: format. This works like writefln.

Show Raw
import std.stdio;

import Dgame.Window.Window;
import Dgame.Window.Event;
import Dgame.Graphic.Text;
import Dgame.System.Font;
import Dgame.System.StopWatch;

void main() {
    Window wnd = Window(640, 480, "Dgame Test");

    StopWatch clock;
    Font fnt = Font("samples/font/arial.ttf", 22);
    Text curFps = new Text(fnt);

    bool running = true;
    Event event;
    while (running) {
        while (wnd.poll(&event)) {
            switch (event.type) {
                case Event.Type.Quit:
                    writeln("Quit Event");
                    running = false;
                default: break;
        curFps.format("Current FPS: %d.", clock.getCurrentFPS());

You now should see something similar like this:

Font-Text output

And if you want another color for your Text? No problem. You can set a foreground and brackground color by changing the values of foreground and background.

« Go back