PNG to WASM-4 Source (png2wasm4src)
Convert indexed PNG images to Rust source code for WASM-4 engine.
https://gitlab.com/claudiomattera/png2wasm4src
WASM-4 is an old-style fantasy game console implemented in WebAssembly.
Games can be developed in Rust (and in other languages), and the runtime has support for drawing sprites.
Sprites must either have a bit depth of one bit per pixel, or two bits per pixel, and must be properly encoded in variables, which can be done using the WASM-4 w4 command-line application.
This crate allows to perform the conversion from within Rust code, which allows to dynamically create variables from PNG images using a build.rs build script.
Usage
This crate can be used to automatically generate Rust variables from PNG images on cargo build.
This way the Rust variables always reflect the current PNG image, and there is no risk of forgetting to update them.
Assume the following crate structure.
Directory assets contains a subdirectory sprites, which contains all the sprites.
Sprites are organized in subdirectories: sprite letters.png is inside directory fonts, and sprite tiles.png is inside directory tiles.
.
├── assets
│ └── sprites
│ ├── fonts
│ │ └── letters.png
│ └── tiles
│ └── tiles.png
├── build.rs
├── Cargo.lock
├── Cargo.toml
└── src
└── lib.rs
Now it is possible to generate the Rust code from the sprites PNG image inside a build.rs build script.
use var;
use ;
use Write;
use PathBuf;
use Result;
use build_sprite_modules_tree;
The build script generates the following code, and writes it into the file ${OUT_DIR}/sprites.rs.
From any of the crate modules (for instance in lib.rs) it is possible to include that file, and use all entities defined there.
use *;
// Include the generated file in the current module.
//
// Note: this is done at top level, not inside any function (but it could be
// inside a module).
include!;
License
Copyright Claudio Mattera 2021
You are free to copy, modify, and distribute this application with attribution under the terms of the MIT license. See the License.txt file for details.