hello-rust-sdl2-wasm
hello-rust-sdl2-wasm copied to clipboard
A Rust repo which uses SDL2 for user interaction, and which can be run locally or compiled to WASM and run on the web
hello-rust-sdl2-wasm
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.
Prerequisites
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)
clone this repo and cd into it
git clone https://github.com/awwsmm/hello-rust-sdl2-wasm.git && cd hello-rust-sdl2-wasm
Building locally
To build a desktop (standalone binary executable) app, run
cargo run
Building for WASM
To build the WASM app instead, there are a few more setup steps.
install the target architecture toolchain
rustup target add asmjs-unknown-emscripten
We use emscripten to convert Rust LLVM bytecode to WASM.
install the emscripten compiler
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
set this one environment variable
export EMCC_CFLAGS="-s USE_SDL=2"
build
cargo build --target asmjs-unknown-emscripten
Open the browser app with
open index.html
Resources
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).