Giter Site home page Giter Site logo

tantandev / assets_manager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from a1phyr/assets_manager

2.0 2.0 1.0 1.01 MB

Conveniently load, cache, and reload external resources

Home Page: https://docs.rs/assets_manager

License: Other

Rust 99.99% Logos 0.01%

assets_manager's Introduction

Assets-manager

Crates.io Docs.rs Minimum rustc version

This crate aims at providing a filesystem abstraction to easily load external resources. It was originally thought for games, but can of course be used in other contexts.

Original idea was inspired by Veloren's assets system.

This crate follow semver convention and supports rustc 1.56 and higher. Changing this is considered a breaking change.

Goals

This crates focuses on:

  • Good performances:
    Crucial for perfomance-oriented applications such as games.
    Loaded assets are cached so loading one several times is as fast as loading it once. This crate was thought for use with concurrency.

  • Hot-reloading:
    Hot-reloading means updating assets in memory as soon as the corresponding file is changed, without restarting your program. It may greatly ease development.
    Your time is precious, and first-class support of hot-reloading helps you saving it.

  • Pleasant to use:
    A well-documented high-level API, easy to learn.
    Built-in support of common formats: serialization, images, sounds.
    Can load assets from a file system, a zip archive, or even embed them in a binary.

  • Lightness:
    Pay for what you take, no dependency bloat.

Example

Suppose that you have a file assets/common/position.ron containing this:

Point(
    x: 5,
    y: -6,
)

Then you can load it this way (with feature ron enabled):

use assets_manager::{Asset, AssetCache, loader};
use serde::Deserialize;

// The struct you want to load
#[derive(Deserialize)]
struct Point {
    x: i32,
    y: i32,
}

// Specify how you want the structure to be loaded
impl Asset for Point {
    // The extension of the files to look into
    const EXTENSION: &'static str = "ron";

    // The serialization format (RON)
    type Loader = loader::RonLoader;
}


// Create a new cache to load assets under the "./assets" folder
let cache = AssetCache::new("assets")?;

// Get a handle on the asset
// This will load the file `./assets/common/position.ron`
let handle = cache.load::<Point>("common.position")?;

// Lock the asset for reading
// Any number of read locks can exist at the same time,
// but none can exist when the asset is reloaded
let point = handle.read();

// The asset is now ready to be used
assert_eq!(point.x, 5);
assert_eq!(point.y, -6);

// Loading the same asset retreives it from the cache
let other_handle = cache.load("common.position")?;
assert!(other_handle.same_handle(&handle));

Hot-reloading is also very easy to use:

let cache = AssetCache::new("assets")?;
let handle = cache.load::<Point>("common.position")?;

loop {
    // Reload all cached files that changed
    cache.hot_reload();

    // Assets are updated without any further work
    println!("Current value: {:?}", handle.read());
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

assets_manager's People

Contributors

a1phyr avatar rijenkii avatar xmac94x avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

esser50k

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.