1
0
Fork 0

Add FPS display, get webassembly build working

This commit is contained in:
Timothy Warren 2022-02-03 11:46:29 -05:00
parent 3a9ad932e4
commit 2c0f2408a6
8 changed files with 66 additions and 12 deletions

4
.gitignore vendored
View File

@ -142,3 +142,7 @@ Cargo.lock
# Ignore save game file
savegame.json
# Webassembly
wasm/*.wasm
wasm/*js

View File

@ -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"
serde = { version = "1.0.93", features = ["derive"] }
serde_json = "1.0.44"

View File

@ -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
.phony: run-pi clean check run fmt fix lint docs build build-wasm check-wasm build-wasm-dev

View File

@ -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())

View File

@ -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));
}
}
}

View File

@ -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);
}

View File

@ -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);
}
}

16
wasm/index.html Normal file
View File

@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Roguelike-game</title>
</head>
<body>
<canvas id="canvas" width="640" height="480"></canvas>
<script src="roguelike_tutorial.js"></script>
<script>
window.addEventListener('load', async () => {
await wasm_bindgen('roguelike_tutorial_bg.wasm');
});
</script>
</body>
</html>