1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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);
}
}
}
|