Skip to content

Commit 71e2c7f

Browse files
authored
Debug text example: render fps and frame time (#978)
Display fps and frame time in text_debug example
1 parent 59010ca commit 71e2c7f

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

examples/ui/text_debug.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
use bevy::prelude::*;
2-
extern crate rand;
1+
use bevy::{
2+
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
3+
prelude::*,
4+
};
35

46
/// This example is for debugging text layout
57
fn main() {
68
App::build()
9+
.add_resource(WindowDescriptor {
10+
vsync: false,
11+
..Default::default()
12+
})
713
.add_plugins(DefaultPlugins)
14+
.add_plugin(FrameTimeDiagnosticsPlugin)
815
.add_startup_system(infotext_system)
916
.add_system(change_text_system)
1017
.run();
@@ -83,7 +90,7 @@ fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) {
8390
value: "This text changes in the bottom right".to_string(),
8491
font: font.clone(),
8592
style: TextStyle {
86-
font_size: 50.0,
93+
font_size: 30.0,
8794
color: Color::WHITE,
8895
alignment: TextAlignment::default(),
8996
},
@@ -120,11 +127,31 @@ fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) {
120127
});
121128
}
122129

123-
fn change_text_system(mut query: Query<(&mut Text, &TextChanges)>) {
124-
for (mut text, _text_changes) in query.iter_mut() {
130+
fn change_text_system(
131+
time: Res<Time>,
132+
diagnostics: Res<Diagnostics>,
133+
mut query: Query<&mut Text, With<TextChanges>>,
134+
) {
135+
for mut text in query.iter_mut() {
136+
let mut fps = 0.0;
137+
if let Some(fps_diagnostic) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
138+
if let Some(fps_avg) = fps_diagnostic.average() {
139+
fps = fps_avg;
140+
}
141+
}
142+
143+
let mut frame_time = time.delta_seconds_f64();
144+
if let Some(frame_time_diagnostic) = diagnostics.get(FrameTimeDiagnosticsPlugin::FRAME_TIME)
145+
{
146+
if let Some(frame_time_avg) = frame_time_diagnostic.average() {
147+
frame_time = frame_time_avg;
148+
}
149+
}
150+
125151
text.value = format!(
126-
"This text changes in the bottom right {}",
127-
rand::random::<u16>(),
152+
"This text changes in the bottom right - {:.1} fps, {:.3} ms/frame",
153+
fps,
154+
frame_time * 1000.0,
128155
);
129156
}
130157
}

0 commit comments

Comments
 (0)