Skip to content

Commit 4d3bd80

Browse files
committed
move fps check to FpsLimiter
1 parent 7c249c7 commit 4d3bd80

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

src/window/mod.rs

Lines changed: 15 additions & 9 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,28 +27,33 @@ 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(max_fps: NonZeroU32) -> Duration {
43+
Duration::from_secs_f32(1.0 / 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());
48-
thread::sleep(sleep_duration);
47+
match self.max_fps {
48+
Some(max_fps) => {
49+
let sleep_duration = (self.frame_start + Self::desired_loop_duration(max_fps))
50+
.saturating_duration_since(Instant::now());
51+
thread::sleep(sleep_duration);
4952

50-
self.frame_start = Instant::now();
53+
self.frame_start = Instant::now();
54+
}
55+
None => {}
56+
}
5157
}
5258
}
5359

@@ -204,6 +210,6 @@ impl Window {
204210

205211
/// Sets the FPS limit of the window.
206212
pub fn set_max_fps(&mut self, max_fps: u32) {
207-
self.fps_limiter.max_fps = max_fps;
213+
self.fps_limiter.max_fps = NonZeroU32::new(max_fps);
208214
}
209215
}

src/window/multi_window.rs

Lines changed: 2 additions & 2 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

@@ -127,7 +127,7 @@ impl MultiWindow {
127127

128128
/// Sets the FPS limit of the window.
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)