1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-23 06:02:55 +00:00

FPS counter

This commit is contained in:
dawnDus 2022-01-25 20:37:45 -05:00
parent b7f226b322
commit fba36467ea
No known key found for this signature in database
GPG key ID: 972AABDE81848F21
3 changed files with 36 additions and 1 deletions

View file

@ -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)?;
}

View file

@ -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,
}
}
}

View file

@ -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,
_ => {}
}