aspen_engine/timing/
mod.rs

1use std::time::Instant;
2
3pub struct TimingStruct {
4    pub begin_time: Instant,
5    pub prev_time: Instant,
6    pub current_time: Instant,
7    pub cumulative: f64,
8}
9
10impl TimingStruct {
11    pub fn new() -> TimingStruct {
12        TimingStruct {
13            begin_time: Instant::now(),
14            prev_time: Instant::now(),
15            current_time: Instant::now(),
16            cumulative: 0.0
17        }
18    }
19
20    /// Updates self and returns info
21    pub fn update(&mut self, fixed_rate: u16) -> UpdateTimes {
22        self.prev_time = self.current_time;
23        self.current_time = Instant::now();
24        let delta = self.current_time.duration_since(self.prev_time).as_secs_f64();
25        self.cumulative += delta;
26
27        let fixed_delta = 1.0 / fixed_rate as f64;
28        let fixed_steps = (self.cumulative / fixed_delta) as u64;
29        self.cumulative %= fixed_delta;
30
31        UpdateTimes {
32            delta,
33            fixed_delta,
34            fixed_steps
35        }
36    }
37}
38
39pub struct UpdateTimes {
40    pub delta: f64,
41    pub fixed_delta: f64,
42    pub fixed_steps: u64
43}