diff options
author | tomaka <pierre.krieger1708@gmail.com> | 2015-04-24 14:05:35 +0200 |
---|---|---|
committer | tomaka <pierre.krieger1708@gmail.com> | 2015-04-24 14:05:35 +0200 |
commit | b6252d9de2f487982193569212e301fb598593dd (patch) | |
tree | 3061216335701a5faa8bd3c5c9ec0499a357be5a /src/api/win32/make_current_guard.rs | |
parent | c1af76550f311e3da7a08d393b4ea9805cb61a7b (diff) | |
parent | 3ad7f9a58429b02b11b18f6a70ac011f698b6f4b (diff) | |
download | glutin-b6252d9de2f487982193569212e301fb598593dd.tar.gz glutin-b6252d9de2f487982193569212e301fb598593dd.zip |
Merge pull request #393 from tomaka/reorganization
Create reorganization
Diffstat (limited to 'src/api/win32/make_current_guard.rs')
-rw-r--r-- | src/api/win32/make_current_guard.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/api/win32/make_current_guard.rs b/src/api/win32/make_current_guard.rs new file mode 100644 index 0000000..8983899 --- /dev/null +++ b/src/api/win32/make_current_guard.rs @@ -0,0 +1,52 @@ +use std::marker::PhantomData; +use std::io; + +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: {}", + format!("{}", io::Error::last_os_error())))); + } + + Ok(CurrentContextGuard { + previous_hdc: previous_hdc, + previous_hglrc: previous_hglrc, + marker1: PhantomData, + marker2: PhantomData, + }) + } +} + +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); + } + } +} |