-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::{Diagnostic, DiagnosticId, Diagnostics}; | ||
use bevy_app::AppPlugin; | ||
use bevy_core::time::Time; | ||
use legion::prelude::{IntoSystem, Resource, ResourceMut}; | ||
|
||
#[derive(Default)] | ||
pub struct FrameTimeDiagnosticsPlugin; | ||
|
||
impl AppPlugin for FrameTimeDiagnosticsPlugin { | ||
fn build(&self, app: &mut bevy_app::AppBuilder) { | ||
app.add_startup_system(Self::setup_system.system()) | ||
.add_system(Self::diagnostic_system.system()); | ||
} | ||
} | ||
|
||
impl FrameTimeDiagnosticsPlugin { | ||
pub const FPS: DiagnosticId = DiagnosticId::from_u128(288146834822086093791974408528866909483); | ||
pub const FRAME_TIME: DiagnosticId = | ||
DiagnosticId::from_u128(54021991829115352065418785002088010276); | ||
|
||
pub fn setup_system(mut diagnostics: ResourceMut<Diagnostics>) { | ||
diagnostics.add(Diagnostic::new(Self::FRAME_TIME, "frame_time", 10)); | ||
diagnostics.add(Diagnostic::new(Self::FPS, "fps", 10)); | ||
} | ||
|
||
pub fn diagnostic_system(mut diagnostics: ResourceMut<Diagnostics>, time: Resource<Time>) { | ||
if time.delta_seconds_f64 == 0.0 { | ||
return; | ||
} | ||
|
||
diagnostics.add_measurement(Self::FRAME_TIME, time.delta_seconds_f64); | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters