Skip to main content

Sprite

Struct Sprite 

Source
pub struct Sprite<'a> { /* private fields */ }
Expand description

A 1-bps or 2-bps bitmap drawable on screen

Sprites

WASM-4 natively supports sprites with the functions wasm4fun_core::blit and wasm4fun_core::blit_sub. Instead of passing sprite information as separate arguments, this structure conveniently wraps them in a single place. It also makes it easier to apply transformations using a SpriteViewImpl.

They can be generated by PNG images with the command w4 png2src --rust.

use wasm4fun_graphics::Sprite;

const CAR_WIDTH: u32 = 16;
const CAR_HEIGHT: u32 = 16;
const CAR_FLAGS: u32 = 1; // BLIT_2BPP
const CAR: [u8; 256] = [0; 256];

let car = Sprite::new(CAR_WIDTH, CAR_HEIGHT, CAR_FLAGS, &CAR);

// Draw car to screen
car.blit(10, 40);

// Create a clipped view of the original sprite
let clip = car.clip(0, 0, 8, 8);
clip.blit(10, 40);

Implementations§

Source§

impl<'a> Sprite<'a>

Source

pub const fn new(width: u32, height: u32, flags: u32, data: &'a [u8]) -> Self

Create a new sprite

Source

pub const fn width(&self) -> u32

Get the sprite width

Source

pub const fn height(&self) -> u32

Get the sprite height

Source

pub const fn rotate(&self, rotation: Rotation) -> SpriteViewImpl<'_>

Apply a rotation to the sprite

Source

pub fn flip_horizontally(&self, flip_horizontally: bool) -> SpriteViewImpl<'_>

Flip the sprite horizontally

Source

pub const fn flip_vertically(&self, flip_vertically: bool) -> SpriteViewImpl<'_>

Flip the sprite vertically

Source

pub const fn clip( &self, src_x: u32, src_y: u32, width: u32, height: u32, ) -> SpriteViewImpl<'_>

Clip the sprite to a subregion

Source

pub fn blit(&self, x: i32, y: i32)

Draw the sprite to the screen

The sprite is drawn at the point x, y.

Trait Implementations§

Source§

impl<'a> Clone for Sprite<'a>

Source§

fn clone(&self) -> Sprite<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Sprite<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a Sprite<'a>> for SpriteViewImpl<'a>

Source§

fn from(sprite: &'a Sprite<'_>) -> Self

Converts to this type from the input type.
Source§

impl<'a> SpriteView<'a> for Sprite<'a>

Source§

fn width(&self) -> u32

Get the sprite width
Source§

fn height(&self) -> u32

Get the sprite height
Source§

fn rotate(&'a self, rotation: Rotation) -> SpriteViewImpl<'a>

Apply a rotation to the sprite view
Source§

fn flip_horizontally(&'a self, flip_horizontally: bool) -> SpriteViewImpl<'a>

Flip the sprite view horizontally
Source§

fn flip_vertically(&'a self, flip_vertically: bool) -> SpriteViewImpl<'a>

Flip the sprite view vertically
Source§

fn clip( &'a self, src_x: u32, src_y: u32, width: u32, height: u32, ) -> SpriteViewImpl<'a>

Clip the sprite view to a subregion
Source§

fn blit(&self, x: i32, y: i32)

Draw the sprite view to the screen Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Sprite<'a>

§

impl<'a> RefUnwindSafe for Sprite<'a>

§

impl<'a> Send for Sprite<'a>

§

impl<'a> Sync for Sprite<'a>

§

impl<'a> Unpin for Sprite<'a>

§

impl<'a> UnsafeUnpin for Sprite<'a>

§

impl<'a> UnwindSafe for Sprite<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.