一个简单的 Rust → WebAssembly 组件,将 Phigros 官方铺面文件渲染为紧凑的结构体。
这里有两种安装方式:
这个仓库具有Github Action的自动构建分支,运行
npm install phasetida/phasetida-wasm-core#dist欸欸欸?这么信不过咱吗?
- 安装Cargo(如果已经安装了,请跳过这一步;如果没有的话,可以参考这个教程)
- 安装wasm-pack:
cargo install wasm-pack
- 克隆这个仓库
git clone https://github.com/phasetida/phasetida-wasm-core
- 进入仓库目录,然后执行wasm构建:
wasm-pack build --target web
- 最后,在
package.json文件里引用仓库里的pkg目录就可以啦
这个WASM组件会读取window.inputBuffer,window.inputBufferLength,window.outputBuffer和window.outputBufferLength,所以请准备好这些缓冲区和缓冲区长度。
以下为示例代码
import init, { pre_draw, load_level } from "phasetida_wasm_core.js";
const OUTPUT_BUFFER_LENGTH = 65536;
const outputBufferRaw = new ArrayBuffer(
OUTPUT_BUFFER_LENGTH * Uint8Array.BYTES_PER_ELEMENT
);
const outputBuffer = new Uint8Array(outputBufferRaw);
const outputBufferLength = outputBuffer.length;
window.outputBuffer = outputBuffer;
window.outputBufferLength = outputBufferLength;
const INPUT_BUFFER_LENGTH = 1024;
const inputBufferRaw = new ArrayBuffer(
INPUT_BUFFER_LENGTH * Uint8Array.BYTES_PER_ELEMENT
);
const inputBuffer = new Uint8Array(inputBufferRaw);
const inputBufferLength = inputBuffer.length;
window.inputBuffer = inputBuffer;
window.inputBufferLength = inputBufferLength;
await init();
//...调用load_level时,WASM会接受参数的JSON字符串,并载入铺面文件。
调用pre_draw时,WASM会从window.inputBuffer读取输入数据,并将绘制结构化数据写入window.outputBuffer,具体结构化数据格式,请见src/renders.rs以及src/input.rs
本仓库为实验性玩具项目,所以文档十分潦草。
由于Android WebView对共享缓冲区的限制,本仓库不使用共享缓冲区作为数据交换的方式!