use glyphon::{Attrs, Buffer, Color, Family, Metrics, Shaping, TextArea, TextBounds}; use crate::{RenderInput, RenderState}; pub(super) struct FpsIndicator { buffer: Buffer, update_counter: u32, } impl FpsIndicator { pub fn new(state: &mut RenderState) -> Self { let mut buffer = Buffer::new(&mut state.text_font_system, Metrics::new(15.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, 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!( "Frame: {:#?} ({:05.00})\nShips: {:05.02}%\nPhys: {:05.02}%\n", input.timing.frame, 1.0 / input.timing.frame.as_secs_f32(), 100.0 * (input.timing.physics_sim.as_secs_f32() / input.timing.frame.as_secs_f32()), 100.0 * (input.timing.physics_ship.as_secs_f32() / input.timing.frame.as_secs_f32()), ), Attrs::new().family(Family::Monospace), 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), } } }