Skip to main content

classicube_sys\graphics/
owned_gfx_texture.rs

1use crate::bindings::{Bitmap, Gfx_CreateTexture, Gfx_DeleteTexture, GfxResourceID};
2
3pub struct OwnedGfxTexture {
4    pub resource_id: GfxResourceID,
5}
6
7impl OwnedGfxTexture {
8    /// Returns `None` if the GPU rejects the bitmap — e.g. the graphics
9    /// context is currently lost (mid-device-reset on Windows D3D9) or the
10    /// power-of-two-rounded dimensions exceed the backend's texture limits.
11    pub fn new(bmp: &mut Bitmap, managed_pool: bool, mipmaps: bool) -> Option<Self> {
12        let resource_id =
13            unsafe { Gfx_CreateTexture(bmp, u8::from(managed_pool), u8::from(mipmaps)) };
14
15        if resource_id as usize == 0 {
16            return None;
17        }
18
19        Some(Self { resource_id })
20    }
21}
22
23impl Drop for OwnedGfxTexture {
24    fn drop(&mut self) {
25        unsafe {
26            Gfx_DeleteTexture(&raw mut self.resource_id);
27        }
28    }
29}