bevy_diagnostic/
frame_time_diagnostics_plugin.rs

1use crate::{
2    Diagnostic, DiagnosticPath, Diagnostics, FrameCount, RegisterDiagnostic,
3    DEFAULT_MAX_HISTORY_LENGTH,
4};
5use bevy_app::prelude::*;
6use bevy_ecs::prelude::*;
7use bevy_time::{Real, Time};
8
9/// Adds "frame time" diagnostic to an App, specifically "frame time", "fps" and "frame count"
10///
11/// # See also
12///
13/// [`LogDiagnosticsPlugin`](crate::LogDiagnosticsPlugin) to output diagnostics to the console.
14pub struct FrameTimeDiagnosticsPlugin {
15    /// The total number of values to keep for averaging.
16    pub max_history_length: usize,
17    /// The smoothing factor for the exponential moving average. Usually `2.0 / (history_length + 1.0)`.
18    pub smoothing_factor: f64,
19}
20impl Default for FrameTimeDiagnosticsPlugin {
21    fn default() -> Self {
22        Self::new(DEFAULT_MAX_HISTORY_LENGTH)
23    }
24}
25impl FrameTimeDiagnosticsPlugin {
26    /// Creates a new `FrameTimeDiagnosticsPlugin` with the specified `max_history_length` and a
27    /// reasonable `smoothing_factor`.
28    pub fn new(max_history_length: usize) -> Self {
29        Self {
30            max_history_length,
31            smoothing_factor: 2.0 / (max_history_length as f64 + 1.0),
32        }
33    }
34}
35
36impl Plugin for FrameTimeDiagnosticsPlugin {
37    fn build(&self, app: &mut App) {
38        app.register_diagnostic(
39            Diagnostic::new(Self::FRAME_TIME)
40                .with_suffix("ms")
41                .with_max_history_length(self.max_history_length)
42                .with_smoothing_factor(self.smoothing_factor),
43        )
44        .register_diagnostic(
45            Diagnostic::new(Self::FPS)
46                .with_max_history_length(self.max_history_length)
47                .with_smoothing_factor(self.smoothing_factor),
48        )
49        // An average frame count would be nonsensical, so we set the max history length
50        // to zero and disable smoothing.
51        .register_diagnostic(
52            Diagnostic::new(Self::FRAME_COUNT)
53                .with_smoothing_factor(0.0)
54                .with_max_history_length(0),
55        )
56        .add_systems(Update, Self::diagnostic_system);
57    }
58}
59
60impl FrameTimeDiagnosticsPlugin {
61    pub const FPS: DiagnosticPath = DiagnosticPath::const_new("fps");
62    pub const FRAME_COUNT: DiagnosticPath = DiagnosticPath::const_new("frame_count");
63    pub const FRAME_TIME: DiagnosticPath = DiagnosticPath::const_new("frame_time");
64
65    pub fn diagnostic_system(
66        mut diagnostics: Diagnostics,
67        time: Res<Time<Real>>,
68        frame_count: Res<FrameCount>,
69    ) {
70        diagnostics.add_measurement(&Self::FRAME_COUNT, || frame_count.0 as f64);
71
72        let delta_seconds = time.delta_secs_f64();
73        if delta_seconds == 0.0 {
74            return;
75        }
76
77        diagnostics.add_measurement(&Self::FRAME_TIME, || delta_seconds * 1000.0);
78
79        diagnostics.add_measurement(&Self::FPS, || 1.0 / delta_seconds);
80    }
81}