use crate::instance::push_instance;
use crate::plan::{Batch, Plan, Resolved};
use crate::view::pixel_zoom;
use crate::{Camera2D, FrameStats, SpriteDraw};
#[derive(Debug)]
pub struct SpriteBatcher<K> {
plan: Plan<K>,
packed: Vec<f32>,
}
impl<K: Copy + Eq> SpriteBatcher<K> {
pub fn new() -> Self {
Self {
plan: Plan::new(),
packed: Vec::new(),
}
}
pub fn build(
&mut self,
sprites: &[SpriteDraw],
camera: &Camera2D,
mut texture: impl FnMut(&SpriteDraw) -> Option<(K, bool)>,
) {
self.plan.build(sprites, |sprite| match texture(sprite) {
None => Resolved::Skip,
Some((texture, false)) => Resolved::Draw(texture),
Some((texture, true)) => Resolved::Placeholder(texture),
});
let zoom = pixel_zoom(camera.zoom);
self.packed.clear();
for drawn in &self.plan.order {
push_instance(
&mut self.packed,
&sprites[drawn.sprite as usize],
drawn.placeholder,
Some(zoom),
);
}
}
pub fn instances(&self) -> &[f32] {
&self.packed
}
pub fn stats(&self) -> FrameStats {
FrameStats {
sprites: self.plan.order.len() as u32,
skipped: self.plan.skipped,
batches: self.plan.batches.len() as u32,
tiles: 0,
chunks: 0,
}
}
pub(crate) fn batches(&self) -> &[Batch<K>] {
&self.plan.batches
}
pub(crate) fn position(&self, layer: u8, depth: u32) -> u32 {
self.plan.position(layer, depth)
}
}
impl<K: Copy + Eq> Default for SpriteBatcher<K> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use nadir_assets::Handle;
use nadir_core::{GenIndex, Vec2};
use super::*;
use crate::SPRITE_INSTANCE_FLOATS;
fn sprite(texture: u32, y: f32) -> SpriteDraw {
let id = GenIndex::new(texture, std::num::NonZeroU32::MIN);
SpriteDraw::new(Handle::new(id), Vec2::new(10.0, y), Vec2::splat(8.0))
}
#[test]
fn it_skips_what_is_not_ready_and_packs_the_rest_in_draw_order() {
let sprites = [
sprite(0, 30.0),
sprite(1, 10.0),
sprite(2, 20.0),
sprite(0, 5.0),
];
let mut batcher = SpriteBatcher::new();
batcher.build(&sprites, &Camera2D::default(), |sprite| {
match sprite.texture.id().index() {
1 => None,
texture => Some((texture, texture == 2)),
}
});
assert_eq!(
batcher.stats(),
FrameStats {
sprites: 3,
skipped: 1,
batches: 3,
tiles: 0,
chunks: 0,
}
);
let order: Vec<u32> = batcher
.batches()
.iter()
.map(|batch| batch.texture)
.collect();
assert_eq!(order, [0, 2, 0], "by depth: y 5, then 20, then 30");
assert_eq!(batcher.instances().len(), 3 * SPRITE_INSTANCE_FLOATS);
batcher.build(&sprites[..1], &Camera2D::default(), |_| Some((0, false)));
assert_eq!(batcher.stats().sprites, 1);
assert_eq!(batcher.instances().len(), SPRITE_INSTANCE_FLOATS);
}
}