summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-17 10:02:56 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-17 10:28:44 -0500
commitbaae7dbc38ad4e131c107d9f0f638530ac250e2e (patch)
treecb6c6dbab81424999617eef09885e798ee2c21c9
parentFeedback and grade for Checkpoint (diff)
downloadDungeonCrawl-baae7dbc38ad4e131c107d9f0f638530ac250e2e.tar.gz
DungeonCrawl-baae7dbc38ad4e131c107d9f0f638530ac250e2e.tar.bz2
DungeonCrawl-baae7dbc38ad4e131c107d9f0f638530ac250e2e.zip
wasm support!
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock76
-rw-r--r--Cargo.toml1
-rw-r--r--Dockerfile51
-rw-r--r--Makefile30
-rw-r--r--dungeon/Cargo.toml4
-rw-r--r--flake.nix2
-rw-r--r--game/Cargo.toml5
-rw-r--r--game/src/lib.rs103
-rw-r--r--game/src/main.rs159
-rw-r--r--game/www/index.html44
-rw-r--r--graphics/src/audio.rs2
-rw-r--r--graphics/src/render.rs2
13 files changed, 357 insertions, 123 deletions
diff --git a/.gitignore b/.gitignore
index ea8c4bf..8b84bc4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
+/dist
/target
diff --git a/Cargo.lock b/Cargo.lock
index 5143d17..d558689 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -67,6 +67,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
+name = "bumpalo"
+version = "3.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
+
+[[package]]
name = "cc"
version = "1.2.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -117,6 +123,7 @@ dependencies = [
name = "dungeon"
version = "0.1.0"
dependencies = [
+ "getrandom",
"rand",
"strum",
"strum_macros",
@@ -150,9 +157,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
dependencies = [
"cfg-if",
+ "js-sys",
"libc",
"r-efi",
"wasip2",
+ "wasm-bindgen",
]
[[package]]
@@ -204,6 +213,16 @@ dependencies = [
]
[[package]]
+name = "js-sys"
+version = "0.3.82"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65"
+dependencies = [
+ "once_cell",
+ "wasm-bindgen",
+]
+
+[[package]]
name = "libc"
version = "0.2.177"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -254,6 +273,12 @@ dependencies = [
]
[[package]]
+name = "once_cell"
+version = "1.21.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
+
+[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -376,6 +401,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
[[package]]
+name = "rustversion"
+version = "1.0.22"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
+
+[[package]]
name = "seq-macro"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -452,6 +483,51 @@ dependencies = [
]
[[package]]
+name = "wasm-bindgen"
+version = "0.2.105"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60"
+dependencies = [
+ "cfg-if",
+ "once_cell",
+ "rustversion",
+ "wasm-bindgen-macro",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.105"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.105"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc"
+dependencies = [
+ "bumpalo",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.105"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index fd65dd7..1a2ad4a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,6 +18,7 @@ rust-version = "1.87"
[workspace.dependencies]
dungeon = { path = "dungeon" }
game = { path = "game" }
+getrandom = "0.3"
graphics = { path = "graphics" }
strum_macros = "0.27"
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..b14aa15
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,51 @@
+FROM debian:trixie
+
+# enable more components and update repos
+RUN apt update -y
+
+# install libraries
+RUN apt install -y \
+ libasound2-dev \
+ libpulse-dev \
+ libwayland-dev \
+ libxrandr-dev \
+ libxinerama-dev \
+ libxcursor-dev \
+ libxi-dev \
+ libxkbcommon-dev \
+ libsdl2-dev \
+ libclang-dev
+
+# Install build tools
+RUN apt install -y \
+ rustup \
+ gcc \
+ g++ \
+ cmake \
+ git \
+ xz-utils
+
+# Install emscripten
+RUN git clone https://github.com/emscripten-core/emsdk.git /emsdk
+RUN /emsdk/emsdk install latest
+RUN /emsdk/emsdk activate latest
+
+# add build user
+RUN useradd -u 1000 -m builder -d /home/builder
+USER builder
+
+# Install rust and toolchain
+RUN rustup default stable
+RUN rustup target add wasm32-unknown-emscripten
+
+# set emscripten env
+ENV RUSTFLAGS="-C panic=unwind"
+ENV EMCC_CFLAGS="-O3 -sUSE_GLFW=3 -sASSERTIONS=1 -sWASM=1 -sASYNCIFY -sGL_ENABLE_GET_PROC_ADDRESS=1 -sEXPORTED_RUNTIME_METHODS=HEAPF32,ccall,cwrap"
+ENV EMSDK=/emsdk
+ENV EMSDK_NODE=/emsdk/node/22.16.0_64bit/bin/node
+ENV PATH="${EMSDK}/upstream/bin:${EMSDK}/upstream/emscripten:${EMSDK_NODE}:${PATH}"
+
+# build the code
+VOLUME /data
+WORKDIR /data
+CMD ["cargo", "build", "--target", "wasm32-unknown-emscripten", "--release"]
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..02714d0
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,30 @@
+.PHONY: web test dist clean realclean
+
+ASSETS_SRC = $(wildcard game/www/*)
+ASSETS_DST = $(patsubst game/www/%,dist/%,$(ASSETS_SRC))
+
+TEST_PORT ?= 8000
+
+IMAGE := dungeon_crawl_builder
+
+web: dist $(ASSETS_DST)
+
+test: web
+ cd dist && python3 -m http.server $(TEST_PORT)
+
+image:
+ docker build -t $(IMAGE) .
+
+dist:
+ docker run --rm -it -v .:/data -v ~/.cargo:/home/builder/.cargo $(IMAGE)
+ mkdir -p dist
+ cp ./target/wasm32-unknown-emscripten/release/game.{js,wasm} dist
+
+dist/%: game/www/%
+ cp -r $< $@
+
+clean:
+ rm -rf dist
+
+realclean: clean
+ cargo clean
diff --git a/dungeon/Cargo.toml b/dungeon/Cargo.toml
index 906058d..213fc3b 100644
--- a/dungeon/Cargo.toml
+++ b/dungeon/Cargo.toml
@@ -8,9 +8,13 @@ publish.workspace = true
rust-version.workspace = true
[dependencies]
+getrandom.workspace = true
rand.workspace = true
strum.workspace = true
strum_macros.workspace = true
[lints]
workspace = true
+
+[target.'cfg(target_arch = "wasm32")'.dependencies]
+getrandom = { workspace = true, features = ["wasm_js"] }
diff --git a/flake.nix b/flake.nix
index 47900be..fc49406 100644
--- a/flake.nix
+++ b/flake.nix
@@ -47,6 +47,8 @@
cargo
cargo-flamegraph
clippy
+ # web
+ python3
# raylib
cmake
clang
diff --git a/game/Cargo.toml b/game/Cargo.toml
index 5cdf4af..af4a533 100644
--- a/game/Cargo.toml
+++ b/game/Cargo.toml
@@ -8,7 +8,6 @@ publish.workspace = true
rust-version.workspace = true
[dependencies]
-argh.workspace = true
dungeon.workspace = true
graphics.workspace = true
@@ -21,3 +20,7 @@ x11 = ["graphics/x11"]
wayland = ["graphics/wayland"]
sdl = ["graphics/sdl"]
static = ["graphics/static"]
+
+# desktop dependencies
+[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
+argh.workspace = true
diff --git a/game/src/lib.rs b/game/src/lib.rs
new file mode 100644
index 0000000..77942b4
--- /dev/null
+++ b/game/src/lib.rs
@@ -0,0 +1,103 @@
+use dungeon::{Dungeon, UpdateResult, player_input::PlayerInput, pos::Direction};
+use graphics::{Key, Window};
+
+pub struct Game {
+ window: Window,
+ dungeon: Dungeon,
+ // to ensure the most recently-pressed direction key is used:
+ current_dir: Option<(Direction, Key)>,
+}
+
+impl Game {
+ pub fn new(window: Window, seed: Option<u64>) -> Self {
+ let dungeon = match seed {
+ Some(s) => Dungeon::new(s),
+ None => Dungeon::random(),
+ };
+
+ Self {
+ window,
+ dungeon,
+ current_dir: None,
+ }
+ }
+
+ fn player_dir(&mut self) -> Option<Direction> {
+ const MOVE_KEYS: [(Direction, [Key; 2]); 4] = [
+ (Direction::North, [Key::Up, Key::W]),
+ (Direction::West, [Key::Left, Key::A]),
+ (Direction::South, [Key::Down, Key::S]),
+ (Direction::East, [Key::Right, Key::D]),
+ ];
+
+ // if a key was just pressed, use it for the new direction to move in
+ for (dir, keys) in MOVE_KEYS {
+ for key in keys {
+ if self.window.is_key_pressed(key) {
+ self.current_dir = Some((dir, key));
+ return Some(dir);
+ }
+ }
+ }
+ // otherwise, use existing direction, so long as the key is still down
+ match self.current_dir {
+ Some((dir, key)) if self.window.is_key_down(key) => return Some(dir),
+ _ => self.current_dir = None,
+ }
+ // otherwise, use any key that is already down
+ for (dir, keys) in MOVE_KEYS {
+ for key in keys {
+ if self.window.is_key_down(key) {
+ self.current_dir = Some((dir, key));
+ return Some(dir);
+ }
+ }
+ }
+ // otherwise, no direction key is pressed, so return None
+ None
+ }
+
+ pub fn run(&mut self) {
+ // Main game loop
+ while self.window.is_open() {
+ // Handle debug keys
+ if self.window.is_key_pressed(Key::F3) {
+ self.window.toggle_debug();
+ }
+ if self.window.is_key_pressed(Key::F4) {
+ self.dungeon
+ .msg
+ .set_message("Lorem ipsum dolor sit amet consectetur adipiscing elit");
+ }
+
+ let inputs = PlayerInput {
+ direction: self.player_dir(),
+ interact: self.window.is_key_pressed(Key::Return),
+ };
+
+ // Update game state
+ let result = self
+ .dungeon
+ .update(inputs, self.window.delta_time().as_secs_f32());
+ match result {
+ UpdateResult::EntityMovement => {}
+ UpdateResult::NextFloor => {
+ // TODO: stairs audio
+ }
+ UpdateResult::MessageUpdated(changed) => {
+ if changed {
+ self.window.audio().speak.play();
+ }
+ }
+ }
+
+ // Update on screen message
+ if self.dungeon.msg.update(inputs) {
+ self.window.audio().speak.play();
+ }
+
+ // Draw a single frame
+ self.window.draw_frame(&self.dungeon);
+ }
+ }
+}
diff --git a/game/src/main.rs b/game/src/main.rs
index 5446b5f..a44f317 100644
--- a/game/src/main.rs
+++ b/game/src/main.rs
@@ -1,130 +1,49 @@
-use argh::FromArgs;
-use dungeon::{Dungeon, UpdateResult, player_input::PlayerInput, pos::Direction};
-use graphics::{Key, Window, WindowBuilder};
+#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
+mod arch {
+ use argh::FromArgs;
+ use game::Game;
+ use graphics::WindowBuilder;
-struct Game {
- window: Window,
- dungeon: Dungeon,
- // to ensure the most recently-pressed direction key is used:
- current_dir: Option<(Direction, Key)>,
-}
-
-impl Game {
- fn new(window: Window, seed: Option<u64>) -> Self {
- let dungeon = match seed {
- Some(s) => Dungeon::new(s),
- None => Dungeon::random(),
- };
-
- Self {
- window,
- dungeon,
- current_dir: None,
- }
+ /// Play a dungeon crawl game
+ #[derive(FromArgs)]
+ struct Args {
+ /// enable vsync
+ #[argh(switch)]
+ vsync: bool,
+ /// enable verbose logging
+ #[argh(switch, short = 'v')]
+ verbose: bool,
+ /// set the map seed
+ #[argh(option)]
+ seed: Option<u64>,
}
- fn player_dir(&mut self) -> Option<Direction> {
- const MOVE_KEYS: [(Direction, [Key; 2]); 4] = [
- (Direction::North, [Key::Up, Key::W]),
- (Direction::West, [Key::Left, Key::A]),
- (Direction::South, [Key::Down, Key::S]),
- (Direction::East, [Key::Right, Key::D]),
- ];
-
- // if a key was just pressed, use it for the new direction to move in
- for (dir, keys) in MOVE_KEYS {
- for key in keys {
- if self.window.is_key_pressed(key) {
- self.current_dir = Some((dir, key));
- return Some(dir);
- }
- }
- }
- // otherwise, use existing direction, so long as the key is still down
- match self.current_dir {
- Some((dir, key)) if self.window.is_key_down(key) => return Some(dir),
- _ => self.current_dir = None,
- }
- // otherwise, use any key that is already down
- for (dir, keys) in MOVE_KEYS {
- for key in keys {
- if self.window.is_key_down(key) {
- self.current_dir = Some((dir, key));
- return Some(dir);
- }
- }
- }
- // otherwise, no direction key is pressed, so return None
- None
+ pub fn main() -> graphics::Result<()> {
+ // Parse arguments
+ let args: Args = argh::from_env();
+ // Load the window
+ let window = WindowBuilder::new()
+ .vsync(args.vsync)
+ .verbose(args.verbose)
+ .build()?;
+ Game::new(window, args.seed).run();
+ Ok(())
}
+}
- fn run(&mut self) {
- // Main game loop
- while self.window.is_open() {
- // Handle debug keys
- if self.window.is_key_pressed(Key::F3) {
- self.window.toggle_debug();
- }
- if self.window.is_key_pressed(Key::F4) {
- self.dungeon
- .msg
- .set_message("Lorem ipsum dolor sit amet consectetur adipiscing elit");
- }
-
- let inputs = PlayerInput {
- direction: self.player_dir(),
- interact: self.window.is_key_pressed(Key::Return),
- };
-
- // Update game state
- let result = self
- .dungeon
- .update(inputs, self.window.delta_time().as_secs_f32());
- match result {
- UpdateResult::EntityMovement => {}
- UpdateResult::NextFloor => {
- // TODO: stairs audio
- }
- UpdateResult::MessageUpdated(changed) => {
- if changed {
- self.window.audio().speak.play();
- }
- }
- }
-
- // Update on screen message
- if self.dungeon.msg.update(inputs) {
- self.window.audio().speak.play();
- }
+#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
+mod arch {
+ use game::Game;
+ use graphics::WindowBuilder;
- // Draw a single frame
- self.window.draw_frame(&self.dungeon);
- }
+ pub fn main() -> graphics::Result<()> {
+ // Load the window
+ let window = WindowBuilder::new().build()?;
+ Game::new(window, None).run();
+ Ok(())
}
}
-/// Play a dungeon crawl game
-#[derive(FromArgs)]
-struct Args {
- /// enable vsync
- #[argh(switch)]
- vsync: bool,
- /// enable verbose logging
- #[argh(switch, short = 'v')]
- verbose: bool,
- /// set the map seed
- #[argh(option)]
- seed: Option<u64>,
-}
-
-fn main() -> graphics::Result<()> {
- // Parse arguments
- let args: Args = argh::from_env();
- // Load the window
- let window = WindowBuilder::new()
- .vsync(args.vsync)
- .verbose(args.verbose)
- .build()?;
- Game::new(window, args.seed).run();
- Ok(())
+pub fn main() -> graphics::Result<()> {
+ arch::main()
}
diff --git a/game/www/index.html b/game/www/index.html
new file mode 100644
index 0000000..2181c25
--- /dev/null
+++ b/game/www/index.html
@@ -0,0 +1,44 @@
+<!doctype html>
+<html lang="en-us">
+ <head>
+ <meta charset="utf-8">
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <meta name="viewport" content="width=device-width">
+ <meta property="og:title" content="Dungeon Crawl">
+ <meta property="og:description" content="Dungeon Crawl game!">
+ <title>Dungeon Crawl</title>
+ <style>
+ * {
+ margin: 0;
+ padding: none;
+ border: none;
+ }
+
+ canvas {
+ width: 100%;
+ height: 100%;
+ display: block;
+ background: black;
+ }
+ </style>
+ </head>
+ <body>
+ <canvas id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
+ <script type='text/javascript'>
+ var Module = {
+ canvas: (function () {
+ var canvas = document.querySelector('#canvas');
+ canvas.addEventListener("webglcontextlost", function (e) {
+ alert('WebGL context lost. You will need to reload the page.');
+ e.preventDefault();
+ }, false);
+ return canvas;
+ })(),
+ print: console.log,
+ printErr: console.error,
+ setStatus: console.debug,
+ };
+ </script>
+ <script src="game.js"></script>
+ </body>
+</html>
diff --git a/graphics/src/audio.rs b/graphics/src/audio.rs
index 3a20f62..981defb 100644
--- a/graphics/src/audio.rs
+++ b/graphics/src/audio.rs
@@ -4,7 +4,7 @@ use raylib::audio::RaylibAudio;
macro_rules! load_audio {
($handle:expr, $filepath:expr) => {
- if cfg!(feature = "static") {
+ if cfg!(any(feature = "static", target_arch = "wasm32")) {
let bytes = include_bytes!(concat!("../../", $filepath));
let wave = $handle.new_wave_from_memory(".ogg", bytes)?;
$handle.new_sound_from_wave(&wave)?
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
index cbeb613..1c10f57 100644
--- a/graphics/src/render.rs
+++ b/graphics/src/render.rs
@@ -58,7 +58,7 @@ macro_rules! draw_text {
macro_rules! load_texture {
($handle:expr, $thread:expr, $filepath:expr) => {
- if cfg!(feature = "static") {
+ if cfg!(any(feature = "static", target_arch = "wasm32")) {
let bytes = include_bytes!(concat!("../../", $filepath));
let image = ::raylib::texture::Image::load_image_from_mem(".bmp", bytes)?;
$handle.load_texture_from_image($thread, &image)?