diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..7259cf1 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,11 @@ +[target.wasm32-unknown-emscripten] +rustflags = [ + "-C", "link-arg=-sUSE_GLFW=3", + "-C", "link-arg=-sASYNCIFY", + "-C", "link-arg=-sASYNCIFY_STACK_SIZE=1048576", + "-C", "link-arg=-sGL_ENABLE_GET_PROC_ADDRESS=1", + "-C", "link-arg=-sWASM=1", + "-C", "link-arg=-sALLOW_MEMORY_GROWTH=1", + "-C", "link-arg=-sINITIAL_MEMORY=67108864", + "-C", "link-arg=-sFULL_ES3=1", +] diff --git a/.gitignore b/.gitignore index ea8c4bf..192eff6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +web/*.js +web/*.wasm diff --git a/Cargo.toml b/Cargo.toml index 5028925..f1ed811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,14 @@ edition = "2024" [dependencies] rand = "0.10.0" + +# wayland is a Linux display-server feature; excluded for WASM builds. +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] raylib = { version = "5.5.1", features = ["wayland"] } +[target.'cfg(target_arch = "wasm32")'.dependencies] +raylib = { version = "5.5.1" } + [profile.release] opt-level = 3 lto = "thin" diff --git a/README.md b/README.md index 8aebaaf..9ef4065 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,17 @@ cargo run cargo run --release ``` +**Web (WebAssembly)** + +Prerequisites: [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html) activated in your shell. + +```sh +./build_web.sh # produces web/*.js + web/*.wasm +./build_web.sh --serve # build then serve at http://localhost:8080 +``` + +The web build uses ASYNCIFY so the main game loop requires no changes between native and browser targets. + **Tests** ```sh diff --git a/build_web.sh b/build_web.sh new file mode 100755 index 0000000..76ee9a5 --- /dev/null +++ b/build_web.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Build the endless runner for WebAssembly via Emscripten. +# Usage: ./build_web.sh [--serve] +set -euo pipefail + +# Flags required by raylib-sys for WASM compilation. +export EMCC_CFLAGS="-O3 -sUSE_GLFW=3 -sASSERTIONS=1 -sWASM=1 -sASYNCIFY -sGL_ENABLE_GET_PROC_ADDRESS=1" + +echo "Building for wasm32-unknown-emscripten..." +cargo build --target wasm32-unknown-emscripten --release + +OUT=target/wasm32-unknown-emscripten/release + +echo "Copying build artefacts to web/..." +cp "$OUT/endless_runner.js" web/ +cp "$OUT/endless_runner.wasm" web/ + +echo "" +echo "Build complete. Output in web/" + +if [[ "${1-}" == "--serve" ]]; then + echo "Serving at http://localhost:8080" + python3 -m http.server 8080 --directory web +fi diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..0c84df9 --- /dev/null +++ b/web/index.html @@ -0,0 +1,47 @@ + + +
+ + +