From ea17038ac2a25982ffc8e9308eec312864938d20 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 20 May 2015 22:38:06 +0200 Subject: Detach wgl and win32 --- src/api/wgl/make_current_guard.rs | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/api/wgl/make_current_guard.rs (limited to 'src/api/wgl/make_current_guard.rs') diff --git a/src/api/wgl/make_current_guard.rs b/src/api/wgl/make_current_guard.rs new file mode 100644 index 0000000..b890c82 --- /dev/null +++ b/src/api/wgl/make_current_guard.rs @@ -0,0 +1,47 @@ +use std::marker::PhantomData; +use std::io; + +use libc; +use winapi; +use CreationError; + +use super::gl; +/// 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(hdc: winapi::HDC, context: winapi::HGLRC) + -> Result, CreationError> + { + let previous_hdc = gl::wgl::GetCurrentDC() as winapi::HDC; + let previous_hglrc = gl::wgl::GetCurrentContext() as winapi::HGLRC; + + let result = gl::wgl::MakeCurrent(hdc as *const _, context as *const _); + 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); + } + } +} -- cgit v1.2.3