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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use bevy::{
prelude::*,
utils::{Duration, Instant},
};
#[derive(Debug, Default, Clone)]
pub struct ScreenDiagsPlugin {
pub settings: ScreenDiagsSettings,
}
impl Plugin for ScreenDiagsPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_resource(self.settings.clone())
.add_startup_system(setup.system())
.add_system(update.system());
}
}
#[derive(Debug, Clone)]
pub struct ScreenDiagsSettings {
pub interval: Duration,
pub enabled: bool,
}
impl Default for ScreenDiagsSettings {
fn default() -> Self {
ScreenDiagsSettings {
interval: Duration::from_secs(1),
enabled: true,
}
}
}
#[derive(Debug, Default, Clone)]
struct ScreenDiagsText {
state: Option<ScreenDiagsState>,
}
#[derive(Debug, Clone, Copy)]
struct ScreenDiagsState {
last_time: Instant,
frame_count: u32,
}
fn update(
settings: Res<ScreenDiagsSettings>,
time: Res<Time>,
mut query: Query<(&mut Text, &mut ScreenDiagsText)>,
) {
let now: Instant = time.last_update().unwrap_or_else(|| time.startup());
for (mut text, mut marker) in query.iter_mut() {
if let Some(state) = marker.state.as_mut() {
state.frame_count += 1;
let so_far = now - state.last_time;
if so_far > settings.interval {
if settings.enabled {
let fps = state.frame_count as f64 / so_far.as_secs_f64();
text.value = format!("FPS: {:4.0}", fps);
} else {
text.value = "".to_owned();
}
marker.state = None;
}
}
if marker.state.is_none() {
marker.state = Some(ScreenDiagsState {
last_time: now,
frame_count: 0,
});
}
}
}
fn setup(commands: &mut Commands, mut assets: ResMut<Assets<Font>>) {
let font_bytes = include_bytes!("../assets/fonts/FiraSans-Bold.ttf").to_vec();
let font_struct = Font::try_from_bytes(font_bytes).expect("Font should be present and valid");
let font = assets.add(font_struct);
commands
.spawn(CameraUiBundle::default())
.spawn(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
..Default::default()
},
text: Text {
value: "FPS: ...".to_owned(),
font,
style: TextStyle {
font_size: 32.0,
color: Color::WHITE,
..Default::default()
},
},
..Default::default()
})
.with(ScreenDiagsText::default());
}