classicube_sys\drawer_2d/
owned_context_2d.rs1use core::{borrow::Borrow, ptr};
2
3use crate::{
4 Math_NextPowOf2,
5 bindings::{Bitmap, BitmapCol, Context2D},
6 std_types::{Vec, c_int, vec},
7};
8
9pub struct OwnedContext2D {
10 context_2d: Context2D,
11
12 #[allow(dead_code)]
13 pixels: Vec<BitmapCol>,
14}
15
16impl OwnedContext2D {
17 #[must_use]
21 pub fn new(width: c_int, height: c_int, color: BitmapCol) -> Self {
22 let w = usize::try_from(width).expect("context width must be non-negative");
23 let h = usize::try_from(height).expect("context height must be non-negative");
24 let mut pixels = vec![color; w * h];
25 let scan0 = pixels.as_mut_ptr();
26
27 Self {
28 context_2d: Context2D {
29 bmp: Bitmap {
30 scan0,
31 width,
32 height,
33 },
34 width,
35 height,
36 meta: ptr::null_mut(),
37 },
38 pixels,
39 }
40 }
41
42 #[must_use]
43 pub fn new_cleared(width: c_int, height: c_int) -> Self {
44 Self::new(width, height, 0x0000_0000)
45 }
46
47 #[must_use]
48 pub fn new_pow_of_2(width: c_int, height: c_int, color: BitmapCol) -> OwnedContext2D {
49 let width = Math_NextPowOf2(width);
50 let height = Math_NextPowOf2(height);
51
52 Self::new(width, height, color)
53 }
54
55 #[must_use]
56 pub fn new_pow_of_2_cleared(width: c_int, height: c_int) -> OwnedContext2D {
57 Self::new_pow_of_2(width, height, 0x0000_0000)
58 }
59
60 #[must_use]
61 pub fn as_context_2d(&self) -> &Context2D {
62 &self.context_2d
63 }
64
65 pub fn as_context_2d_mut(&mut self) -> &mut Context2D {
66 &mut self.context_2d
67 }
68
69 #[must_use]
70 pub fn as_bitmap(&self) -> &Bitmap {
71 &self.context_2d.bmp
72 }
73
74 pub fn as_bitmap_mut(&mut self) -> &mut Bitmap {
75 &mut self.context_2d.bmp
76 }
77}
78
79impl Borrow<Bitmap> for OwnedContext2D {
80 fn borrow(&self) -> &Bitmap {
81 self.as_bitmap()
82 }
83}