mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
port time and diagnostic systems to system fns
This commit is contained in:
parent
0800ce9b92
commit
37b4dff172
4 changed files with 91 additions and 93 deletions
|
@ -4,6 +4,7 @@ pub mod transform;
|
|||
|
||||
use bevy_app::{stage, AppBuilder, AppPlugin};
|
||||
use bevy_transform::transform_system_bundle;
|
||||
use legion::prelude::IntoSystem;
|
||||
use time::{start_timer_system, stop_timer_system};
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -16,7 +17,7 @@ impl AppPlugin for CorePlugin {
|
|||
}
|
||||
|
||||
app.add_resource(time::Time::new())
|
||||
.add_system_to_stage(stage::FIRST, start_timer_system())
|
||||
.add_system_to_stage(stage::LAST, stop_timer_system());
|
||||
.add_system_to_stage(stage::FIRST, start_timer_system.system())
|
||||
.add_system_to_stage(stage::LAST, stop_timer_system.system());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,18 +30,10 @@ impl Time {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn start_timer_system() -> Box<dyn Schedulable> {
|
||||
SystemBuilder::new("start_timer")
|
||||
.write_resource::<Time>()
|
||||
.build(|_, _, time, _| {
|
||||
time.start();
|
||||
})
|
||||
pub fn start_timer_system(mut time: ResourceMut<Time>) {
|
||||
time.start();
|
||||
}
|
||||
|
||||
pub fn stop_timer_system() -> Box<dyn Schedulable> {
|
||||
SystemBuilder::new("stop_timer")
|
||||
.write_resource::<Time>()
|
||||
.build(|_, _, time, _| {
|
||||
time.stop();
|
||||
})
|
||||
}
|
||||
pub fn stop_timer_system(mut time: ResourceMut<Time>) {
|
||||
time.stop();
|
||||
}
|
|
@ -12,82 +12,83 @@ pub const FRAME_TIME: DiagnosticId = DiagnosticId(Uuid::from_bytes([
|
|||
216, 184, 55, 12, 28, 116, 69, 201, 187, 137, 176, 77, 83, 89, 251, 241,
|
||||
]));
|
||||
|
||||
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(
|
||||
resources: &Resources,
|
||||
max_history_length: usize,
|
||||
) -> Box<dyn Schedulable> {
|
||||
let mut diagnostics = resources.get_mut::<Diagnostics>().unwrap();
|
||||
diagnostics.add(Diagnostic::new(
|
||||
FRAME_TIME,
|
||||
"frame_time",
|
||||
max_history_length,
|
||||
));
|
||||
diagnostics.add(Diagnostic::new(FPS, "fps", max_history_length));
|
||||
SystemBuilder::new("frame_time_diagnostic")
|
||||
.read_resource::<Time>()
|
||||
.write_resource::<Diagnostics>()
|
||||
.build(move |_, _world, (time, ref mut diagnostics), _queries| {
|
||||
if time.delta_seconds_f64 == 0.0 {
|
||||
return;
|
||||
}
|
||||
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 fn print_diagnostics_system(wait: Duration) -> Box<dyn Schedulable> {
|
||||
let mut elasped = 0.0;
|
||||
let wait_seconds = wait.as_secs_f64();
|
||||
SystemBuilder::new("print_diagnostics")
|
||||
.read_resource::<Time>()
|
||||
.read_resource::<Diagnostics>()
|
||||
.build(move |_, _world, (time, diagnostics), _queries| {
|
||||
elasped += time.delta_seconds_f64;
|
||||
if elasped >= wait_seconds {
|
||||
elasped = 0.0;
|
||||
for diagnostic in diagnostics.iter() {
|
||||
if let Some(value) = diagnostic.value() {
|
||||
print!("{:<10}: {:<9.6}", diagnostic.name, value);
|
||||
if let Some(average) = diagnostic.average() {
|
||||
print!(" (avg {:.6})", average);
|
||||
}
|
||||
|
||||
println!("\n");
|
||||
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 fn print_diagnostics_debug_system(wait: Duration) -> Box<dyn Schedulable> {
|
||||
let mut elasped = 0.0;
|
||||
let wait_seconds = wait.as_secs_f64();
|
||||
SystemBuilder::new("print_diagnostics_debug")
|
||||
.read_resource::<Time>()
|
||||
.read_resource::<Diagnostics>()
|
||||
.build(move |_, _world, (time, diagnostics), _queries| {
|
||||
elasped += time.delta_seconds_f64;
|
||||
if elasped >= wait_seconds {
|
||||
elasped = 0.0;
|
||||
for diagnostic in diagnostics.iter() {
|
||||
println!("{:#?}\n", diagnostic);
|
||||
}
|
||||
}
|
||||
})
|
||||
pub struct PrintDiagnosticsState {
|
||||
elapsed: f64,
|
||||
wait_seconds: f64,
|
||||
}
|
||||
|
||||
impl PrintDiagnosticsState {
|
||||
pub fn new(wait: Duration) -> Self {
|
||||
PrintDiagnosticsState {
|
||||
elapsed: 0.,
|
||||
wait_seconds: wait.as_secs_f64(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_diagnostics_system(
|
||||
mut state: ResourceMut<PrintDiagnosticsState>,
|
||||
time: Resource<Time>,
|
||||
diagnostics: Resource<Diagnostics>,
|
||||
) {
|
||||
state.elapsed += time.delta_seconds_f64;
|
||||
if state.elapsed >= state.wait_seconds {
|
||||
state.elapsed = 0.0;
|
||||
for diagnostic in diagnostics.iter() {
|
||||
if let Some(value) = diagnostic.value() {
|
||||
print!("{:<10}: {:<9.6}", diagnostic.name, value);
|
||||
if let Some(average) = diagnostic.average() {
|
||||
print!(" (avg {:.6})", average);
|
||||
}
|
||||
|
||||
println!("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_diagnostics_debug_system(
|
||||
mut state: ResourceMut<PrintDiagnosticsState>,
|
||||
time: Resource<Time>,
|
||||
diagnostics: Resource<Diagnostics>,
|
||||
) {
|
||||
state.elapsed += time.delta_seconds_f64;
|
||||
if state.elapsed >= state.wait_seconds {
|
||||
state.elapsed = 0.0;
|
||||
for diagnostic in diagnostics.iter() {
|
||||
println!("{:#?}\n", diagnostic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,11 @@ pub mod diagnostics;
|
|||
pub use diagnostic::*;
|
||||
|
||||
use bevy_app::{AppBuilder, AppPlugin};
|
||||
use diagnostics::{frame_time_diagnostic_system, print_diagnostics_system};
|
||||
use diagnostics::{
|
||||
frame_time_diagnostic_system, print_diagnostics_system, setup_frame_time_diagnostic_system,
|
||||
PrintDiagnosticsState,
|
||||
};
|
||||
use legion::prelude::IntoSystem;
|
||||
use std::time::Duration;
|
||||
|
||||
pub struct DiagnosticsPlugin {
|
||||
|
@ -24,15 +28,15 @@ impl Default for DiagnosticsPlugin {
|
|||
|
||||
impl AppPlugin for DiagnosticsPlugin {
|
||||
fn build(&self, app: &mut AppBuilder) {
|
||||
app.add_resource(Diagnostics::default());
|
||||
app.add_resource_init::<Diagnostics>();
|
||||
if self.add_defaults {
|
||||
let frame_time_diagnostic_system =
|
||||
{ frame_time_diagnostic_system(app.resources_mut(), 10) };
|
||||
app.add_system(frame_time_diagnostic_system);
|
||||
app.add_startup_system(setup_frame_time_diagnostic_system.system())
|
||||
.add_system(frame_time_diagnostic_system.system());
|
||||
}
|
||||
|
||||
if self.print_diagnostics {
|
||||
app.add_system(print_diagnostics_system(self.print_wait_duration));
|
||||
app.add_resource(PrintDiagnosticsState::new(self.print_wait_duration))
|
||||
.add_system(print_diagnostics_system.system());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue