Skip to main content

antbox_engine/
ticktimer.rs

1use std::time::{Duration, Instant};
2
3/// A timer for controlling engine update frequency
4#[derive(Debug)]
5pub struct TickTimer {
6    interval: Duration,
7    next: Instant,
8}
9
10impl TickTimer {
11    /// Start a new timer with a `millis` interval in ms
12    pub fn start(millis: u64) -> Self {
13        let interval = Duration::from_millis(millis);
14        TickTimer {
15            interval,
16            next: Instant::now() + interval,
17        }
18    }
19
20    pub(crate) fn wait_for_tick(&mut self) {
21        std::thread::sleep(self.next - Instant::now());
22        self.next = Instant::now() + self.interval;
23        // log::debug!("TICK");
24    }
25}