# Sprites

> What an object looks like: a texture, or an Aseprite file whose tags are the animations you play by name.

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

An object's [`sprite`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#sprite) is its picture. It is either a plain texture or a sheet read straight out of an Aseprite file, with the frames, the time each one shows for and the named animations you drew. You save in Aseprite, and the game has the new animation.

## A picture

Give the sprite a texture and the object draws it, centred on its position and turned, scaled and flipped with it:

```cpp
sprite.texture = rmp::assets::load_texture("ship.png");
```

An object with a sprite draws the sprite; one without draws its [`shape`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#shape). The rest of [`rmp::Sprite`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html) says how the picture is put on the screen:

| Field | Default | Means |
|---|---|---|
| [`texture`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#texture) | empty | A loose picture, for an object that does not animate. |
| [`sheet`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#sheet) | empty | An Aseprite sheet. When there is one, it is drawn and `texture` is not. |
| [`source`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#source) | `{0,0,0,0}` | The part of `texture` to draw; all of it when empty. A sheet chooses its own frame and ignores this. |
| [`origin`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#origin) | `{0.5, 0.5}` | Which point of the picture sits on the object's position, as a fraction of its size: the centre by default, `{0.5, 1}` for the middle of the bottom edge. Rotation turns about it too. |
| [`size`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#size) | `{0,0}` | The size to draw at, before the object's `scale`; the picture's own size when empty. |
| [`tint`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#tint) | `WHITE` | Multiplies the picture's colours. White leaves them alone; its alpha fades the picture. |
| [`speed`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#speed) | `1` | How fast a sheet animates: 2 is twice as fast, 0 is frozen, -1 runs backwards. |

A handful of the object's own fields act on the picture as well: [`scale`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#scale) multiplies its size, [`rotation`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#rotation) turns it, [`flip_x`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#flip_x) and [`flip_y`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#flip_y) mirror it, [`visible`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#visible) hides it without taking the object out of the game, and [`layer`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#layer) decides what is drawn on top of what.

### Flipping and tinting

Drawing a character twice, once facing each way, is unnecessary: flip it. Flipping mirrors the picture in place and costs nothing. Only the picture: a collider moved sideways with an `offset` stays on the side it was put.

```cpp
if (velocity.x < 0) flip_x = true;
if (velocity.x > 0) flip_x = false;
```

The tint is a colour every pixel of the picture is multiplied by. A flash of red when hit, a ghost at half opacity:

```cpp
sprite.tint = RED;                 // the hit flash
sprite.tint = Fade(WHITE, 0.5f);   // half transparent, colours unchanged
sprite.tint = WHITE;               // back to the picture as drawn
```

### One frame of a bigger picture

A texture that holds many pictures — a sheet of tiles, a set of icons — is drawn one part at a time through `source`, in the texture's pixels:

```cpp
sprite.texture = rmp::assets::load_texture("items.png");
sprite.source = Rectangle{ 32, 0, 16, 16 }; // the third 16-pixel icon of the first row
```

For anything that animates, use a sheet instead: it knows its own frames and how long each one lasts.

## An animated sheet

[`rmp::assets::load_sheet()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_sheet) reads an `.aseprite` or `.ase` file, and [`play()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#play) starts one of its animations by name:

```cpp
sprite.sheet = rmp::assets::load_sheet("player.aseprite");
sprite.play("idle");
```

The names are yours. They are the *tags* in your Aseprite file: the framework does not know or expect an `idle` or a `walk`, and `play("idle")` works because the file has a tag called that. A tag is a run of frames with a name, and each frame shows for the time you gave it in Aseprite, so the animation plays at the speed you drew it at. The name is a `std::string_view`, so a literal, a `std::string` or a view of one goes in as it is.

### Choosing the animation every frame

`play()` starts a tag only when it is not already playing it. That makes the natural way to write it — say, every frame, which animation the object should be showing — the right one: the walk is not restarted sixty times a second while the player walks.

```cpp
class Player : public rmp::Object {
    void _ready() override {
        sprite.sheet = rmp::assets::load_sheet("player.aseprite");
        sprite.play("idle");
    }

    void _update(float delta) override {
        if (velocity.y != 0) {
            sprite.play("jump");
        } else if (velocity.x != 0) {
            sprite.play("walk");
        } else {
            sprite.play("idle");
        }
    }
};
```

The platformer in [examples/games/07_platformer](https://omardev29.github.io/rmp-docs/examples/games/07_platformer.html) does exactly this for its player, with a `hurt` tag on top and the art turned to face the way it moves: [player.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/games/07_platformer/src/objects/player.cpp#L64-L76).

### Once, and then stop

An animation plays in a loop unless you say otherwise. With `loop` false it stops *on* its last frame — not after it, so a death animation does not vanish into a blank frame — and [`finished()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#finished) turns true:

```cpp
class Enemy : public rmp::Object {
public:
    void die() {
        sprite.play("die", false);
        _dying = true;
    }

private:
    void _update(float delta) override {
        if (_dying && sprite.finished()) destroy();
    }

    bool _dying = false;
};
```

A tag that has finished is no longer "already playing", so asking for it again starts it again. A one-shot animation is asked for once, when the thing it shows happens, and not every frame.

### The rest of the clock

[`playing()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#playing)

The name of the tag last started with `play()`, or `""` before any has been. It keeps naming that tag after `stop()` and after a tag that does not loop has finished; `finished()` is what says the clock has stopped. It is a `std::string_view` of the sheet's own copy of the name, good for as long as the sheet is loaded.

[`stop()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#stop)

Holds the current frame, as a finished animation does.

[`frame_index()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#frame_index) and [`set_frame(i)`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#set_frame)

The frame showing, as an index into the whole sheet, and a way to put it somewhere. A sheet nobody called `play()` on shows its first frame and stays there, so a sheet used as a set of still pictures — a door closed and open, an icon per item — is driven with `set_frame()` alone.

[`speed`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html#speed)

Multiplies how fast the frames go by: 2 shows each for half its time. Negative plays the tag backwards.

The clock moves in the update pass, after the object has moved, and not when it is drawn. A scene that is still updated under a pause menu keeps animating; one frozen under it holds its frames, and picks up where it was when it resumes. A long frame — the game hitched for a moment — advances as many animation frames as the time covered, so an animation keeps time on a slow machine instead of slowing down with it.

## The Aseprite workflow

[Aseprite](https://www.aseprite.org/) files are read directly; there is no export step and no JSON beside the picture. The whole of the workflow is:

1. Draw the frames on one canvas. The canvas is the size of every frame.
2. Set each frame's duration (Frame Properties). That time, in milliseconds, is how long the frame shows in the game.
3. Tag each animation: select its frames and add a tag with the name you will pass to `play()`. The tag's direction is honoured, all four of Aseprite's: Forward, Reverse, Ping-pong, which plays to the end and back, and Ping-pong Reverse, which starts at the end.
4. Save the `.aseprite` into `resources/`.

Every visible layer is blended into the frame, the way Aseprite shows it with the Normal blend mode; a hidden layer is left out, so a sketch or a reference layer can stay in the file. Other blend modes are not reproduced — merge such a layer down in Aseprite if it matters. RGBA, grayscale and indexed sprites all load. A tag's name is kept whole, however long it is: the [`name`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SheetTag-name) of each [`rmp::SheetTag`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SheetTag) is a `std::string`.

All the frames are packed side by side into one texture, so a hundred enemies drawn from the same sheet are one texture and one name in the resource table: loading `"bat.aseprite"` for each of them loads it once. Each object's sprite still has its own clock, so they do not flap in step unless they started together. The packed texture is as wide as all the frames together; a GPU has a largest texture it accepts, which a long animation of large frames can reach.

### Slices

An artist marks parts of the canvas with Aseprite's slice tool and names them: the icons of an atlas, the frame of a button. They are read with the pictures, into the sheet's [`slices`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-SheetData.html#slices), in the file's order, up to 256 of them. Each [`rmp::SheetSlice`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SheetSlice) has its [`name`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SheetSlice-name); its [`bounds`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SheetSlice-bounds), in pixels of the packed texture, already moved to where its frame is; and, when "9-Slices" is ticked for it in Aseprite, [`nine`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SheetSlice-nine) set and its middle in [`center`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#SheetSlice-center), relative to the bounds. [`slice(name)`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-SheetData.html#slice) finds one — the first, when two share a name — or an empty one, and [`slice_count()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-SheetData.html#slice_count) says how many there are.

```cpp
const rmp::SpriteSheet skin = rmp::assets::load_sheet("skin.aseprite");
const rmp::SheetSlice &coin = skin.raw().slice("coin");
const Rectangle where = coin.bounds;
(void)where;
```

A slice is read where its first key is: a slice that moves from frame to frame is where it starts. One that does not lie inside the canvas, or does not read whole, is left out, and the pictures load all the same: a slice is never a reason to refuse a file. The interface takes slices by name as pictures and nine-slices: [slices of an Aseprite file](https://omardev29.github.io/rmp-docs/manual/modules/ui-style.html#slices-of-an-aseprite-file).

### Directions from movement

The [`rmp::behavior::TopDown`](https://omardev29.github.io/rmp-docs/reference/behaviors/TopDown.html) behavior chooses the tag for you from the direction the object faces: it plays `walk_e`, `walk_n` and the rest when the sheet has them, and `walk` with `flip_x` when it does not, so a sheet with two, four or eight directions works without configuration. Its tag names are fields you can change.

Draw the frames that have no direction in their name — `walk`, `idle` — facing right: the behavior mirrors them while the object faces left. A tag with a direction is drawn for that direction and played unflipped, so `walk_w` shows exactly as you drew it. [TopDown](https://omardev29.github.io/rmp-docs/manual/core/behaviors.html#topdown) has the rest.

## Collision and the picture

An object with no `shape` and no [`collider`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#collider) collides as its sprite: the rectangle the current frame covers. That is right until the picture includes air, hair or a cape, which is most characters, and then a smaller collider is one line:

```cpp
sprite.sheet = rmp::assets::load_sheet("player.aseprite");
collider = rmp::rect({ 12, 20 });   // the body, not the whole 24-pixel frame
collider.offset = { 0, 2 };         // standing on the bottom edge of the frame
```

## When something is wrong

A tag that is not in the sheet warns once and changes nothing — the frame already showing stays, rather than the sprite going blank, and the console does not fill at sixty lines a second:

`SPRITE: no animation tag "jmup" in this sheet. It has 4: the names are the tags in your .aseprite.`

`play()` on a sprite with no sheet says so the same way. Both are mistakes in the game, and with `[dev] strict` on, the first of them aborts a debug build, so it is met in the debugger instead of scrolling past in a log. Release builds never carry it:

```toml
[dev]
strict = true
```

A file that is not an Aseprite file — the wrong file, a cut-short download, a PNG renamed — is refused before it is parsed, comes back as an empty sheet, and counts as a [failed load](https://omardev29.github.io/rmp-docs/manual/modules/assets.html#when-a-load-fails):

`SHEET: [player.aseprite] is not an .aseprite file this can read`

On a machine with no GPU — a test, a server — the sheet's texture is empty and everything else is there: the frames, their durations and the tags, which is what lets the animation clock be tested without a window.
