Skip to main content

classicube_sys\core/
owned_texture.rs

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