diff --git a/src/lib.rs b/src/lib.rs index 2050dc1..98e0ad7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ use crate::framework::vfs::PhysicalFS; use crate::scene::loading_scene::LoadingScene; use crate::scene::Scene; use crate::scripting::tsc::text_script::ScriptMode; -use crate::shared_game_state::{SharedGameState, TimingMode}; +use crate::shared_game_state::{Fps, SharedGameState, TimingMode}; use crate::texture_set::{G_MAG, I_MAG}; mod bmfont; @@ -82,6 +82,7 @@ pub struct Game { last_tick: u128, next_tick: u128, loops: u64, + fps: Fps, } impl Game { @@ -94,6 +95,7 @@ impl Game { last_tick: 0, next_tick: 0, loops: 0, + fps: Fps::new(), }; Ok(s) @@ -191,6 +193,10 @@ impl Game { )?; } + if state_ref.settings.fps_counter { + self.fps.act(state_ref, ctx, self.last_tick)?; + } + self.ui.draw(state_ref, ctx, scene)?; } diff --git a/src/settings.rs b/src/settings.rs index 73e1576..b91d496 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -41,6 +41,8 @@ pub struct Settings { pub infinite_booster: bool, #[serde(skip)] pub debug_outlines: bool, + #[serde(skip, default = "default_true")] + pub fps_counter: bool, } fn default_true() -> bool { true } @@ -131,6 +133,7 @@ impl Default for Settings { god_mode: false, infinite_booster: false, debug_outlines: false, + fps_counter: true, } } } diff --git a/src/shared_game_state.rs b/src/shared_game_state.rs index 98b16aa..086aecf 100644 --- a/src/shared_game_state.rs +++ b/src/shared_game_state.rs @@ -6,6 +6,7 @@ use chrono::{Datelike, Local}; use crate::bmfont_renderer::BMFontRenderer; use crate::caret::{Caret, CaretType}; use crate::common::{ControlFlags, Direction, FadeState}; +use crate::components::draw_common::{draw_number, Alignment}; use crate::engine_constants::EngineConstants; use crate::framework::backend::BackendTexture; use crate::framework::context::Context; @@ -65,6 +66,30 @@ impl TimingMode { } } +pub struct Fps { + pub frame_count: u32, + pub fps: u32, + last_capture: u128, +} + +impl Fps { + pub fn new() -> Fps { + Fps { frame_count: 0, fps: 0, last_capture: 0 } + } + + pub fn act(&mut self, state: &mut SharedGameState, ctx: &mut Context, time: u128) -> GameResult { + if time - self.last_capture > 1000000000 { + self.fps = self.frame_count; + self.frame_count = 0; + self.last_capture = time; + } else { + self.frame_count += 1; + } + draw_number(state.canvas_size.0 - 8.0, 8.0, self.fps as usize, Alignment::Right, state, ctx)?; + Ok(()) + } +} + #[derive(PartialEq, Eq, Copy, Clone)] pub enum Season { None, @@ -274,6 +299,7 @@ impl SharedGameState { } } ScanCode::F10 => self.settings.debug_outlines = !self.settings.debug_outlines, + ScanCode::F11 => self.settings.fps_counter = !self.settings.fps_counter, ScanCode::F12 => self.debugger = !self.debugger, _ => {} }