One frame
Sixty times a second, or as often as the screen asks, the framework samples the
input, moves the world, draws it and tidies up. The order those happen in is the contract every
hook you write relies on: when _update runs, what _collision sees, why a
scene you leave is still there until the frame ends. This page follows one frame from the top,
and every step is pinned to the line of the framework that does it.
Almost every rule on this page has the same reason behind it. A hook is your code, and your code is entitled to do anything — spawn, destroy, change scene, quit — from anywhere. The order is built so that none of those can pull the floor out from under something still running.
Who calls the frame#
RMP_GAME is
RMP_ENTRY_POINT with its three hooks written for
you, and the entry point is the code that calls the frame. Where the loop lives depends on the
platform, and only there:
- Windows, Linux, macOS, the BSDs and Android: a
main()with awhileloop, the ordinary raylib shape. On Android raylib's own native glue calls thatmain(). - The web: the browser owns the loop.
main()hands one frame function toemscripten_set_main_loop()and the browser calls it every animation frame. Awhileloop there would need Emscripten's ASYNCIFY, which rewrites and slows the whole program; this shape does not. - iOS: UIKit owns the loop and calls three functions the macro defines.
All
three do the same four things each time around: ask whether the game is still running, sample the
input, hand the frame hook a delta, and finish the frame. On the desktop that is the
whole loop:
while (keep_running()) {
begin_frame();
FRAME(step_delta());
end_frame();
}A
game stops running for one of three reasons, checked in one place so the platforms cannot drift
apart: the window was closed, rmp::app::quit() was
called, or the CI frame budget is spent. quit() only raises a flag, so the frame that
asked to quit finishes normally and the game shuts down exactly as it does when the window is
closed. On the web and iOS there is no window to close and WindowShouldClose() is
never called.
The delta#
The
delta every hook receives is raylib's GetFrameTime() with two corrections:
a negative reading, or one that is not a number, becomes 0, and a reading longer than
[app] max_delta — a
twentieth of a second unless you change it — is cut down to it. A frame that stalled for half a
second, because the window was dragged or the debugger stopped, becomes a moment of slow motion
instead of a jump that carries a bullet through a wall. That is the one place the framework reads
the clock, in src/rmp/app/clock.cpp, and the number it hands out is remembered for the
rest of the frame: the UI animates by it too.
Time is a parameter says why.
1. Input, once#
The
runner first asks whether anyone can see the window
(out of sight, below), and then samples every input device once,
before it calls the frame hook. The keyboard, the mouse —
which a touch screen drives too — and the first gamepad are read into one snapshot, and every
pressed and
just_pressed of the frame answers from it.
Two scenes asking about the same key in the same frame cannot disagree, and a key is "just
pressed" for exactly one frame however many times it is asked about.
It is the runner's step and not the scene stack's, so it is the same whether the hook is
RMP_GAME's or a game's own on_frame, and it happens exactly once: a
second sample in the same frame would compare every key with itself, and every press of that frame
would be lost. tests/input_play.cpp drives one program through the real desktop runner
both ways, a hook of its own and a scene under frame(), and each press has to be seen
exactly once.
The first frame has nothing to compare against, so it compares against itself: a key already held when the game starts is held, not just pressed, and does not fire whatever it is bound to.
The
frame proper is frame(float delta) in src/rmp/app/app.cpp, the function
RMP_GAME's frame hook calls, and the first thing it does is open the UI's frame, which
advances the UI's animations by this frame's delta, and its scroll, once, however many
scenes draw a menu.
The window out of sight#
Before
anything else, begin_pace() in src/rmp/app/clock.cpp asks raylib whether the
window is minimized and whether it has the focus, and decides two things for the frame: whether
the scenes move, and whether anything is drawn.
- Minimized (on the desktop): the scenes move, the music plays and the clock runs, but nothing is drawn, and the loop sleeps a thirtieth of a second instead of waiting for a vsync nobody sees. The world does not stop because its player looked away.
- Out of focus, with
[window] pause_unfocused(off by default): the scenes stop, the clock stops, every sound and the music pause, and the last picture is drawn again ten times a second. The top scene hears_suspend()when it starts and_resume()when the window has its focus back, and the firstdeltaafter it is 0, so nothing jumps by the time the player was away. Minimized under it is a pause too.
There
is no #if for it: a browser stops calling a hidden tab's frame and a phone stops a
backgrounded app on their own, and raylib says a window there is never minimized. A frame that
draws nothing still polls the window's events itself, because raylib does that in
EndDrawing(), which does not run: without it the window would never hear it was
restored. And the frames raylib did not time are timed by the clock itself, so the first one
drawn after a minimized stretch is not handed the whole stretch as its delta.
Performance has what it saves.
2. Update#
Then
the scenes run. Not all of them: the stack is walked down from the top for as long as each scene
lets the one under it run (updates_below),
and from the lowest one that runs, back up to the top. Bottom up, because the world moves first
and whatever is layered over it reacts to where the world ended up.
For each of those scenes, in this order:
- Whether input reaches it. A scene hears the input only if every scene above
it lets it through (
input_below). One that does not reads every action as not pressed, so a game running under a HUD that took the input simply does not shoot. - The pointer. A press and a release on the same object call its
on_click; holding and moving calls itson_dragwith the movement in world units, taken through the scene's camera. Only visible objects with a shape and one of those two callbacks are considered, and the topmost of them under the pointer wins: an object with neither callback drawn on top does not stop the click reaching the one below. The UI gets first refusal: a button drawn over the world takes the click, and the object under it never hears it. This comes before everything else so a click is acted on in the frame it happened. - The scene's
_update(delta). The scene sets up the frame, and its objects then move inside it. - Its objects, one at a time, in the order they were spawned — below.
- Collision, once every object has moved — below.
- The camera, last — below.
Each object#
An object that is alive goes through these steps, and an object destroyed by any of them stops there:
- Its behaviors'
_update, in the order they were added. A behavior runs before the object's own code so that yours has the last word on what the frame leaves behind. - Its
_update(delta). - Where it is, remembered — after your code, so that writing
positionyourself is a teleport, not a journey the collision pass would sweep along. - The move. The forces from
apply_forceand the scene'sgravitytimes the object'sgravity_scalechange the velocity, and the velocity moves the object. Asolidobject that is notimmovable, in a scene with a map, moves through the map one axis at a time and stops at its solid cells. On a frame whose delta is 0 or less this step is skipped whole, forces included, which keeps a push made in_readyfor the first frame that can use it. The other steps still run. - The edges: what
edgessays to do at the edge of the object's bounds — clamp, bounce, wrap or be destroyed. - The animation clock of its sprite, here and not in the draw, so a scene that runs without being drawn still has time passing in it.
- Its behaviors'
_late_update, for the behavior whose job is to correct where the object ended up — snapping it to a grid, say — which has to see the position after the move, not before.
An object spawned during this pass gets its _ready() at once, inside the
spawn() call, and its first _update on the next frame. A behavior added
during the pass is the same. One spawned before the pass — from the pointer's callbacks or the
scene's _update — is updated in this one; one spawned after it, from a collision, waits
for the next frame like the rest.
Collision#
Once every object of the scene has moved, the scene's pairs are found and dealt with, in four passes, and the order of the passes is the point:
- Rewind an object the swept test caught: one that crossed something between two frames without ever overlapping it is put back where it met it. Only when both are solid — a bullet passing through a coin is told, and keeps going.
- Separate the pairs that are both solid: each moves half the overlap, or the movable one moves all of it when the other is immovable. A push that would end inside a solid cell of the map is undone, so a moving platform cannot shove the player through a wall.
- Notify, so your code reads the positions as they were resolved.
- Take the velocity out of the contact: whoever was pushed loses the part of its
velocity going into what pushed it, and keeps the rest. After the notification, so a
_collisionreads the velocity it hit with, and a bounce that turns it round is left alone.
For
each touching pair, once per frame and whether or not either is solid, the notification goes to
both sides in this order: the first object's
_collision, then its behaviors', then the
second object's _collision and its behaviors', then the first object's
on_collision and the second's. Each side
is told if it was alive when its pair came up, even when the other side has just died: the shot
that kills an alien still counts as a hit.
The camera#
Last of
all, the scene's camera settles on where things ended
up, after collision has had its say: it moves toward what it
follows, at a rate that is the same at any frame
rate, is clamped to its limits, and advances its
shake. Anything that reads the camera next — the draw, the pointer next frame — sees the settled
view.
3. Draw#
The
frame is opened with BeginDrawing() and cleared. The colour is the
background of the lowest scene that is
drawn, because everything above it is drawn over it; when that is BLANK, the
default, the clear colour is the UI theme's background.
Then
the scenes draw, bottom up, from the lowest one the stack shows
(draws_below). Each one, in this order:
- The camera opens:
BeginMode2D()with the scene's camera, so what follows is drawn in world units — fitted to the window the way[window] stretchsays. When the stretch letterboxes, the world is also cut to the game's area, so the bars around it keep the clear colour. - The map, under everything, because it is the level the objects stand on — only the tiles that reach into what the camera shows (Tilemaps).
- The objects that are alive and visible, sorted by
layerand, within a layer, in the order they were spawned. Each one draws its sprite, or its shape when it has no sprite, then its behaviors'_draw, then its own_draw(). - The camera closes:
EndMode2D(), and the cut with it. - The scene's
_draw(), in screen space. This is where a scene'srmp::ui::begin()andrmp::ui::end()go, andend()is what draws the UI — over the world, and standing still while the camera moves.
Objects first and the scene's own drawing after is what puts a HUD over the game rather than under it, without either one saying so. The same layer-then-spawn order, walked backwards, is the order the pointer finds objects in: the last thing drawn is the first thing clicked.
A scene the input cannot reach is drawn exactly the same, UI included, but no widget in it can be hovered or pressed: the HUD under an open pause menu is visible and inert, and neither scene wrote a line about it.
Two
things of the interface's go around the scenes. When the last frame's
overlay() asked for a blur, this frame is drawn
into a texture from BeginDrawing() on, clear included, so that the overlay has the
picture under it to blur; after the last scene, if no overlay took it, the texture goes on screen
as it is. And after every scene, what rmp::ui::notify()
queued is drawn over everything, taking no input.
4. The CI capture#
Between the last draw and EndDrawing(), the framework calls
SmokeTest_CaptureFrame from tests/smoke_test.h. In a normal run it returns
at once. Under the frame budget CI sets (RAY_TEST_MAX_FRAMES), on one chosen frame it
flushes raylib's batch, reads the frame back and checks it has something on it — and that is the
only place it can: before the flush the frame holds nothing but the clear, and after
EndDrawing() it may already be gone. Because the framework makes the call, a game
cannot put it in the wrong place. Testing has what CI
does with the result.
5. Present#
EndDrawing() puts the frame on screen, and where the frame rate is capped —
[window] fps, or vertical sync
with [window] vsync — it is
also where the frame waits. Then the UI's frame closes: a UI that drew nothing this frame lets go
of the pointer and the keyboard, so a pause menu that just closed under the cursor does not keep
the game's mouse.
And
when the focus has just moved, with its ring showing, onto a control a scroll area has clipped
away, the area is scrolled to show it — here, once every scene has drawn and the frame knows where
everything is.
6. Apply what was deferred#
Only now, with nothing mid-frame, does anything go away.
Destroyed objects are freed. An object stops updating and drawing the moment
destroy() is called on it, but its memory stays
until here. That is why the reference spawn()
returns is good for the whole frame, and why other in a _collision is
always a real object even when it is dying. Freeing a slot bumps its generation, so every
rmp::Handle to it stays empty — as it has been since
destroy() — even once the slot holds a new object.
Scene
changes are applied, in the order they were asked for, so push then
pop in one frame leaves the stack as it was. A
change ends every scene top down; a
push suspends the top and enters the new one; a
replace ends only the top; a
pop ends the top and resumes the one under it. A
scene that ends gets _end() first and its objects go after it, so a scene tidying up
can still walk what it spawned. A transition asked for from inside a _ready() or an
_end() that runs here belongs to the next frame.
This is why if (rmp::ui::button("Play")) change<GameScene>(); is safe inside
the menu's own _draw(): the menu is not destroyed until its _draw(), and
everything else in the frame, has finished.
After the frame#
Back
in the loop, outside frame(), the same last things happen on every platform. The
music stream is fed: here, and not in the scene stack, so it keeps playing under a pause menu that
freezes the game, and in a game that does not use scenes at all. A game with a frame hook of its
own has the UI's frame closed, which the runner opened before the hook; a begin() it
left without its end() is reported once and closed for it. The CI frame budget
advances, and on the budget's last frame the framework prints how many of the game's mistakes it
has reported, which a correct game keeps at 0. Last, the end of the frame is reported to whoever
is watching: nobody, unless a test that plays the game from
a script is, and then it sees the frame drawn and its scene changes applied.
Before the first frame, and after the last#
Before
the first frame the entry point prepares the run: it tells the asset layer where this
executable's resources/ are, seeds rmp::random
from the wall clock — or from the seed a test that plays the game names — turns on [dev]
strict in a debug build that asks for it, has a debug build say each of the
framework's warnings on screen as well, with rmp::ui::notify(),
reads the CI frame budget, moves into the app
bundle on iOS or next to the executable in a desktop release, and opens the resource pack if there
is one.
The
move matters for a released game. It reads its files from ./resources/, and
. is the working directory, which is wherever the player started it from: a file
manager, a shortcut, another folder in a terminal. So a production build on a desktop — Windows,
Linux (DRM included), macOS, FreeBSD — changes into the executable's own folder when
there is a resources/ folder there, before the pack is opened, and the release starts
the same from anywhere. A change of
directory rather than an absolute path, so a plain raylib LoadTexture() of a
RMP_RESOURCES_PATH path keeps working too. Without a resources/ beside
it — a release run from the source tree, whose executable is in build/release/ — it keeps the
working directory. NetBSD and OpenBSD cannot say where an executable is, so there it keeps the
working directory and logs ASSETS: this system does not say where the executable is, so …
is read from the working directory, ….
Then
the ready hook. RMP_GAME's opens the window from [window] — vsync and a
resizable window first, the frame cap before InitWindow() because raylib's DRM backend
chooses the display mode from it, and never on the web or iOS, where the browser and the display
pace the frame — and only then makes your first scene and enters it, so its members and its
_ready() both load with the window open. A texture, a font or a sheet that was asked
for before that line is named once, as the window opens: it has nothing to draw. The window opens at [window] width by height; only under the
CI frame budget can RAY_TEST_WINDOW open it at another size, so that a
[window] stretch can be booted on a screen of another shape.
Right
after the ready hook, on every runner, Escape is taken back from raylib. InitWindow()
makes it the key that closes the window, and in a game it is the key for "back" and "pause":
rmp::ui goes back on it, and the factory ui_cancel action is bound to
it. After the hook is where every window is open — RMP_GAME's, or the one a game's
own on_ready opened — so the entry point calls SetExitKey(KEY_NULL)
there. The window's close button still closes the game. A SetExitKey() of your own in
on_ready is overwritten; to quit on a key, say so where the game reads its keys:
if (IsKeyPressed(KEY_ESCAPE)) rmp::app::quit();Then, under CI, a save is written to the platform's real folder and read back, and the boot is reported.
After the last frame, the order matters more than anywhere else, because the window owns the GPU context and everything holding a texture has to let go of it while the window still exists:
- The scenes, top down, each with its
_end()and its objects. Transitions still queued are dropped: a scene nobody will see does not get a_ready()to load assets in. - The globals from
rmp::global, in reverse order of first use — after the scenes, because a scene's_end()may write to one. - The UI and its font, the sounds and music, and then every texture, font and sound still loaded.
- The stop hook:
RMP_GAME's closes the window. - The sound device and the resource pack, which hold no GPU objects and can go after the window.
On Android, rmp::app::quit() also finishes the Activity, so the app does not
linger in the recent-apps list. On iOS it does nothing at all and says so once: an app that closes
itself looks to the user as if it crashed, and App Review rejects it.
It says so
through the same once-only warning as a mistake in the game, so with
[dev] strict on, a debug iOS
build aborts at the first quit() — a Quit button pressed in the simulator is enough. A
game that keeps a Quit button can leave it out on iOS:
#if !defined(PLATFORM_IOS)
if (rmp::ui::button("Quit")) rmp::app::quit();
#endifA game that owns its frame#
A game that wants none of the scene stack writes its own three hooks and hands them to
RMP_ENTRY_POINT:
static void on_ready() {
InitWindow(RMP_WINDOW_WIDTH, RMP_WINDOW_HEIGHT, RMP_WINDOW_TITLE);
}
static void on_frame(float delta) {
(void)delta;
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("My own frame", 20, 20, 20, DARKGRAY);
EndDrawing();
}
static void on_exit() { CloseWindow(); }
RMP_ENTRY_POINT(on_ready, on_frame, on_exit);Then none of steps 2 to 6 runs: on_frame is called where frame()
would have been, and what happens inside it is yours. What stays the framework's is the loop
around it — the three ways a run ends, the input snapshot of step 1, the clamped
delta, the CI frame budget, the music stream — and the start and the stop: the
resource pack, the seed, Escape taken back from raylib, and the ordered release of everything
loaded. The examples under UI are written this
way, because a menu on its own needs nothing more.
How the order is kept#
Every interesting property of a scene stack is an ordering property, so the tests check
transcripts rather than return values: tests/scene_test.cpp records which hooks ran,
in what order, across pushes, pops, changes made mid-frame and shutdown; and
tests/object_test.cpp and tests/behavior_test.cpp check that behaviors
run before the object's _update, that _late_update runs after the move,
that the edges come after the integration and that the draw order is layer first. The claims on
this page are pinned to the source lines they describe, and the site's build fails if one stops
matching.