forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.rs
47 lines (44 loc) · 1.47 KB
/
text.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
prelude::*,
};
/// This example illustrates how to create text and update it in a system. It displays the current FPS in the upper left hand corner.
fn main() {
App::build()
.add_default_plugins()
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_startup_system(setup.system())
.add_system(text_update_system.system())
.run();
}
fn text_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text>) {
for mut text in &mut query.iter() {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.average() {
text.value = format!("FPS: {:.2}", average);
}
}
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let font_handle = asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap();
commands
// 2d camera
.spawn(UiCameraComponents::default())
// texture
.spawn(TextComponents {
style: Style {
align_self: AlignSelf::FlexEnd,
..Default::default()
},
text: Text {
value: "FPS:".to_string(),
font: font_handle,
style: TextStyle {
font_size: 60.0,
color: Color::WHITE,
},
},
..Default::default()
});
}