One header per module
There is no header that includes the whole framework. There are
16 headers under include/rmp/, one for each part of the framework you
can name — the UI, the input, the assets, the audio — and a file includes the ones it uses. That
is what makes each module optional: you take the whole ecosystem, or you take one piece of it.
A file that draws the HUD looks like this:
#include <rmp/ui.h>
#include <string>
void draw_hud(int score, float health) {
rmp::ui::begin({ .placement = rmp::ui::Align::TOP_CENTER });
rmp::ui::row({ .gap = 24 }, [&] {
rmp::ui::text(std::to_string(score));
rmp::ui::progress(health);
});
rmp::ui::end();
}It knows about the interface and nothing else, so it recompiles when the UI changes and not when the asset pipeline does. And when it reaches for something it did not include, the compiler says so, by name:
#include <rmp/ui.h>
void draw_pause_hint() {
rmp::ui::begin();
if (rmp::input::just_pressed("pause")) rmp::ui::text("Paused");
rmp::ui::end();
}snippet.cpp:5:14: error: no member named 'input' in namespace 'rmp'
5 | if (rmp::input::just_pressed("pause")) rmp::ui::text("Paused");
| ~~~~~^That error is the truth: the file is missing rmp/input.h. An umbrella header would
have hidden it, and every file would have paid to compile all of the framework to use one part of
it. An example holds the framework to this:
it includes only rmp/ui.h, and the examples build fails if that stops being
enough.
A header is a module you can name#
Splitting is not free for the person using the framework: every header is one more thing they
have to know exists. So a new header has to be a part of the framework you can name and decide to
use or not — rmp::ui, rmp::audio, rmp::save. Anything else
goes into a header that already exists. Splitting for tidiness only taxes the reader, and the
number of includes a game needs is a number that should go down over time, never up.
Our headers do not include each other#
A header under include/rmp/ declares what it needs from another one instead of
including it, wherever the language allows. Where it does not — a type held by value has to be
complete — the header includes the other one and says at its top why it could not avoid it. Those
are the exceptions, and they are few:
| Header | Includes | Because |
|---|---|---|
rmp/scene.h | rmp/object.h, rmp/tilemap.h | a scene spawns objects, and holds its map by value |
rmp/object.h | rmp/assets.h | a sprite holds its texture by value |
rmp/tilemap.h | rmp/object.h | a map's object factories are callbacks |
rmp/behavior.h | rmp/object.h, rmp/scene.h | the catalogue holds handles and callbacks by value, and a spawner hands you the scene |
If a pair of headers ever needed each other, one of them should not exist: they would be one module pretending to be two.
The
technique that makes most of this possible is moving the call into a .cpp. The
entry-point macros in rmp/app.h have to start the asset layer, sample the input
every frame and close the UI, but naming rmp::assets, rmp::input or
rmp::ui there would make every file with an entry point compile all three. So the macros call a handful of functions declared in rmp/app.h
and defined in src/rmp/app/app.cpp, which is the one file that includes the rest.
The one header every header includes#
Every
public header includes rmp/config.h, the values from the .toml, so
RMP_WINDOW_WIDTH and the rest are simply there wherever you are writing and you
never type that include. It is the exception to the rule above, and it earns it by measurement:
it has one include, rmp/generated/config.h, the #defines the
.toml hands the code, which includes nothing, and parsing the two costs no more than
parsing an empty file. tools/header_check.sh compiles every public header on its
own and uses RMP_WINDOW_WIDTH after it, so a header that forgets it — or that only
compiles when something else was included first — fails the framework's tests rather than a
game's build.
The standard library is allowed; its cost is measured#
The rule is about the framework's own headers. It was never a rule against the standard library
or raylib: <memory>, <vector>,
<string_view> and raylib.h are used where the job needs them. What
the framework does instead is measure what each include costs, and keep the number from creeping
up unnoticed.
tools/header_cost.py preprocesses a file that includes one public header and
nothing else, and counts the lines of code that come out. That count is the same on every run with
the same compiler, so it is what the gate compares, against tools/header_budget.txt,
inside the pinned build image the budget was measured in: a header that grows by more than a fifth
fails, and so does one that shrinks by as much, because a budget left too high would let the next
growth through. A header with no line in the budget fails everywhere. Milliseconds are reported too, because they
are what a person feels, but they are never gated: they depend on the machine.
The measurements are written down where the decisions are made, and some of them decided the API:
rmp::Callbackexists because<functional>measured 131 ms in every file that includesrmp/object.h, which is every file with an object in it, and the part ofstd::functionthe framework needs is small. It turned out to have a second reason, in Ownership.rmp/math.his a header of its own because the C library's<math.h>under raymath is most of its cost, andrmp/ui.hmust not make a file that only draws menus pay for it.