1
0
mirror of https://github.com/4yn/slidershim.git synced 2025-02-02 04:27:58 +01:00

fix time overflow

This commit is contained in:
4yn 2022-02-24 10:35:35 +08:00
parent af16cd43c8
commit 47b90603e2

View File

@ -43,6 +43,7 @@ impl Error for ShimError {
pub struct LoopTimer { pub struct LoopTimer {
cap: usize, cap: usize,
cur: usize, cur: usize,
init: usize,
buf: Vec<Instant>, buf: Vec<Instant>,
freq: Arc<AtomicF64>, freq: Arc<AtomicF64>,
} }
@ -52,16 +53,22 @@ impl LoopTimer {
Self { Self {
cap: 100, cap: 100,
cur: 0, cur: 0,
buf: vec![Instant::now() - Duration::from_secs(10_000); 100], init: 0,
buf: vec![Instant::now(); 100],
freq: Arc::new(AtomicF64::new(0.0)), freq: Arc::new(AtomicF64::new(0.0)),
} }
} }
pub fn tick(&mut self) { pub fn tick(&mut self) {
let last = self.buf[self.cur]; let last = self.buf[self.cur];
let now = Instant::now(); let now = Instant::now();
if self.init < 100 {
self.init += 1;
}
self.buf[self.cur] = now; self.buf[self.cur] = now;
let delta = (now - last) / 100 + Duration::from_micros(1); let delta = (now - last) / (self.init as u32) + Duration::from_micros(1);
let freq = Duration::from_millis(1000) let freq = Duration::from_millis(1000)
.div_duration_f64(delta) .div_duration_f64(delta)
.clamp(0.0, 9999.0); .clamp(0.0, 9999.0);
@ -75,7 +82,8 @@ impl LoopTimer {
#[allow(dead_code)] #[allow(dead_code)]
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.buf = vec![Instant::now() - Duration::from_secs(10); 100]; self.init = 0;
self.buf = vec![Instant::now(); 100];
self.cur = 0; self.cur = 0;
} }