logo
pub type TextureHandle = u64;
Expand description

Handle to a texture in Ark.

Safety

Care should be taken around the validity of a raw TextureHandle. Some validity issues are:

To safely handle these cases we recommend wrapping your TextureHandle in a std::rc::Rc or std::sync::Arc. See the examples section for a safe implementation.

Examples

Safe implementation example around TextureHandle.

use ark_api_ffi::render_v0::TextureHandle;

pub struct Texture {
    handle: std::rc::Rc<TextureHandle>,
}

impl Texture {
    // Pass the handle retrieved from `create_texture` here.
    pub fn new(handle: TextureHandle) -> Self {
        Self {
            handle: std::rc::Rc::new(handle),
        }
    }

    pub fn handle(&self) -> TextureHandle {
        *self.handle
    }
}

impl Drop for Texture {
    // Never call `destroy_texture` yourself, let `Drop` call it.
    fn drop(&mut self) {
        if std::rc::Rc::strong_count(&self.handle) == 1 {
             destroy_texture(*self.handle);
        }
     }
}

impl Clone for Texture {
    fn clone(&self) -> Self {
        Self {
           handle: std::rc::Rc::clone(&self.handle),
        }
    }
}