2020-05-04 20:21:14 +00:00
|
|
|
use crate::{Diagnostic, DiagnosticId, Diagnostics};
|
2020-07-17 01:47:51 +00:00
|
|
|
use bevy_app::prelude::*;
|
2020-07-17 01:26:21 +00:00
|
|
|
use bevy_core::Time;
|
2020-12-16 05:57:16 +00:00
|
|
|
use bevy_ecs::{IntoSystem, Res, ResMut};
|
2020-05-04 20:21:14 +00:00
|
|
|
|
2020-10-14 22:13:43 +00:00
|
|
|
/// Adds "frame time" diagnostic to an App, specifically "frame time", "fps" and "frame count"
|
2020-05-04 20:21:14 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct FrameTimeDiagnosticsPlugin;
|
|
|
|
|
2020-10-14 22:13:43 +00:00
|
|
|
pub struct FrameTimeDiagnosticsState {
|
|
|
|
frame_count: f64,
|
|
|
|
}
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for FrameTimeDiagnosticsPlugin {
|
2020-05-04 20:21:14 +00:00
|
|
|
fn build(&self, app: &mut bevy_app::AppBuilder) {
|
2020-12-16 05:57:16 +00:00
|
|
|
app.add_startup_system(Self::setup_system.system())
|
2020-10-14 22:13:43 +00:00
|
|
|
.add_resource(FrameTimeDiagnosticsState { frame_count: 0.0 })
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_system(Self::diagnostic_system.system());
|
2020-05-04 20:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FrameTimeDiagnosticsPlugin {
|
|
|
|
pub const FPS: DiagnosticId = DiagnosticId::from_u128(288146834822086093791974408528866909483);
|
2020-10-14 22:13:43 +00:00
|
|
|
pub const FRAME_COUNT: DiagnosticId =
|
|
|
|
DiagnosticId::from_u128(54021991829115352065418785002088010277);
|
2020-05-04 20:21:14 +00:00
|
|
|
pub const FRAME_TIME: DiagnosticId =
|
2020-10-14 22:13:43 +00:00
|
|
|
DiagnosticId::from_u128(73441630925388532774622109383099159699);
|
2020-05-04 20:21:14 +00:00
|
|
|
|
2020-05-14 00:52:47 +00:00
|
|
|
pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
|
2020-05-18 21:53:57 +00:00
|
|
|
diagnostics.add(Diagnostic::new(Self::FRAME_TIME, "frame_time", 20));
|
|
|
|
diagnostics.add(Diagnostic::new(Self::FPS, "fps", 20));
|
2020-10-14 22:13:43 +00:00
|
|
|
diagnostics.add(Diagnostic::new(Self::FRAME_COUNT, "frame_count", 1));
|
2020-05-04 20:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 22:13:43 +00:00
|
|
|
pub fn diagnostic_system(
|
|
|
|
mut diagnostics: ResMut<Diagnostics>,
|
|
|
|
time: Res<Time>,
|
|
|
|
mut state: ResMut<FrameTimeDiagnosticsState>,
|
|
|
|
) {
|
|
|
|
state.frame_count += 1.0;
|
|
|
|
diagnostics.add_measurement(Self::FRAME_COUNT, state.frame_count);
|
|
|
|
|
2020-11-28 21:08:31 +00:00
|
|
|
if time.delta_seconds_f64() == 0.0 {
|
2020-05-04 20:21:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-28 21:08:31 +00:00
|
|
|
diagnostics.add_measurement(Self::FRAME_TIME, time.delta_seconds_f64());
|
2020-05-04 20:21:14 +00:00
|
|
|
if let Some(fps) = diagnostics
|
|
|
|
.get(Self::FRAME_TIME)
|
|
|
|
.and_then(|frame_time_diagnostic| {
|
|
|
|
frame_time_diagnostic
|
|
|
|
.average()
|
|
|
|
.and_then(|frame_time_average| {
|
|
|
|
if frame_time_average > 0.0 {
|
|
|
|
Some(1.0 / frame_time_average)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
{
|
|
|
|
diagnostics.add_measurement(Self::FPS, fps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|