Time is a parameter
Every update hook is handed the time the frame took, in seconds, as a
parameter called delta. Nothing that moves the game reads the clock for itself: one
function reads it, and hands that number out. It is a small decision with two large
consequences: the framework can be tested without a window, and a game written with it plays the
same at any frame rate.
class Bullet : public rmp::Object {
public:
float seconds_left = 2.0f;
void _update(float delta) override {
seconds_left -= delta;
if (seconds_left <= 0) destroy();
}
};A scene's _update(delta), an object's
_update(delta), a behavior's
_update(self, delta) and the on_frame(delta) of a game with its own entry
point all get the same number, and it is always called delta — not dt,
not deltaTime — so the most common parameter in a game has one name everywhere you
read it.
Where the clock is read#
Once
a frame, in one function of src/rmp/app/clock.cpp, which asks raylib for the frame time,
corrects it, remembers it, and hands it to the frame. It is the one call to
GetFrameTime() in the framework. Every entry point — the desktop loop, the browser's
callback, iOS's — goes through that function, so the correction applies to all of them, and to a
game that writes its own frame as much as to one that uses scenes.
Nothing downstream asks raylib what time it is. The scene stack passes delta to
the scenes, the scenes to their objects, the objects to their behaviors; the integrator moves an
object by its velocity times delta; the camera's smoothing, an animation's frame
timing, a Timer, a
Tween are all driven by the number they were
handed.
So it can be tested#
A test of the framework creates a scene, spawns objects into it and runs a frame of exactly 1/30 of a second, with no window and no GPU, and checks where everything ended up. It can do that because time arrives as an argument: a function that read the clock itself would give a different answer every run, and a test that fails one time in twenty teaches everyone to ignore red.
The rule is checked.
tools/seam_check.sh fails the framework's tests if any file under
src/rmp/ or include/rmp/ calls GetFrameTime(),
GetTime(), reads the keyboard, the mouse or the gamepad, asks for the screen size, or
uses raylib's random numbers — except a short list of files, each with the reason it is allowed,
and every one of them a seam itself. The same goes for the other things that change between two
runs. The game's input is sampled in one place, rmp::input, into a snapshot a test can
replace, and the UI samples the pointer, the keys it navigates with and what a text field types
through samplers of its own that a test replaces the same way. Randomness comes from
rmp::random, one generator with a seed: a test keeps
the fixed default and gets the same sequence every time, while a shipped game is seeded from the
wall clock when it starts, and current_seed()
tells you which run you are in.
The
UI has no clock of its own either. Its transitions, its scrolling, the repeat of a held arrow key,
the blink of a text field's caret, a tooltip's wait and a notice's time on screen take their time
from frame_time(), which is the delta the frame was handed: the number
step_delta() remembered.
In
a loop of the game's own with no runner to call step_delta(), it is the frame raylib
timed, clamped the same way. A menu and the game under it cannot disagree about how long a
frame took. The layout tests run the UI in a mode where no time passes unless the test says how
much does, so a layout comes out the same on every run.
A
text field is no exception. What it types — the characters and Backspace — comes through the UI's
seam, so a test types into it as a player would, and its caret blinks on
frame_time() like everything else in the UI, shown the moment the field takes the
keyboard and then for the first half of every second it types.
The
file is not on the check's list, so a GetTime() or a key read straight from raylib
there fails it, as in any other file that is not a seam.
One
clock is also what lets a whole game be played the same way twice. A test can fix the step, so that
every frame is exactly as long as it says whatever the machine took — still clamped by
[app] max_delta — and name the seed the run starts from. That is how the framework's tests
play an example game from a script and compare what it
draws, pixel for pixel. A game that reads GetTime() or GetFrameTime()
itself steps outside that: what it draws depends on how fast the machine was.
The
platformer's flag waves on the time its _update() adds up, and not on
GetTime(), for that reason.
So it plays the same at any frame rate#
A speed times delta is a distance, whatever the frame rate. That is the whole
trick, and the framework is built so you get it without thinking about it:
- Velocity is integrated for you. Set
velocityin units per second and the object moves the same distance in a second at 30 or 240 frames. - Forces and impulses are two calls.
apply_forceis meant to be called every frame, and is scaled bydelta;apply_impulseis meant to be called once, and is not. An impulse applied every frame would go twice as fast at 120 frames a second as at 60 — which is exactly the bug the two names exist to prevent. - The camera's
smoothing is a rate. The usual
position += (target - position) * 0.1f, run once a frame, follows twice as fast at 120 frames a second as at 60. The camera instead covers1 - exp(-smoothing * delta)of the distance each frame, which comes to the same fraction in a second at any frame rate.
The framework checks this on a real game: the bot that plays the platformer example plays it to the flag at 60 frames a second and again at 240, and jumps from its moving platform at both.
[app] max_delta#
Real elapsed time has no ceiling. Drag the window, stop at a breakpoint, let the disk stall, and the next frame time comes back as half a second. A bullet at 900 units a second then moves 450 units in one step — through the wall and out the other side, without ever overlapping it.
So the
delta the game is handed is clamped to
[app] max_delta, a
twentieth of a second unless you change it. A frame that really took longer arrives as a twentieth
of a second, and the game runs a moment of slow motion instead of teleporting. Nobody minds a
hitch; everybody minds falling out of the world.
[app]
max_delta = 0.050 switches the clamp off and hands the real time through, which makes a stalled frame the game's
problem. Sensible values are between a sixtieth and a tenth of a second, and
configure.py refuses the ones that cannot be meant — longer than a second, which
clamps nothing, or shorter than a frame at 240 Hz, which would clamp every frame:
[app]
max_delta = 0.001 raylib_multiplatform.toml:2: max_delta = 0.001
^^^^^^^^^^^^^^^^^
configure: error: [app] max_delta = 0.001 is shorter than a frame at 240 Hz, so every
frame would be clamped and the game would run in permanent slow
motion. That is never what anyone means. Use 0 to switch it off.It also refuses a frame cap that cannot coexist with the clamp: with
[window] fps at 15, every frame
is longer than a twentieth of a second, and the game would run at three quarters speed forever —
the config pipeline shows that refusal.
The clamp is half of the answer to tunnelling. The other half is the swept test in the collision pass, which catches a fast object crossing a thin one between two frames even at a perfectly normal frame rate, where no clamp applies.
The frame with no time in it#
The first frame of a game reports a
frame time of 0, and a delta of 0 or less is never integrated at all. That matters more than it
sounds: a push given in _ready() with apply_force waits in the object for
the first frame that has time in it, instead of being spent on a frame that could not move
anything. A negative frame time, which a clock that went backwards across a suspend could produce,
is handed to the game as 0.