classicube_sys\bitmap/
owned_bitmap.rs

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