#[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
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.sheet: SheetIdWhich registered sheet this sprite samples.
rect: [f32; 4]World-space rectangle before rotation: [x, y, w, h], minimum corner
first, Y up.
rotation: f32Rotation 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
impl Sprite
Sourcepub const fn new(sheet: SheetId, rect: [f32; 4], uv: [f32; 4]) -> Self
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.
Sourcepub const fn with_rotation(self, radians: f32) -> Self
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.
Sourcepub const fn with_tint(self, tint: [f32; 4]) -> Self
pub const fn with_tint(self, tint: [f32; 4]) -> Self
The same sprite, with tint multiplied into every texel it
samples.
Sourcepub const fn instance(&self, sheet: [f32; 4]) -> SpriteInstance
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.