mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2024-12-23 19:31:30 +11:00
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:
parent
2c3fc9526c
commit
adffe0814f
|
@ -27,7 +27,6 @@ cc = "1.0"
|
|||
|
||||
[dependencies]
|
||||
cast = "0.2"
|
||||
time = "0.1.34"
|
||||
raw-window-handle = "0.3.3"
|
||||
|
||||
[target.'cfg(windows)'.dependencies.winapi]
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use crate::{InputCallback, Key, KeyRepeat};
|
||||
use std::mem;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub struct KeyHandler {
|
||||
pub key_callback: Option<Box<dyn InputCallback>>,
|
||||
prev_time: f64,
|
||||
delta_time: f32,
|
||||
prev_time: Instant,
|
||||
delta_time: Duration,
|
||||
keys: [bool; 512],
|
||||
keys_prev: [bool; 512],
|
||||
keys_down_duration: [f32; 512],
|
||||
|
@ -19,8 +20,8 @@ impl KeyHandler {
|
|||
keys: [false; 512],
|
||||
keys_prev: [false; 512],
|
||||
keys_down_duration: [-1.0; 512],
|
||||
prev_time: time::precise_time_s(),
|
||||
delta_time: 0.0,
|
||||
prev_time: Instant::now(),
|
||||
delta_time: Duration::from_secs(0),
|
||||
key_repeat_delay: 0.250,
|
||||
key_repeat_rate: 0.050,
|
||||
}
|
||||
|
@ -49,10 +50,10 @@ impl KeyHandler {
|
|||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
let current_time = time::precise_time_s();
|
||||
let delta_time = (current_time - self.prev_time) as f32;
|
||||
let current_time = Instant::now();
|
||||
self.delta_time = self.prev_time.elapsed();
|
||||
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() {
|
||||
if self.keys[i] {
|
||||
|
@ -128,10 +129,11 @@ impl KeyHandler {
|
|||
}
|
||||
|
||||
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 rate = self.key_repeat_rate;
|
||||
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;
|
||||
}
|
||||
|
|
20
src/rate.rs
20
src/rate.rs
|
@ -1,8 +1,8 @@
|
|||
use std::time::Duration;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub struct UpdateRate {
|
||||
target_rate: Option<Duration>,
|
||||
prev_time: f64,
|
||||
prev_time: Instant,
|
||||
}
|
||||
|
||||
impl UpdateRate {
|
||||
|
@ -10,7 +10,7 @@ impl UpdateRate {
|
|||
UpdateRate {
|
||||
// Default limit to 4 ms
|
||||
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) {
|
||||
if let Some(rate) = self.target_rate {
|
||||
let target_rate = rate.as_secs_f64();
|
||||
let current_time = time::precise_time_s();
|
||||
let delta = current_time - self.prev_time;
|
||||
if let Some(target_rate) = self.target_rate {
|
||||
let delta = self.prev_time.elapsed();
|
||||
|
||||
if delta < target_rate {
|
||||
let sleep_time = target_rate - delta;
|
||||
if sleep_time > 0.0 {
|
||||
//println!("sleeping {} ms", sleep_time * 1000.0);
|
||||
std::thread::sleep(Duration::from_secs_f64(sleep_time));
|
||||
}
|
||||
//eprintln!("sleeping {} ms", sleep_time.as_secs_f64() * 1000.);
|
||||
std::thread::sleep(sleep_time);
|
||||
}
|
||||
|
||||
self.prev_time = time::precise_time_s();
|
||||
self.prev_time = Instant::now();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue