Skip to main content

embedded_3dgfx/
command_buffer.rs

1use heapless::Vec;
2
3use crate::{
4    DrawPrimitive,
5    error::{BudgetKind, RenderError},
6};
7
8#[derive(Debug, Clone)]
9pub enum RenderCommand {
10    ClearColor(embedded_graphics_core::pixelcolor::Rgb565),
11    ClearDepth(u32),
12    Draw(DrawPrimitive),
13}
14
15pub struct CommandBuffer<const MAX: usize> {
16    commands: Vec<RenderCommand, MAX>,
17}
18
19impl<const MAX: usize> CommandBuffer<MAX> {
20    pub const fn new() -> Self {
21        Self {
22            commands: Vec::new(),
23        }
24    }
25
26    pub fn clear(&mut self) {
27        self.commands.clear();
28    }
29
30    pub fn len(&self) -> usize {
31        self.commands.len()
32    }
33
34    pub fn is_empty(&self) -> bool {
35        self.commands.is_empty()
36    }
37
38    pub fn push(&mut self, cmd: RenderCommand) -> Result<(), RenderError> {
39        self.commands.push(cmd).map_err(|_| {
40            RenderError::OutOfBudget(BudgetKind::DrawPrimitives {
41                attempted: self.commands.len() + 1,
42                max: MAX,
43            })
44        })
45    }
46
47    pub fn iter(&self) -> core::slice::Iter<'_, RenderCommand> {
48        self.commands.iter()
49    }
50
51    pub fn get(&self, index: usize) -> Option<&RenderCommand> {
52        self.commands.get(index)
53    }
54}
55
56impl<const MAX: usize> Default for CommandBuffer<MAX> {
57    fn default() -> Self {
58        Self::new()
59    }
60}