On this page
  1. Run it
  2. Keys
  3. The code

Font

A menu in a font of your own: a title, its options and its credits, one scene each, every word in Hack Bold through one rmp::ui::set_font(). The font is baked again at every size it is drawn at, so a 48 title is as sharp as a 20 button.

Font, as it starts

Run it

From a clone of the framework:

rmp example 12_font

Keys

Read from the code below: the actions it defines with rmp::input::action, the ones that come defined that it reads by name, and the keys it asks raylib about directly.

ActionKeys
ui_cancelEsc, gamepad right face right

The code

Every file of examples/ui/12_font, as it is at the commit this site documents (on GitHub).

src/main.cpp 29 lines
// ---------------------------------------------------------------------------
// examples/ui/12_font/src/main.cpp
//
// A menu in a font of your own: a title screen, its options and its credits,
// one scene each, every word in Hack Bold.
//
// One call puts it on everything rmp::ui draws, from the title down to a
// dropdown's items:
//
//     rmp::ui::set_font(rmp::assets::load_font("fonts/hack-bold.ttf", 20));
//
// The size you load it at does not limit the sizes it is drawn at. The UI
// keeps the font's FILE and bakes it again at each size it needs, so the
// title at 48 is as sharp as the buttons at 20 -- and the letters a language
// needs come when they are first drawn, not before.
//
// [ui] font in the .toml does the same for a whole game, from the first
// frame. set_font() is for when it changes: a setting, a language, a scene of
// its own. The examples share one .toml, so this one calls it.
//
// One scene per screen under src/scenes/, change<>() between them.
// Run it: `rmp example 12_font`.
// ---------------------------------------------------------------------------

#include <rmp/app.h>

#include "scenes/scenes.h"

RMP_GAME(TitleScene);
src/scenes/credits.cpp 25 lines
// The credits, which is where a font's author goes.
#include "scenes.h"

#include <rmp/input.h>
#include <rmp/ui.h>

void CreditsScene::_update(float) {
    if (rmp::input::just_pressed("ui_cancel")) change<TitleScene>();
}

void CreditsScene::_draw() {
    rmp::ui::begin();
    rmp::ui::panel({ .box = { .width = 640 } }, [] {
        rmp::ui::text("CREDITS", { .size = rmp::ui::Size::LARGE });
        rmp::ui::text("Font: Hack Bold, by Source Foundry");
        rmp::ui::text(
            "MIT licence; derived from DejaVu and Bitstream Vera",
            { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });
        rmp::ui::text(
            "Made with raylib and rmp",
            { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });
        if (rmp::ui::button("Back")) change<TitleScene>();
    });
    rmp::ui::end();
}
src/scenes/options.cpp 29 lines
// The options: every kind of control, and every one of them in the font.
#include "scenes.h"

#include <rmp/input.h>
#include <rmp/ui.h>

#include <array>
#include <string_view>

namespace {
constexpr std::array<std::string_view, 3> DIFFICULTY{ "Relaxed", "Normal", "Wide awake" };
} // namespace

void OptionsScene::_update(float) {
    // Escape, or the gamepad's B: back to the title.
    if (rmp::input::just_pressed("ui_cancel")) change<TitleScene>();
}

void OptionsScene::_draw() {
    rmp::ui::begin();
    rmp::ui::panel({ .box = { .width = 420 } }, [] {
        rmp::ui::text("OPTIONS", { .size = rmp::ui::Size::LARGE });
        rmp::ui::checkbox("Show hints", options.hints);
        rmp::ui::slider("Text speed", options.text_speed, 0.0f, 1.0f, { .step = 0.1f });
        rmp::ui::dropdown("Difficulty", options.difficulty, DIFFICULTY);
        if (rmp::ui::button("Back")) change<TitleScene>();
    });
    rmp::ui::end();
}
src/scenes/scenes.h 28 lines
#pragma once
#include <rmp/scene.h>

// What the options change, and what outlives the screen that changed it.
struct Options {
    bool hints = true;
    float text_speed = 0.5f;
    int difficulty = 1;
};
inline Options options;

class TitleScene : public rmp::Scene {
public:
    void _ready() override;
    void _draw() override;
};

class OptionsScene : public rmp::Scene {
public:
    void _update(float delta) override;
    void _draw() override;
};

class CreditsScene : public rmp::Scene {
public:
    void _update(float delta) override;
    void _draw() override;
};
src/scenes/title.cpp 34 lines
// The title screen, and where the font is set.
#include "scenes.h"

#include <rmp/app.h>
#include <rmp/assets.h>
#include <rmp/ui.h>

void TitleScene::_ready() {
    // Every scene after this one draws in it too: the font is the UI's, not
    // the scene's. Setting it again when the title comes back costs nothing --
    // the asset is loaded once, by name.
    rmp::ui::set_font(rmp::assets::load_font("fonts/hack-bold.ttf", 20));
}

void TitleScene::_draw() {
    rmp::ui::begin();

    rmp::ui::text("NIGHT SHIFT", { .size = 48 });
    rmp::ui::text("a game about staying awake",
                  { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });
    rmp::ui::spacer(12);

    if (rmp::ui::button("New game", { .style = rmp::ui::Variant::PRIMARY })) {
        rmp::ui::notify("No game yet: this is a menu");
    }
    if (rmp::ui::button("Options")) change<OptionsScene>();
    if (rmp::ui::button("Credits")) change<CreditsScene>();
#if !defined(PLATFORM_IOS)
    // Inert on iPhone: Apple rejects apps that terminate themselves.
    if (rmp::ui::button("Quit", { .style = rmp::ui::Variant::GHOST })) rmp::app::quit();
#endif

    rmp::ui::end();
}