classicube_sys\bitmap/
owned_bitmap.rs

1use core::borrow::Borrow;
2
3use crate::{
4    bindings::{Bitmap, BitmapCol},
5    std_types::{c_int, vec, Vec},
6    Math_NextPowOf2,
7};
8
9pub struct OwnedBitmap {
10    bitmap: Bitmap,
11
12    #[allow(dead_code)]
13    pixels: Vec<BitmapCol>,
14}
15
16impl OwnedBitmap {
17    pub fn new(width: c_int, height: c_int, color: BitmapCol) -> Self {
18        let mut pixels = vec![color; width as usize * height as usize];
19        let scan0 = pixels.as_mut_ptr();
20
21        Self {
22            pixels,
23            bitmap: Bitmap {
24                width,
25                height,
26                scan0,
27            },
28        }
29    }
30
31    pub fn new_cleared(width: c_int, height: c_int) -> Self {
32        Self::new(width, height, 0x0000_0000)
33    }
34
35    pub fn new_pow_of_2(width: c_int, height: c_int, color: BitmapCol) -> OwnedBitmap {
36        let width = Math_NextPowOf2(width);
37        let height = Math_NextPowOf2(height);
38
39        Self::new(width, height, color)
40    }
41
42    pub fn new_pow_of_2_cleared(width: c_int, height: c_int) -> OwnedBitmap {
43        Self::new_pow_of_2(width, height, 0x0000_0000)
44    }
45
46    pub fn as_bitmap(&self) -> &Bitmap {
47        &self.bitmap
48    }
49
50    pub fn as_bitmap_mut(&mut self) -> &mut Bitmap {
51        &mut self.bitmap
52    }
53
54    /// # Safety
55    ///
56    /// The `OwnedBitmap` needs to live longer than the `Bitmap` return here.
57    pub unsafe fn get_bitmap(&self) -> Bitmap {
58        Bitmap { ..self.bitmap }
59    }
60}
61
62impl Borrow<Bitmap> for OwnedBitmap {
63    fn borrow(&self) -> &Bitmap {
64        self.as_bitmap()
65    }
66}