Sprites
An object's 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:
sprite.texture = rmp::assets::load_texture("ship.png");An object with a sprite draws the sprite; one without draws its
shape. The rest of
rmp::Sprite says how the picture is put on the
screen:
| Field | Default | Means |
|---|---|---|
texture | empty | A loose picture, for an object that does not animate. |
sheet | empty | An Aseprite sheet. When there is one, it is drawn and texture is not. |
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 | {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 | {0,0} | The size to draw at, before the object's scale; the picture's own size when empty. |
tint | WHITE | Multiplies the picture's colours. White leaves them alone; its alpha fades the picture. |
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 multiplies its size,
rotation turns it,
flip_x and
flip_y mirror it,
visible hides it without taking the object out
of the game, and 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.
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:
sprite.tint = RED; // the hit flash
sprite.tint = Fade(WHITE, 0.5f); // half transparent, colours unchanged
sprite.tint = WHITE; // back to the picture as drawnOne 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:
sprite.texture = rmp::assets::load_texture("items.png");
sprite.source = Rectangle{ 32, 0, 16, 16 }; // the third 16-pixel icon of the first rowFor 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() reads an
.aseprite or .ase file, and play()
starts one of its animations by name:
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.
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
does exactly this for its player, with a hurt tag on top and the art turned to face
the way it moves: player.cpp.
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() turns true:
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()- The name of the tag last started with
play(), or""before any has been. It keeps naming that tag afterstop()and after a tag that does not loop has finished;finished()is what says the clock has stopped. It is astd::string_viewof the sheet's own copy of the name, good for as long as the sheet is loaded. stop()- Holds the current frame, as a finished animation does.
frame_index()andset_frame(i)- 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 withset_frame()alone. 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 files are read directly; there is no export step and no JSON beside the picture. The whole of the workflow is:
- Draw the frames on one canvas. The canvas is the size of every frame.
- Set each frame's duration (Frame Properties). That time, in milliseconds, is how long the frame shows in the game.
- 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. - Save the
.asepriteintoresources/.
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 of each
rmp::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, in the file's order, up to 256 of them.
Each rmp::SheetSlice has its
name; its
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 set and its middle in
center, relative to the bounds.
slice(name) finds one —
the first, when two share a name — or an empty one, and
slice_count() says how many there are.
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.
Directions from movement#
The rmp::behavior::TopDown 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 has the rest.
Collision and the picture#
An object with no shape and no 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:
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 frameWhen 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:
[dev]
strict = trueA 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:
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.