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

Made get/set_swap_interval take and return an enum instead of i32 #684

Merged
merged 2 commits into from
Jul 14, 2017
Merged
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
32 changes: 28 additions & 4 deletions src/sdl2/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,26 @@ impl WindowContext {
}
}

/// Represents a setting for vsync/swap interval.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
#[repr(i32)]
pub enum SwapInterval {
Immediate = 0,
VSync = 1,
LateSwapTearing = -1,
}

impl From<i32> for SwapInterval {
fn from(i: i32) -> Self {
match i {
-1 => SwapInterval::LateSwapTearing,
0 => SwapInterval::Immediate,
1 => SwapInterval::VSync,
other => panic!("Invalid value for SwapInterval: {}; valid values are -1, 0, 1", other),
}
}
}

/// Represents the "shell" of a `Window`.
///
/// You can set get and set many of the SDL_Window properties (i.e., border, size, `PixelFormat`, etc)
Expand Down Expand Up @@ -771,12 +791,16 @@ impl VideoSubsystem {
}
}

pub fn gl_set_swap_interval(&self, interval: i32) -> bool {
unsafe { ll::SDL_GL_SetSwapInterval(interval as c_int) == 0 }
pub fn gl_set_swap_interval<S: Into<SwapInterval>>(&self, interval: S) -> bool {
unsafe { ll::SDL_GL_SetSwapInterval(interval.into() as c_int) == 0 }
}

pub fn gl_get_swap_interval(&self) -> i32 {
unsafe { ll::SDL_GL_GetSwapInterval() as i32 }
pub fn gl_get_swap_interval(&self) -> SwapInterval {
unsafe {
let interval = ll::SDL_GL_GetSwapInterval() as i32;
assert!(interval == -1 || interval == 0 || interval == 1);
mem::transmute(interval)
}
}
}

Expand Down