A minimal working "game" written in Rust with SDL2, compiled to WASM.
Note: these instructions are written for macOS and Ubuntu. If you'd like to submit instructions for any other OS, please open a PR.
install the SDL2 development libraries
On macOS, this is as easy as
brew install sdl2
Make sure these libraries are on your LIBRARY_PATH
, as well. If you installed with brew
, run
export LIBRARY_PATH="$LIBRARY_PATH:$(brew --prefix)/lib"
So you don't have to do this in each new shell you open, maybe also add this to your ~/.bashrc
or ~/.zshrc
(or other shell startup script).
On Ubuntu:
sudo apt install libsdl2-dev
(setting LIBRARY_PATH
is not needed on Ubuntu)
git clone https://github.com/awwsmm/hello-rust-sdl2-wasm.git && cd hello-rust-sdl2-wasm
To build a desktop (standalone binary executable) app, run
cargo run
To build the WASM app instead, there are a few more setup steps.
rustup target add asmjs-unknown-emscripten
We use emscripten to convert Rust LLVM bytecode to WASM.
On MacOS:
brew install emscripten
(This will take a few minutes.)
On Ubuntu, follow emscripten's recommended installation instructions:
git clone https://github.com/emscripten-core/emsdk.git && cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
export EMCC_CFLAGS="-s USE_SDL=2"
cargo build --target asmjs-unknown-emscripten
Open the browser app with
open index.html
This work is largely based on Michał Kalbarczyk's blog post here, with some help from Greg Buchholz's message board post here, which is itself based on Tobias Widlund's work here. Thanks, all!
See Mozilla's Rust to WASM guide if you want a simpler Rust-to-WASM example (which doesn't use SDL2).