Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 13 additions & 8 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
env,
fs::File,
io::BufReader,
num::NonZeroU32,
ops::Deref,
process, thread,
time::{Duration, Instant},
Expand All @@ -26,25 +27,29 @@ mod multi_window;
pub use multi_window::MultiWindow;

pub(crate) struct FpsLimiter {
max_fps: u32,
max_fps: Option<NonZeroU32>,
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<Duration> {
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();
Expand Down Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions src/window/multi_window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, num::NonZeroU32};

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

Expand Down Expand Up @@ -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);
}
}

Expand Down
Loading