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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! FPS tracker utility
//!
//! PRECONDITION: plugin dependencies
//! - bevy::time::TimePlugin
//!

//local shortcuts

//third-party shortcuts
use bevy::prelude::*;
use bevy_fn_plugin::bevy_plugin;

//standard shortcuts
use std::collections::VecDeque;
use std::time::Duration;

//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

//todo: pass by value on construction
const FPS_TRACKER_NUM_RECORDS : u8  = 30;

//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

/// Update FPS tracker with new time.
fn update_fps_tracker(mut tracker: ResMut<FpsTracker>, time: Res<Time>)
{
    tracker.update(time.delta_seconds(), time.elapsed());
}

//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

/// FPS tracker resource.
#[derive(Resource)]
pub struct FpsTracker
{
    max_records     : u8,
    delta_record_ns : VecDeque<u64>,
    total_delta_ns  : u64,
    previous_time   : Duration,
    current_time    : Duration
}

impl FpsTracker
{
    /// Make new tracker.
    pub fn new(max: u8) -> FpsTracker
    {
        let mut new_tracker = 
            FpsTracker{
                    max_records     : max,
                    delta_record_ns : VecDeque::new(),
                    total_delta_ns  : 0u64,
                    previous_time   : Duration::from_secs(0),
                    current_time    : Duration::from_secs(0)
                };
        new_tracker.delta_record_ns.reserve((max + 1) as usize);
        return new_tracker
    }

    /// Average delta recorded (in seconds).
    pub fn average_delta_seconds(&self) -> f32
    {
        (self.average_delta_nanoseconds() as f32) / 1_000_000_000.0
    }

    /// Average delta recorded (in nanoseconds).
    pub fn average_delta_nanoseconds(&self) -> u64
    {
        match self.delta_record_ns.len()
        {
            0           => 0u64,
            num_records => self.total_delta_ns / (num_records as u64)
        }
    }

    /// Get FPS estimate.
    pub fn fps(&self) -> u16
    {
        match (1_000_000_000u64).checked_div(self.average_delta_nanoseconds())
        {
            Some(rate) => rate as u16,
            None       => 0
        }
    }

    pub fn previous_time(&self) -> Duration { self.previous_time }
    pub fn current_time(&self)  -> Duration { self.current_time  }

    /// Update the tracker with a new time.
    pub fn update(self: &mut FpsTracker, delta: f32, current_time: Duration)
    {
        // 1. add new record
        let delta_ns = (delta * 1_000_000_000.0) as u64;
        self.delta_record_ns.push_back(delta_ns);
        self.total_delta_ns += delta_ns;

        // 2. update current time
        self.previous_time = self.current_time;
        self.current_time  = current_time;

        // 3. remove excess records
        while self.delta_record_ns.len() > (self.max_records as usize)
        {
            self.total_delta_ns -= self.delta_record_ns.get(0).unwrap();
            self.delta_record_ns.pop_front();
        }
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// System set where the [`FpsTracker`] resource is updated.
#[derive(SystemSet, PartialEq, Eq, Debug, Hash, Clone)]
pub struct FpsTrackerSet;

/// Tracks FPS. Use the [`FpsTracker`] resource to access the fps with [`FpsTracker::fps()`].
#[bevy_plugin]
pub fn FpsTrackerPlugin(app: &mut App)
{
    app
        .insert_resource::<FpsTracker>(FpsTracker::new(FPS_TRACKER_NUM_RECORDS))
        .configure_sets(First, FpsTrackerSet.after(bevy::time::TimeSystem))
        .add_systems(First, update_fps_tracker.in_set(FpsTrackerSet));
}

//-------------------------------------------------------------------------------------------------------------------