Skip to content

Commit

Permalink
Improve Simulator CPU Usage up to 50%. (#45)
Browse files Browse the repository at this point in the history
* only create texture at start of program

* limit window draw loop to 60Hz

* allow user to set max fps
  • Loading branch information
grantshandy authored Mar 6, 2023
1 parent 3b7bf85 commit 11ec451
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 340 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

## [Unreleased] - ReleaseDate

### Added

- [#45](https://github.com/embedded-graphics/simulator/pull/45) Added `OutputSettingsBuilder::max_fps` to set the maximum FPS of the simulator.

### Changed

- [#45](https://github.com/embedded-graphics/simulator/pull/45) Limit simulator to 60FPS by default.

## [0.4.0] - 2022-09-19

### Changed
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ image = "0.23.14"
base64 = "0.13.0"
embedded-graphics = "0.7.1"
sdl2 = { version = "0.35.1", optional = true }
ouroboros = { version = "0.15.5", optional = true }

[features]
default = [ "with-sdl" ]
fixed_point = [ "embedded-graphics/fixed_point" ]
with-sdl = [ "sdl2" ]
default = ["with-sdl"]
fixed_point = ["embedded-graphics/fixed_point"]
with-sdl = ["sdl2", "ouroboros"]
12 changes: 12 additions & 0 deletions src/output_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct OutputSettings {
pub pixel_spacing: u32,
/// Binary color theme.
pub theme: BinaryColorTheme,
/// Maximum frames per second shown in the window.
pub max_fps: u32,
}

impl OutputSettings {
Expand Down Expand Up @@ -51,6 +53,7 @@ pub struct OutputSettingsBuilder {
scale: Option<u32>,
pixel_spacing: Option<u32>,
theme: BinaryColorTheme,
max_fps: Option<u32>,
}

impl OutputSettingsBuilder {
Expand All @@ -60,6 +63,7 @@ impl OutputSettingsBuilder {
scale: None,
pixel_spacing: None,
theme: BinaryColorTheme::Default,
max_fps: None,
}
}

Expand Down Expand Up @@ -112,12 +116,20 @@ impl OutputSettingsBuilder {
self
}

/// Sets the FPS limit of the window.
pub fn max_fps(mut self, max_fps: u32) -> Self {
self.max_fps = Some(max_fps);

self
}

/// Builds the output settings.
pub fn build(self) -> OutputSettings {
OutputSettings {
scale: self.scale.unwrap_or(1),
pixel_spacing: self.pixel_spacing.unwrap_or(0),
theme: self.theme,
max_fps: self.max_fps.unwrap_or(60),
}
}
}
Loading

0 comments on commit 11ec451

Please sign in to comment.