Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug text example: render fps and frame time #978

Merged
merged 5 commits into from
Dec 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
}
}