diff --git a/Cargo.lock b/Cargo.lock index 598409e..27a9171 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,12 +17,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - [[package]] name = "cfg-if" version = "1.0.0" @@ -46,15 +40,6 @@ dependencies = [ "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]] name = "lazy_static" version = "1.4.0" @@ -122,7 +107,6 @@ dependencies = [ "tracing", "tracing-error", "tracing-subscriber", - "web-time", ] [[package]] @@ -394,70 +378,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 93f89c7..0fa1a90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,3 @@ spin_sleep = "1.1.1" tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]} tracing-error = "0.2.0" tracing-subscriber = {version = "0.3.17", features = ["env-filter"]} -web-time = "1.1.0" diff --git a/assets/map.png b/assets/map.png deleted file mode 100644 index 731f68a..0000000 Binary files a/assets/map.png and /dev/null differ diff --git a/src/main.rs b/src/main.rs index f945cfa..1d14b4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,11 @@ -use std::cell::RefCell; use std::process; -use std::rc::Rc; +use std::time::Duration; -use rand::seq::IteratorRandom; use sdl2::event::Event; use sdl2::image::LoadTexture; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; use sdl2::rect::{Point, Rect}; -use web_time::{Duration, Instant}; static BLACK: Color = Color::RGB(0, 0, 0); @@ -60,6 +57,8 @@ fn main() { let texture_creator = canvas.texture_creator(); let mut point = Point::new(0, 0); + let mut prev = now(); + // let ctx = Rc::new(RefCell::new(ctx)); // let canvas = Rc::new(RefCell::new(canvas)); // let texture_creator = Rc::new(texture_creator); @@ -70,9 +69,11 @@ fn main() { let target_fps = 60; let frame_time = Duration::from_secs_f32(1.0) / target_fps; - println!("Frame time: {:?}", frame_time); let mut frame = 0; + let mut n = 0; + let mut avg_fps = 0f64; + loop { let start = now(); let mut moved = false; @@ -93,11 +94,6 @@ fn main() { if frame > 7 { 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 => { point.x -= 32; @@ -158,15 +154,18 @@ fn main() { if elapsed < frame_time { let sleep_time = frame_time - elapsed; sleep(sleep_time.as_millis() as u32); - // println!("elapsed: {:?} sleep: {:?}ms ({:?})", elapsed, sleep_time, t2); } else { let excess = elapsed - frame_time; println!("! excess: {:?} ({:?})", excess, t2); } - let t3 = now(); - let duration = Duration::from_secs_f64(t3 - start); + let duration = Duration::from_secs_f64(now() - prev); 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; } }