Galactica/crates/render/src/ui/fpsindicator.rs

67 lines
1.6 KiB
Rust
Raw Normal View History

2024-01-10 18:53:19 -08:00
use glyphon::{Attrs, Buffer, Color, Family, Metrics, Shaping, TextArea, TextBounds};
use crate::{datastructs::RenderState, RenderInput};
pub(super) struct FpsIndicator {
buffer: Buffer,
update_counter: u32,
}
impl FpsIndicator {
pub fn new(state: &mut RenderState) -> Self {
2024-01-10 22:44:22 -08:00
let mut buffer = Buffer::new(&mut state.text_font_system, Metrics::new(15.0, 20.0));
2024-01-10 18:53:19 -08:00
buffer.set_size(
&mut state.text_font_system,
state.window_size.width as f32,
state.window_size.height as f32,
);
buffer.shape_until_scroll(&mut state.text_font_system);
Self {
buffer,
update_counter: 0,
}
}
}
impl FpsIndicator {
pub fn update(&mut self, input: &RenderInput, state: &mut RenderState) {
// Update once every n frames
if self.update_counter > 0 {
self.update_counter -= 1;
return;
}
self.update_counter = 100;
self.buffer.set_text(
&mut state.text_font_system,
&format!(
2024-01-10 22:44:22 -08:00
"Frame: {:05.02?}%\nGame: {:05.02?}%\nShips: {:05.02?}%\nPhys: {:05.02?}%\nRender: {:.02?}",
2024-01-10 19:47:30 -08:00
100.0 * (input.timing.frame / input.timing.render),
100.0 * (input.timing.galaxy / input.timing.frame),
100.0 * (input.timing.physics_sim / input.timing.frame),
100.0 * (input.timing.physics_ship / input.timing.frame),
1.0 / input.timing.render
2024-01-10 18:53:19 -08:00
),
2024-01-10 19:47:30 -08:00
Attrs::new().family(Family::Monospace),
2024-01-10 18:53:19 -08:00
Shaping::Basic,
);
}
pub fn get_textarea(&self) -> TextArea {
TextArea {
buffer: &self.buffer,
left: 10.0,
top: 400.0,
scale: 1.0,
bounds: TextBounds {
left: 10,
top: 400,
right: 300,
bottom: 800,
},
default_color: Color::rgb(255, 255, 255),
}
}
}