From 902b2b3136a49738de32a6b2fd1df78d18cf6048 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 17 Jan 2021 16:06:24 +0100 Subject: [PATCH] Use durations everywhere! --- Cargo.lock | 307 +++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- src/common.rs | 2 - src/core/app.rs | 33 ++---- src/core/controller.rs | 28 ++--- src/core/game.rs | 14 +-- 6 files changed, 331 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 55e8342..dd144ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,12 +13,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] +name = "base-x" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" + +[[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] +name = "bumpalo" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" + +[[package]] name = "c_vec" version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -31,6 +43,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] name = "cloudabi" version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -40,12 +58,30 @@ dependencies = [ ] [[package]] +name = "const_fn" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b9d6de7f49e22cf97ad17fc4036ece69300032f45f78f30b4a4482cdc3f4a6" + +[[package]] +name = "discard" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" + +[[package]] name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" [[package]] +name = "itoa" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" + +[[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -53,9 +89,18 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.81" +version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" +checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" + +[[package]] +name = "log" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcf3805d4480bb5b86070dcfeb9e2cb2ebc148adb753c5cca5f884d1d65a42b2" +dependencies = [ + "cfg-if 0.1.10", +] [[package]] name = "num" @@ -99,6 +144,30 @@ dependencies = [ ] [[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +dependencies = [ + "proc-macro2", +] + +[[package]] name = "rand" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -214,6 +283,21 @@ dependencies = [ ] [[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] name = "sdl-test" version = "0.1.0" dependencies = [ @@ -243,26 +327,231 @@ version = "0.32.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34e71125077d297d57e4c1acfe8981b5bdfbf5a20e7b589abfdcb33bf1127f86" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", ] [[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "serde" +version = "1.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bdd36f49e35b61d49efd8aa7fc068fd295961fd2286d0b2ee9a4c7a14e99cc3" + +[[package]] +name = "serde_derive" +version = "1.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" + +[[package]] +name = "standback" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c66a8cff4fa24853fdf6b51f75c6d7f8206d7c75cab4e467bcd7f25c2b1febe0" +dependencies = [ + "version_check", +] + +[[package]] +name = "stdweb" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5" +dependencies = [ + "discard", + "rustc_version", + "stdweb-derive", + "stdweb-internal-macros", + "stdweb-internal-runtime", + "wasm-bindgen", +] + +[[package]] +name = "stdweb-derive" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "serde_derive", + "syn", +] + +[[package]] +name = "stdweb-internal-macros" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11" +dependencies = [ + "base-x", + "proc-macro2", + "quote", + "serde", + "serde_derive", + "serde_json", + "sha1", + "syn", +] + +[[package]] +name = "stdweb-internal-runtime" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" + +[[package]] +name = "syn" +version = "1.0.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] name = "time" -version = "0.1.44" +version = "0.2.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +checksum = "273d3ed44dca264b0d6b3665e8d48fb515042d42466fad93d2a45b90ec4058f7" dependencies = [ + "const_fn", "libc", - "wasi", + "standback", + "stdweb", + "time-macros", + "version_check", "winapi", ] [[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" +name = "time-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "957e9c6e26f12cb6d0dd7fc776bb67a706312e7299aed74c8dd5b17ebb27e2f1" +dependencies = [ + "proc-macro-hack", + "time-macros-impl", +] + +[[package]] +name = "time-macros-impl" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5c3be1edfad6027c69f5491cf4cb310d1a71ecd6af742788c6ff8bced86b8fa" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "standback", + "syn", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "wasm-bindgen" +version = "0.2.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6ac8995ead1f084a8dea1e65f194d0973800c7f571f6edd70adf06ecf77084" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158" [[package]] name = "winapi" diff --git a/Cargo.toml b/Cargo.toml index 4486569..de70b9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Tomas Wenström "] [dependencies] rand = "0.6.5" -time = "0.1.42" +time = "0.2.24" [dependencies.sdl2] version = "0.32.1" diff --git a/src/common.rs b/src/common.rs index 6306bf6..d2121b0 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,7 +1,5 @@ use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg}; -pub type Nanoseconds = u64; - #[macro_export] macro_rules! point { ( $x:expr, $y:expr ) => { diff --git a/src/core/app.rs b/src/core/app.rs index ddfe3d9..be546c1 100644 --- a/src/core/app.rs +++ b/src/core/app.rs @@ -1,5 +1,5 @@ use boll::*; -use common::{Nanoseconds, Point2D, Rect}; +use common::{Point2D, Rect}; use core::controller::ControllerManager; use point; // defined in common, but loaded from main... use rand::Rng; @@ -13,7 +13,7 @@ use sdl2::video::{FullscreenType, SwapInterval, Window}; use sdl2::{EventPump, VideoSubsystem}; use sprites::SpriteManager; use std::f32::consts::PI; -use time::PreciseTime; +use time::{Duration, Instant, prelude::*}; const FPS: u32 = 60; const NS_PER_FRAME: u32 = 1_000_000_000 / FPS; @@ -144,9 +144,7 @@ impl App { } pub fn start(&mut self) { - // let mut frame_count: u64 = 0; - // let mut fps_time = PreciseTime::now(); - let mut last_time = PreciseTime::now(); + let mut last_time = Instant::now(); self.state.enter(&mut self.ctrl_man); @@ -155,22 +153,13 @@ impl App { break 'running; } - let duration = - last_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as Nanoseconds; - last_time = PreciseTime::now(); + let duration = Instant::now() - last_time; + last_time = Instant::now(); + self.ctrl_man.update(duration); self.state.update(duration); self.render(); - - // frame_count += 1; - // if frame_count == FPS as u64 { - // let duration = fps_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as f64 - // / 1_000_000_000.0; - // // println!("fps: {}", frame_count as f64 / duration); - // frame_count = 0; - // fps_time = PreciseTime::now(); - // } } self.state.leave(); @@ -259,7 +248,7 @@ impl App { pub trait AppState { fn enter(&mut self, ctrl_man: &mut ControllerManager); fn leave(&mut self); - fn update(&mut self, dt: Nanoseconds); + fn update(&mut self, dt: Duration); fn render(&mut self, canvas: &mut Canvas, sprites: &mut SpriteManager); fn handle_event(&mut self, event: Event); } @@ -310,16 +299,16 @@ impl ActiveState { } impl AppState for ActiveState { - fn enter(&mut self, ctrl_man: &mut ControllerManager) {} + fn enter(&mut self, _ctrl_man: &mut ControllerManager) {} - fn update(&mut self, dt: Nanoseconds) { + fn update(&mut self, dt: Duration) { for b in &mut self.bolls { b.update(); } match dt { - ns if ns < (NS_PER_FRAME - 90_0000) as u64 => self.change_boll_count(100), - ns if ns > (NS_PER_FRAME + 90_0000) as u64 => self.change_boll_count(-100), + ns if ns < (NS_PER_FRAME - 90_0000).nanoseconds() => self.change_boll_count(100), + ns if ns > (NS_PER_FRAME + 90_0000).nanoseconds() => self.change_boll_count(-100), _ => {} } } diff --git a/src/core/controller.rs b/src/core/controller.rs index 127c309..15b3a70 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -1,31 +1,31 @@ -use sdl2::JoystickSubsystem; -use common::Nanoseconds; -use common::Radians; use common::Point2D; +use common::Radians; use sdl2::HapticSubsystem; +use sdl2::JoystickSubsystem; use sdl2::event::Event; use sdl2::haptic::Haptic; use sdl2::joystick::Joystick; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; +use time::{Duration, prelude::*}; #[derive(Debug, Default)] pub struct Button { - pub time_pressed: Nanoseconds, - pub time_released: Nanoseconds, + pub time_pressed: Duration, + pub time_released: Duration, pub is_pressed: bool, pub was_pressed: bool, pub toggle: bool, } impl Button { - fn update(&mut self, device: &Joystick, dt: Nanoseconds, btn: u8) { + fn update(&mut self, device: &Joystick, dt: Duration, btn: u8) { self.was_pressed = self.is_pressed; self.is_pressed = match device.button(btn as u32) { Ok(true) => { if !self.was_pressed { - self.time_pressed = 0; + self.time_pressed = 0.seconds(); self.toggle = !self.toggle; } self.time_pressed += dt; @@ -33,7 +33,7 @@ impl Button { } Ok(false) => { if self.was_pressed { - self.time_released = 0; + self.time_released = 0.seconds(); } self.time_released += dt; false @@ -50,7 +50,7 @@ pub struct Axis { impl Axis { #[allow(dead_code)] - fn update(&mut self, device: &Joystick, _dt: Nanoseconds, axis: u8) { + fn update(&mut self, device: &Joystick, _dt: Duration, axis: u8) { self.val = match device.axis(axis as u32) { Ok(val) => val as f32 / 32768.0, Err(_) => panic!("invalid axis {}", axis), @@ -67,7 +67,7 @@ pub struct Stick { } impl Stick { - fn update(&mut self, device: &Joystick, _dt: Nanoseconds, x_axis: u8, y_axis: u8) { + fn update(&mut self, device: &Joystick, _dt: Duration, x_axis: u8, y_axis: u8) { self.x = match device.axis(x_axis as u32) { Ok(val) => val as f32 / 32768.0, Err(_) => panic!("invalid x axis {}", x_axis), @@ -141,7 +141,7 @@ impl Controller { } } - pub fn update(&mut self, dt: Nanoseconds) { + pub fn update(&mut self, dt: Duration) { self.mov.update(&self.device, dt, 0, 1); // left stick self.aim.update(&self.device, dt, 3, 4); // right stick self.jump.update(&self.device, dt, 4); // left shoulder @@ -150,9 +150,9 @@ impl Controller { } /// strength [0 - 1] - pub fn rumble(&self, strength: f32, duration_ms: u32) { + pub fn rumble(&self, strength: f32, duration: Duration) { if let Some(h) = &self.haptic { - h.borrow_mut().rumble_play(strength, duration_ms); + h.borrow_mut().rumble_play(strength, duration.whole_milliseconds() as u32); } } } @@ -182,7 +182,7 @@ impl ControllerManager { } } - pub fn update(&mut self, dt: Nanoseconds) { + pub fn update(&mut self, dt: Duration) { self.controllers.iter().for_each(|(_, v)| v.borrow_mut().update(dt)); } diff --git a/src/core/game.rs b/src/core/game.rs index bf7d2df..80e1e36 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -1,5 +1,4 @@ use AppState; -use common::Nanoseconds; use common::Point2D; use core::controller::Controller; use core::controller::ControllerManager; @@ -11,6 +10,7 @@ use sdl2::video::Window; use sprites::SpriteManager; use std::cell::RefCell; use std::rc::Rc; +use time::Duration; ////////// GAMESTATE /////////////////////////////////////////////////////////// @@ -36,7 +36,7 @@ impl AppState for GameState { fn leave(&mut self) {} - fn update(&mut self, dt: Nanoseconds) { + fn update(&mut self, dt: Duration) { self.world.update(dt); } @@ -66,7 +66,7 @@ impl World { } } - pub fn update(&mut self, dt: Nanoseconds) { + pub fn update(&mut self, dt: Duration) { let mut breeding_ground = vec!(); for i in (0..self.objects.len()).rev() { @@ -116,7 +116,7 @@ impl Level { type Objects = Vec>; pub trait Object { - fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Nanoseconds) -> ObjectState; + fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState; fn render(&self, _canvas: &mut Canvas, _sprites: &mut SpriteManager) {} } @@ -147,7 +147,7 @@ impl Character { } impl Object for Character { - fn update(&mut self, objects: &mut Objects, lvl: &Level, _dt: Nanoseconds) -> ObjectState { + fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState { self.vel += lvl.gravity; self.pos += self.vel; @@ -173,7 +173,7 @@ impl Object for Character { bounces: 2, })); } - ctrl.rumble(1.0, _dt as u32 / 1_000_000); + ctrl.rumble(1.0, dt); } match ctrl.mov.x { @@ -223,7 +223,7 @@ pub struct Boll { } impl Object for Boll { - fn update(&mut self, _objects: &mut Objects, lvl: &Level, _dt: Nanoseconds) -> ObjectState { + fn update(&mut self, _objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState { self.vel += lvl.gravity; self.pos += self.vel; -- 2.11.0