# 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().

Source: https://omardev29.github.io/rmp-docs/examples/ui/12_font.html

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.

## Run it

From a clone of the framework:

```shell
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.

| Action | Keys |
|---|---|
| `ui_cancel` | `Esc`, `gamepad right face right` |

## The code

Every file of `examples/ui/12_font`, as it is at the commit this site documents ([on GitHub](https://github.com/omardev29/raylib_multiplatform/tree/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/12_font)).

**`src/main.cpp` 29 lines**

[src/main.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/12_font/src/main.cpp)

```cpp
// ---------------------------------------------------------------------------
// 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**

[src/scenes/credits.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/12_font/src/scenes/credits.cpp)

```cpp
// 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**

[src/scenes/options.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/12_font/src/scenes/options.cpp)

```cpp
// 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**

[src/scenes/scenes.h](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/12_font/src/scenes/scenes.h)

```cpp
#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**

[src/scenes/title.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/12_font/src/scenes/title.cpp)

```cpp
// 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();
}
```
