#debug-mode #assets #game-assets #static #debugging #file-io #build

resource

Macros for statically including assets in release mode, but dynamically loading them in debug mode. This is primarily intended for games, allowing you to both avoid file IO in release builds and dynamically reload assets in debug mode

5 releases (3 breaking)

0.5.0 Jun 18, 2020
0.4.0 Jan 14, 2020
0.3.0 Oct 20, 2019
0.2.1 Aug 6, 2018
0.2.0 Aug 2, 2018

#381 in Filesystem

Download history 782/week @ 2024-06-18 1015/week @ 2024-06-25 764/week @ 2024-07-02 892/week @ 2024-07-09 647/week @ 2024-07-16 863/week @ 2024-07-23 1117/week @ 2024-07-30 1244/week @ 2024-08-06 706/week @ 2024-08-13 715/week @ 2024-08-20 830/week @ 2024-08-27 841/week @ 2024-09-03 744/week @ 2024-09-10 808/week @ 2024-09-17 1200/week @ 2024-09-24 725/week @ 2024-10-01

3,806 downloads per month
Used in 12 crates (9 directly)

CC0 license

28KB
572 lines

resource

Build Status Crates.io Docs.rs

The resource crate contains macros for statically including assets in release mode, but dynamically loading them in debug mode.

This is primarily intended for games, allowing you to both avoid file IO in release builds and dynamically reload assets in debug mode.

You can change the default behaviour, in debug or release mode, by using the force-static and force-dynamic features.

When resources are included statically, they are constant in memory and are included in the final binary executable. This allows you to avoid packaging individual files with the released application.

When resources are included dynamically, they are loaded from file at runtime, and can therefore be switched and updated while the app runs.

Usage

[dependencies]
resource = "~0.4.0"

Basic usage

use resource::{resource, resource_str};

let text = resource_str!("assets/text_asset.txt");
println!("Text is: {}", text);

let bytes = resource!("assets/binary_asset.bin");
println!("Binary data is: {:?}", bytes);

let (a, b, c) = resource_str!(("a.txt", "b.txt", "c.txt"));
println!("Contents of the three files are: `{}`, `{}`, `{}`");

let decoded_images = resource!(["a.png", "b.png", "c.png"], |image: &[u8]| decode(image));

Reloading

use resource::resource_str;

let mut message = resource_str!("message.txt");

loop {
    println!("Hello: {}", message.as_ref());

    // Wait one second
    std::thread::sleep(std::time::Duration::from_secs(5));

    // You can edit the contents of message.txt

    // Reload the message so the new version is printed next iteration
    message.reload_if_changed();
}

Dependencies

~13KB