Designated initialisers
Almost everything in the framework that takes options takes a plain struct, and
the struct is written with C++20's designated initialisers: { .speed = 320 }. It is
the closest C++ has to named arguments, and it is why the API reads the way it does. It comes with
one rule the language imposes, and the framework is arranged around that rule so you rarely
meet it.
What it buys#
A call says what each value is, leaves out everything it does not change, and keeps working when a new option is added:
auto &wall = spawn({ .position = { 400, 225 }, .size = { 20, 200 } });
wall.solid = true;
wall.immovable = true;
auto &player = spawn({ .position = { 100, 300 }, .size = { 24, 40 }, .layer = 1 });
player.add<rmp::behavior::TopDown>({ .speed = 220 });The same shape runs through the whole framework: what
spawn() takes
(rmp::ObjectOptions), every container and widget
of rmp::ui, the volume and pitch of a sound, the options of a save, a raycast's query,
and every behavior in the catalogue. Each field has its default written next to it in the header,
so the defaults are visible in the one place you would look.
Declaration order#
The rule: C++20 requires designated initialisers to be written in the order the fields are
declared. Here the struct declares grow_x, width, grow_y,
height:
rmp::ui::begin();
rmp::ui::column({ .width = 240, .grow_y = true }, [] {
rmp::ui::text("Inventory");
});
rmp::ui::end();Written the other way round, it does not compile:
rmp::ui::begin();
rmp::ui::column({ .grow_y = true, .width = 240 }, [] {
rmp::ui::text("Inventory");
});
rmp::ui::end();snippet.cpp:2:44: error: ISO C++ requires field designators to be specified in declaration order; field 'grow_y' will be initialized after field 'width' [-Werror,-Wreorder-init-list]
2 | rmp::ui::column({ .grow_y = true, .width = 240 }, [] {
| ~~~~~~~~~^~~
snippet.cpp:2:29: note: previous initialization for field 'grow_y' is here
2 | rmp::ui::column({ .grow_y = true, .width = 240 }, [] {
| ^~~~GCC and MSVC refuse it on their own. Clang, left to itself, only warns
(-Wreorder-init-list) and initialises the fields in declaration order anyway — and
Clang is what [dev] compiler builds your debug builds with unless you choose another,
while CI builds the Windows x64 release and the NetBSD one with GCC. A game that built clean on the
laptop would fail on those builds twenty minutes into CI.
So
the framework makes Clang refuse it too, in each of the three build systems. The rmp
library carries -Werror=reorder-init-list for Clang and AppleClang as a public option,
which every game, example and test that links it inherits, and Android's native build, which the
NDK's Clang compiles, carries the same flag.
The
iOS app is built by Xcode from a project of its own, and configure.py writes the same
flag into that project's OTHER_CPLUSPLUSFLAGS, ahead of any of the game's own in
[ios.settings], which are kept after it. Your own build gives the answer above, and so does every build in CI that could have
disagreed with it.
A configure that builds the tests checks that this
holds: it reads the options back off the rmp target and compiles one initialiser out
of order and the same one in order with them. The first must fail and the second must compile, or
the configure stops and says which.
Because
the rule cannot be switched off, every options struct in the framework is declared in the order its fields are most likely
to be written, rather than alphabetically or by importance: an image's options start with its
size, a button's with its style, a container's with its gap and padding. In rmp::ui,
an id is the last field of every struct that has one. And the sizing fields of a
container are interleaved by axis, grow_x, width, grow_y, height, rather than grouped
by kind: grouped, the most ordinary layout there is — a fixed-width sidebar that fills the height,
{ .width = 240, .grow_y = true } — would be illegal. This way round every combination
is in order.
Only an aggregate can be written this way#
Designated initialisers work on an aggregate: a struct with public fields and no constructors, no virtual functions and no virtual or private base. Give a struct a virtual function and the syntax stops working, with an error both compilers agree on:
struct Spin {
virtual ~Spin() = default;
float degrees_per_second = 90;
void _update(rmp::Object &self, float delta) {
self.rotation += degrees_per_second * delta;
}
};
Spin fast{ .degrees_per_second = 180 };snippet.cpp:9:6: error: initialization of non-aggregate type 'Spin' with a designated initializer list
9 | Spin fast{ .degrees_per_second = 180 };
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~That is the whole reason a behavior inherits from nothing.
The obvious design — a Behavior base class with a virtual update — would
have turned add<TopDown>({ .speed = 220 }) into a constructor taking an options
struct, and every later read into something like get<TopDown>()->options.speed.
Without the base class, the fields are the configuration, and they stay live:
add<rmp::behavior::TopDown>({ .speed = 220 });
get<rmp::behavior::TopDown>()->speed = 400;A
behavior with a virtual function would compile up to the moment someone wrote its options, so
add() refuses one outright, with a sentence that says why. The hooks a behavior may
have are found at compile time with requires instead of through virtual
functions.
The same reasoning is why the small struct an rmp::Sprite
uses to track its animation is public: a private member would stop the sprite being an
aggregate.
Through a template, the type needs a name#
A braced list on its own has no type, and a function template that forwards its arguments
cannot deduce one for it. So data passed to a scene through
change(), push() or
replace() cannot be a bare braced list:
rmp::Scene::change<GameScene>({ .level = 3, .hard = true });snippet.cpp:1:1: error: no matching function for call to 'change'
1 | rmp::Scene::change<GameScene>({ .level = 3, .hard = true });
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~Give the struct a name and it works, designators and all:
class GameScene : public rmp::Scene {
public:
struct Start {
int level = 1;
bool hard = false;
};
explicit GameScene(Start start) : _start(start) {}
private:
Start _start;
};
void play_level_three() {
rmp::Scene::change<GameScene>(GameScene::Start{ .level = 3, .hard = true });
}That is C++, not a decision of the framework's. spawn() does not have the problem,
because it is not variadic: it takes an rmp::ObjectOptions, and the braced list knows
what to become.