Debug text example: render fps and frame time (#978)

Display fps and frame time in text_debug example
This commit is contained in:
Robert Swain 2020-12-03 01:15:31 +01:00 committed by GitHub
parent 59010caff5
commit 71e2c7f4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,17 @@
use bevy::prelude::*;
extern crate rand;
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
prelude::*,
};
/// This example is for debugging text layout
fn main() {
App::build()
.add_resource(WindowDescriptor {
vsync: false,
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(FrameTimeDiagnosticsPlugin)
.add_startup_system(infotext_system)
.add_system(change_text_system)
.run();
@ -83,7 +90,7 @@ fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) {
value: "This text changes in the bottom right".to_string(),
font: font.clone(),
style: TextStyle {
font_size: 50.0,
font_size: 30.0,
color: Color::WHITE,
alignment: TextAlignment::default(),
},
@ -120,11 +127,31 @@ fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) {
});
}
fn change_text_system(mut query: Query<(&mut Text, &TextChanges)>) {
for (mut text, _text_changes) in query.iter_mut() {
fn change_text_system(
time: Res<Time>,
diagnostics: Res<Diagnostics>,
mut query: Query<&mut Text, With<TextChanges>>,
) {
for mut text in query.iter_mut() {
let mut fps = 0.0;
if let Some(fps_diagnostic) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(fps_avg) = fps_diagnostic.average() {
fps = fps_avg;
}
}
let mut frame_time = time.delta_seconds_f64();
if let Some(frame_time_diagnostic) = diagnostics.get(FrameTimeDiagnosticsPlugin::FRAME_TIME)
{
if let Some(frame_time_avg) = frame_time_diagnostic.average() {
frame_time = frame_time_avg;
}
}
text.value = format!(
"This text changes in the bottom right {}",
rand::random::<u16>(),
"This text changes in the bottom right - {:.1} fps, {:.3} ms/frame",
fps,
frame_time * 1000.0,
);
}
}