diff --git a/.gitignore b/.gitignore index 5c35a86..8c96ffc 100644 --- a/.gitignore +++ b/.gitignore @@ -142,3 +142,7 @@ Cargo.lock # Ignore save game file savegame.json + +# Webassembly +wasm/*.wasm +wasm/*js \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index e5f8e80..7040c0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,17 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["rltk/opengl", "rltk/serde", "rltk/threaded"] +curses = ["rltk/curses", "rltk/serde"] +js = ["getrandom/js", "rltk/opengl", "rltk/serde"] + [dependencies] +getrandom = { version = "0.2.4" } lazy_static = "1.4.0" regex = "1.5.4" -rltk = { version = "0.8.0", features=["serde"] } -specs = { version = "0.16.1", features=["serde"] } +rltk = { version = "0.8.1", default-features = false } +specs = { version = "0.16.1", features = ["serde"] } specs-derive = "0.4.1" -serde= { version="1.0.93", features = ["derive"] } -serde_json = "1.0.39" \ No newline at end of file +serde = { version = "1.0.93", features = ["derive"] } +serde_json = "1.0.44" \ No newline at end of file diff --git a/Makefile b/Makefile index d60401a..d35417c 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,20 @@ run: $(call print-help, run, Runs the binary in develop mode) run-pi: $(call print-help, run-pi, Sets appropriate flags so that the game runs on a Raspberry Pi) MESA_GL_VERSION_OVERRIDE=3.0 MESA_GLSL_VERSION_OVERRIDE=330 cargo run +run-curses: $(call print-help, run-curses, Run the game in a console window via the curses library) + cargo run --features curses --no-default-features + +build: + cargo build + +build-wasm: $(call print-help, build-wasm, Build the webassembly version of the game) + cargo build --release --target wasm32-unknown-unknown --features js + wasm-bindgen target/wasm32-unknown-unknown/release/roguelike_tutorial.wasm --out-dir wasm --no-modules --no-typescript + +build-wasm-dev: $(call print-help, build-wasm-dev, Build the webassembly version of the game (dev version)) + cargo build --target wasm32-unknown-unknown --features js + wasm-bindgen target/wasm32-unknown-unknown/debug/roguelike_tutorial.wasm --out-dir wasm --no-modules --no-typescript + clean: $(call print-help, clean, Removes save file and compilation artifacts) rm -f savegame.json cargo clean @@ -13,6 +27,9 @@ clean: $(call print-help, clean, Removes save file and compilation artifacts) check: $(call print-help, check, Check code syntax) cargo check +check-wasm: $(call print-help, check-wasm, Check code syntax for webassembly build) + cargo check --target wasm32-unknown-unknown --features js + lint: $(call print-help, lint, Check code syntax and style) cargo clippy @@ -21,9 +38,9 @@ fmt: $(call print-help, fmt, Runs formatter on code) fix: $(call print-help, fix, Fixes some warnings, then runs the formatter) cargo fix --allow-dirty --allow-staged - cargo +nightly fmt + make fmt docs: $(call print-help, docs, Generates code docs) cargo doc -.phony: run-pi clean check run fmt fix lint docs \ No newline at end of file +.phony: run-pi clean check run fmt fix lint docs build build-wasm check-wasm build-wasm-dev \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7970c25..1b7079f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -192,6 +192,7 @@ fn main() -> ::rltk::BError { .with_title("Roguelike Tutorial") .with_font("vga8x16.png", 8, 16) .with_sparse_console(80, 30, "vga8x16.png") + .with_vsync(false) .build()?; main_loop(context, init_state()) diff --git a/src/state.rs b/src/state.rs index 91ed5a1..a7c1aa4 100644 --- a/src/state.rs +++ b/src/state.rs @@ -13,6 +13,9 @@ use crate::{camera, colors, damage_system, gamelog, player, saveload_system, spa /// Whether to show a visual representation of map generation pub const SHOW_MAPGEN_VISUALIZER: bool = false; +/// Whether to show an FPS counter +pub const SHOW_FPS: bool = true; + /// The main actions possible with a vendor #[derive(PartialEq, Copy, Clone)] pub enum VendorMode { @@ -546,5 +549,8 @@ impl GameState for State { damage_system::delete_the_dead(&mut self.ecs); ::rltk::render_draw_buffer(ctx).expect("Failed to render the Rltk buffer"); + if SHOW_FPS { + ctx.print(1, 59, &format!("FPS: {}", ctx.fps)); + } } } diff --git a/src/systems/dispatcher.rs b/src/systems/dispatcher.rs index acbef55..b791df3 100644 --- a/src/systems/dispatcher.rs +++ b/src/systems/dispatcher.rs @@ -14,6 +14,12 @@ pub use single_thread::*; use super::*; +#[cfg(not(target_arch = "wasm32"))] +pub trait UnifiedDispatcher { + fn run_now(&mut self, ecs: &mut World); +} + +#[cfg(target_arch = "wasm32")] pub trait UnifiedDispatcher { fn run_now(&mut self, ecs: *mut World); } diff --git a/src/systems/dispatcher/multi_thread.rs b/src/systems/dispatcher/multi_thread.rs index 0fc5e42..5f03c6d 100644 --- a/src/systems/dispatcher/multi_thread.rs +++ b/src/systems/dispatcher/multi_thread.rs @@ -22,7 +22,7 @@ macro_rules! construct_dispatcher { .build(); let dispatch = MultiThreadedDispatcher { - dispatcher: dispatcher + dispatcher }; return Box::new(dispatch); @@ -35,10 +35,8 @@ pub struct MultiThreadedDispatcher { } impl<'a> UnifiedDispatcher for MultiThreadedDispatcher { - fn run_now(&mut self, ecs: *mut World) { - unsafe { - self.dispatcher.dispatch(&mut *ecs); - crate::effects::run_effects_queue(&mut *ecs); - } + fn run_now(&mut self, ecs: &mut World) { + self.dispatcher.dispatch(ecs); + crate::effects::run_effects_queue(ecs); } } diff --git a/wasm/index.html b/wasm/index.html new file mode 100644 index 0000000..226d714 --- /dev/null +++ b/wasm/index.html @@ -0,0 +1,16 @@ + + + + + Roguelike-game + + + + + + + \ No newline at end of file