classicube_sys\core/
owned_texture.rs

1use core::borrow::Borrow;
2
3use crate::{
4    bindings::{cc_uint16, Bitmap, Texture, TextureRec},
5    std_types::c_short,
6    OwnedGfxTexture,
7};
8
9pub struct OwnedTexture {
10    texture: Texture,
11
12    #[allow(dead_code)]
13    gfx_texture: OwnedGfxTexture,
14}
15
16impl OwnedTexture {
17    pub fn new(
18        bmp: &mut Bitmap,
19        coords: (c_short, c_short),
20        size: (cc_uint16, cc_uint16),
21        uv: TextureRec,
22    ) -> Self {
23        let gfx_texture = OwnedGfxTexture::new(bmp, true, false);
24        let texture = Texture {
25            ID: gfx_texture.resource_id,
26            x: coords.0,
27            y: coords.1,
28            width: size.0,
29            height: size.1,
30            uv,
31        };
32
33        Self {
34            gfx_texture,
35            texture,
36        }
37    }
38
39    pub fn as_texture(&self) -> &Texture {
40        &self.texture
41    }
42
43    pub fn as_texture_mut(&mut self) -> &mut Texture {
44        &mut self.texture
45    }
46
47    /// # Safety
48    ///
49    /// The `OwnedTexture` needs to live longer than the `Texture` return here.
50    pub unsafe fn get_texture(&self) -> Texture {
51        Texture { ..self.texture }
52    }
53}
54
55impl Borrow<Texture> for OwnedTexture {
56    fn borrow(&self) -> &Texture {
57        self.as_texture()
58    }
59}