breakwater_parser/framebuffer/
mod.rs

1pub mod simple;
2
3pub trait FrameBuffer {
4    fn get_width(&self) -> usize;
5
6    fn get_height(&self) -> usize;
7
8    fn get_size(&self) -> usize {
9        self.get_width() * self.get_height()
10    }
11
12    #[inline(always)]
13    fn get(&self, x: usize, y: usize) -> Option<u32> {
14        if x < self.get_width() && y < self.get_height() {
15            Some(unsafe { self.get_unchecked(x, y) })
16        } else {
17            None
18        }
19    }
20
21    /// # Safety
22    /// make sure x and y are in bounds
23    unsafe fn get_unchecked(&self, x: usize, y: usize) -> u32;
24
25    fn set(&self, x: usize, y: usize, rgba: u32);
26
27    /// We can *not* take an `&[u32]` for the pixel here, as `std::slice::from_raw_parts` requires the data to be
28    /// aligned. As the data already is stored in a buffer we can not guarantee it's correctly aligned, so let's just
29    /// treat the pixels as raw bytes.
30    ///
31    /// Returns the coordinates where we landed after filling
32    #[inline(always)]
33    fn set_multi(&self, start_x: usize, start_y: usize, pixels: &[u8]) -> (usize, usize) {
34        let starting_index = start_x + start_y * self.get_width();
35        let pixels_copied = self.set_multi_from_start_index(starting_index, pixels);
36
37        let new_x = (start_x + pixels_copied) % self.get_width();
38        let new_y = start_y + (pixels_copied / self.get_width());
39
40        (new_x, new_y)
41    }
42
43    /// Returns the number of pixels copied
44    fn set_multi_from_start_index(&self, starting_index: usize, pixels: &[u8]) -> usize;
45
46    fn as_bytes(&self) -> &[u8];
47
48    fn as_pixels(&self) -> &[u32];
49}