Skip to main content

LayerStack

Struct LayerStack 

Source
pub struct LayerStack { /* private fields */ }
Expand description

A frame’s sprites, grouped into parallax layers and drawn back to front.

Built once and reused: LayerStack::clear empties the sprites and keeps the layers and their Vecs, so a steady-state frame allocates nothing — the same discipline SpriteRenderer’s instance ring follows.

use crcbl_hal::null::NullInstance;
use crcbl_hal::{DeviceDesc, Format, Instance, QueueKind};
use crcbl_render::{LayerStack, Parallax, SheetDesc, Sprite, SpriteRenderer};

let mut stack = LayerStack::new();
let hills = stack.push_layer(Parallax::new(0.5).expect("finite"));
let ground = stack.push_layer(Parallax::WORLD);
let hud = stack.push_layer(Parallax::CAMERA);

// Submitted in whatever order the game finds them; the layer decides depth.
stack.push(ground, sprite(0.0));
stack.push(hills, sprite(0.0));
stack.push(hud, sprite(0.0));

// The camera has moved 100 units right.
let frame = stack.resolve([100.0, 0.0]);
// Back to front: hills, then ground, then the HUD.
assert_eq!(frame[0].rect[0], 50.0, "the hills took half the camera");
assert_eq!(frame[1].rect[0], 0.0, "the ground is where the world put it");
assert_eq!(frame[2].rect[0], 100.0, "the HUD follows the camera exactly");

Implementations§

Source§

impl LayerStack

Source

pub fn new() -> Self

An empty stack with no layers.

Source

pub fn push_layer(&mut self, parallax: Parallax) -> Layer

Adds a layer in front of every layer already added, and returns it.

Depth is the order layers are added, not a number the caller invents: there is no way to write two layers at the same depth, and no comparison anywhere that could tie.

Source

pub fn layer_count(&self) -> usize

How many layers the stack has.

Source

pub fn parallax(&self, layer: Layer) -> Parallax

layer’s parallax factor.

§Panics

If layer came from a different stack — the same contract as indexing a Vec, and for the same reason: a silently ignored push would lose sprites rather than report anything.

Source

pub fn sprites(&self, layer: Layer) -> &[Sprite]

The sprites pushed onto layer so far, in submission order and before parallax is applied.

§Panics

If layer came from a different stack.

Source

pub fn push(&mut self, layer: Layer, sprite: Sprite)

Adds one sprite to the front of layer’s own order.

§Panics

If layer came from a different stack.

Source

pub fn extend( &mut self, layer: Layer, sprites: impl IntoIterator<Item = Sprite>, )

Adds several, keeping their order — NineQuads::sprites feeds straight into this.

§Panics

If layer came from a different stack.

Source

pub fn clear(&mut self)

Empties every layer, keeping the layers themselves and their capacity.

The per-frame call. LayerStack::push_layer is startup work — a stack rebuilt every frame would hand out fresh Layers a game then has to store somewhere anyway.

Source

pub fn sprite_count(&self) -> usize

How many sprites are in the stack across all layers.

Source

pub fn resolve(&mut self, camera: [f32; 2]) -> &[Sprite]

Flattens the stack into one frame’s submission order. Back layer first, each layer’s sprites offset by its share of camera.

camera is the camera’s world position on the sprite plane — [camera.eye.x, camera.eye.y] for the Camera the same frame’s view_projection came from. Passing a different one silently desynchronises the parallax from the projection; there is no way for this to check that, which is why it takes the position rather than pretending to derive it.

This is the reordering, and it is the caller’s. sprite_pass never sorts; this does, once, where the caller can see it. Within a layer nothing moves: the result is the layers concatenated in depth order, each in its own push order, so the batching rule downstream still sees exactly the order it was handed.

Source

pub fn resolved(&self) -> &[Sprite]

The frame LayerStack::resolve last produced.

Trait Implementations§

Source§

impl Clone for LayerStack

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 Debug for LayerStack

Source§

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

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

impl Default for LayerStack

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.