Skip to content

Commit

Permalink
Limit and occasionally report framerate
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Dec 9, 2023
1 parent 5ad4bb9 commit a2c4b36
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/frontend/sdl.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
})
})
}
Expand Down Expand Up @@ -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(())
}
}
Expand Down

0 comments on commit a2c4b36

Please sign in to comment.