add diagnostic examples

This commit is contained in:
Carter Anderson 2020-05-04 14:14:49 -07:00
parent 704a742661
commit 4735c68ab4
4 changed files with 55 additions and 2 deletions

View file

@ -109,6 +109,14 @@ path = "examples/app/headless.rs"
name = "plugin"
path = "examples/app/plugin.rs"
[[example]]
name = "custom_diagnostic"
path = "examples/diagnostics/custom_diagnostic.rs"
[[example]]
name = "print_diagnostics"
path = "examples/diagnostics/print_diagnostics.rs"
[[example]]
name = "event"
path = "examples/ecs/event.rs"

View file

@ -73,7 +73,7 @@ impl PrintDiagnosticsPlugin {
if state.elapsed >= state.wait_seconds {
state.elapsed = 0.0;
println!("Diagnostics:");
println!("{}", "-".repeat(50));
println!("{}", "-".repeat(60));
if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) {
Self::print_diagnostic(diagnostic);
@ -95,7 +95,7 @@ impl PrintDiagnosticsPlugin {
if state.elapsed >= state.wait_seconds {
state.elapsed = 0.0;
println!("Diagnostics (Debug):");
println!("{}", "-".repeat(30));
println!("{}", "-".repeat(60));
if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) {
println!("{:#?}\n", diagnostic);

View file

@ -0,0 +1,33 @@
use bevy::{
diagnostic::{Diagnostic, DiagnosticId, Diagnostics, PrintDiagnosticsPlugin},
prelude::*,
};
fn main() {
App::build()
.add_default_plugins()
// The "print diagnostics" plugin is optional. It just visualizes our diagnostics in the console
.add_plugin(PrintDiagnosticsPlugin::default())
.add_startup_system(setup_diagnostic_system.system())
.add_system(my_system.system())
.run();
}
// All diagnostics should have a unique DiagnosticId. for each new diagnostic, generate a new random number
pub const SYSTEM_ITERATION_COUNT: DiagnosticId =
DiagnosticId::from_u128(337040787172757619024841343456040760896);
fn setup_diagnostic_system(mut diagnostics: ResourceMut<Diagnostics>) {
// Diagnostics must be initialized before measurements can be added.
// In general it's a good idea to set them up in a "startup system".
diagnostics.add(Diagnostic::new(
SYSTEM_ITERATION_COUNT,
"system_iteration_count",
10,
));
}
fn my_system(mut diagnostics: ResourceMut<Diagnostics>) {
// Add a measurement of 10.0 for our diagnostic each time this system runs
diagnostics.add_measurement(SYSTEM_ITERATION_COUNT, 10.0);
}

View file

@ -0,0 +1,12 @@
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, PrintDiagnosticsPlugin},
prelude::*,
};
fn main() {
App::build()
.add_default_plugins()
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(PrintDiagnosticsPlugin::default())
.run();
}