Reputation: 97
I've successfully compiled my game to WASM with the flags:
EMCC_CFLAGS="-sUSE_GLFW=3 -sGL_ENABLE_GET_PROC_ADDRESS -sASYNCIFY -sASSERTIONS --preload-file src/resources" cargo build --release --target=wasm32-unknown-emscripten
However, the game crashes immediately when opened from the browser with the error:
WARNING: FILEIO: [src/resources/image.png] Failed to open file
thread 'main' panicked at src/main.rs:126:58:
could not load background image: "Image data is null. Either the file doesnt exist or the image type is unsupported."
Which means the browser is unable to load these images, which in my code are loaded like:
let sh_walk =
Image::load_image("src/resources/walk.png").expect("could not load background image");
How are assets loaded in WASM compiled games running in the binary? I'm considering pulling the images at runtime through HTTP, and using the regular loading locally.
I've already added the "src/resources" folder to the web server root, and they load in the browser, but the WASM binary does not pick it up.
Upvotes: 1
Views: 379
Reputation: 97
The solution is to compile with --embed-file
:
EMCC_CFLAGS="-sUSE_GLFW=3 -sGL_ENABLE_GET_PROC_ADDRESS -sASYNCIFY -sASSERTIONS --embed-file src/resources" cargo build --release --target=wasm32-unknown-emscripten
This will use the simulated file system from Emscripten, which is basically all the assets bundled in a .data file.
Upvotes: 1