Skip to content

Commit cb6825e

Browse files
SakiiCoderfuest
andauthored
Add setting to disable the FPS limiter (#72)
* move fps check to FpsLimiter * update docs and changelog * check for duration Co-authored-by: Ralf Fuest <mail@rfuest.de> * change desired_loop_duration signature Co-authored-by: Ralf Fuest <mail@rfuest.de> * fix option --------- Co-authored-by: Ralf Fuest <mail@rfuest.de>
1 parent 7c249c7 commit cb6825e

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
- **(breaking)** [#71](https://github.com/embedded-graphics/simulator/pull/71) Added support for non-square pixels.
1212

13+
### Changed
14+
15+
- [#72](https://github.com/embedded-graphics/simulator/pull/72) `set_max_fps` now accepts `0` to disable the FPS limiter.
16+
1317
### Fixed
1418

1519
- [#71](https://github.com/embedded-graphics/simulator/pull/71) Fixed pixel spacing for unscaled outputs.

src/window/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
env,
33
fs::File,
44
io::BufReader,
5+
num::NonZeroU32,
56
ops::Deref,
67
process, thread,
78
time::{Duration, Instant},
@@ -26,25 +27,29 @@ mod multi_window;
2627
pub use multi_window::MultiWindow;
2728

2829
pub(crate) struct FpsLimiter {
29-
max_fps: u32,
30+
max_fps: Option<NonZeroU32>,
3031
frame_start: Instant,
3132
}
3233

3334
impl FpsLimiter {
3435
pub(crate) fn new() -> Self {
3536
Self {
36-
max_fps: 60,
37+
max_fps: NonZeroU32::new(60),
3738
frame_start: Instant::now(),
3839
}
3940
}
4041

41-
fn desired_loop_duration(&self) -> Duration {
42-
Duration::from_secs_f32(1.0 / self.max_fps as f32)
42+
fn desired_loop_duration(&self) -> Option<Duration> {
43+
Some(Duration::from_secs_f32(1.0 / self.max_fps?.get() as f32))
4344
}
4445

4546
fn sleep(&mut self) {
46-
let sleep_duration = (self.frame_start + self.desired_loop_duration())
47-
.saturating_duration_since(Instant::now());
47+
let Some(duration) = self.desired_loop_duration() else {
48+
return;
49+
};
50+
51+
let sleep_duration =
52+
(self.frame_start + duration).saturating_duration_since(Instant::now());
4853
thread::sleep(sleep_duration);
4954

5055
self.frame_start = Instant::now();
@@ -202,8 +207,8 @@ impl Window {
202207
.events(&self.output_settings)
203208
}
204209

205-
/// Sets the FPS limit of the window.
210+
/// Changes the FPS limit of the window. Set to `0` to disable the FPS limiter.
206211
pub fn set_max_fps(&mut self, max_fps: u32) {
207-
self.fps_limiter.max_fps = max_fps;
212+
self.fps_limiter.max_fps = NonZeroU32::new(max_fps);
208213
}
209214
}

src/window/multi_window.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, num::NonZeroU32};
22

33
use embedded_graphics::{pixelcolor::Rgb888, prelude::*};
44

@@ -125,9 +125,9 @@ impl MultiWindow {
125125
display.bounding_box().contains(p).then_some(p)
126126
}
127127

128-
/// Sets the FPS limit of the window.
128+
/// Changes the FPS limit of the window. Set to `0` to disable the FPS limiter.
129129
pub fn set_max_fps(&mut self, max_fps: u32) {
130-
self.fps_limiter.max_fps = max_fps;
130+
self.fps_limiter.max_fps = NonZeroU32::new(max_fps);
131131
}
132132
}
133133

0 commit comments

Comments
 (0)