Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to get the display's refresh rate #345

Merged
merged 1 commit into from
Mar 28, 2024
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
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
Loading