port time and diagnostic systems to system fns
This commit is contained in:
parent
0800ce9b92
commit
37b4dff172
@ -4,6 +4,7 @@ pub mod transform;
|
|||||||
|
|
||||||
use bevy_app::{stage, AppBuilder, AppPlugin};
|
use bevy_app::{stage, AppBuilder, AppPlugin};
|
||||||
use bevy_transform::transform_system_bundle;
|
use bevy_transform::transform_system_bundle;
|
||||||
|
use legion::prelude::IntoSystem;
|
||||||
use time::{start_timer_system, stop_timer_system};
|
use time::{start_timer_system, stop_timer_system};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@ -16,7 +17,7 @@ impl AppPlugin for CorePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.add_resource(time::Time::new())
|
app.add_resource(time::Time::new())
|
||||||
.add_system_to_stage(stage::FIRST, start_timer_system())
|
.add_system_to_stage(stage::FIRST, start_timer_system.system())
|
||||||
.add_system_to_stage(stage::LAST, stop_timer_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> {
|
pub fn start_timer_system(mut time: ResourceMut<Time>) {
|
||||||
SystemBuilder::new("start_timer")
|
time.start();
|
||||||
.write_resource::<Time>()
|
|
||||||
.build(|_, _, time, _| {
|
|
||||||
time.start();
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop_timer_system() -> Box<dyn Schedulable> {
|
pub fn stop_timer_system(mut time: ResourceMut<Time>) {
|
||||||
SystemBuilder::new("stop_timer")
|
time.stop();
|
||||||
.write_resource::<Time>()
|
}
|
||||||
.build(|_, _, 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,
|
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(
|
pub fn frame_time_diagnostic_system(
|
||||||
resources: &Resources,
|
mut diagnostics: ResourceMut<Diagnostics>,
|
||||||
max_history_length: usize,
|
time: Resource<Time>,
|
||||||
) -> Box<dyn Schedulable> {
|
) {
|
||||||
let mut diagnostics = resources.get_mut::<Diagnostics>().unwrap();
|
if time.delta_seconds_f64 == 0.0 {
|
||||||
diagnostics.add(Diagnostic::new(
|
return;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
diagnostics.add_measurement(FRAME_TIME, time.delta_seconds_f64);
|
diagnostics.add_measurement(FRAME_TIME, time.delta_seconds_f64);
|
||||||
if let Some(fps) = diagnostics
|
if let Some(fps) = diagnostics
|
||||||
.get(FRAME_TIME)
|
.get(FRAME_TIME)
|
||||||
.and_then(|frame_time_diagnostic| {
|
.and_then(|frame_time_diagnostic| {
|
||||||
frame_time_diagnostic
|
frame_time_diagnostic
|
||||||
.average()
|
.average()
|
||||||
.and_then(|frame_time_average| {
|
.and_then(|frame_time_average| {
|
||||||
if frame_time_average > 0.0 {
|
if frame_time_average > 0.0 {
|
||||||
Some(1.0 / frame_time_average)
|
Some(1.0 / frame_time_average)
|
||||||
} else {
|
} else {
|
||||||
None
|
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(FPS, fps);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_diagnostics_debug_system(wait: Duration) -> Box<dyn Schedulable> {
|
pub struct PrintDiagnosticsState {
|
||||||
let mut elasped = 0.0;
|
elapsed: f64,
|
||||||
let wait_seconds = wait.as_secs_f64();
|
wait_seconds: f64,
|
||||||
SystemBuilder::new("print_diagnostics_debug")
|
}
|
||||||
.read_resource::<Time>()
|
|
||||||
.read_resource::<Diagnostics>()
|
impl PrintDiagnosticsState {
|
||||||
.build(move |_, _world, (time, diagnostics), _queries| {
|
pub fn new(wait: Duration) -> Self {
|
||||||
elasped += time.delta_seconds_f64;
|
PrintDiagnosticsState {
|
||||||
if elasped >= wait_seconds {
|
elapsed: 0.,
|
||||||
elasped = 0.0;
|
wait_seconds: wait.as_secs_f64(),
|
||||||
for diagnostic in diagnostics.iter() {
|
}
|
||||||
println!("{:#?}\n", diagnostic);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
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::*;
|
pub use diagnostic::*;
|
||||||
|
|
||||||
use bevy_app::{AppBuilder, AppPlugin};
|
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;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub struct DiagnosticsPlugin {
|
pub struct DiagnosticsPlugin {
|
||||||
@ -24,15 +28,15 @@ 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.add_resource(Diagnostics::default());
|
app.add_resource_init::<Diagnostics>();
|
||||||
if self.add_defaults {
|
if self.add_defaults {
|
||||||
let frame_time_diagnostic_system =
|
app.add_startup_system(setup_frame_time_diagnostic_system.system())
|
||||||
{ frame_time_diagnostic_system(app.resources_mut(), 10) };
|
.add_system(frame_time_diagnostic_system.system());
|
||||||
app.add_system(frame_time_diagnostic_system);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.print_diagnostics {
|
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 New Issue
Block a user