Remove usage of the time crate (#183)

Replace calls to time::precise_time_s with std::time::Instant.

Also replaced the type of a time variable with std::time::Duration
because it played nicely with using Instant
This commit is contained in:
Arif Roktim 2020-05-21 03:42:43 -04:00 committed by GitHub
parent 2c3fc9526c
commit adffe0814f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 21 deletions

View file

@ -27,7 +27,6 @@ cc = "1.0"
[dependencies] [dependencies]
cast = "0.2" cast = "0.2"
time = "0.1.34"
raw-window-handle = "0.3.3" raw-window-handle = "0.3.3"
[target.'cfg(windows)'.dependencies.winapi] [target.'cfg(windows)'.dependencies.winapi]

View file

@ -1,10 +1,11 @@
use crate::{InputCallback, Key, KeyRepeat}; use crate::{InputCallback, Key, KeyRepeat};
use std::mem; use std::mem;
use std::time::{Duration, Instant};
pub struct KeyHandler { pub struct KeyHandler {
pub key_callback: Option<Box<dyn InputCallback>>, pub key_callback: Option<Box<dyn InputCallback>>,
prev_time: f64, prev_time: Instant,
delta_time: f32, delta_time: Duration,
keys: [bool; 512], keys: [bool; 512],
keys_prev: [bool; 512], keys_prev: [bool; 512],
keys_down_duration: [f32; 512], keys_down_duration: [f32; 512],
@ -19,8 +20,8 @@ impl KeyHandler {
keys: [false; 512], keys: [false; 512],
keys_prev: [false; 512], keys_prev: [false; 512],
keys_down_duration: [-1.0; 512], keys_down_duration: [-1.0; 512],
prev_time: time::precise_time_s(), prev_time: Instant::now(),
delta_time: 0.0, delta_time: Duration::from_secs(0),
key_repeat_delay: 0.250, key_repeat_delay: 0.250,
key_repeat_rate: 0.050, key_repeat_rate: 0.050,
} }
@ -49,10 +50,10 @@ impl KeyHandler {
} }
pub fn update(&mut self) { pub fn update(&mut self) {
let current_time = time::precise_time_s(); let current_time = Instant::now();
let delta_time = (current_time - self.prev_time) as f32; self.delta_time = self.prev_time.elapsed();
self.prev_time = current_time; self.prev_time = current_time;
self.delta_time = delta_time; let delta_time = self.delta_time.as_secs_f32();
for i in 0..self.keys.len() { for i in 0..self.keys.len() {
if self.keys[i] { if self.keys[i] {
@ -128,10 +129,11 @@ impl KeyHandler {
} }
if repeat == KeyRepeat::Yes && t > self.key_repeat_delay { if repeat == KeyRepeat::Yes && t > self.key_repeat_delay {
let delta_time = self.delta_time.as_secs_f32();
let delay = self.key_repeat_delay; let delay = self.key_repeat_delay;
let rate = self.key_repeat_rate; let rate = self.key_repeat_rate;
if (((t - delay) % rate) > rate * 0.5) if (((t - delay) % rate) > rate * 0.5)
!= (((t - delay - self.delta_time) % rate) > rate * 0.5) != (((t - delay - delta_time) % rate) > rate * 0.5)
{ {
return true; return true;
} }

View file

@ -1,8 +1,8 @@
use std::time::Duration; use std::time::{Duration, Instant};
pub struct UpdateRate { pub struct UpdateRate {
target_rate: Option<Duration>, target_rate: Option<Duration>,
prev_time: f64, prev_time: Instant,
} }
impl UpdateRate { impl UpdateRate {
@ -10,7 +10,7 @@ impl UpdateRate {
UpdateRate { UpdateRate {
// Default limit to 4 ms // Default limit to 4 ms
target_rate: Some(Duration::from_millis(4)), target_rate: Some(Duration::from_millis(4)),
prev_time: 0.0, prev_time: Instant::now(),
} }
} }
@ -20,20 +20,16 @@ impl UpdateRate {
} }
pub fn update(&mut self) { pub fn update(&mut self) {
if let Some(rate) = self.target_rate { if let Some(target_rate) = self.target_rate {
let target_rate = rate.as_secs_f64(); let delta = self.prev_time.elapsed();
let current_time = time::precise_time_s();
let delta = current_time - self.prev_time;
if delta < target_rate { if delta < target_rate {
let sleep_time = target_rate - delta; let sleep_time = target_rate - delta;
if sleep_time > 0.0 { //eprintln!("sleeping {} ms", sleep_time.as_secs_f64() * 1000.);
//println!("sleeping {} ms", sleep_time * 1000.0); std::thread::sleep(sleep_time);
std::thread::sleep(Duration::from_secs_f64(sleep_time));
}
} }
self.prev_time = time::precise_time_s(); self.prev_time = Instant::now();
} }
} }
} }