use crate::gfx::raster;
#[derive(Debug, Clone)]
pub struct DrawCall {
pub depth: f32,
pub color: u32,
pub kind: DrawKind,
}
#[derive(Debug, Clone)]
pub enum DrawKind {
Triangle { x0:f32, y0:f32, x1:f32, y1:f32, x2:f32, y2:f32 },
Line { x0:f32, y0:f32, x1:f32, y1:f32 },
}
#[derive(Default, Debug)]
pub struct DepthQueue {
calls: Vec<DrawCall>,
}
impl DepthQueue {
pub fn push_triangle(
&mut self, depth: f32, color: u32,
x0:f32, y0:f32, x1:f32, y1:f32, x2:f32, y2:f32,
) {
self.calls.push(DrawCall {
depth, color,
kind: DrawKind::Triangle { x0, y0, x1, y1, x2, y2 },
});
}
pub fn push_line(
&mut self, depth: f32, color: u32,
x0:f32, y0:f32, x1:f32, y1:f32,
) {
self.calls.push(DrawCall {
depth, color,
kind: DrawKind::Line { x0, y0, x1, y1 },
});
}
pub fn flush(mut self, buf: &mut Vec<u32>, width: usize, height: usize) {
self.calls.sort_unstable_by(|a, b| {
b.depth.partial_cmp(&a.depth).unwrap_or(std::cmp::Ordering::Equal)
});
for call in &self.calls {
match call.kind {
DrawKind::Triangle { x0, y0, x1, y1, x2, y2 } =>
raster::fill_triangle(buf, width, height, call.color,
x0, y0, x1, y1, x2, y2),
DrawKind::Line { x0, y0, x1, y1 } =>
raster::draw_line(buf, width, height, call.color,
x0, y0, x1, y1),
}
}
}
pub fn is_empty(&self) -> bool { self.calls.is_empty() }
}