Crates.io | mini_asset_loader |
lib.rs | mini_asset_loader |
version | 0.2.1 |
source | src |
created_at | 2023-05-12 19:26:08.435621 |
updated_at | 2023-05-13 21:06:09.838704 |
description | A composable game asset-loading system. |
homepage | https://github.com/emctague/mini_asset_loader |
repository | https://github.com/emctague/mini_asset_loader |
max_upload_size | |
id | 863235 |
size | 22,212 |
mini_asset_loader
provides an extensible asset loading system, intended to load assets from
various sources for use in a game.
The asset types, loaded sources, caching behaviour, etc. is entirely customizable. Assets are reference-counted and thread-safe.
A simple asset type based on serde_json
and type tags is provided in the asset
module.
This example makes use of the asset
feature. This feature provides a simple Tagged JSON
asset type, which only works on nightly rust. However, substituting this for a custom asset
type will work in stable rust.
use serde::{Serialize, Deserialize};
use mini_asset_loader::asset::{TaggedJsonAsset, TaggedJsonAssetCreationHandler};
use mini_asset_loader::loaders::ToCached;
use mini_asset_loader::{TypedAssetLoader, asset_loader_vec};
use std::path::PathBuf;
// Creating an asset type is as easy as making a Serializable/Deserializable struct...
#[derive(Serialize, Deserialize)]
struct StringAsset {
value: String
}
// ...and then tagging it with these two lines:
#[typetag::serde]
impl TaggedJsonAsset for StringAsset {}
// ...Then, when we want to *load* assets...
fn main() {
// We create our loader setup as usual...
let mut loader = asset_loader_vec![
PathBuf::from("assets/"),
PathBuf::from("/global_assets/")
].to_cached();
// Make a TaggedJsonAssetCreationHandler...
let mut handler = TaggedJsonAssetCreationHandler;
// And we can load our assets!
if let Some(my_string_asset) = loader.load_typed_asset::<StringAsset>(&mut handler, "my_string_asset.json") {
println!("String asset loaded: {}", my_string_asset.read().value);
}
}