1use std::time::{Duration, Instant};
3pub struct UpdateTracker {
6 next_redraw_update: Instant,
9 next_tick_update: Instant,
10}
11
12impl Default for UpdateTracker {
13 fn default() -> Self {
14 Self::new()
15 }
16}
17
18impl UpdateTracker {
19 pub fn new() -> Self {
20 let now = Instant::now();
21 UpdateTracker {
22 next_redraw_update: now,
25 next_tick_update: now,
26 }
27 }
28
29 pub fn update(&mut self, redraw_ms: u64, tick_ms: u64) -> (bool, bool) {
30 let mut redraw_update = false;
31 let mut tick_update = false;
32 let now = Instant::now();
33 let redraw_period = Duration::from_millis(redraw_ms.max(1));
34 let tick_period = Duration::from_millis(tick_ms.max(1));
35
36 if now >= self.next_redraw_update {
43 redraw_update = true;
44 while self.next_redraw_update <= now {
45 self.next_redraw_update += redraw_period;
46 }
47 }
48
49 if now >= self.next_tick_update {
50 tick_update = true;
51 while self.next_tick_update <= now {
52 self.next_tick_update += tick_period;
53 }
54 }
55
56 (redraw_update, tick_update)
57 }
58
59 }