diff options
author | tomaka <pierre.krieger1708@gmail.com> | 2015-03-01 14:21:22 +0100 |
---|---|---|
committer | tomaka <pierre.krieger1708@gmail.com> | 2015-03-01 14:21:22 +0100 |
commit | 2ccc7b84586e5c9713f0b8e5442a1e168e844b93 (patch) | |
tree | e515b0af0d7a06f9ee6ff6ab1d3b64c8e69419e9 /src/win32/make_current_guard.rs | |
parent | 65046ffc41790e9a7c4fe26995c2751e8a940af6 (diff) | |
parent | d091323b22e2b8521a74273821d45a7a4ae08b2a (diff) | |
download | glutin-2ccc7b84586e5c9713f0b8e5442a1e168e844b93.tar.gz glutin-2ccc7b84586e5c9713f0b8e5442a1e168e844b93.zip |
Merge pull request #302 from tomaka/win32
Win32
Diffstat (limited to 'src/win32/make_current_guard.rs')
-rw-r--r-- | src/win32/make_current_guard.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/win32/make_current_guard.rs b/src/win32/make_current_guard.rs new file mode 100644 index 0000000..d6bcc8e --- /dev/null +++ b/src/win32/make_current_guard.rs @@ -0,0 +1,53 @@ +use std::marker::PhantomData; +use std::os; + +use libc; +use winapi; +use CreationError; + +use super::gl; +use super::ContextWrapper; +use super::WindowWrapper; + +/// A guard for when you want to make the context current. Destroying the guard restores the +/// previously-current context. +pub struct CurrentContextGuard<'a, 'b> { + previous_hdc: winapi::HDC, + previous_hglrc: winapi::HGLRC, + marker1: PhantomData<&'a ()>, + marker2: PhantomData<&'b ()>, +} + +impl<'a, 'b> CurrentContextGuard<'a, 'b> { + pub unsafe fn make_current(window: &'a WindowWrapper, context: &'b ContextWrapper) + -> Result<CurrentContextGuard<'a, 'b>, CreationError> + { + let previous_hdc = gl::wgl::GetCurrentDC() as winapi::HDC; + let previous_hglrc = gl::wgl::GetCurrentContext() as winapi::HGLRC; + + let result = gl::wgl::MakeCurrent(window.1 as *const libc::c_void, + context.0 as *const libc::c_void); + + if result == 0 { + return Err(CreationError::OsError(format!("wglMakeCurrent function failed: {}", + os::error_string(os::errno())))); + } + + Ok(CurrentContextGuard { + previous_hdc: previous_hdc, + previous_hglrc: previous_hglrc, + marker1: PhantomData, + marker2: PhantomData, + }) + } +} + +#[unsafe_destructor] +impl<'a, 'b> Drop for CurrentContextGuard<'a, 'b> { + fn drop(&mut self) { + unsafe { + gl::wgl::MakeCurrent(self.previous_hdc as *const libc::c_void, + self.previous_hglrc as *const libc::c_void); + } + } +} |