Skip to main content

rustapi/
misc.rs

1// use crate::prelude::*;
2use std::time::{Duration, Instant};
3// use theframework::prelude::*;
4
5pub struct UpdateTracker {
6    //update_counter: u32,
7    //last_fps_check: Instant,
8    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            //update_counter: 0,
23            //last_fps_check: Instant::now(),
24            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        // self.update_counter += 1;
37
38        // if self.last_fps_check.elapsed() >= Duration::from_secs(1) {
39        //     self.calculate_and_reset_fps();
40        // }
41
42        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    // fn calculate_and_reset_fps(&mut self) {
60    //     //let fps = self.update_counter;
61    //     self.update_counter = 0;
62    //     self.last_fps_check = Instant::now();
63    //     //println!("FPS: {}", fps);
64    // }
65}