mirror of
https://github.com/Xevion/rust-sdl2-emscripten.git
synced 2025-12-06 11:16:19 -06:00
Moving average FPS, remove web-time pkg, remove map sprite
This commit is contained in:
80
Cargo.lock
generated
80
Cargo.lock
generated
@@ -17,12 +17,6 @@ version = "1.3.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bumpalo"
|
|
||||||
version = "3.16.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@@ -46,15 +40,6 @@ dependencies = [
|
|||||||
"wasi",
|
"wasi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "js-sys"
|
|
||||||
version = "0.3.69"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
|
|
||||||
dependencies = [
|
|
||||||
"wasm-bindgen",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@@ -122,7 +107,6 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
"tracing-error",
|
"tracing-error",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"web-time",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -394,70 +378,6 @@ version = "0.11.0+wasi-snapshot-preview1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen"
|
|
||||||
version = "0.2.92"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"wasm-bindgen-macro",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-backend"
|
|
||||||
version = "0.2.92"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
|
|
||||||
dependencies = [
|
|
||||||
"bumpalo",
|
|
||||||
"log",
|
|
||||||
"once_cell",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
"wasm-bindgen-shared",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-macro"
|
|
||||||
version = "0.2.92"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
|
|
||||||
dependencies = [
|
|
||||||
"quote",
|
|
||||||
"wasm-bindgen-macro-support",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-macro-support"
|
|
||||||
version = "0.2.92"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
"wasm-bindgen-backend",
|
|
||||||
"wasm-bindgen-shared",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-shared"
|
|
||||||
version = "0.2.92"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "web-time"
|
|
||||||
version = "1.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
|
|
||||||
dependencies = [
|
|
||||||
"js-sys",
|
|
||||||
"wasm-bindgen",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|||||||
@@ -14,4 +14,3 @@ spin_sleep = "1.1.1"
|
|||||||
tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]}
|
tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]}
|
||||||
tracing-error = "0.2.0"
|
tracing-error = "0.2.0"
|
||||||
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
|
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
|
||||||
web-time = "1.1.0"
|
|
||||||
|
|||||||
BIN
assets/map.png
BIN
assets/map.png
Binary file not shown.
|
Before Width: | Height: | Size: 28 KiB |
27
src/main.rs
27
src/main.rs
@@ -1,14 +1,11 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::rc::Rc;
|
use std::time::Duration;
|
||||||
|
|
||||||
use rand::seq::IteratorRandom;
|
|
||||||
use sdl2::event::Event;
|
use sdl2::event::Event;
|
||||||
use sdl2::image::LoadTexture;
|
use sdl2::image::LoadTexture;
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use sdl2::rect::{Point, Rect};
|
use sdl2::rect::{Point, Rect};
|
||||||
use web_time::{Duration, Instant};
|
|
||||||
|
|
||||||
static BLACK: Color = Color::RGB(0, 0, 0);
|
static BLACK: Color = Color::RGB(0, 0, 0);
|
||||||
|
|
||||||
@@ -60,6 +57,8 @@ fn main() {
|
|||||||
let texture_creator = canvas.texture_creator();
|
let texture_creator = canvas.texture_creator();
|
||||||
let mut point = Point::new(0, 0);
|
let mut point = Point::new(0, 0);
|
||||||
|
|
||||||
|
let mut prev = now();
|
||||||
|
|
||||||
// let ctx = Rc::new(RefCell::new(ctx));
|
// let ctx = Rc::new(RefCell::new(ctx));
|
||||||
// let canvas = Rc::new(RefCell::new(canvas));
|
// let canvas = Rc::new(RefCell::new(canvas));
|
||||||
// let texture_creator = Rc::new(texture_creator);
|
// let texture_creator = Rc::new(texture_creator);
|
||||||
@@ -70,9 +69,11 @@ fn main() {
|
|||||||
|
|
||||||
let target_fps = 60;
|
let target_fps = 60;
|
||||||
let frame_time = Duration::from_secs_f32(1.0) / target_fps;
|
let frame_time = Duration::from_secs_f32(1.0) / target_fps;
|
||||||
println!("Frame time: {:?}", frame_time);
|
|
||||||
let mut frame = 0;
|
let mut frame = 0;
|
||||||
|
|
||||||
|
let mut n = 0;
|
||||||
|
let mut avg_fps = 0f64;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let start = now();
|
let start = now();
|
||||||
let mut moved = false;
|
let mut moved = false;
|
||||||
@@ -93,11 +94,6 @@ fn main() {
|
|||||||
if frame > 7 {
|
if frame > 7 {
|
||||||
frame = 0;
|
frame = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// random number between 2 and 6
|
|
||||||
let sleep_time = (15..=500).choose(&mut rand::thread_rng()).unwrap();
|
|
||||||
println!("Sleeping for {} ms", sleep_time);
|
|
||||||
sleep(sleep_time);
|
|
||||||
}
|
}
|
||||||
Keycode::Left => {
|
Keycode::Left => {
|
||||||
point.x -= 32;
|
point.x -= 32;
|
||||||
@@ -158,15 +154,18 @@ fn main() {
|
|||||||
if elapsed < frame_time {
|
if elapsed < frame_time {
|
||||||
let sleep_time = frame_time - elapsed;
|
let sleep_time = frame_time - elapsed;
|
||||||
sleep(sleep_time.as_millis() as u32);
|
sleep(sleep_time.as_millis() as u32);
|
||||||
// println!("elapsed: {:?} sleep: {:?}ms ({:?})", elapsed, sleep_time, t2);
|
|
||||||
} else {
|
} else {
|
||||||
let excess = elapsed - frame_time;
|
let excess = elapsed - frame_time;
|
||||||
println!("! excess: {:?} ({:?})", excess, t2);
|
println!("! excess: {:?} ({:?})", excess, t2);
|
||||||
}
|
}
|
||||||
|
|
||||||
let t3 = now();
|
let duration = Duration::from_secs_f64(now() - prev);
|
||||||
let duration = Duration::from_secs_f64(t3 - start);
|
|
||||||
let fps = 1f64 / (duration.as_secs_f64());
|
let fps = 1f64 / (duration.as_secs_f64());
|
||||||
println!("FPS: {}", fps);
|
prev = now();
|
||||||
|
|
||||||
|
n += 1;
|
||||||
|
let a = 1f64 / n as f64;
|
||||||
|
let b = 1f64 - a;
|
||||||
|
avg_fps = a * fps + b * avg_fps;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user