mini_asset_loader

Crates.iomini_asset_loader
lib.rsmini_asset_loader
version0.2.1
sourcesrc
created_at2023-05-12 19:26:08.435621
updated_at2023-05-13 21:06:09.838704
descriptionA composable game asset-loading system.
homepagehttps://github.com/emctague/mini_asset_loader
repositoryhttps://github.com/emctague/mini_asset_loader
max_upload_size
id863235
size22,212
Ethan McTague (emctague)

documentation

README

mini_asset_loader

Crates.io docs.rs GitHub Workflow Status Crates.io

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.

Example

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);
    }
}
Commit count: 13

cargo fmt