Interface style
A control in rmp::ui says what it means — primary, danger, a
muted line of text — and how important it is — small, medium, large — and the theme decides what
that looks like. Restyling a game is then one theme, and no call site is revisited. When one widget,
or one screen, has to look like nothing else — a shop drawn by an artist, a title with an outline,
a button that grows under the pointer — a style says so for that widget and
everything inside it, over the theme, and the simple calls stay as they are.


rmp::ui::button("Start game", { .style = rmp::ui::Variant::PRIMARY, .size = rmp::ui::Size::LARGE });
rmp::ui::text("v1.0.2", { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });Themes#
A rmp::ui::Theme is plain data: every colour the
widgets are drawn with, as raylib's Color, and every metric, in design units. There is no
inheritance, no cascade and no stylesheet. Customising one is assignment — copy it, change what you
want, set it back:
rmp::ui::Theme theme = rmp::ui::current_theme();
theme.primary = GOLD;
theme.primary_hover = YELLOW;
theme.primary_press = ORANGE;
theme.corner_radius = 0; // square corners
rmp::ui::set_theme(theme);current_theme() returns the one theme in
use, and set_theme() copies a new one over it.
Anything drawn after the call uses the new one, in the same frame. Both work at any time, before the
window exists too.
Two themes come with the framework, theme_dark()
and theme_light(). Each returns a copy, so the same
copy-modify-set works on top of either. Which one the game starts with is [ui] theme:
[ui]
theme = "light"Only those two names exist, and a typo is refused when the configuration is read instead of falling back to dark at runtime:
[ui]
theme = "blue" raylib_multiplatform.toml:2: theme = "blue"
^^^^^^^^^^^^^^
configure: error: [ui] theme has to be one of dark, light, got 'blue'.
It only picks which one the app starts with — rmp::ui::set_theme() changes it at runtime.After that it is a runtime call, so an appearance setting in the game is one line:
if (rmp::ui::checkbox("Light theme", light))
rmp::ui::set_theme(light ? rmp::ui::theme_light() : rmp::ui::theme_dark());The dark theme is a default-constructed Theme; its values are the member initialisers
in include/rmp/ui.h. The light one is not the dark one with the numbers flipped. A dark
interface separates its surfaces with their own shadows and a light one has none, so the light theme
sets border_width to 1, and normal buttons
and panels get an outline that keeps a pale button on a pale page a button. Its accents are darker,
because the blue that reads as bright on near-black looks washed out on near-white, and white text on
it stops being legible.
Colours#
Each colour of the theme has a job, and these are all of them:
| Field | Paints |
|---|---|
background | Behind the interface. No widget paints it: a game of scenes is cleared with it every frame unless the scene sets its own background; a loop of your own passes it to ClearBackground(). |
panel | A panel, and a dropdown's open list. |
surface | A normal button at rest, the field of a dropdown and of a text field, a slider's rail, an unticked checkbox, the track of a progress bar. |
surface_hover, surface_press | A normal, outline or ghost button under the pointer, and while the pointer is held down on it; also a checkbox's row, a dropdown's field and the items of its open list. |
border | An outline button, an unticked checkbox, a dropdown's open list; and normal buttons and panels when border_width is above 0. |
text | Text, the labels of normal, outline and ghost buttons and of every control. |
text_muted | Muted text, a slider's percentage, a text field's placeholder. |
text_on_accent | The label of a primary or danger button, and the dot of a ticked checkbox. |
primary, primary_hover, primary_press | A primary button in its three states; also primary text, a ticked checkbox, the filled part of a slider and of a progress bar. |
danger, danger_hover, danger_press | A danger button in its three states; also danger text. |
disabled, disabled_text | The fill and the label of a button or control whose enabled is false: the fill is a button's, a checkbox's box, a slider's filled part and a text field's field. A disabled dropdown keeps surface for its field and greys only its label and value. |
focus | The ring around whatever the keyboard or gamepad is on. A controller build cannot be used without it, which is why it is the theme's and not each widget's. |
Text picks one of four of these through a ColorRole,
never a colour of its own: TEXT is text, MUTED is
text_muted, PRIMARY is primary and DANGER is
danger.
Variants#
A button's style is a Variant: what
the button is for, not what colour it is.
| Variant | For | Looks |
|---|---|---|
NORMAL | Most buttons; the default. | A filled surface. |
PRIMARY | The one thing you want pressed on this screen. | primary, with text_on_accent. |
DANGER | Something destructive, which should look it. | danger, with text_on_accent. |
OUTLINE | A secondary action. | An outline and a label; it fills in under the pointer. |
GHOST | Toolbars, "back" links. | The label alone, in either theme, until the pointer is on it. |
rmp::ui::row({ .grow_x = true }, [&] {
rmp::ui::button("Start game", { .style = rmp::ui::Variant::PRIMARY });
rmp::ui::button("Load");
rmp::ui::button("Settings", { .style = rmp::ui::Variant::OUTLINE });
rmp::ui::button("Back", { .style = rmp::ui::Variant::GHOST });
rmp::ui::button("Delete", { .style = rmp::ui::Variant::DANGER });
});Being disabled is not a sixth variant, because it can happen to any of them: it is the
enabled flag, and a disabled button of any variant is drawn in disabled and
disabled_text.
Sizes#
Anywhere a size is asked for — a button's, a text's — the same field takes one of three steps of the theme's type scale, or a plain number of design units:
rmp::ui::button("Play", { .size = rmp::ui::Size::LARGE });
rmp::ui::text("Chapter One", { .size = 34 });| Step | Theme field | Default |
|---|---|---|
Size::SMALL | font_size_small | 0.8 × [ui] font_size |
Size::MEDIUM | font_size | [ui] font_size, 20 unless set |
Size::LARGE | font_size_large | 1.4 × [ui] font_size |
The steps are what to reach for almost always: they come from the theme, so they stay in proportion when someone changes the type scale. A number is the escape hatch — for a title, not for a button. It is one field that takes two spellings rather than two fields that could disagree.
On a button the step moves the type, the padding and the minimum height together, so a large
button is large all over and not a normal one with bigger letters. The one exception is a touch
screen: on Android and iOS no button is shorter than min_touch_size, small ones
included. The labels of checkboxes, sliders, dropdowns and text fields are always at
font_size.
The three type sizes are three fields of the theme. Changing font_size moves the
medium step only, so making all the text bigger is all three:
rmp::ui::Theme theme = rmp::ui::current_theme();
theme.font_size *= 1.15f;
theme.font_size_small *= 1.15f;
theme.font_size_large *= 1.15f;
rmp::ui::set_theme(theme);Metrics#
The rest of the theme is measurements, every one in design units:
| Field | Default | Is |
|---|---|---|
padding_x | 20 | Inside a button, left and right of the label. |
padding_y | 12 | Inside a button, above and below it. |
gap | 12 | Between siblings. |
panel_padding | 20 | Inside a panel, and around what begin() lays out. |
corner_radius | 8 | Of buttons, panels and the controls' fields; 0 is square. |
border_width | 0 dark, 1 light | The outline of normal buttons and of panels; 0 draws none. |
control_size | 22 | A checkbox's box, and the height of a slider's drag area. |
track_thickness | 6 | A slider's rail. |
focus_ring | 2 | The outline on the focused control. |
min_touch_size | 44 | The shortest a button can be: Apple's touch-target guidance, close to Material's 48 dp. It follows a button's type size, except on Android and iOS. |
transition | 0.12 | Seconds, not design units: how long a colour takes to change. See below. |
Transitions#
Controls fade between their colours — at rest, under the pointer, pressed — instead of snapping,
and a checkbox's box fills in instead of switching. There is nothing to opt into and one knob,
transition, the time a change takes. A control seen for the first time starts at the
colour it should have, so nothing fades in from nowhere.
A transition moves only colour. Nothing about the layout is animated, so a
control is always exactly where it was drawn and an animation can never make the player miss what
they aimed at. A style's motion — growing under the pointer, a panel sliding
in — moves the drawing and never the layout either, and is off unless a game asks for it.
transition = 0 turns it off — every control takes its new colour on the frame it
changes — which is what a "reduce motion" setting does:
rmp::ui::Theme theme = rmp::ui::current_theme();
theme.transition = reduce_motion ? 0.0f : 0.12f;
rmp::ui::set_theme(theme);Breakpoints and scale#
Two mechanisms make an interface fit a screen, and knowing which to reach for is most of the skill. The scale makes everything bigger or smaller together; it is automatic, and you rarely touch it. A breakpoint changes the shape of a layout, which no amount of growing, fitting or fixing can do: on a phone held upright, a sidebar beside the content has to go above it.
if (rmp::ui::compact()) {
rmp::ui::column([&] { sidebar(); content(); });
} else {
rmp::ui::row([&] { sidebar(); content(); });
}current_breakpoint() classifies the
screen by its aspect ratio, width over height, and
compact() is the case that comes up:
| Breakpoint | Width ÷ height | In practice |
|---|---|---|
COMPACT | below 1 | A phone held upright, a narrow window. |
MEDIUM | 1 up to 1.6 | A tablet, a small desktop window, 4:3. |
EXPANDED | 1.6 and wider | An ordinary desktop, a TV, a phone on its side. |
Aspect ratio and not pixels, deliberately: a pixel threshold would call a phone 1080 pixels wide
a desktop, and the scale has already dealt with how big everything is. What is left — the only thing
that decides whether a row still fits — is how wide the screen is next to how tall. It reads the
window's size when it is called, so it needs no begin() first; a window with no height
yet is MEDIUM.
More
exactly, it is the shape of the game's area. Under
[window] stretch
"letterbox" — the default of a game rmp new makes — that is the design
inside its bars, the same shape whatever the window does, and the breakpoint never changes. An
interface that should change shape with the window wants "none" or
"expand", which give it the whole window; the responsive example sets
NONE for that reason.
Reach for a breakpoint when the layout has to become a different layout. Using one to pick a size undoes the work the scale already did, and is how a layout ends up right on exactly one machine. The responsive example turns a sidebar and its content from a row into a column as the window is resized.
Styles#
A rmp::ui::Style is how one thing looks: a
button, a panel, a line of text. Every field is optional, and a field left unset is taken from around
it. A widget you keep has one in
its style field:
const rmp::ui::Style gold{
.background = GOLD,
.radius = 0,
.shadow = rmp::ui::Shadow{},
.text = BLACK,
.text_outline = rmp::ui::TextOutline{ .color = WHITE },
};
rmp::ui::Button buy{ .text = "Buy", .style = gold };The fields are written in the order they are declared — C++20 requires it of designated initialisers — and they are declared in the order you would think of them: the box, its spacing, its text, and how it feels.
| Field | Type | Is |
|---|---|---|
background | Color | The fill of the box. On a button, a checkbox, a dropdown or a field, the hover and press colours are worked out from it — a little lighter, a little darker — unless the next two give them. |
background_hover | Color | The fill under the pointer. Given without a background, it is used alone over the theme's fill. |
background_press | Color | The fill while held down; alone too, like the one above. |
background_disabled | Color | The fill when enabled is false. |
skin | Skin | Pictures instead of the fill, one per state. See skins. |
tint | Color | Multiplies the colours of a skin or an image; WHITE leaves them as drawn. |
border | Color | The outline's colour, drawn when border_width is above 0. |
border_width | float | The outline's width in design units; 0 draws none. |
radius | float | The corners, in design units; 0 is square. |
shadow | Shadow | A soft shadow under the box. See shadows and outlines. |
padding_x | float | Inside, left and right, in design units. On a panel it is its padding all round. |
padding_y | float | Inside, above and below. |
gap | float | Between the children of a container. |
text | Color | The colour of its text, over the role a call gives it. |
text_disabled | Color | The same, when enabled is false. |
font | rmp::Font | A font you loaded. The interface draws with that font's file; see fonts. An empty one is inherited. |
font_size | float | What Size::MEDIUM is here, in design units. SMALL and LARGE follow it, in proportion. |
text_outline | TextOutline | An outline around every letter. |
text_shadow | TextShadow | A copy of the text under it, offset. |
motion | Motion | How it moves under the pointer, as it appears and when it shakes. See motion. |
sounds | Sounds | A sound per event. See sounds. |
Every field but font and sounds is a std::optional, and
"unset" is what it says: not 0, not black. font is unset when it is an empty handle, and
each sound of sounds is an optional of its own.
Where a style comes from#
A field a widget's own style leaves unset is looked for, in this order, and the first that sets it wins:
- the widget's own
style; - the
childrenstyle of thePanels it is inside, the nearest first; - the theme's style for its kind of widget,
Theme::styles; - the theme's own colours and metrics, as the rest of this page describes them.
Inside a widget, current_theme() answers
with the theme as that style changes it, so every widget reads its style the way it already read
the theme, and a style can do nothing the theme could not do on its own but for one widget.
One style for every widget of a kind#
Theme::styles holds one
Style for each kind of widget. What is set there is how every widget of that kind looks
before its own style — the ones written the simple way, rmp::ui::button("Play"),
included. A skinned game is then one theme, and its menus stay three lines:
rmp::ui::Theme theme = rmp::ui::current_theme();
theme.styles.button.skin = rmp::ui::Skin{ .normal = frame };
theme.styles.button.text = WHITE;
theme.styles.panel.shadow = rmp::ui::Shadow{};
rmp::ui::set_theme(theme);Field of styles | Under |
|---|---|
button | button(), image_button(), Button |
label | text(), Label |
image | image(), Image |
panel | panel(), Panel |
checkbox | checkbox(), Checkbox |
slider | slider(), Slider |
dropdown | dropdown(), Dropdown |
text_input | text_input(), TextInput |
progress | progress(), ProgressBar |
Two of them are not empty in theme_dark() and theme_light(), and they
are the framework's own look. styles.button grows a button 3 % under the pointer, gives
it 3 % while it is held, and sits it on a soft shadow; styles.panel puts a panel on a
deeper shadow and fades it in, rising 10 design units, over 0.18 seconds the first frame it is drawn
— an overlay's panel included, so a pause menu arrives instead of appearing. The light theme's
shadows are the same, fainter. There is never a text shadow by default. The other seven are empty,
and so are the theme's sounds and
motion: silent.
rmp::ui::Theme theme = rmp::ui::theme_dark();
theme.styles.button.shadow.reset(); // buttons with no shadow, still growing
theme.styles.panel = {}; // panels as flat and still as text
rmp::ui::set_theme(theme);A Style{} is empty, so theme.styles.panel = {} takes the whole look of
panels away. theme.styles = {} is not that: it is a new Styles, which has
the framework's look in it again.
A look for everything inside a container#
A kept Panel has two styles, the way a Godot
container has its own theme override and one for its children:
style is how the panel itself looks, and
children is a style every widget inside it
inherits, however deep. Panels nest, and the nearest one's wins:
rmp::ui::Panel shop{
.style = { .background = DARKBROWN, .shadow = rmp::ui::Shadow{} },
.children = { .text = GOLD, .font = title },
};
shop.show([] {
rmp::ui::text("Shop");
rmp::ui::button("Buy");
});With background = false a panel is a bare
row or column — no fill of its own unless its style gives one, no padding unless asked — that still
styles its children: a way to give one part of a screen a font or a colour without drawing a box.
A number that is no number#
A length, a time or a scale that is not a finite number in its range — a NaN from a division by zero, a radius of minus four — is drawn the way no style would draw it, and said once: UI: a Style's … of … is not from … to …; … is used. A negative length is 0 without a word: a border of -4 is no border.
Pictures#
Everything in the interface that shows an image — image(), an icon on a button, a
skin behind a button or a panel, the fill of a bar — takes an
rmp::ui::Picture, and a picture is made from whatever
you have, without naming it:
| From | Is |
|---|---|
an rmp::Texture | The whole texture. |
{ texture, Rectangle{ … } } | A part of it: an atlas, a sheet laid out by hand. |
an rmp::Sprite | What the sprite shows now: its texture, or its sheet's current frame. A snapshot: make one each frame to see it move, or keep an Image, which plays it. |
a NineSlice | A picture that stretches by its middle. See below. |
slice(sheet, name) | A slice of an Aseprite file, by its name. |
rmp::ui::image(logo);
rmp::ui::image({ atlas, Rectangle{ 32, 0, 16, 16 } });
rmp::ui::image(hero);A picture holds counted handles to what it shows, so the texture stays loaded for as long as a
picture that shows it exists — through the end of the frame that draws it — and a temporary is
safe. empty() is true when there is nothing to
draw: no texture, or one that did not load; an empty picture draws nothing and takes no room.
size() is its size in pixels of the source, and
sliced() is true when it has nine-slice
margins. Its fields are public — texture,
sheet,
source, the margins
left,
top,
right,
bottom and
slice_scale — but one of the
constructors normally sets them.
Nine-slices#
A NineSlice is the classic "9-patch": four
margins cut the picture into a three-by-three grid, and it is drawn stretched to any box with its
corners at their own size, its edges stretched along one axis and its middle along both. One small
frame becomes a button of any length:
const rmp::ui::NineSlice frame{ .texture = ui_art,
.source = { 0, 0, 24, 24 },
.left = 8,
.top = 8,
.right = 8,
.bottom = 8,
.scale = 3 };
rmp::ui::Button play{ .text = "Play", .style = { .skin = rmp::ui::Skin{ .normal = frame } } };| Field | Default | Is |
|---|---|---|
texture | empty | The picture. |
sheet | empty | Or an Aseprite sheet's texture, when texture is empty. |
source | all of it | The part of the texture to cut, in pixels. |
left, top, right, bottom | 0 | The margins, in pixels of the source. |
scale | 0, which is 1 | How big one source pixel is drawn, in design units, so the corners grow with the interface's scale like everything else. Pixel art wants a whole number. |
A box smaller than its corners shrinks the corners together rather than letting them overlap. All four margins 0 is a picture that simply stretches.
Slices of an Aseprite file#
An artist marks the parts of an .aseprite with the slice tool and names them: an
icon of an atlas, the frame of a button. slice(sheet, name)
is the part of the first frame the slice covers, as a picture, and one with "9-Slices" ticked in
Aseprite comes with its margins, so it is already a nine-slice.
nine_slice(sheet, name) is the same slice as a
NineSlice, to change its scale or keep it:
static const rmp::SpriteSheet icons = rmp::assets::load_sheet("skin.aseprite");
rmp::ui::image(rmp::ui::slice(icons, "coin"));
rmp::ui::NineSlice frame = rmp::ui::nine_slice(icons, "button");
frame.scale = 3;A name the sheet does not have is an empty picture, and said once:
UI: rmp::ui::…(sheet, "…"): the sheet has no slice by that name. A slice without
9-slices asked for as one stretches whole, and says
UI: rmp::ui::nine_slice(sheet, "…"): the slice has no 9-slices; it stretches whole. The
slices themselves are in the sheet's SheetData::slices;
sprites says what is read from the file.
An image you can press#
rmp::ui::image_button is a button that is a
picture: an icon, an inventory slot, a "Play" drawn by the artist. It is pressed and focused like
button(), and wears theme.styles.button — its skin behind the picture, its
sounds:
if (rmp::ui::image_button(gear)) open_settings();
if (rmp::ui::image_button(gear, { .width = 64, .height = 64, .id = "settings" })) open_settings();ImageButtonOptions:
width and
height in design units, 0 for
the picture's own size;
enabled; and
id, since a picture has no label to be
told apart by — without one, unnamed image buttons are told apart by their order. It has no padding
around it and no minimum height, except on Android and iOS, where it is still as tall as a thumb needs.
Skins#
A Skin is pictures instead of a flat fill, one per
state. Only normal is needed: a state without
its own picture shows normal, changed the way the colours of that state would be —
lit under the pointer, darker while held, grey and faded when disabled. Give the others when the
artist drew them:
const rmp::ui::Style wooden{ .skin = rmp::ui::Skin{ .normal = up, .press = down } };| Field | Shown | When it is missing |
|---|---|---|
normal | at rest | no skin at all: the theme's fill |
hover | under the pointer | normal, drawn a second time on top, faintly, to light it |
press | held down | normal, darker |
disabled | enabled = false | normal, grey and a little transparent |
focus | with the keyboard or gamepad focus | normal; the focus ring is drawn either way |
The picture is the box: a skinned button has no flat fill over it or under it, and its
style's tint multiplies it. On an image, the skin is a frame behind the picture, as far
in as the style's padding.
Shadows and outlines#
A Shadow under a box is drawn as a few rounded
rectangles, each fainter than the last, outwards. That reads as blurred on every target — the
software renderer and OpenGL ES 2.0 included — and costs a handful of rectangles:
const rmp::ui::Style card{ .shadow = rmp::ui::Shadow{ .offset = { 0, 6 }, .blur = 12 } };| Field | Default | Is |
|---|---|---|
offset | { 0, 4 } | Where it falls, in design units: down a little, as light from above. |
blur | 8 | How far it fades out past the box; 0 is one hard shadow. |
spread | 0 | How much bigger than the box it is before it fades. |
color | black at 90 of 255 | Its colour at the box. |
Text has two of its own. A TextOutline draws
copies of the text around it, so a title reads over any background —
width in design units (1 by default; keep
it from 1 to 3, every copy is drawn), color
black. A TextShadow is one copy under it —
offset { 1, 2 } design units,
color black at 160 of 255. A box's own
outline is border and border_width.
rmp::ui::Label title{ .text = "Game over",
.size = rmp::ui::Size::LARGE,
.style = { .text = GOLD,
.text_outline = rmp::ui::TextOutline{ .width = 2 },
.text_shadow = rmp::ui::TextShadow{} } };Motion#
A Motion is movement. The framework's themes have
a little of it, in their styles: buttons grow under the pointer and give when
pressed, and panels fade and rise in. Anything more is a choice of style, and a game that wants it
says so. Nothing here moves the layout. A control is drawn bigger, lifted or
sliding, with everything inside it, but it is laid out — and clicked — where it always was.
rmp::ui::Theme theme = rmp::ui::current_theme();
theme.styles.button.motion = rmp::ui::Motion{ .hover_scale = 1.05f, .press_scale = 0.95f };
theme.styles.panel.motion = rmp::ui::Motion{ .appear = 0.25f, .appear_offset = 24 };
rmp::ui::set_theme(theme);| Field | Default | Is |
|---|---|---|
hover_scale | 1 | How big under the pointer: 1.05 is 5 % bigger. |
press_scale | 1 | How big while held: 0.95 is 5 % smaller. |
hover_lift | 0 | How far it rises under the pointer, in design units, its shadow falling further below it: a card lifting off the table. |
speed | 0.12 | Seconds hover and press take to get there. |
appear | 0 | Seconds a box takes to fade in the first frame it is drawn; 0, it is simply there. Panels and overlays are what it is for. |
appear_offset | 0 | How far below it starts as it appears, in design units, sliding up. |
appear_scale | 1 | How big it starts as it appears, growing to 1. |
shake | 0.4 | Seconds a shake() lasts. |
shake_strength | 8 | How far side to side it shakes, in design units, dying away. |
A shake is a kept widget's shake() — "that did not work": a wrong password, a coin
you do not have. It needs no Motion to be set: the defaults above shake. Hover and press
follow the same clock as the scenes, so a script that plays the game sees the same frames every
run.
Sounds#
A Sounds names a sound for each thing a control
does, by the name rmp::audio::play() takes —
"click" is click.wav, click.ogg… in resources/. In
the theme, every control makes them; in a style, one widget does, or everything inside a
panel:
rmp::ui::Theme theme = rmp::ui::current_theme();
theme.sounds.hover = "hover";
theme.sounds.click = "click";
theme.sounds.volume = 0.6f;
rmp::ui::set_theme(theme);
rmp::ui::Button quiet{ .text = "Back", .style = { .sounds = { .hover = "" } } };| Field | Plays when |
|---|---|
hover | the pointer arrives on a control |
press | the pointer goes down on it |
release | …and comes up |
click | it is pressed: the pointer, Enter, the gamepad's A button |
focus | the keyboard or gamepad focus arrives |
change | a checkbox, slider, dropdown or text field changes its value |
open | a dropdown opens its list |
close | …and closes it |
shake | a widget's shake() starts |
volume | not a sound: 0 to 1, for all of them; 1 when nothing sets it |
Each one is inherited on its own, like every field of a style, so an empty name is
silence: .hover = "" turns off a hover sound the theme or a panel set. Unset is
"whatever is around it"; empty is "nothing".
A name is a SoundName, written as a string — a
literal, a std::string or a std::string_view, copied — with
view() to read it,
empty() for silence and == to
compare it with a string. It is a type of its own for one reason: writing one is what brings
rmp::audio's player and its decoders into the game. A game whose interface names no sound
links none of them.
Every sound goes through rmp::audio, so it follows the
mixer's sound-effect volume and master volume, and a sound
that is missing says so once, as any play() does. Anything else that should happen on an
event — a vibration, a particle — is
on_event().
Fonts#
The interface draws its text in one global font unless a style gives another. The global font
starts as [ui] font, at sizes measured from [ui] font_size; that is only
where the game starts, and it can change at any time.
Text is drawn at the pixel size the layout asks for: its size in design units — a step or a number — times the scale.
Left empty, the default, the font is raylib's built-in one: no file, no licence, and it works on every platform. It is a bitmap 10 pixels tall, stretched to whatever size the text asks for.
A font of your own is a file in resources/, named relative to it:
[ui]
font = "ui.ttf"
font_size = 24font_size is the medium step in design units, a whole number from 6 to 200; the small
and large steps start at 0.8 and 1.4 times it.
Changing it while the game runs#
set_font() changes the global font: by its name in
resources/, or from a font the game loaded — the interface takes that font's file and
bakes it itself. "", an empty font or one that failed to load is raylib's built-in one.
font_name() says which it is now. A language with
other letters, an accessibility setting for a plainer face:
rmp::ui::set_font(dyslexia_friendly ? "plain.ttf" : "ui.ttf");A font for one widget#
Style::font gives one widget, or everything in a
panel, a font of its own: one the game loaded with
rmp::assets::load_font. The size it was loaded
at does not matter. The interface draws with that font's file, baked by itself at every
size it draws, so a title at 48 and a caption at 14 in the same font are both sharp:
static const rmp::Font title = rmp::assets::load_font("title.ttf", 32);
rmp::ui::Label heading{ .text = "Chapter one",
.size = rmp::ui::Size::LARGE,
.style = { .font = title } };Every letter, on demand#
A font file is a set of
outlines, and drawing it means baking the letters it needs into a texture first. The interface bakes
a letter the first time a text uses it — Spanish, Polish, Russian, Japanese, whatever the file has —
and then bakes the face again with it, before the frame is drawn. Latin-1 and Latin Extended-A are
there from the start. A game that draws more than 8192 different letters with one font draws the ones
past that as ?, and says so once:
UI: more than … different letters drawn with a [ui] font; the ones past that are drawn as '?'.
It is loaded through
rmp::assets, so it works the same loose in resources/ and packed in a release.
A font file is only sharp at the size it was rasterised at, so the interface rasterises it once for
every pixel size it draws — SMALL, MEDIUM, LARGE and every
number, each times the scale, for every font in use — and keeps each of those faces until the
interface shuts down. Text is then drawn from a face made at exactly its size, so it stays sharp at
any scale. Sixteen faces are kept at once; one more makes room by letting go of the face used
longest ago, never of one drawn in the same frame, whose glyphs may still be waiting to be drawn. A
frame that draws more than sixteen sizes draws the extra ones from the nearest face of the same font,
stretched.
A font file that is missing or cannot be read does not switch the interface off: it falls back to the built-in font and says so once, UI: font '…' could not be loaded; using the built-in font.