frame time diagnostic plugin
This commit is contained in:
		
							parent
							
								
									75f1362433
								
							
						
					
					
						commit
						ea16f6fc56
					
				| @ -3,42 +3,6 @@ use bevy_core::time::Time; | |||||||
| use legion::prelude::*; | use legion::prelude::*; | ||||||
| use std::time::Duration; | use std::time::Duration; | ||||||
| 
 | 
 | ||||||
| pub const FPS: DiagnosticId = DiagnosticId::from_u128(288146834822086093791974408528866909483); |  | ||||||
| pub const FRAME_TIME: DiagnosticId = |  | ||||||
|     DiagnosticId::from_u128(54021991829115352065418785002088010276); |  | ||||||
| 
 |  | ||||||
| pub fn setup_frame_time_diagnostic_system(mut diagnostics: ResourceMut<Diagnostics>) { |  | ||||||
|     diagnostics.add(Diagnostic::new(FRAME_TIME, "frame_time", 10)); |  | ||||||
|     diagnostics.add(Diagnostic::new(FPS, "fps", 10)); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub fn frame_time_diagnostic_system( |  | ||||||
|     mut diagnostics: ResourceMut<Diagnostics>, |  | ||||||
|     time: Resource<Time>, |  | ||||||
| ) { |  | ||||||
|     if time.delta_seconds_f64 == 0.0 { |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     diagnostics.add_measurement(FRAME_TIME, time.delta_seconds_f64); |  | ||||||
|     if let Some(fps) = diagnostics |  | ||||||
|         .get(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(FPS, fps); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub struct PrintDiagnosticsState { | pub struct PrintDiagnosticsState { | ||||||
|     elapsed: f64, |     elapsed: f64, | ||||||
|     wait_seconds: f64, |     wait_seconds: f64, | ||||||
|  | |||||||
							
								
								
									
										49
									
								
								crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs
									
									
									
									
									
										Normal file
									
								
							| @ -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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,12 +1,11 @@ | |||||||
| mod diagnostic; | mod diagnostic; | ||||||
| pub mod diagnostics; | pub mod diagnostics; | ||||||
|  | mod frame_time_diagnostics_plugin; | ||||||
| pub use diagnostic::*; | pub use diagnostic::*; | ||||||
|  | pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin; | ||||||
| 
 | 
 | ||||||
| use bevy_app::{AppBuilder, AppPlugin}; | use bevy_app::{AppBuilder, AppPlugin}; | ||||||
| use diagnostics::{ | use diagnostics::{print_diagnostics_system, PrintDiagnosticsState}; | ||||||
|     frame_time_diagnostic_system, print_diagnostics_system, setup_frame_time_diagnostic_system, |  | ||||||
|     PrintDiagnosticsState, |  | ||||||
| }; |  | ||||||
| use legion::prelude::IntoSystem; | use legion::prelude::IntoSystem; | ||||||
| use std::time::Duration; | use std::time::Duration; | ||||||
| 
 | 
 | ||||||
| @ -17,7 +16,6 @@ pub struct PrintDiagnostics { | |||||||
| 
 | 
 | ||||||
| pub struct DiagnosticsPlugin { | pub struct DiagnosticsPlugin { | ||||||
|     pub print_diagnostics: Option<PrintDiagnostics>, |     pub print_diagnostics: Option<PrintDiagnostics>, | ||||||
|     pub add_defaults: bool, |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Default for DiagnosticsPlugin { | impl Default for DiagnosticsPlugin { | ||||||
| @ -27,7 +25,6 @@ impl Default for DiagnosticsPlugin { | |||||||
|                 wait_duration: Duration::from_secs_f64(1.0), |                 wait_duration: Duration::from_secs_f64(1.0), | ||||||
|                 filter: None, |                 filter: None, | ||||||
|             }), |             }), | ||||||
|             add_defaults: true, |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -35,11 +32,6 @@ impl Default for DiagnosticsPlugin { | |||||||
| impl AppPlugin for DiagnosticsPlugin { | impl AppPlugin for DiagnosticsPlugin { | ||||||
|     fn build(&self, app: &mut AppBuilder) { |     fn build(&self, app: &mut AppBuilder) { | ||||||
|         app.init_resource::<Diagnostics>(); |         app.init_resource::<Diagnostics>(); | ||||||
|         if self.add_defaults { |  | ||||||
|             app.add_startup_system(setup_frame_time_diagnostic_system.system()) |  | ||||||
|                 .add_system(frame_time_diagnostic_system.system()); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         if let Some(ref print_diagnostics) = self.print_diagnostics { |         if let Some(ref print_diagnostics) = self.print_diagnostics { | ||||||
|             app.add_resource(PrintDiagnosticsState::new(print_diagnostics.wait_duration)) |             app.add_resource(PrintDiagnosticsState::new(print_diagnostics.wait_duration)) | ||||||
|                 .add_system(print_diagnostics_system.system()); |                 .add_system(print_diagnostics_system.system()); | ||||||
|  | |||||||
| @ -1,10 +1,11 @@ | |||||||
| use bevy::prelude::*; | use bevy::{diagnostic::FrameTimeDiagnosticsPlugin, prelude::*}; | ||||||
| use rand::{rngs::StdRng, Rng, SeedableRng}; | use rand::{rngs::StdRng, Rng, SeedableRng}; | ||||||
| 
 | 
 | ||||||
| fn main() { | fn main() { | ||||||
|     App::build() |     App::build() | ||||||
|         .add_default_plugins() |         .add_default_plugins() | ||||||
|         .add_plugin(DiagnosticsPlugin::default()) |         .add_plugin(DiagnosticsPlugin::default()) | ||||||
|  |         .add_plugin(FrameTimeDiagnosticsPlugin::default()) | ||||||
|         .add_startup_system(setup) |         .add_startup_system(setup) | ||||||
|         .add_system(move_system.system()) |         .add_system(move_system.system()) | ||||||
|         .run(); |         .run(); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Carter Anderson
						Carter Anderson