Skip to main content

Sprite

Struct Sprite 

Source
#[non_exhaustive]
pub struct Sprite { pub sheet: SheetId, pub rect: [f32; 4], pub rotation: f32, pub uv: [f32; 4], pub tint: [f32; 4], }
Expand description

One sprite to draw this frame.

The submission order of a &[Sprite] is the compositing order. Nothing here sorts, by sheet or by anything else: a caller that interleaves two sheets gets two batches, which is the price of the ordering it asked for.

Layer and parallax ordering live above this, in crate::layers: a LayerStack groups sprites into bands and flattens them back to front when the caller asks it to, so the frame this slice receives is still exactly the order it was handed.

§Building one, and why there is no literal

Sprite::new takes the three things a sprite cannot be drawn without — the sheet, the rectangle and the UVs — and the two that mean something specific when left alone arrive through with_rotation and with_tint.

The type is #[non_exhaustive], so a caller outside this crate cannot write the struct literal or use functional-update syntax. That is deliberate: rotation was added to a five-field literal and broke every construction in the workspace, none of which had anything to do with turning, and the next field would break them all again. The fields stay pub, so reading and assigning one is unchanged; it is only construction that now goes through the constructor, which is what makes the field after this one a non-event.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§sheet: SheetId

Which registered sheet this sprite samples.

§rect: [f32; 4]

World-space rectangle before rotation: [x, y, w, h], minimum corner first, Y up.

§rotation: f32

Rotation in radians, counter-clockwise, about the centre of rect — 0.0 for a sprite that does not turn.

§The pivot is the centre, and only the centre

A ship, a rock and a bullet all turn about themselves, so the centre is the case every rotating sprite actually wants. An arbitrary pivot is two more floats on a lane that has one component left, and it is not needed to express one: rotating a rectangle about an outside point gives the same rectangle turned about its own centre and then translated, so a caller that wants an orbit computes the translated rect and gets it exactly. A pivot offset is deliberately not half-built here — see docs/backlog.md.

§What it does to SampleMode

Nothing, where it is 0.0: sprite.slang branches on the angle and the zero case is the arithmetic that was there before rotation existed, not a general formula that happens to reduce to it, so a Smooth or an unrotated Pixel sprite is bit-for-bit what it always was.

Where it is not zero, SampleMode::Pixel’s snap stops rounding each corner — a rotated quad has no axis-aligned rectangle to round onto, and rounding its corners independently shears it — and instead translates the whole quad rigidly so its centre lands on the pixel grid. Sharp-bilinear itself needs no change at all; sprite.slang’s sharpen records why.

§uv: [f32; 4]

Normalised sheet UVs: [u0, v0, u1, v1], top-left corner first.

§tint: [f32; 4]

Straight-alpha RGBA multiplied into the sampled texel. [1.0; 4] is “the sheet as authored”.

Implementations§

Source§

impl Sprite

Source

pub const fn new(sheet: SheetId, rect: [f32; 4], uv: [f32; 4]) -> Self

A sprite that draws uv of sheet into rect, unrotated and untinted.

rotation starts at 0.0 and tint at [1.0; 4] — both the value that means “do nothing”, so a sprite built here and drawn is the sheet as authored, at the rectangle asked for.

§Two [f32; 4]s the compiler cannot tell apart

rect is world units, minimum corner first and Y up; uv is normalised sheet coordinates, top-left corner first. Swapping them at a call site compiles and draws a wrong picture — the hazard SheetDesc became a struct to avoid. Nothing here can catch it. What does is the golden images under crates/crcbl/tests/golden/, and the sample suites that assert on a resolved sprite’s rect and uv — so a sprite call site is not a place to take the argument order on trust.

Source

pub const fn with_rotation(self, radians: f32) -> Self

The same sprite, turned by radians — rotation’s units, pivot and effect on SampleMode in full.

Source

pub const fn with_tint(self, tint: [f32; 4]) -> Self

The same sprite, with tint multiplied into every texel it samples.

Source

pub const fn instance(&self, sheet: [f32; 4]) -> SpriteInstance

The bytes the shader reads for this sprite.

sheet is the sheet_lane of the sheet this sprite names, which the Sprite itself does not carry — a caller writes a SheetId and the renderer holds the size and the mode it was registered with, so there is exactly one place either can be wrong.

The lane’s fourth component is not the one sheet_lane produced: it is this sprite’s rotation, because the angle is the one thing in that float4 that belongs to the sprite. A zero rotation writes the 0.0 that was already there, so the bytes are unchanged.

Trait Implementations§

Source§

impl Clone for Sprite

Source§

fn clone(&self) -> Self

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 Copy for Sprite

Source§

impl Debug for Sprite

Source§

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

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

impl PartialEq for Sprite

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Sprite

Auto Trait Implementations§

§

impl Forget for Sprite

§

impl Freeze for Sprite

§

impl Move for Sprite

§

impl RefUnwindSafe for Sprite

§

impl Send for Sprite

§

impl Sync for Sprite

§

impl Unpin for Sprite

§

impl UnsafeUnpin for Sprite

§

impl UnwindSafe for Sprite

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> HalThreadSafe for T
where T: Send + Sync,

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.