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
impl LayerStack
Sourcepub fn push_layer(&mut self, parallax: Parallax) -> Layer
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.
Sourcepub fn layer_count(&self) -> usize
pub fn layer_count(&self) -> usize
How many layers the stack has.
Sourcepub fn sprites(&self, layer: Layer) -> &[Sprite]
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.
Sourcepub fn extend(
&mut self,
layer: Layer,
sprites: impl IntoIterator<Item = Sprite>,
)
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.
Sourcepub fn clear(&mut self)
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.
Sourcepub fn sprite_count(&self) -> usize
pub fn sprite_count(&self) -> usize
How many sprites are in the stack across all layers.
Sourcepub fn resolve(&mut self, camera: [f32; 2]) -> &[Sprite]
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.
Sourcepub fn resolved(&self) -> &[Sprite]
pub fn resolved(&self) -> &[Sprite]
The frame LayerStack::resolve last produced.