diff --git a/CHANGELOG.md b/CHANGELOG.md index 68a0102..5a5aa17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ - **(breaking)** [#71](https://github.com/embedded-graphics/simulator/pull/71) Added support for non-square pixels. +### Changed + +- [#72](https://github.com/embedded-graphics/simulator/pull/72) `set_max_fps` now accepts `0` to disable the FPS limiter. + ### Fixed - [#71](https://github.com/embedded-graphics/simulator/pull/71) Fixed pixel spacing for unscaled outputs. diff --git a/src/window/mod.rs b/src/window/mod.rs index 6d35aec..fa9c695 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -2,6 +2,7 @@ use std::{ env, fs::File, io::BufReader, + num::NonZeroU32, ops::Deref, process, thread, time::{Duration, Instant}, @@ -26,25 +27,29 @@ mod multi_window; pub use multi_window::MultiWindow; pub(crate) struct FpsLimiter { - max_fps: u32, + max_fps: Option, frame_start: Instant, } impl FpsLimiter { pub(crate) fn new() -> Self { Self { - max_fps: 60, + max_fps: NonZeroU32::new(60), frame_start: Instant::now(), } } - fn desired_loop_duration(&self) -> Duration { - Duration::from_secs_f32(1.0 / self.max_fps as f32) + fn desired_loop_duration(&self) -> Option { + Some(Duration::from_secs_f32(1.0 / self.max_fps?.get() as f32)) } fn sleep(&mut self) { - let sleep_duration = (self.frame_start + self.desired_loop_duration()) - .saturating_duration_since(Instant::now()); + let Some(duration) = self.desired_loop_duration() else { + return; + }; + + let sleep_duration = + (self.frame_start + duration).saturating_duration_since(Instant::now()); thread::sleep(sleep_duration); self.frame_start = Instant::now(); @@ -202,8 +207,8 @@ impl Window { .events(&self.output_settings) } - /// Sets the FPS limit of the window. + /// Changes the FPS limit of the window. Set to `0` to disable the FPS limiter. pub fn set_max_fps(&mut self, max_fps: u32) { - self.fps_limiter.max_fps = max_fps; + self.fps_limiter.max_fps = NonZeroU32::new(max_fps); } } diff --git a/src/window/multi_window.rs b/src/window/multi_window.rs index 45b6b7f..a6586ef 100644 --- a/src/window/multi_window.rs +++ b/src/window/multi_window.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, num::NonZeroU32}; use embedded_graphics::{pixelcolor::Rgb888, prelude::*}; @@ -125,9 +125,9 @@ impl MultiWindow { display.bounding_box().contains(p).then_some(p) } - /// Sets the FPS limit of the window. + /// Changes the FPS limit of the window. Set to `0` to disable the FPS limiter. pub fn set_max_fps(&mut self, max_fps: u32) { - self.fps_limiter.max_fps = max_fps; + self.fps_limiter.max_fps = NonZeroU32::new(max_fps); } }