Skip to main content

classicube_sys\graphics/
owned_vertex_buffer.rs

1use crate::{
2    bindings::{Gfx_CreateDynamicVb, Gfx_DeleteVb, GfxResourceID, VertexFormat},
3    std_types::c_int,
4};
5
6pub struct OwnedGfxVertexBuffer {
7    pub resource_id: GfxResourceID,
8}
9
10impl OwnedGfxVertexBuffer {
11    /// Returns `None` if the GPU rejects the buffer — typically because the
12    /// graphics context is currently lost (mid-device-reset on Windows D3D9).
13    #[must_use]
14    pub fn new(fmt: VertexFormat, max_vertices: c_int) -> Option<Self> {
15        let resource_id = unsafe { Gfx_CreateDynamicVb(fmt, max_vertices) };
16
17        if resource_id as usize == 0 {
18            return None;
19        }
20
21        Some(Self { resource_id })
22    }
23}
24
25impl Drop for OwnedGfxVertexBuffer {
26    fn drop(&mut self) {
27        unsafe {
28            Gfx_DeleteVb(&raw mut self.resource_id);
29        }
30    }
31}