classicube_sys\drawer_2d/
owned_context_2d.rs

1use core::{borrow::Borrow, ptr};
2
3use crate::{
4    bindings::{Bitmap, BitmapCol, Context2D},
5    std_types::{c_int, vec, Vec},
6    Math_NextPowOf2,
7};
8
9pub struct OwnedContext2D {
10    context_2d: Context2D,
11
12    #[allow(dead_code)]
13    pixels: Vec<BitmapCol>,
14}
15
16impl OwnedContext2D {
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            context_2d: Context2D {
23                bmp: Bitmap {
24                    height,
25                    width,
26                    scan0,
27                },
28                height,
29                width,
30                meta: ptr::null_mut(),
31            },
32            pixels,
33        }
34    }
35
36    pub fn new_cleared(width: c_int, height: c_int) -> Self {
37        Self::new(width, height, 0x0000_0000)
38    }
39
40    pub fn new_pow_of_2(width: c_int, height: c_int, color: BitmapCol) -> OwnedContext2D {
41        let width = Math_NextPowOf2(width);
42        let height = Math_NextPowOf2(height);
43
44        Self::new(width, height, color)
45    }
46
47    pub fn new_pow_of_2_cleared(width: c_int, height: c_int) -> OwnedContext2D {
48        Self::new_pow_of_2(width, height, 0x0000_0000)
49    }
50
51    pub fn as_context_2d(&self) -> &Context2D {
52        &self.context_2d
53    }
54
55    pub fn as_context_2d_mut(&mut self) -> &mut Context2D {
56        &mut self.context_2d
57    }
58
59    pub fn as_bitmap(&self) -> &Bitmap {
60        &self.context_2d.bmp
61    }
62
63    pub fn as_bitmap_mut(&mut self) -> &mut Bitmap {
64        &mut self.context_2d.bmp
65    }
66}
67
68impl Borrow<Bitmap> for OwnedContext2D {
69    fn borrow(&self) -> &Bitmap {
70        self.as_bitmap()
71    }
72}