Skip to content

Sprite Sheet ​

Sprite sheets and frame animation.

Header: #include <asw/modules/sprite_sheet.h>
Namespace: asw

Camera example
Runs in your browser. Keyboard and mouse recommended.Open in new tab ↗

From the camera example.

SpriteSheet ​

A texture divided into frames of equal size. Frames are read from left to right, then from top to bottom.

cpp
class SpriteSheet;

Constructors ​

cpp
SpriteSheet();
SpriteSheet(const Texture& texture, const Vec2<float>& frame_size, int frame_count = 0);
ParameterTypeDefaultDescription
textureconst Texture&The texture that holds the frames
frame_sizeconst Vec2<float>&The size of one frame in pixels
frame_countint0The number of frames. 0 uses all frames that fit in the texture

The default constructor makes an empty sprite sheet.

Methods ​

MethodReturnDescription
get_texture() constconst Texture&Get the texture
get_frame_size() constVec2<float>Get the size of one frame in pixels
get_frame_count() constintGet the number of frames
get_frame(int index) constQuad<float>Get the source area of a frame in the texture, in pixels
draw_frame(int index, const Quad<float>& dest) constvoidDraw a frame stretched to an area of the screen

For get_frame and draw_frame, an index after the last frame wraps around to the start.

Animation ​

Steps through frames over time. Use it with a SpriteSheet.

cpp
class Animation;

Constructors ​

cpp
Animation();
Animation(int frame_count, float frame_duration, bool loop = true);
ParameterTypeDefaultDescription
frame_countintThe number of frames
frame_durationfloatSeconds that each frame shows
loopbooltrueStart again after the last frame

The default constructor makes an animation with one frame.

Methods ​

MethodReturnDescription
update(float dt)voidAdvance the animation by dt seconds
reset()voidGo back to the first frame
set_frame_duration(float frame_duration)voidSet the number of seconds that each frame shows
get_frame() constintGet the frame index to draw
is_finished() constbooltrue when an animation that does not loop is at its last frame. Always false for looping animations

Example ​

cpp
auto texture = asw::assets::load_texture("player_run.png");
asw::SpriteSheet sheet(texture, asw::Vec2<float>(16.0f, 16.0f));
asw::Animation run(sheet.get_frame_count(), 0.08f);

// Update
run.update(dt);

// Draw at 2x size
sheet.draw_frame(run.get_frame(), asw::Quad<float>(100.0f, 100.0f, 32.0f, 32.0f));

Released under the MIT License.