Sprite

:= {
    texture : Texture,
    source : Rect,
    pos : Vec2,
    origin : Vec2,
    rotation : F32,
    scale : Vec2,
    tint : Rgba,
}

Pure helpers for texture sprites and simple animations.

A compact game-facing shape for sprites, spritesheet frame rectangles, and animation state. Everything here but Sprite.draw! is pure, so an app can advance animation and compute frame rectangles in update!; the draw itself takes a Draw.Frame and is legal in render! only.

Each transform is a noun named for what it sets: sprite.pos(p), sprite.scale(2), sprite.tint(Color.red). A with_* function of the same name exists for each and is deprecated; it calls the receiver and is kept so older code keeps building.

source : Sprite, Rect -> Sprite

Return a copy using a new texture source rectangle.

pos : Sprite, Vec2 -> Sprite

Return a copy at a new destination position.

origin : Sprite, Vec2 -> Sprite

Return a copy with a new rotation and scaling origin.

centered : Sprite -> Sprite

Return a copy whose origin is centered in the scaled sprite.

rotation : Sprite, F32 -> Sprite

Return a copy with rotation in degrees.

scale_xy : Sprite, Vec2 -> Sprite

Return a copy with independent horizontal and vertical scale.

scale : Sprite, F32 -> Sprite

Return a copy with uniform scale.

tint : Sprite, Rgba -> Sprite

Return a copy with a multiplicative tint.

to_texture_draw : Sprite -> TextureDraw

Resolve the sprite to a Draw.TextureDraw configuration.

draw! : Sprite, Frame => {  }

Draw the sprite using its current transform and tint. Legal in render! only.

from_texture : Texture -> Sprite

Create a sprite covering the complete texture with identity transform.

with_source : Sprite, Rect -> Sprite

Deprecated: use the source receiver.

with_pos : Sprite, Vec2 -> Sprite

Deprecated: use the pos receiver.

with_origin : Sprite, Vec2 -> Sprite

Deprecated: use the origin receiver.

with_scale_xy : Sprite, Vec2 -> Sprite

Deprecated: use the scale_xy receiver.

with_scale : Sprite, F32 -> Sprite

Deprecated: use the scale receiver.

with_tint : Sprite, Rgba -> Sprite

Deprecated: use the tint receiver.

sheet_frame : { frame_size : Vec2, row : U64, col : U64 } -> Rect

Return a source rectangle for a regular grid spritesheet.

animation : { frame_count : U64, fps : F32 } -> Animation

Create animation state at its first frame.

step : Animation, F32 -> Animation

Advance animation state by elapsed seconds without allocating.

animation_source : Animation, { frame_size : Vec2, row : U64 } -> Rect

Return the current frame's source rectangle for a spritesheet row.

Animation : {
    frame : U64,
    frame_count : U64,
    fps : F32,
    elapsed : F32,
}

Frame index and elapsed-time state for a regular spritesheet animation.