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
use std::{
time::Duration,
f64::NAN,
};
pub struct FrameCounter {
fps: f64,
tacc: Duration,
pub decay: f64,
pub log_period: Option<Duration>,
}
impl FrameCounter {
pub fn new() -> Self {
Self {
fps: NAN, tacc: Duration::from_secs(0),
decay: 1e-1, log_period: None,
}
}
pub fn new_with_log(log_period: Duration) -> Self {
Self {
fps: NAN, tacc: Duration::from_secs(0),
decay: 1e-1, log_period: Some(log_period),
}
}
pub fn fps(&self) -> f64 {
self.fps
}
pub fn step_frame(&mut self, time: Duration, n_passes: usize) {
let cfps = (n_passes as f64)/((time.as_secs() as f64) + 1e-6*(time.as_micros() as f64));
if self.fps.is_nan() {
self.fps = cfps;
} else {
self.fps = (1.0 - self.decay)*self.fps + self.decay*cfps;
}
self.tacc += time;
if let Some(log_period) = self.log_period {
if self.tacc > log_period {
println!("FPS: {:.2}", self.fps);
self.tacc = Duration::from_secs(0);
}
}
}
}