1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// use crate::prelude::*;
use std::time::{Duration, Instant};
// use theframework::prelude::*;
pub struct UpdateTracker {
//update_counter: u32,
//last_fps_check: Instant,
next_redraw_update: Instant,
next_tick_update: Instant,
}
impl Default for UpdateTracker {
fn default() -> Self {
Self::new()
}
}
impl UpdateTracker {
pub fn new() -> Self {
let now = Instant::now();
UpdateTracker {
//update_counter: 0,
//last_fps_check: Instant::now(),
next_redraw_update: now,
next_tick_update: now,
}
}
pub fn update(&mut self, redraw_ms: u64, tick_ms: u64) -> (bool, bool) {
let mut redraw_update = false;
let mut tick_update = false;
let now = Instant::now();
let redraw_period = Duration::from_millis(redraw_ms.max(1));
let tick_period = Duration::from_millis(tick_ms.max(1));
// self.update_counter += 1;
// if self.last_fps_check.elapsed() >= Duration::from_secs(1) {
// self.calculate_and_reset_fps();
// }
if now >= self.next_redraw_update {
redraw_update = true;
while self.next_redraw_update <= now {
self.next_redraw_update += redraw_period;
}
}
if now >= self.next_tick_update {
tick_update = true;
while self.next_tick_update <= now {
self.next_tick_update += tick_period;
}
}
(redraw_update, tick_update)
}
// fn calculate_and_reset_fps(&mut self) {
// //let fps = self.update_counter;
// self.update_counter = 0;
// self.last_fps_check = Instant::now();
// //println!("FPS: {}", fps);
// }
}