Sprite

:= {
    data : List(U8),
    bpp : [BPP1, BPP2],
    stride : U32,
    region : { src_x : U32, src_y : U32, width : U32, height : U32 },
}

Sprites for drawing to the WASM-4 framebuffer.

A Sprite is simply a list of bytes encoded with either 1 bit per pixel (BPP1) or 2 bits per pixel (BPP2), along with information about how to read the pixel data.

Refer to the WASM-4 docs for more information

new : { data : List(U8), bpp : [BPP1, BPP2], width : U32, height : U32 } -> Sprite

Create a Sprite to be drawn or blit to the screen.

fruit_sprite = Sprite.new({
    data: [0x00, 0xa0, 0x02, 0x00, 0x0e, 0xf0, 0x36, 0x5c, 0xd6, 0x57, 0xd5, 0x57, 0x35, 0x5c, 0x0f, 0xf0],
    bpp: BPP2,
    width: 8,
    height: 8,
})
sub : Sprite, SubRegion -> Try(Sprite, [OutOfBounds])

Creates a Sprite referencing a subregion of the current Sprite. This will return an error if the subregion does not fit in the current Sprite.

sub_sprite_result = Sprite.sub(sprite, { src_x: 20, src_y: 0, width: 20, height: 20 })

Note: If your program should never generate an invalid subregion, sub_or_crash enables avoiding the result and simpler code.

sub_or_crash : Sprite, SubRegion -> Sprite

Equivalent to the sub function, but will crash on error. This is really useful for static sprite sheet data that needs subSprites extracted.

sub_sprite = Sprite.sub_or_crash(sprite, { src_x: 20, src_y: 0, width: 20, height: 20 })

Warning: Will crash if the subregion is not contained within the sprite.