1

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.

5
  • 2
    The wasm will have to make a HTTP request if it wants to fetch resources served by your server, Image::load_image is only for local files (which IIRC aren't supported by WASM). Commented Jul 16, 2024 at 22:44
  • @cafce25 great thanks. I'll add an env variable to host them locally or fetch assets depending on env. Commented Jul 16, 2024 at 23:15
  • @cafce25 emscripten supports a fake filesystem. Commented Jul 17, 2024 at 10:36
  • @ChayimFriedman Took a while but I figured it out. --embed-file assets/files does the trick. Commented Jul 17, 2024 at 14:55
  • @coolguy123 Then please post a self-answer. Commented Jul 17, 2024 at 15:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.