bevy_diagnostic/
frame_time_diagnostics_plugin.rs1use crate::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic};
2use bevy_app::prelude::*;
3use bevy_core::FrameCount;
4use bevy_ecs::prelude::*;
5use bevy_time::{Real, Time};
6
7#[derive(Default)]
13pub struct FrameTimeDiagnosticsPlugin;
14
15impl Plugin for FrameTimeDiagnosticsPlugin {
16 fn build(&self, app: &mut App) {
17 app.register_diagnostic(Diagnostic::new(Self::FRAME_TIME).with_suffix("ms"))
18 .register_diagnostic(Diagnostic::new(Self::FPS))
19 .register_diagnostic(Diagnostic::new(Self::FRAME_COUNT).with_smoothing_factor(0.0))
20 .add_systems(Update, Self::diagnostic_system);
21 }
22}
23
24impl FrameTimeDiagnosticsPlugin {
25 pub const FPS: DiagnosticPath = DiagnosticPath::const_new("fps");
26 pub const FRAME_COUNT: DiagnosticPath = DiagnosticPath::const_new("frame_count");
27 pub const FRAME_TIME: DiagnosticPath = DiagnosticPath::const_new("frame_time");
28
29 pub fn diagnostic_system(
30 mut diagnostics: Diagnostics,
31 time: Res<Time<Real>>,
32 frame_count: Res<FrameCount>,
33 ) {
34 diagnostics.add_measurement(&Self::FRAME_COUNT, || frame_count.0 as f64);
35
36 let delta_seconds = time.delta_secs_f64();
37 if delta_seconds == 0.0 {
38 return;
39 }
40
41 diagnostics.add_measurement(&Self::FRAME_TIME, || delta_seconds * 1000.0);
42
43 diagnostics.add_measurement(&Self::FPS, || 1.0 / delta_seconds);
44 }
45}