maplibre/util/
fps_meter.rs

1use std::time::Duration;
2
3use instant::Instant;
4
5/// Measures the frames per second.
6///
7/// # Example
8/// ```
9/// use maplibre::util::FPSMeter;
10///
11/// let mut meter = FPSMeter::new();
12///
13/// // call the following the the render loop
14/// meter.update_and_print();
15/// ```
16pub struct FPSMeter {
17    next_report: Instant,
18    frame_count: u32,
19}
20
21impl FPSMeter {
22    pub fn new() -> Self {
23        let start = Instant::now();
24        Self {
25            next_report: start + Duration::from_secs(1),
26            frame_count: 0,
27        }
28    }
29
30    pub fn update_and_print(&mut self) {
31        self.frame_count += 1;
32        let now = Instant::now();
33        if now >= self.next_report {
34            log::warn!("{} FPS", self.frame_count);
35            self.frame_count = 0;
36            self.next_report = now + Duration::from_secs(1);
37        }
38    }
39}
40
41impl Default for FPSMeter {
42    fn default() -> Self {
43        Self::new()
44    }
45}