Macros
The framework is built on raylib, which is C, and it does not pretend otherwise:
raylib's macros — RAYWHITE, CLITERAL, DEG2RAD — are used
as they are, in the framework and in your game. The framework adds macros of its own only where a
macro is the one tool that can do the job, and keeps one rule about them: no header makes you use
one, except for the line that starts your game.
The entry point#
The clearest case. Depending on the platform, the entry point of a program is one of three
different things: a main() that runs a loop, a main() that hands one
frame function to the browser and returns, or three extern "C" functions that iOS
calls. No function and no template can define main() on one platform and three other
functions on another; the preprocessor can. So the entry point is a macro:
RMP_GAME(MainMenuScene);
RMP_GAME writes the three hooks for the scene stack and
hands them to RMP_ENTRY_POINT, which picks the shape
for the platform being compiled. A game that wants its own hooks calls
RMP_ENTRY_POINT directly, as One frame
shows. The bodies are written once, in rmp/app.h, and every platform-specific thing
they do is a call to a function compiled in src/rmp/app/app.cpp: the macro is only the
shape of the entry point.
One more macro rides along
with them, RMP_DECLARE_ENTRY_POINT_ONCE,
and its only job is to make two mistakes read like sentences. Each entry point defines a function
called rmp_entry_point_is_declared_exactly_once, and the framework refers to it. A
program with two entry points gets a duplicate definition of that name from the linker; a program
with none is told that name is missing — instead of a message about main that says
nothing to someone who never wrote one.
The
reference is the constructor of an object at namespace scope in src/rmp/app/app.cpp,
because a static initializer is the one kind of code every linker keeps. A reference from an
ordinary function would not do: in a program with no entry point nothing calls that function, and
a linker that discards unused sections throws it away, unresolved reference and all, before it
reports anything. At run time the constructor calls the empty function once.
The values from the .toml#
Every value of
raylib_multiplatform.toml a game can read in code is a macro of
rmp/config.h, through the generated file it includes: RMP_WINDOW_WIDTH, RMP_WINDOW_TITLE,
RMP_MAX_DELTA and the rest. They are macros and not constants for two reasons. A game
in plain C uses them too, and C has no namespace to keep a constant in:
#include <rmp/config.h>
int main(void) {
InitWindow(RMP_WINDOW_WIDTH, RMP_WINDOW_HEIGHT, RMP_WINDOW_TITLE);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
}
CloseWindow();
return 0;
}And the preprocessor can test a macro, so code that only makes sense with a setting can be left out of the build when the setting is off. clang-tidy's suggestion to turn macros into an enum is switched off for exactly these.
Said once#
Inside the framework,
one macro carries every diagnostic a game can trigger every frame — an action nobody defined, a
tag that is not in the sprite sheet, a pop() with nothing underneath. It is
RMP_REPORT_ONCE, in src/rmp/internal.h, and it is a macro because every place it is
written needs a variable of its own, and only a macro can declare one there for you.
#define RMP_REPORT_ONCE(...) \
do { \
static const char rmp_report_once_site = 0; \
::rmp::detail::report_once(&rmp_report_once_site, __VA_ARGS__); \
} while (0)Each place the macro is written gets its own static variable, and that variable's address is
the key. The report function keeps the keys it has seen and prints a line only for a new one, so a
mistake in a game's _update is one line in the log and not sixty a second. A second
form takes a key as well, so a missing action called "jump" and a missing action called "fire" are
two lines. Under [dev] strict
the first report aborts a debug build. It is the framework's own tool and not part of the API:
No exceptions says what it reports and what it
deliberately does not.
Its
message is a printf format, because it ends in raylib's TraceLog(), and
GCC and Clang check that format against the values like printf's own — as an error,
not a warning. A %s handed a number would print rubbish, and only on the one frame the
mistake happens, which in a diagnostic is the frame nobody is watching; it does not compile
instead.
Set by the build#
Two
more macros come from the build system rather than from the .toml.
RMP_PRODUCTION_BUILD is 1 in a release build and 0 otherwise, so a game can keep debugging aids
out of what it ships:
#if !RMP_PRODUCTION_BUILD
DrawFPS(10, 10);
#endifEvery build system defines it — CMake from its PRODUCTION_BUILD option, Gradle per
build variant, the Xcode project per configuration — and the framework refuses to compile if one
forgets, with an #error that says so. That refusal exists because of how the
preprocessor treats a name it does not know: in an #if it is not an error, it is 0, and
a release build would quietly include everything meant only for debugging.
The other is RMP_RESOURCES_PATH, the folder assets are read from: the absolute path of the
project's resources/ while you develop, so the game finds its assets from any working
directory, and ./resources/ in a release. That is the CMake builds, the desktops and
the web. The two mobile builds define it their own way, the same in debug and release: the iOS
project as ./resources/, because the folder is copied into the app bundle under that
name and the app moves into its bundle when it starts, and Android's native build as an empty
string, because Gradle copies what is in resources/ to the root of the APK's
assets.
On a desktop the entry point moves into
the executable's folder when there is a resources/ beside it, before the first asset
is read, so that path means the same wherever the game was started from
(One frame has the detail). Each executable
gets its own, which is how an example with its own art reads its own folder.
The CMake options themselves keep CMake's own names, PRODUCTION_BUILD and
RESOURCES_PATH: they live in CMake's cache and never reach the preprocessor. What C++
sees is always spelled RMP_.
The rule: a header never makes you write one#
Clay, the layout engine under
rmp::ui, is a C library whose API is mostly macros: an element is
CLAY(...) { ... }. rmp::ui does not hand you that. Its containers are
functions that take a lambda, and behind them rmp/ui.h declares a function that opens
an element and one that closes it, both compiled in src/rmp/ui/, where they call
Clay's own functions rather than its macros. So no Clay type or macro appears in the header you
include, and there is no closing call to forget: the container closes when your lambda
returns.
Clay's macros are not forbidden to you, only kept out of your way. A game can include Clay and use them directly, and an example does.
What the framework adds is a constant#
raylib's
colours are macros, because raylib is C. A value the framework adds is a
constexpr constant in a namespace instead:
rmp::behavior::MAX_SPAWNED, how many
objects one Spawner keeps track of, is a constant of rmp::behavior.
Outside the cases above, that is the rule: a value the framework defines is a constant in a
namespace, where it cannot collide with anything.
Both halves are
checked. clang-tidy's cppcoreguidelines-macro-usage refuses a macro of the
framework's that is not one of the cases above — it is told their names, and a new one is a
question for review, not something to slip past it. And a query of the framework's own
.clang-tidy refuses a constant at namespace scope in a header that is not
inline constexpr: a plain const or constexpr there is a
copy in every file that includes it, where inline makes it one object for the whole
program.
One namespace for every macro#
The preprocessor has a single namespace, shared by raylib, the C library, every vendored
library and your game, and it runs before the compiler knows what a namespace is. Three rules
follow, and tools/naming_check.sh enforces all three:
- Every macro of the framework starts with
RMP_, and never with__or an underscore and a capital letter, which belong to the compiler and the C library. A vendored library's own switches keep their names, in the one file that configures it. - No constant is named like somebody else's macro.
PI,MIN,MAX,EPSILON: each is a macro on some platform, and a constant with that name compiles everywhere except there. Naming lists them. - No retired name is left in an
#if. When a macro is renamed, a test of the old name does not fail: the old name is undefined, and undefined is 0.
And one practical note. The platform
entry-point macros in rmp/app.h are the one place the framework's sources are exempt
from automatic formatting, because their bodies hold block comments, and every formatter mangles
a block comment inside a macro.