# raylib in your game

> What the framework calls on raylib in your place — the window, the frame, the clock — and everything else, which is yours to call from your scenes.

Source: https://omardev29.github.io/rmp-docs/manual/raylib/raylib.html

A game made with the framework is a raylib game. The framework makes a small number of raylib calls for you — the ones every raylib program writes the same way: opening the window, beginning and ending the frame, closing it all at the end. Everything else in raylib is yours, from any scene, with nothing to unlock and nothing to convert.

The split is easy to remember: the framework owns *when* things happen, and you own *what* happens. The frame loop, the window and the shutdown order are the framework's; drawing, input, loading, sound and the rest of raylib are yours, inside the hooks it calls.

## What the framework owns

[`RMP_GAME`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_GAME) in `src/main.cpp` writes the entry point for the platform you are building, and these are the raylib calls it makes so that you do not:

| raylib call | When the framework makes it |
|---|---|
| `SetConfigFlags()` | Once, before the window opens: `FLAG_WINDOW_RESIZABLE`, plus `FLAG_VSYNC_HINT` when `[window] vsync = true`. |
| `SetTargetFPS()` | Once, before the window opens, when `[window] fps` is above 0 — and never on the web or iOS, where the browser and the display link pace the frame. |
| `InitWindow()` | Once, at `[window] width` × `height`, titled `[window] title`. Your first scene's `_ready()` runs after it, so a texture loads there. |
| `SetExitKey()` | Once, with `KEY_NULL`, right after your first scene's `_ready()` returns. raylib's `InitWindow()` makes Escape the key that closes the window, and in a game Escape means "back" or "pause". See [below](https://omardev29.github.io/rmp-docs/manual/raylib/raylib.html#escape-is-the-games-key). |
| `WindowShouldClose()` | Every frame, to see whether the player closed the window — except on the web and iOS, which have no such window and where it is never called. |
| `GetFrameTime()` | Every frame, clamped to `[app] max_delta`, and handed to `_update(float delta)` as `delta`. |
| `BeginDrawing()` and `ClearBackground()` | Every frame, cleared with the [`background`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#background) of the lowest scene being drawn, or the theme's when that is `BLANK`. |
| `BeginMode2D()` and `EndMode2D()` | Every frame, around each scene's map and objects, with that scene's [`camera`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#camera). |
| `EndDrawing()` | Every frame, after the last scene's `_draw()`. |
| `CloseWindow()` | Once, at the very end, after every scene, every [`rmp::global`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#global) and every resource that lives on the GPU has been released. |

The sound device is not on that list on purpose: [`rmp::audio`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html) calls `InitAudioDevice()` the first time a sound is loaded or played, so a game that never makes a sound never opens one.

Before your first scene's `_ready()` the framework has also opened the asset pack, if there is one, and routed raylib's own file loading through it; seeded [`rmp::random`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html); on iOS moved into the app bundle, where the resources are; and in a desktop release moved into the executable's folder, when there is a `resources/` in it, so that `./resources/` means the one shipped next to the game wherever it was started from — see [Desktop](https://omardev29.github.io/rmp-docs/manual/platforms/desktop.html#running-a-release-build).

### One frame, in order

Every frame of a game made with `RMP_GAME` is the same sequence, and every hook you write has a fixed place in it:

1. The input devices are sampled, once, for the whole frame — by the loop itself, which does the same before the frame hook of a game written with `RMP_ENTRY_POINT`.
2. The scenes are updated from the bottom of the stack upwards, skipping the ones a scene above freezes. For each one: the pointer, first — a click or a drag on one of its objects calls that object's `on_click` or `on_drag` handler, so a handler can spawn or destroy before anything else looks — then its `_update(delta)`, then its objects, then the collision pass, then its camera, which follows where everything ended up.
3. `BeginDrawing()`, and the clear — into a texture instead of the screen while an [`overlay()` blurs](https://omardev29.github.io/rmp-docs/manual/modules/ui.html#the-blur) what is under it.
4. Every scene that is visible, from the bottom up: `BeginMode2D()` with its camera, its map, its objects, `EndMode2D()`, and then its own `_draw()` — outside the camera, in screen space.
5. The [notices](https://omardev29.github.io/rmp-docs/manual/modules/ui.html#notices) of `rmp::ui::notify()`, over everything.
6. `EndDrawing()`.
7. The scene changes asked for during the frame — `change`, `push`, `replace`, `pop` — are applied.

That fourth step is why a HUD drawn in a scene's `_draw()` stays put while the camera moves: the camera is closed before `_draw()` is called, and it is the reason the [scene](https://omardev29.github.io/rmp-docs/manual/core/scenes.html) hooks say which space they draw in.

### And when the game ends

The shutdown runs in an order that was chosen so nothing touches a dead GL context. First, everything that owns something on the GPU, while the window still exists: the scenes are ended from the top of the stack down — each gets its [`_end()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#_end) — then the globals are destroyed, then the UI's font, then every texture, font and sound still loaded through `rmp::assets`. Then `CloseWindow()`. Then what owns nothing on the GPU: the asset pack, raylib's loader hook, and the sound device.

It is the same sequence whether the player closed the window, the game called [`rmp::app::quit()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-app.html#quit), or a CI run reached its frame budget.

## What stays yours

All of raylib: shapes, textures, text, models, shaders, sound, input, files, the window's state. Most headers under `include/rmp/` already include `<raylib.h>`, so a scene can call raylib with nothing more than the header it already has.

### In `_update()`, use the delta you are given

`delta` is the seconds since the last frame, the same number `GetFrameTime()` returns — with one difference that matters. A frame that really took longer than `[app] max_delta` arrives clamped, so dragging the window or stopping at a breakpoint gives the game a moment of slow motion instead of moving a bullet through a wall. Call `GetFrameTime()` yourself and you get the real number, unclamped.

```cpp
Vector2 ship{ 400, 225 };

void _update(float delta) override {
    if (IsKeyDown(KEY_RIGHT)) ship.x += 300 * delta;
    if (IsKeyDown(KEY_LEFT)) ship.x -= 300 * delta;
}

void _draw() override {
    DrawTriangle({ ship.x, ship.y - 20 }, { ship.x - 15, ship.y + 15 },
                 { ship.x + 15, ship.y + 15 }, SKYBLUE);
}
```

### Input: raylib's, or routed

raylib's input functions work everywhere, and read the same frame of input the framework does. What they do not know about is *who should hear it*. A query by name through [`rmp::input`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html) is routed: an action bound to the mouse goes quiet while the interface wants the pointer, and every action goes quiet in a scene whose input is taken by a scene above it. `IsMouseButtonPressed()` called directly fires on the click that pressed a button in your menu as well.

```cpp
// raw: also fires when the click lands on a UI button
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) shoot();

// routed: silent while rmp::ui wants the pointer
rmp::input::action("fire", MOUSE_BUTTON_LEFT, KEY_SPACE);
if (rmp::input::just_pressed("fire")) shoot();
```

The raw form has a spelling of its own in `rmp::input` too: [`rmp::input::pressed(KEY_W)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html#pressed) is exactly `IsKeyDown(KEY_W)`, never routed. The type of the argument says which of the two you asked for. The [`rmp::ui`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html) controls read the keyboard and the gamepad through a seam of their own, so a menu navigates with the arrow keys whether or not the game defines a single action.

### Escape is the game's key

raylib's `InitWindow()` makes Escape its exit key: pressed, it closes the window. In a game it is the key for "back" and "pause" — `rmp::ui` goes back on it, and the `ui_cancel` action is bound to it — so the framework calls `SetExitKey(KEY_NULL)` once the window is open, right after the ready hook returns, on every target: after your first scene's `_ready()` with `RMP_GAME`, after your own `on_ready()` with [`RMP_ENTRY_POINT`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_ENTRY_POINT). Upstream raylib's Win32 and DRM backends close on Escape whatever the exit key is; the copy the framework ships is patched so that they obey it (`thirdparty/raylib/PATCHES.md`). The window's close button still closes the game: that is a request from the window, not a key.

A `SetExitKey()` of your own in that first ready hook is overwritten. To quit on a key, say so where the game reads its keys, with [`rmp::app::quit()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-app.html#quit), which ends the game the way the close button does:

```cpp
if (IsKeyPressed(KEY_ESCAPE)) rmp::app::quit();
```

### In `_draw()`, screen space — and the world when you open the camera

A scene's [`_draw()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#_draw) runs after its map and its objects have been drawn, with the camera already closed. A coordinate there is a pixel of the window. To draw in the same world units as the objects, open the camera yourself, for exactly the lines that need it, with [`camera.raylib()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#raylib):

```cpp
Vector2 target{ 640, 300 };

void _draw() override {
    // the world, through the same camera the objects were drawn with
    BeginMode2D(camera.raylib());
    DrawCircleLinesV(target, 24, RED);
    EndMode2D();

    // the screen: stays put while the camera moves
    DrawText("Find the target", 10, 10, 20, RAYWHITE);
}
```

On a window of exactly the design size, with a camera nobody touched, the two spaces are the same and the difference is invisible. On any other window they are not, which is why Pong in `examples/games/01_pong` opens the camera to draw its net: drawn in screen pixels, the net and the court would come apart as soon as the window is resized.

An object's own [`_draw()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#_draw) is different: it runs inside the camera already, because objects live in the world. Override it to draw an object with raylib calls of your own, in world units, without opening anything.

`rmp::ui` draws when `rmp::ui::end()` is called, so the order inside `_draw()` is the order on screen: what you draw before `rmp::ui::begin()` is under the interface, and what you draw after `rmp::ui::end()` is over it.

### 3D

Nothing in the framework is 2D-only on raylib's side. A scene that wants a 3D view keeps a `Camera3D` of its own and opens it in `_draw()`:

```cpp
Camera3D view{ .position = { 6, 6, 6 },
               .target = { 0, 0, 0 },
               .up = { 0, 1, 0 },
               .fovy = 45,
               .projection = CAMERA_PERSPECTIVE };

void _draw() override {
    BeginMode3D(view);
    DrawCube({ 0, 0, 0 }, 2, 2, 2, MAROON);
    DrawGrid(10, 1);
    EndMode3D();

    DrawText("3D, from a scene", 10, 10, 20, RAYWHITE);
}
```

The scene's own [`map`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#map) and objects are 2D and are drawn before `_draw()`, so a 3D scene simply has neither. `DrawCube()` and `DrawGrid()` live in raylib's `rmodels` module; a game that lists `"rmodels"` in `[raylib] disabled_modules` does not have them — except on iOS, where that setting has no effect, because iOS links a prebuilt raylib (see [iOS](https://omardev29.github.io/rmp-docs/manual/platforms/ios.html)).

### What you load yourself

raylib's loaders work, and they read through the asset pack when there is one: opening the pack also routes raylib's own file loading through it, so `LoadTexture()`, `LoadModel()` and `LoadShader()` find a packed file by the same path they would find a loose one. Build every path from RMP_RESOURCES_PATH, the macro every build system defines with the folder its `resources/` are read from:

```cpp
Texture2D picture{};

void _ready() override { picture = LoadTexture(RMP_RESOURCES_PATH "rabbit.png"); }

void _end() override { UnloadTexture(picture); }

void _draw() override { DrawTexture(picture, 0, 0, WHITE); }
```

What you load yourself you unload yourself, and `_end()` is a safe place for it: at shutdown every scene is ended while the window — and the GL context with it — still exists. The same goes for a `RenderTexture2D` from `LoadRenderTexture()`. The alternative is [`rmp::assets::load_texture()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_texture), whose [`rmp::Texture`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Texture) releases itself and converts to a `Texture2D` wherever raylib wants one.

One of raylib's loaders stays outside the pack, by raylib's design: `LoadMusicStream()` opens its file itself, so music loaded with it has to ship as a loose file — and a desktop archive that CI packs ships the pack alone (`tools/ship_resources.sh`). [`rmp::audio::music()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#music) streams from the pack instead. The pack holds every file below `resources/`, folders too, under its path there.

If your code calls `InitAudioDevice()` itself, the framework notices and leaves closing it to you; when `rmp::audio` opened it, the framework closes it after the window.

## What never to call

A short list, and each one is on it because the framework already does the same job at a moment it has chosen for a reason.

`InitWindow()` and `CloseWindow()`

The window is the framework's, from before your first `_ready()` to after your last `_end()`. A `CloseWindow()` of your own takes the GL context away while the framework still has textures, a font and your scenes' resources to release through it. To end the game, call `rmp::app::quit()`: the frame you are in finishes, and the shutdown above runs.

`BeginDrawing()` and `EndDrawing()` inside a hook

The frame is already open when `_draw()` runs. `EndDrawing()` is what ends it — it submits the batch, swaps the buffers and polls the input — so a pair of your own inside a hook puts a half-drawn frame on the screen and then ends the frame a second time.

`SetTargetFPS()` on the web and on iOS

The browser and the display link pace the frame there, and the framework hands them the loop for that reason. A frame cap is a wait inside their callback, which only stalls it. The cap you want is `[window] fps`, which the framework applies before the window opens on every target where it means something. If you change it at run time — a battery-saving option — keep the call off those two:

```cpp
#if !defined(__EMSCRIPTEN__) && !defined(PLATFORM_IOS)
SetTargetFPS(30);
#endif
```

`__EMSCRIPTEN__` rather than `PLATFORM_WEB`, because the second is only defined by one of the two `[web] backend` choices.

`WindowShouldClose()`

The framework asks it every frame on the targets that have a window to close. On the web it is never called: there it returns `false` and calls `emscripten_sleep()`, which a build without ASYNCIFY — and this one has none — cannot do. To know whether the game is on its way out, ask [`rmp::app::quit_requested()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-app.html#quit_requested).

`exit()`, `std::exit()`, `abort()`

They end the process where it stands: your scenes never get `_end()`, the window is never closed, and on Android the Activity is left behind with its process gone. On iOS an app that ends itself "will appear to the user to have crashed", in Apple's own words (QA1561), and App Review rejects it. `rmp::app::quit()` is the way out everywhere it exists — and on iOS it deliberately does nothing but say, once, `APP: quit() does nothing on iOS — Apple rejects apps that terminate themselves (QA1561). Ignoring.`

A `while` loop that draws

A loading screen that loops on `BeginDrawing()` until the work is done works on a desktop and freezes the tab on the web, where the browser owns the loop. Do the work a slice per frame in `_update()` instead, or in a scene of its own.

## Include what you use

There is no umbrella header. Each header under `include/rmp/` is one part of the framework you can name — `rmp/app.h`, `rmp/scene.h`, `rmp/ui.h`, `rmp/assets.h` and the rest — and a file includes the ones whose names it uses. A file that draws the HUD with `rmp::ui` includes `rmp/ui.h` and recompiles when the interface changes, not when the asset pipeline does; `examples/platform/03_minimal_includes` is one translation unit that includes `rmp/ui.h` and nothing else of ours, compiled on every run to prove that it is enough.

For raylib the same rule reads naturally: include `<raylib.h>` in a file that calls raylib, rather than relying on which `rmp/` header happens to bring it. The other headers you may want are on the include path of every target and are never included for you:

| Header | For |
|---|---|
| `<raymath.h>`, or `rmp/math.h` | Vector and matrix maths, with C++ operators on raylib's own `Vector2`. `rmp/math.h` adds raylib-cpp's maths classes on top. |
| `<rlgl.h>` | raylib's GL layer. See [rlgl and OpenGL](https://omardev29.github.io/rmp-docs/manual/raylib/rlgl.html). |
| `<clay.h>` | The layout engine under `rmp::ui`. See [Dropping to Clay](https://omardev29.github.io/rmp-docs/manual/raylib/clay.html). |
| `<raymob.h>` | Android's extras — vibration, the soft keyboard, the sensors — declared on Android only. See [Android](https://omardev29.github.io/rmp-docs/manual/platforms/android.html). |

The values from `raylib_multiplatform.toml` — `RMP_WINDOW_WIDTH`, `RMP_WINDOW_TITLE` and the rest, listed on the [macros](https://omardev29.github.io/rmp-docs/reference/macros.html#from-the-toml) page — come with every `rmp/` header, so you never include `rmp/config.h` yourself unless the file includes none of them.

## Telling the platforms apart

Most games need no platform branch at all. When one does, the build defines a `PLATFORM_*` macro that names which of raylib's backends it was compiled for:

| Macro | Defined when |
|---|---|
| `PLATFORM_DESKTOP` | Windows, Linux, macOS and the BSDs on the default GLFW backend. |
| `PLATFORM_DESKTOP_WIN32` | `[windows] backend = "win32"`. |
| `PLATFORM_DESKTOP_RGFW` | `[windows] backend` or `[linux] backend` set to `"rgfw"`. |
| `PLATFORM_DRM` | The Linux DRM/KMS targets, which draw with no X11 and no Wayland. |
| `PLATFORM_WEB` | The web, with `[web] backend = "glfw"`. |
| `PLATFORM_WEB_EMSCRIPTEN` | The web, with `[web] backend = "emscripten"`. |
| `PLATFORM_ANDROID` | Android. |
| `PLATFORM_IOS` | iOS. |
| `PLATFORM_MEMORY` | raylib's software renderer, with no window and no GPU: what `rmp test render` and CI's render checks build. |

For the operating system itself, use what the compiler defines: `_WIN32`, `__APPLE__` (macOS and iOS both), `__linux__` (Android too — test `__ANDROID__` first), `__ANDROID__`, `__EMSCRIPTEN__` (both web backends), and `__FreeBSD__`, `__OpenBSD__` and `__NetBSD__`, since the BSDs share `PLATFORM_DESKTOP` with the other desktops. And RMP_PRODUCTION_BUILD, which every build system defines, is `0` or `1` on every target, for code that belongs in a debug build only:

```cpp
#if !RMP_PRODUCTION_BUILD
DrawFPS(10, 10);
#endif
```
