# One frame

> Everything the framework does between one picture and the next, in the order src/rmp/app/app.cpp and src/rmp/scene/scene.cpp do it, and why the order is what it is.

Source: https://omardev29.github.io/rmp-docs/framework/the-frame.html

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.

One frame. Each step links to its section below.

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`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_GAME) is [`RMP_ENTRY_POINT`](https://omardev29.github.io/rmp-docs/reference/macros.html#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 a `while` loop, the ordinary raylib shape. On Android raylib's own native glue calls that `main()`.
- **The web**: the browser owns the loop. `main()` hands one frame function to `emscripten_set_main_loop()` and the browser calls it every animation frame. A `while` loop 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:

```text
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()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-app.html#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`](https://omardev29.github.io/rmp-docs/reference/configuration.html#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](https://omardev29.github.io/rmp-docs/framework/why/delta-and-time.html) says why.

## 1. Input, once

The runner first asks whether anyone can see the window ([out of sight](https://omardev29.github.io/rmp-docs/framework/the-frame.html#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`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html#pressed) and [`just_pressed`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html#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`](https://omardev29.github.io/rmp-docs/reference/configuration.html#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()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#_suspend) when it starts and [`_resume()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#_resume) when the window has its focus back, and the first `delta` after 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](https://omardev29.github.io/rmp-docs/framework/performance.html#asleep) 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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#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:

1. **Whether input reaches it.** A scene hears the input only if every scene above it lets it through ([`input_below`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#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.
2. **The pointer.** A press and a release on the same object call its [`on_click`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#on_click); holding and moving calls its [`on_drag`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#on_drag) with 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.
3. **The scene's [`_update(delta)`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#_update).** The scene sets up the frame, and its objects then move inside it.
4. **Its objects**, one at a time, in the order they were spawned — [below](https://omardev29.github.io/rmp-docs/framework/the-frame.html#objects).
5. **Collision**, once every object has moved — [below](https://omardev29.github.io/rmp-docs/framework/the-frame.html#collision).
6. **The camera**, last — [below](https://omardev29.github.io/rmp-docs/framework/the-frame.html#camera).

### Each object

An object that is alive goes through these steps, and an object destroyed by any of them stops there:

1. **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.
2. **Its [`_update(delta)`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#_update).**
3. **Where it is, remembered** — after your code, so that writing `position` yourself is a teleport, not a journey the collision pass would sweep along.
4. **The move.** The forces from [`apply_force`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#apply_force) and the scene's [`gravity`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#gravity) times the object's [`gravity_scale`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#gravity_scale) change the velocity, and the velocity moves the object. A [`solid`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#solid) object that is not [`immovable`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#immovable), 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 `_ready` for the first frame that can use it. The other steps still run.
5. **The edges**: what [`edges`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#edges) says to do at the edge of the object's bounds — clamp, bounce, wrap or be destroyed.
6. **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.
7. **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:

1. **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.
2. **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.
3. **Notify**, so your code reads the positions as they were resolved.
4. **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 `_collision` reads 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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#_collision), then its behaviors', then the second object's `_collision` and its behaviors', then the first object's [`on_collision`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html) settles on where things ended up, after collision has had its say: it moves toward what it [`follow`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#follow)s, at a rate that is the same at any frame rate, is clamped to its [`limits`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#draws_below)). Each one, in this order:

1. **The camera opens**: `BeginMode2D()` with the scene's camera, so what follows is drawn in world units — fitted to the window the way [`[window] stretch`](https://omardev29.github.io/rmp-docs/manual/core/camera.html) says. When the stretch letterboxes, the world is also cut to the game's area, so the bars around it keep the clear colour.
2. **The map**, under everything, because it is the level the objects stand on — only the tiles that reach into what the camera shows ([Tilemaps](https://omardev29.github.io/rmp-docs/manual/modules/tilemaps.html)).
3. **The objects** that are alive and visible, sorted by [`layer`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#layer) and, 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()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#_draw).
4. **The camera closes**: `EndMode2D()`, and the cut with it.
5. **The scene's [`_draw()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#_draw)**, in screen space. This is where a scene's `rmp::ui::begin()` and `rmp::ui::end()` go, and `end()` 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()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html#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()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html#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](https://omardev29.github.io/rmp-docs/framework/testing.html#render) has what CI does with the result.

## 5. Present

`EndDrawing()` puts the frame on screen, and where the frame rate is capped — [`[window] fps`](https://omardev29.github.io/rmp-docs/reference/configuration.html#window-fps), or vertical sync with [`[window] vsync`](https://omardev29.github.io/rmp-docs/reference/configuration.html#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()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#destroy) is called on it, but its memory stays until here. That is why the reference [`spawn()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Handle.html) 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`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#change) ends every scene top down; a [`push`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#push) suspends the top and enters the new one; a [`replace`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#replace) ends only the top; a [`pop`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#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](https://omardev29.github.io/rmp-docs/framework/testing.html#e2e) 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`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html) from the wall clock — or from the seed a test that plays the game names — turns on [`[dev]
strict`](https://omardev29.github.io/rmp-docs/reference/configuration.html#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()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html#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:

```cpp
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:

1. **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.
2. **The globals** from [`rmp::global`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#global), in reverse order of first use — after the scenes, because a scene's `_end()` may write to one.
3. **The UI** and its font, **the sounds and music**, and then **every texture, font and sound still loaded**.
4. **The stop hook**: `RMP_GAME`'s closes the window.
5. **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`](https://omardev29.github.io/rmp-docs/reference/configuration.html#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:

```cpp
#if !defined(PLATFORM_IOS)
if (rmp::ui::button("Quit")) rmp::app::quit();
#endif
```

## A 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`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_ENTRY_POINT):

```cpp
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](https://omardev29.github.io/rmp-docs/examples/ui/index.html) are written this way, because a menu on its own needs nothing more.

NoteBoth input layers work in a game that owns its frame. [`rmp::input`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html) answers from the snapshot the runner took before `on_frame`, exactly as it does under `RMP_GAME`. With no scene on the stack there is no camera to go through, so `rmp::input::pointer()` is the pointer's position on the screen, the same as `rmp::input::pointer_screen()`, with nothing said in the log. `rmp::ui` needs nothing either: with no `frame()` to open the UI's frame, the first `rmp::ui::begin()` of a frame opens it and `rmp::ui::end()` closes it. NoteWith your own frame, the CI capture happens after `EndDrawing()` instead of before it. That works under the software renderer CI boots the examples with, where the frame is still in memory after it is presented, and nowhere else.

## 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.
