# Assets

> Put your files in resources/ and load them by name; the handles count themselves, free themselves, and read the same from loose files or from an encrypted pack.

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

A game's pictures, sounds, fonts and levels live in one folder, `resources/`, and are loaded by their file name. What comes back is a handle that knows how many copies of it exist and frees what it loaded when the last one goes. The same call reads the loose files while you work and a single packed, encrypted file in a release, and the game never has to know which.

## Loading by name

Everything in [`rmp::assets`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html) takes the name of a file in `resources/`, not a path. A scene loads what it needs in `_ready()`, keeps the handle as a member, and hands it to raylib wherever raylib wants the picture:

```cpp
class TitleScene : public rmp::Scene {
    rmp::Texture _logo;

    void _ready() override { _logo = rmp::assets::load_texture("logo.png"); }
    void _draw() override { DrawTexture(_logo, 40, 40, WHITE); }
};
```

There is nothing to unload. When the scene ends, `_logo` goes with it, and with it the texture — unless something else still holds the same one.

One loader per kind of thing, each returning its own handle:

| Call | Returns | For |
|---|---|---|
| [`load_texture(name)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_texture) | [`rmp::Texture`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Texture) | A picture on the GPU, to draw. |
| [`load_image(name)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_image) | [`rmp::Image`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Image) | A picture in memory, to read pixels from or make a texture out of. |
| [`load_font(name, size)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_font) | [`rmp::Font`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Font) | A `.ttf` or `.otf`, baked at one size, from 1 to 1024. |
| [`load_sound(name)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_sound) | [`rmp::Sound`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Sound) | A short sound, decoded into memory. Most games play sounds by name with [rmp::audio](https://omardev29.github.io/rmp-docs/manual/modules/audio.html) instead. |
| [`load_sheet(name)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_sheet) | [`rmp::SpriteSheet`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SpriteSheet) | An Aseprite file, with its frames and tags. See [Sprites](https://omardev29.github.io/rmp-docs/manual/modules/sprites.html). |
| [`load_map(name)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_map) | [`rmp::Tilemap`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Tilemap.html) | A level made in LDtk or Tiled. See [Tilemaps](https://omardev29.github.io/rmp-docs/manual/modules/tilemaps.html). |
| [`load_data(name)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_data) | `std::vector<unsigned char>` | The bytes of any other file. |

```cpp
rmp::Texture player = rmp::assets::load_texture("player.png");
rmp::Image mask = rmp::assets::load_image("mask.png");
rmp::Font title = rmp::assets::load_font("title.ttf", 48);
rmp::Sound jump = rmp::assets::load_sound("jump.wav");
rmp::SpriteSheet hero = rmp::assets::load_sheet("hero.aseprite");
std::vector<unsigned char> dialogue = rmp::assets::load_data("dialogue.json");
```

### A name is a path below resources/

The name is the file's path inside `resources/`, with `/` between folders — `"jump.wav"` for a file at the top, `"sfx/jump.wav"` for one in a folder `sfx` inside it — never `"resources/jump.wav"` and never an absolute path. The folder where those names are looked up is set by the build, so the same name works however the game was started.

Keep `resources/` in folders by kind — `sprites/`, `maps/`, `audio/`, `fonts/`, `dialogue/`, `locale/` — the way a game of any size wants it. Every target finds the same name: a debug build reads the loose file, the pack a desktop release ships keeps each file under its path below `resources/` (the packer runs inside that folder), and the web, Android and iOS ship the folder whole.

```cpp
rmp::Texture hero = rmp::assets::load_texture("sprites/hero.png");
rmp::Sound jump = rmp::assets::load_sound("audio/jump.wav");
rmp::Tilemap town = rmp::assets::load_map("maps/town.ldtk");
```

A map names its tilesets and its separate level files by a path relative to itself, as LDtk and Tiled write them: `maps/town.ldtk` with a tileset at `../tiles/town.png` loads `tiles/town.png`. When that file is not there, the map's tileset is looked for by its file name at the top of `resources/`, where maps kept their files before there were folders.

### The fonts are baked at a size

A font is turned into a texture of glyphs at the size you ask for, and the size is part of what identifies it: `load_font("ui.ttf", 16)` and `load_font("ui.ttf", 32)` are two fonts, two textures. Ask for the size you draw at.

A font is baked with the letters most languages written in the Latin alphabet need: printable ASCII, Latin-1 (ñ, á, ü, ç, ¿, ¡, £, °), Latin Extended-A (the Polish, Czech, Hungarian, Turkish, Baltic and Croatian letters), and the dashes, curly quotes, bullet, ellipsis, euro and trademark sign a translated line uses. A letter outside that set is drawn as `?` by raylib's text functions. The interface does not have that limit: it bakes every letter its text uses, on demand — see [every letter, on demand](https://omardev29.github.io/rmp-docs/manual/modules/ui-style.html#every-letter-on-demand).

## The handles

`rmp::Texture`, `rmp::Image`, `rmp::Font` and `rmp::Sound` are the same thing for different raylib types: a [counted reference](https://omardev29.github.io/rmp-docs/reference/classes/rmp-detail-Resource.html) to one loaded resource. Each converts to the raylib struct it wraps, so every raylib function that takes a `Texture2D` takes an `rmp::Texture`. `rmp::SpriteSheet` is the same handle around a struct of the framework's own, [`rmp::SheetData`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-SheetData.html) — the texture, the frames and the tags of an `.aseprite` — and converts to that.

### Copies share

Loading a name that is already loaded does not load it again. The name is the key, and the second load hands back the resource the first one made, with one more reference to it. A copy of a handle is one more reference too:

```cpp
rmp::Texture a = rmp::assets::load_texture("enemy.png");
rmp::Texture b = rmp::assets::load_texture("enemy.png"); // the same texture, loaded once
rmp::Texture c = a;                                      // and one more reference to it
```

That is what lets a hundred enemies each hold their picture without anyone deciding who owns it: each holds a handle, the texture is on the GPU once, and it goes when the last enemy does. A move takes the reference over and leaves the moved-from handle empty.

### When they free

A resource is unloaded when the last handle to it goes out of scope — a scene ending, an object being destroyed, a member being assigned something else. Whatever is still held when the game ends is released by the framework before the window closes, because a texture freed after the GPU context is gone is a crash. A handle that outlives that, such as a `static` one, holds nothing from then on: it hands raylib a zeroed struct, which draws nothing, and its own end does nothing. Unlike the empty handles below, it still answers [`valid()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-detail-Resource.html#valid) with `true`.

Since a handle is the owner, there is one mistake the types refuse: keeping the raylib struct and letting the handle go.

Does not compile

```cpp
Texture2D player = rmp::assets::load_texture("player.png");
```

The compiler refuses it, saying among other things: `deleted function`

If that compiled, the handle would be a temporary that died at the end of the line, taking the texture with it, and `player` would be a texture that had already been unloaded. Only a handle that is still alive converts; keep the `rmp::Texture` and pass it to raylib where raylib wants the picture.

Loading in `_ready()` and keeping the handle is the shape to use. Loading in `_draw()` every frame is a search of the loaded resources every frame, and if no other handle holds the resource it is loaded and freed again each time.

### Handles that are empty

A handle can hold nothing: one that was never assigned, one that was moved from, and one whose load failed. [`valid()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-detail-Resource.html#valid) says which, and so does the handle in an `if`:

```cpp
rmp::Texture logo = rmp::assets::load_texture("logo.png");
if (!logo) TraceLog(LOG_WARNING, "no logo: the title is drawn as text");
```

An empty handle converts to a zeroed raylib struct, and raylib draws nothing for one of those. A missing picture is a hole in the frame, not a crash.

### What is not a handle

The handles come from the loaders. A texture you make yourself with raylib — `LoadTextureFromImage()`, `LoadRenderTexture()`, a model from `LoadModel()` — is a plain raylib struct, under raylib's own rule: what you load, you unload. The types `rmp::Music`, `rmp::Shader` and `rmp::RenderTexture` exist, but nothing in `rmp::assets` returns one; music is played by name through [rmp::audio](https://omardev29.github.io/rmp-docs/manual/modules/audio.html).

An `rmp::Image` is read through its handle and not changed: the image belongs to every handle that loaded the same name. Making a texture from it is the usual use.

## Any other file: load_data

A level in your own format, a shader's source, a JSON table of items: `load_data()` returns the bytes, as a vector that frees itself. It is not cached, so each call reads the file again, and it is empty when the file is not there.

```cpp
const std::vector<unsigned char> bytes = rmp::assets::load_data("intro.txt");
const std::string intro(bytes.begin(), bytes.end());
```

It reads from the pack in a release exactly as the other loaders do. The Aseprite and map loaders are built on it.

## JSON: load_value

Tuning numbers, a table of items, the lines of a dialogue: a JSON file in `resources/` is read into an [`rmp::Value`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html), the same value a [save](https://omardev29.github.io/rmp-docs/manual/modules/saving.html) is, with [`load_value()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_value). It reads from the pack in a release like every loader, and a file that is not there, or is not JSON, is an empty Value and a failed load, said once.

```cpp
const rmp::Value tuning = rmp::assets::load_value("tuning.json");
const float speed = tuning["player"]["speed"].as_float(200);
const int lives = tuning["lives"].as_int(3);
```

## Hot reload

A Value read with `load_value()` remembers its file, and so does a texture read with `load_texture()`. While a Debug build runs on the desktop, [`changed()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html#changed) says when that file is different from what was read — a number tuned, a sprite redrawn and saved in any editor — and [`reload()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html#reload) reads it again. Nothing is read behind the game's back: the game asks, and decides when.

```cpp
rmp::Value tuning = rmp::assets::load_value("tuning.json");
rmp::Texture hero = rmp::assets::load_texture("hero.png");
float speed = 200;

void _update(float) override {
    if (tuning.changed()) tuning.reload();
    if (hero.changed()) hero.reload();
    speed = tuning["speed"].as_float(200);
}
```

- **`changed()` is for every frame.** It compares two numbers. The files themselves are looked at by the framework at most four times a second, the first time something asks after a quarter of a second has gone by: their size and modification time, and their bytes only when one of those moved — so an editor that saves the same contents again is no change.
- **`reload()` of a texture is in place.** The texture is loaded again into the slot every handle to it shares, so the copies a sprite or a picture made draw the new one too, and `changed()` is false again for all of them. A Value reloads only itself: two Values read from one file are two copies, and each reloads on its own.
- **A file half saved is not a change made.** A file that is not JSON now, or a picture that does not decode, leaves the Value or the texture as it was; `reload()` returns `false` and `changed()` stays true, so the next frame tries again. A file that is gone for a moment — an editor writes a new one and renames it over the old — is no change until it is back.
- **Only the desktop's Debug build watches.** A release reads its resources from the pack, which does not change; a phone and a browser have no file anyone edits. There, and with the pack open in a Debug build (`rmp pack`), `changed()` is always `false` and costs nothing.
- **What has no file never changes.** A Value made in code, read from a save, or taken out of another — `tuning["enemy"]` copied into a Value of its own — answers `false`, and its `reload()` does nothing. A copy of the whole Value is the same file.

Only JSON and textures reload. Code does not: a change to the C++ is a new build.

## Plain raylib works too

The `rmp::assets` functions are the short way, not the only one. When a pack is in use, raylib's own file loading is routed through it, so a raylib loader given a path inside `resources/` finds the packed copy:

```cpp
Model ship = LoadModel(RMP_RESOURCES_PATH "ship.obj"); // in a release, read from the pack
// ... and raylib's rule for what raylib loads:
UnloadModel(ship);
```

That covers models and the files they pull in behind your back — an `.obj`'s `.mtl` and the textures it names — as well as shaders, fonts and textures loaded through raylib directly. The path has to start with the macro the build defines for the folder, RMP_RESOURCES_PATH: only files inside it are answered from the pack, by their path below it, so a save file elsewhere on disk that happens to share a name with a packed one is never mistaken for it.

The exceptionraylib's `LoadMusicStream()` opens the file itself and never sees the pack, so a song loaded that way exists only as a loose file. `rmp::audio::music("song")` reads it through the pack like everything else.

## Where resources/ is

The build tells the game where its folder is. A debug build reads the absolute path of the project's `resources/`, so the game finds its files whichever folder it is started from. A release build (`rmp build release`) reads `./resources/`, which is where the released archive puts the folder, beside the executable.

On Windows, macOS, Linux and the BSDs, the entry point makes that relative path start at the executable's folder: before anything is loaded, a release moves into the folder its executable is in, when there is a `resources/` there. A player can start it from a file manager, a shortcut or any terminal folder, and it finds its files — and so does a plain raylib load of an `RMP_RESOURCES_PATH` path, because the path stays relative and only the folder it is relative to changes. The log says where it read from: `ASSETS: reading … next to the executable, in …`. CI starts a release from another folder to prove it.

Two cases keep the folder the game was started from. A release built in the source tree and started from its root — the executable in `build/release/`, with no `resources/` beside it — reads the root's. And NetBSD and OpenBSD do not tell a program where its executable is, so there a release reads `resources/` from the folder it was started in, and says so: `ASSETS: this system does not say where the executable is, so … is read from the working directory, …` The web reads its files from the page, Android from the APK, and iOS moves into the app's bundle the same way.

An example with a `resources/` of its own reads that one and not the game's; one without reads the game's.

## The pack

A release does not have to ship a folder of loose files. `rmp pack` bundles every file below `resources/`, its folders too, into one file, `resources.rres`, inside that same folder, encrypted. The container format is [rres](https://github.com/raysan5/rres), by raylib's author, and the packer is part of the framework, so nothing has to be installed or bought.

```shell
rmp pack      # resources/ into resources/resources.rres
rmp run       # the game now reads the pack
rmp unpack    # delete the pack: the game reads the loose files again
```

At startup the framework looks for `resources.rres` in the game's resources folder. If it is there, every load asks the pack first; if not, the loose files are read. The log says which:

- `ASSETS: Using resource pack … (… entries)`
- `ASSETS: No resource pack found, using loose files from …`
- `ASSETS: … is cut short or corrupt, using loose files`

[`rmp::assets::using_pack()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#using_pack) answers the same question in code. There is no call to make for any of it: the entry point calls [`rmp::assets::init()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#init) before the first scene, which finds the pack, and [`rmp::assets::shutdown()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#shutdown) after the last.

### The loose files are the fallback

A name the pack does not have — a picture you added after packing — is read from the loose files instead, with a warning such as `ASSETS: '…' not found in pack, falling back to loose
file`. In development that means you can keep working after `rmp pack` without packing again. In a release that ships only the pack there is no loose file to fall back to, and the load fails, which is what the warning is there to tell you before a player does.

### Which releases ship it

The release jobs build the pack where the packer can run on the machine that builds the game:

| Release | What its resources/ holds |
|---|---|
| Linux on glibc (x64, arm64, and both DRM builds), Windows x64, macOS, FreeBSD, OpenBSD, NetBSD | `resources.rres` alone. |
| `linux-x64-musl`, `linux-riscv64-glibc`, `windows-arm64` | The loose files, and a `PACK_SKIPPED.txt` saying why: these are cross-compiled, and the packer they build cannot run on the machine that builds them. |
| Web, iOS | The loose files: the folder is preloaded into the page, or copied into the app. |
| Android | The loose files, copied into the APK's assets. A `resources.rres` is left out of the APK, because the pack cannot be opened from inside one. |

A game in plain C, made the way `examples/plain_c` says — no `src/rmp/` — has no framework to read a pack with, so the releases that would ship the pack ship the loose files instead, with a `PACK_SKIPPED.txt` saying why.

The game does not care: a folder with a pack is read through the pack, and one without is read file by file.

### The password

The pack is encrypted with AES, under a key derived from one password, which is set in the `.toml` and reaches both the packer and the game from there:

```toml
[resources]
rres_password = "a phrase of your own"
```

An empty password is refused, because it would ship a pack encrypted with a key of nothing rather than one with no encryption:

```toml
[resources]
rres_password = ""
```

```text
  raylib_multiplatform.toml:2: rres_password = ""
                               ^^^^^^^^^^^^^^^^^^

  configure: error: [resources] rres_password is empty, which ships the pack with an AES key of nothing rather than with no encryption.
                    Put a passphrase here. There is no setting for an unencrypted pack: every release job that builds resources.rres encrypts it with this value. A game reads loose files only where no pack was built -- a development build, or an archive whose resources/ holds PACK_SKIPPED.txt.
```

Each file read from the pack derives its key again (Argon2i, 16 MiB, three passes). That is a cost paid when something loads, never while the game runs.

What the encryption is for It stops someone opening your game's folder and copying the art out of it. It does not stop someone determined: the password is inside the executable you give them, the names of the files in the pack are stored in the clear, and on the releases that ship loose files there is no encryption at all. It is a lock on the door, not a safe.

`rmp new` writes a random password into the new game's `.toml`, so each game has one of its own. Leave the key out and the framework's default applies, which is the same for everyone.

## When a load fails

A load that finds nothing returns an empty handle, and the game keeps running. A missing texture is a gap on the screen; a missing font, sound or sheet is empty in the same way. Nothing throws: no call into the framework throws to the game's code.

The failure is still counted. [`requested_loads()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#requested_loads) is how many loads the game asked for, and [`failed_loads()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#failed_loads) how many of them failed, which means:

- a name that is neither in the pack nor in the folder;
- a file that is there and does not decode: a picture, and so a texture made from it, a sound, a font, or a song that [rmp::audio](https://omardev29.github.io/rmp-docs/manual/modules/audio.html) cannot stream;
- an `.aseprite` file that cannot be read as one;
- a map file that does not parse, or a Tiled map whose tileset file cannot be read.

A file that does not decode — a broken PNG, a download cut short — is an empty handle, and the log names it once: `ASSETS: '…' is in … but could not be decoded`. A font is no exception. raylib answers a font file it cannot read with its own built-in font, and the framework does not hand that back as yours, or keep it under your file's name: the handle is empty, and nothing is cached. No failed load is cached, so a file fixed while the game runs is found by the next load.

A picture that decoded and could not reach the GPU, because there is no window, is not counted: that is every headless test, not a broken file.

### How CI notices

When the game boots under the smoke test, the framework prints one line once the ready hook has returned — in a game of scenes, once the first scene is ready:

`RAY_TEST_BOOT_OK assets_failed=0 …`

`rmp test smoke` on your machine requires `assets_failed=0`, and so does every CI job that boots the game and reads its log — the desktops, the BSDs, the web, the release started from another folder — and the boot of every example. A game that asks for a file it does not ship, or ships one that does not decode, fails the build instead of reaching a player as a blank sprite.

iOS and Android are not counted. iOS is not booted: the hosted simulator does not come up, so that step runs only when the repository variable `IOS_SIMULATOR_TEST` is `true`. Android's test runs in Firebase Test Lab and has no log to read, so it shows that the game starts, not this count.

The count covers what is loaded by then: the first scene's `_ready()`, or a game's own `on_ready`. A file loaded later, by a level the boot never reaches, is outside it.
