|
1 | | -use bevy::prelude::*; |
2 | | -extern crate rand; |
| 1 | +use bevy::{ |
| 2 | + diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, |
| 3 | + prelude::*, |
| 4 | +}; |
3 | 5 |
|
4 | 6 | /// This example is for debugging text layout |
5 | 7 | fn main() { |
6 | 8 | App::build() |
| 9 | + .add_resource(WindowDescriptor { |
| 10 | + vsync: false, |
| 11 | + ..Default::default() |
| 12 | + }) |
7 | 13 | .add_plugins(DefaultPlugins) |
| 14 | + .add_plugin(FrameTimeDiagnosticsPlugin) |
8 | 15 | .add_startup_system(infotext_system) |
9 | 16 | .add_system(change_text_system) |
10 | 17 | .run(); |
@@ -83,7 +90,7 @@ fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) { |
83 | 90 | value: "This text changes in the bottom right".to_string(), |
84 | 91 | font: font.clone(), |
85 | 92 | style: TextStyle { |
86 | | - font_size: 50.0, |
| 93 | + font_size: 30.0, |
87 | 94 | color: Color::WHITE, |
88 | 95 | alignment: TextAlignment::default(), |
89 | 96 | }, |
@@ -120,11 +127,31 @@ fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) { |
120 | 127 | }); |
121 | 128 | } |
122 | 129 |
|
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 | + |
125 | 151 | 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, |
128 | 155 | ); |
129 | 156 | } |
130 | 157 | } |
0 commit comments