# Architecture

> The five layers of a game made with raylib_multiplatform — your code, the modules, the core, raylib, and the targets — and what each one is responsible for.

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

A game made with this framework is five layers deep. At the top is the code you write; under it, the modules you choose to use; under those, a small core that runs the frame; under that, raylib; and at the bottom, the 17 targets the same source is built for. Each layer only reaches down, never up.

Each layer is built on the one under it. A box with a link goes to its page.

The rest of this page takes the layers one at a time, from the top.

## Your game

What a game is made of is deliberately small. The entry point is one line, the screens are scenes, the things in them are objects, the art is in a folder, and every setting is in one file.

### The entry point

The whole of `src/main.cpp`, as the framework ships it and as [`rmp new`](https://omardev29.github.io/rmp-docs/reference/commands.html#new) copies it into a new game:

[src/main.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/src/main.cpp#L1-L13)

```cpp
// Your game starts here, and this is all of it.
//
// RMP_GAME opens the window from [window] in raylib_multiplatform.toml, enters
// the scene you name, runs it every frame, and shuts everything down on the way
// out — on every target, including the two where main() is not a main() at
// all. Your work is in src/scenes/ and src/objects/.

#include <rmp/app.h>

#include "scenes/main_menu.h"

RMP_GAME(MainMenuScene);
```

[`RMP_GAME`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_GAME) opens the window from `[window]` in `raylib_multiplatform.toml`, enters the scene you name, runs the scene stack every frame and closes everything on the way out. There is no `InitWindow()` to write and no loop: those lines were the same in every game, and on the web and on iOS the loop is not even yours to write — the browser and UIKit own it.

The first scene is named in code and not in the `.toml` on purpose. Choosing a scene by name means a table from names to types, and in C++ that table is built by objects that register themselves when the program starts. A linker building a static library or an iOS framework throws away any object file nothing refers to, and a self-registering object is exactly that: the game would build, run on a desktop and find no scenes on a phone. So the type is named where the compiler can see it.

### Scenes, objects and resources

A scene is one screen — a menu, a level, a pause overlay — and an object is anything inside one: the player, a bullet, a coin. Both are classes you derive from [`rmp::Scene`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html) and [`rmp::Object`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html), one per file, under `src/`. A game made by `rmp new` starts with one, `src/scenes/main_menu.cpp`.

Every build system collects `src/` recursively, so a new file or a new folder needs no change to CMake, Gradle or Xcode.

The art, the sounds and the levels go in `resources/` and are loaded by name: `rmp::assets::load_texture("player.png")`. While you develop they are read from that folder; a release packs them into one `resources.rres` file, optionally encrypted, and the same call reads them from there ([`rmp pack`](https://omardev29.github.io/rmp-docs/reference/commands.html#pack) does the packing by hand).

### One configuration file

Everything a build system needs to know about the game — its name, its window, its identifiers on the stores, the targets it is built for — is in `raylib_multiplatform.toml`. It is the only file of that kind you edit: `tools/configure.py` turns it into what CMake, Gradle and Xcode read, so the three cannot disagree with it. [The config pipeline](https://omardev29.github.io/rmp-docs/framework/config-pipeline.html) follows it all the way through, and [Configuration](https://omardev29.github.io/rmp-docs/reference/configuration.html) lists every key.

## The modules

Everything the framework adds to raylib is under `rmp::`, one header per module. There is no header that includes them all: a file that draws the HUD includes `rmp/ui.h` and knows nothing of saving or sound. [Why it is split this way](https://omardev29.github.io/rmp-docs/framework/why/one-header-per-module.html) has the reasons and the measurements.

| Header | What it gives you | Built on |
|---|---|---|
| `rmp/ui.h` | [`rmp::ui`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html): menus, buttons, layout, themes, in screen space | Clay, the layout engine |
| `rmp/input.h` | [`rmp::input`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html): named actions bound to keys, buttons and sticks, and the pointer | raylib's input, sampled once a frame |
| `rmp/assets.h` | [`rmp::assets`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html) and [`rmp::Texture`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Texture) and its siblings: loading by name, released by themselves | rres for the pack, cute_aseprite for sprite sheets |
| `rmp/tilemap.h` | [`rmp::Tilemap`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Tilemap.html): a level made in LDtk or Tiled | cJSON for LDtk, cute_tiled for Tiled |
| `rmp/audio.h` | [`rmp::audio`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html): sounds and music by name, three volume buses | raylib's raudio |
| `rmp/save.h` | [`rmp::save`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-save.html) and [`rmp::Value`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html): save slots in the platform's own folder | cJSON, and Monocypher to seal a file |
| `rmp/random.h` | [`rmp::random`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html): one seeded generator you can replay | xoshiro128++, written out |
| `rmp/ads.h` | [`rmp::ads`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html): interstitial and rewarded ads | raymob's AdMob bridge on Android, once `[android.admob] enabled` turns it on; nothing anywhere else |
| `rmp/math.h` | [arithmetic](https://omardev29.github.io/rmp-docs/manual/basics/headers.html#rmp-math-h): `+`, `-` and `*` on raylib's own vectors, and raylib-cpp's math classes for those who want methods | raylib's raymath, and the math headers of raylib-cpp |
| `rmp/behavior.h` | [`rmp::behavior`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-behavior.html): the catalogue — movement, health, timers, spawners | the core's behavior engine |
| `rmp/scene.h` | [`rmp::Scene`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html) and [`rmp::Camera`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html) | the core |
| `rmp/object.h` | [`rmp::Object`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html), [`rmp::Handle`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Handle.html), [`rmp::Callback`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Callback.html) | the core |
| `rmp/app.h` | [`RMP_GAME`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_GAME), [`rmp::app::quit()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-app.html#quit) and [`rmp::global`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#global) | the core |

Opt-in means two things here, and it is worth being exact about both.

**You include what you use.** Each header is a module you can take or leave, and none of them is an umbrella for the others. A few do bring another one with them, and say why at the top: `rmp/scene.h` includes `rmp/object.h` and `rmp/tilemap.h`, because a scene holds its map by value and spawns objects; `rmp/object.h` includes `rmp/assets.h`, because a sprite holds a texture by value. Every public header also includes `rmp/config.h`, the values from the `.toml`, so you never type that one.

**You pay for what you use.** A camera and a map are fields of every scene: a map nobody loads draws nothing and stops nothing, and a camera nobody touches shows the world at the design resolution as it is. The UI starts Clay on the first `rmp::ui::begin()` of the game, and the sound device opens on the first call that needs it, so a game with no menus has no UI arena and a game with no sound never opens the device.

None of it walls raylib off. An `rmp::Texture` converts to the `Texture2D` it wraps, so every raylib function that takes one takes it; a scene can open the camera and draw with raylib directly; and [a game in plain C](https://omardev29.github.io/rmp-docs/examples/plain_c.html) can skip the framework altogether and still be built for every target but the two where the loop is not the game's to write, the web and iOS.

## The core

Under the modules is the part every game uses, whether it includes a module or not: the code that runs a frame. It is five pieces, and their order within one frame is the subject of [One frame](https://omardev29.github.io/rmp-docs/framework/the-frame.html).

The app and the frame loop

[`RMP_ENTRY_POINT`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_ENTRY_POINT) writes the program's entry point for the platform it is compiled for, because there are three shapes of one. On Windows, Linux, macOS, the BSDs and Android it is a `main()` with a loop. On the web the browser owns the loop and calls a function back once a frame, through `emscripten_set_main_loop()`. On iOS UIKit owns it, and the program is three callbacks. All three do the same things in the same order around your code, and [`RMP_GAME`](https://omardev29.github.io/rmp-docs/reference/macros.html#RMP_GAME) is that macro with the three hooks written for you.

The scene stack

A vector of owning pointers, and a queue of the transitions asked for during the frame. [`change`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#change), [`push`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#push), [`replace`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#replace) and [`pop`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#pop) only record; the stack changes when the frame is over, because the call usually comes from inside the scene that is about to go.

The object storage

Every object of every scene lives in one pool of slots. A slot owns its object through a `std::unique_ptr` and carries a generation number. A [`rmp::Handle`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Handle.html) is the slot's index and that generation, so a handle to an object that died stays dead even when the slot holds something new. Destruction is deferred to the end of the frame, which is what makes the reference [`spawn()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#spawn) returns good for the rest of it.

Collision and the map

Arcade collision, written for the framework: rectangles and circles, a uniform grid so only near pairs are tested, a swept test so a fast object cannot pass through a thin one between two frames, and separation for the pairs that are both [`solid`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#solid). A solid object that is not [`immovable`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#immovable) is stopped by the map's solid cells one axis at a time, so it runs along a floor of tiles without catching on the seams. It is not a physics engine, by decision: a rigid-body library would make `position` a mirror of somebody else's body, and a Pong ball would need a density and a friction to bounce.

The behavior engine

A behavior is a struct with an `_update(rmp::Object &, float)`, or a `_late_update` with the same signature when its whole job is to correct where the object ended up, as [`GridSnap`](https://omardev29.github.io/rmp-docs/reference/behaviors/GridSnap.html) does, and no virtual functions, so that it stays an aggregate. The engine finds its optional hooks at compile time with `requires`, keeps one table of function pointers per type, and owns the struct through a `std::shared_ptr<void>` that carries its destructor. A behavior of yours and one of the catalogue's are the same kind of thing; there is no base class and nothing to register.

In a CMake build — the desktops, the BSDs and the web — the framework is compiled once into a static library called `rmp`, and the game and every example written in C++ link it. The one file kept out of the library is `src/rmp/app/app.cpp`: each executable compiles its own copy, because it refers to the symbol the entry-point macro defines, and that reference is what turns a program with no `RMP_GAME` into a link error that names what is missing. The Android build and the iOS project compile `src/` as a whole into the app, framework and game together.

The library is built only when `src/rmp/` has sources. A game in plain C deletes that folder because nothing in it is used: a game with no C++ of its own in `src/`, outside `src/rmp/`, is linked to raylib alone, so a `src/rmp/` left behind would be the whole framework compiled for nothing in a CMake build, and compiled into the game on Android and iOS, which take everything under `src/`. With it gone the build has no framework library at all — only raylib, the game's own C, and the headers that are plain C.

Those headers are on a plain C game's include path on every target: `rmp/config.h` for the values of the `.toml`, `tests/smoke_test.h` for the CI's hooks, and `<admob.h>` from `thirdparty/raymob` for the ads as C functions, which do nothing anywhere but Android.

## raylib 6.2, modified

Under the core is [raylib](https://www.raylib.com/) 6.2, vendored in `thirdparty/raylib`. The copy is modified, and says so: the zlib licence requires an altered copy to be plainly marked, and [its `PATCHES.md`](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/thirdparty/raylib/PATCHES.md) lists every change. It is raylib 6.2 as raylib_cmake_template carries it — with raylib's own fixes from after 6.2 and the gaps its security audit left closed — with the framework's patches on top. Most of those are build-system and platform-selection patches, so the windowing backend can be chosen from the `.toml`; one makes Android's EGL ask for the OpenGL ES version raylib was compiled for, and the DRM backend's card probe, Escape on its terminal and its flow control are fixed. Among the libraries raylib bundles, three files are changed: the software renderer's nearest-texel lookup is clamped to the texture, a fix of the framework's own that upstream does not have, so the far edge of a texture reads its last texel and not the one past it; the OBJ loader takes raylib's face fix from after 6.2; and RGFW's gamepad code builds on older kernel headers. [Licences](https://omardev29.github.io/rmp-docs/framework/licences.html) has the rest.

The iOS app is the one target that does not build this copy. It links a `raylib.xcframework` built from a fork, `omardev29/raylib-iOS`, pinned at tag `6.0.3-iOS` as the submodule `thirdparty/raylib-ios`, so the changes `PATCHES.md` lists are what every other target runs, not what iOS runs.

raylib is built in modules, and the framework leans on most of them:

| Module | What it does | Can a game leave it out? |
|---|---|---|
| `rcore` | the window, input, timing | No: raylib requires it |
| `rlgl` | the OpenGL layer under every draw call | No: raylib requires it |
| `rshapes` | rectangles, circles, lines | No: `rmp::ui` draws every panel and button with it |
| `rtextures` | images and textures | No: the asset layer and the CI render check are built on it |
| `rtext` | fonts and text | No: the rres loader calls into it |
| `raudio` | sound and music | Yes |
| `rmodels` | 3D models and meshes | Yes |

Leaving one out is [`[raylib]
disabled_modules`](https://omardev29.github.io/rmp-docs/reference/configuration.html#raylib-disabled_modules), and `configure.py` refuses the modules in the third column that say no, with the reason, before anything compiles. The iOS build links a prebuilt raylib and ignores the setting.

```toml
[raylib]
disabled_modules = ["rshapes"]
```

```text
  raylib_multiplatform.toml:2: disabled_modules = ["rshapes"]
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  configure: error: [raylib] disabled_modules: 'rshapes' cannot be disabled — rmp::ui draws every panel, button and border with it — DrawRectangleRounded and three others — and the UI is always compiled in, so there is nothing left to link against.
```

## The targets

At the bottom are the platforms the same source is built for: 17 targets in 6 families. A family is the set of targets one CI workflow builds.

| Target | What it is | Family | Groups it is in |
|---|---|---|---|
| `linux-x64-glibc` | Linux x86-64 | linux | `all`, `desktop`, `linux` |
| `linux-arm64-glibc` | Linux ARM64 | linux | `all`, `desktop`, `linux` |
| `linux-riscv64-glibc` | Linux RISC-V | linux | `all`, `desktop`, `linux` |
| `linux-x64-musl` | Linux x86-64 (musl / Alpine) | linux | `all`, `linux` |
| `windows-x64` | Windows x86-64 | windows | `all`, `desktop`, `windows` |
| `windows-arm64` | Windows ARM64 | windows | `all`, `desktop`, `windows` |
| `macos` | macOS (universal) | apple | `all`, `apple`, `desktop` |
| `ios` | iOS (simulator) | apple | `all`, `apple`, `mobile` |
| `web` | Web (WebAssembly) | web | `all`, `web` |
| `android` | Android | android | `all`, `android`, `mobile` |
| `freebsd-x64` | FreeBSD x86-64 | bsd | `all`, `bsd` |
| `freebsd-arm64` | FreeBSD ARM64 | bsd | `all`, `bsd` |
| `openbsd-x64` | OpenBSD x86-64 | bsd | `all`, `bsd` |
| `openbsd-arm64` | OpenBSD ARM64 | bsd | `all`, `bsd` |
| `netbsd-x64` | NetBSD x86-64 | bsd | `all`, `bsd` |
| `linux-x64-glibc-drm` | Linux DRM/KMS (no X11) | linux | `all`, `drm`, `linux` |
| `linux-arm64-glibc-drm` | Linux ARM64 DRM/KMS (no X11) | linux | `all`, `drm`, `linux` |

A target's name is `<os>-<arch>-<libc>-<env>`, and each part is there only when it can differ. `linux-x64-glibc` and `linux-x64-musl` name their C library because a binary built for one does not start on a system with the other: musl and glibc are two libraries, not two versions of one. The last part is the windowing system — `drm` draws straight to the screen with no X11 and no Wayland — and is absent when the binary carries every backend and picks one when it starts. `windows-x64` and `macos` stop early because Windows has one C library and the macOS build is universal.

Each family has its own build system underneath, all of them fed from the same `.toml`: CMake for the desktops, the BSDs and the web, Gradle for Android, and an Xcode project generated by XcodeGen for iOS. A push to the framework builds Linux x64, the web, Android and Windows x64; a release tag builds every target, boots the ones a runner can boot, and checks that each one drew — [Testing](https://omardev29.github.io/rmp-docs/framework/testing.html) says how.
