From 495a2f02f5668712c7e4ebbcb39e7c12080189bd Mon Sep 17 00:00:00 2001 From: jabu <46233424+jabuwu@users.noreply.github.com> Date: Wed, 13 Sep 2023 05:29:48 -0500 Subject: [PATCH] improve attachment_renderer_object docs and usage (#17) --- examples/miniquad.rs | 4 +++- src/draw/combined.rs | 20 ++++++++++++++------ src/draw/simple.rs | 36 ++++++++++++++++++++++-------------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/examples/miniquad.rs b/examples/miniquad.rs index 1e12329..471e952 100644 --- a/examples/miniquad.rs +++ b/examples/miniquad.rs @@ -4,7 +4,7 @@ //! //! # Texture Creation & Disposal //! -//! Callbacks must be set to handle texture loading upon loading a [`rusty_spine::Atlas`]. +//! Callbacks must first be set to handle texture loading upon loading a [`rusty_spine::Atlas`]. //! See [`SpineTexture`]. //! //! # Texture Runtime Settings @@ -616,6 +616,7 @@ impl EventHandler for Stage { bindings.index_buffer.update(ctx, &renderable.indices); // If there is no attachment (and therefore no texture), skip rendering this renderable + // May also be None if a create texture callback was never set. let Some(attachment_renderer_object) = renderable.attachment_renderer_object else { continue; }; @@ -707,6 +708,7 @@ impl EventHandler for Stage { } fn main() { + // These texture callbacks should be set before loading an atlas. rusty_spine::extension::set_create_texture_cb(|atlas_page, path| { fn convert_filter(filter: AtlasFilter) -> FilterMode { match filter { diff --git a/src/draw/combined.rs b/src/draw/combined.rs index 462975d..a94db6a 100644 --- a/src/draw/combined.rs +++ b/src/draw/combined.rs @@ -118,7 +118,7 @@ impl CombinedDrawer { unreachable!(); }, |region_attachment| { - let next_attachment_renderer_object = Some(unsafe { + let next_attachment_renderer_object = unsafe { region_attachment .renderer_object() .get_atlas_region() @@ -127,13 +127,17 @@ impl CombinedDrawer { .c_ptr_ref() .rendererObject .cast_const() - }); - next_attachment_renderer_object + }; + if next_attachment_renderer_object.is_null() { + None + } else { + Some(next_attachment_renderer_object) + } }, ) }, |mesh_attachment| { - let next_attachment_renderer_object = Some(unsafe { + let next_attachment_renderer_object = unsafe { mesh_attachment .renderer_object() .get_atlas_region() @@ -142,8 +146,12 @@ impl CombinedDrawer { .c_ptr_ref() .rendererObject .cast_const() - }); - next_attachment_renderer_object + }; + if next_attachment_renderer_object.is_null() { + None + } else { + Some(next_attachment_renderer_object) + } }, ); diff --git a/src/draw/simple.rs b/src/draw/simple.rs index ae6099f..5a142ab 100644 --- a/src/draw/simple.rs +++ b/src/draw/simple.rs @@ -248,30 +248,38 @@ impl SimpleDrawer { let attachment_renderer_object = slot.attachment().and_then(|a| a.as_mesh()).map_or_else( || { - slot.attachment().and_then(|a| a.as_region()).map( + slot.attachment().and_then(|a| a.as_region()).and_then( |region_attachment| unsafe { - region_attachment + let attachment_renderer_object = region_attachment .renderer_object() .get_atlas_region() .unwrap() .page() .c_ptr_ref() .rendererObject - .cast_const() + .cast_const(); + if attachment_renderer_object.is_null() { + None + } else { + Some(attachment_renderer_object) + } }, ) }, - |mesh_attachment| { - Some(unsafe { - mesh_attachment - .renderer_object() - .get_atlas_region() - .unwrap() - .page() - .c_ptr_ref() - .rendererObject - .cast_const() - }) + |mesh_attachment| unsafe { + let attachment_renderer_object = mesh_attachment + .renderer_object() + .get_atlas_region() + .unwrap() + .page() + .c_ptr_ref() + .rendererObject + .cast_const(); + if attachment_renderer_object.is_null() { + None + } else { + Some(attachment_renderer_object) + } }, );