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

69 lines
1.5 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 std::time::Instant;
use crate::{datastructs::RenderState, RenderInput};
pub(super) struct FpsIndicator {
buffer: Buffer,
last_update: Instant,
update_counter: u32,
}
impl FpsIndicator {
pub fn new(state: &mut RenderState) -> Self {
let mut buffer = Buffer::new(&mut state.text_font_system, Metrics::new(12.0, 20.0));
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,
last_update: Instant::now(),
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!(
"Game: {:.02?}\nPhys: {:.02?}\nRender: {:.02?}",
1.0 / input.timing.galaxy,
1.0 / input.timing.physics,
1.0 / (self.last_update.elapsed().as_secs_f32() / 100.0)
),
Attrs::new().family(Family::SansSerif),
Shaping::Basic,
);
self.last_update = Instant::now();
}
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),
}
}
}