Skip to content

Commit

Permalink
Debug text example: render fps and frame time (#978)
Browse files Browse the repository at this point in the history
Display fps and frame time in text_debug example
  • Loading branch information
superdump authored Dec 3, 2020
1 parent 59010ca commit 71e2c7f
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions examples/ui/text_debug.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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(),
},
Expand Down Expand Up @@ -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,
);
}
}

0 comments on commit 71e2c7f

Please sign in to comment.