engine/resources/
assets.rs1pub mod audio_clip;
6pub mod sprite;
7
8use core::ops::Range;
9
10macro_rules! gen_asset_handle_code {
11 ($asset_type:ident, $handle_name:ident, $find_fn:ident, $get_fn:ident, $field:ident) => {
12 pub use handle_impl::$handle_name;
13 mod handle_impl {
14 #[allow(unused_imports)] use $crate::resources::ResourceDatabase;
16
17 use super::*;
18
19 #[doc = "Handle for [`"]
20 #[doc = stringify!($asset_type)]
21 #[doc = "`].\n\nCreated with [`"]
22 #[doc = concat!("ResourceDatabase::", stringify!($find_fn))]
23 #[doc = "`], and can be resolved into a borrow of the asset itself with [`"]
24 #[doc = concat!("ResourceDatabase::", stringify!($get_fn))]
25 #[doc = "`]."]
26 #[derive(Clone, Copy, Debug)]
27 pub struct $handle_name(usize);
28 impl $crate::resources::ResourceDatabase {
29 #[doc = "Returns a [`"]
30 #[doc = stringify!($handle_name)]
31 #[doc = "`] if the database contains a [`"]
32 #[doc = stringify!($asset_type)]
33 #[doc = "`] with this name. Cache this, and use [`"]
34 #[doc = concat!("ResourceDatabase::", stringify!($get_fn))]
35 #[doc = "`] to access the actual asset at runtime."]
36 pub fn $find_fn(&self, name: &str) -> Option<$handle_name> {
37 profiling::function_scope!();
38 let Ok(index) = self
39 .$field
40 .binary_search_by(|asset| asset.name.as_str().cmp(name))
41 else {
42 return None;
43 };
44 Some($handle_name(index))
45 }
46
47 #[doc = "Returns the [`"]
48 #[doc = stringify!($asset_type)]
49 #[doc = "`] behind a handle previously queried with [`"]
50 #[doc = concat!("ResourceDatabase::", stringify!($find_fn))]
51 #[doc = "`]. Note that reusing handles between separate [`ResourceDatabase`]s will cause panics."]
52 pub fn $get_fn(&self, handle: $handle_name) -> &$asset_type {
53 &self.$field[handle.0].asset
54 }
55 }
56 }
57 };
58}
59
60pub(crate) use gen_asset_handle_code;
61
62pub trait Asset {
65 fn get_chunks(&self) -> Option<Range<u32>>;
68 fn offset_chunks(&mut self, offset: i32);
70 fn get_sprite_chunks(&self) -> Option<Range<u32>>;
72 fn offset_sprite_chunks(&mut self, offset: i32);
74}