From a2c4b36d56da6a3d6934bd9d34883acaf529b4e5 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 9 Dec 2023 18:42:31 +0100 Subject: [PATCH] Limit and occasionally report framerate --- src/frontend/sdl.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/frontend/sdl.rs b/src/frontend/sdl.rs index 1daae33..e6fc0df 100644 --- a/src/frontend/sdl.rs +++ b/src/frontend/sdl.rs @@ -1,4 +1,6 @@ use std::cell::RefCell; +use std::thread::sleep; +use std::time::{Duration, Instant}; use anyhow::{anyhow, Result}; use sdl2::event::Event; @@ -33,6 +35,11 @@ pub struct SDLRenderer { width: usize, #[allow(dead_code)] height: usize, + last_frame: Instant, + frametime: u64, + + fps_count: u64, + fps_time: Instant, } impl SDLRenderer { @@ -68,6 +75,10 @@ impl Renderer for SDLRenderer { displaybuffer: vec![0; width * height * Self::BPP], width, height, + last_frame: Instant::now(), + frametime: 1000000 / 50, + fps_count: 0, + fps_time: Instant::now(), }) }) } @@ -95,6 +106,24 @@ impl Renderer for SDLRenderer { .map_err(|e| anyhow!(e))?; self.canvas.present(); + self.fps_count += 1; + + if self.fps_time.elapsed().as_secs() >= 2 { + println!( + "Frame rate: {:0.2} frames/second", + self.fps_count as f32 / self.fps_time.elapsed().as_secs_f32() + ); + self.fps_count = 0; + self.fps_time = Instant::now(); + } + + // Limit the framerate + let framelen = self.last_frame.elapsed().as_micros() as u64; + if framelen < self.frametime { + sleep(Duration::from_micros(self.frametime - framelen)); + } + self.last_frame = Instant::now(); + Ok(()) } }