Skip to content

Commit

Permalink
Add the ability to get the display's refresh rate (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
fililip authored Mar 28, 2024
1 parent 837849c commit 66a5abf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ pub enum TetraError {
/// but was unable to do so.
FailedToChangeDisplayMode(String),

/// Returned when your game tried to get the display's refresh rate
/// but was unable to do so.
FailedToGetRefreshRate(String),

/// Returned when a shape cannot be tessellated.
TessellationError(TessellationError),
}
Expand All @@ -95,6 +99,9 @@ impl Display for TetraError {
"Not enough data was provided to fill a buffer - expected {}, found {}.",
expected, actual
),
TetraError::FailedToGetRefreshRate(msg) => {
write!(f, "Failed to get refresh rate: {}", msg)
}
TetraError::FailedToChangeDisplayMode(msg) => {
write!(f, "Failed to change display mode: {}", msg)
}
Expand All @@ -119,6 +126,7 @@ impl Error for TetraError {
TetraError::InvalidSound(reason) => Some(reason),
TetraError::NotEnoughData { .. } => None,
TetraError::NoAudioDevice => None,
TetraError::FailedToGetRefreshRate(_) => None,
TetraError::FailedToChangeDisplayMode(_) => None,
TetraError::TessellationError(reason) => Some(reason),
}
Expand Down
7 changes: 7 additions & 0 deletions src/platform/window_sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ impl Window {
self.sdl_window.raise()
}

pub fn get_refresh_rate(&self) -> Result<i32> {
self.sdl_window
.display_mode()
.map(|display_mode| display_mode.refresh_rate)
.map_err(|e| TetraError::FailedToGetRefreshRate(e.to_string()))
}

pub fn get_window_title(&self) -> &str {
self.sdl_window.title()
}
Expand Down
5 changes: 5 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ pub fn focus(ctx: &mut Context) {
ctx.window.focus();
}

/// Gets the display's refresh rate.
pub fn get_refresh_rate(ctx: &Context) -> Result<i32> {
ctx.window.get_refresh_rate()
}

/// Gets the current title of the window.
pub fn get_title(ctx: &Context) -> &str {
ctx.window.get_window_title()
Expand Down

0 comments on commit 66a5abf

Please sign in to comment.