diff options
author | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-03-01 10:33:25 +0100 |
---|---|---|
committer | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-03-01 10:33:25 +0100 |
commit | cf252c29b4c3a0f4705bc92598c858b895b94f22 (patch) | |
tree | 9149bd97445d583fb79bee9cb544d9956ced2224 /src | |
parent | 77d8a113385e8bb85f145d514724a1e3bd574cfd (diff) | |
download | glutin-cf252c29b4c3a0f4705bc92598c858b895b94f22.tar.gz glutin-cf252c29b4c3a0f4705bc92598c858b895b94f22.zip |
Use the WGL API to determine extended pixel format, plus fix creation
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/win32/init.rs | 68 |
2 files changed, 34 insertions, 35 deletions
@@ -195,6 +195,7 @@ pub enum MouseCursor { /// Describes a possible format. Unused. #[allow(missing_docs)] +#[derive(Debug)] pub struct PixelFormat { pub red_bits: u8, pub green_bits: u8, diff --git a/src/win32/init.rs b/src/win32/init.rs index d468820..6fdafea 100644 --- a/src/win32/init.rs +++ b/src/win32/init.rs @@ -99,9 +99,8 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O // adjusting the window coordinates using the style unsafe { user32::AdjustWindowRectEx(&mut rect, style, 0, ex_style) }; - // getting the address of wglCreateContextAttribsARB and the pixel format - // that we will use - let (extra_functions, pixel_format) = { + // getting the address of wglCreateContextAttribsARB + let extra_functions = { // creating a dummy invisible window for GL initialization let dummy_window = unsafe { let handle = user32::CreateWindowExW(ex_style, class_name.as_ptr(), @@ -132,32 +131,11 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O hdc }; - // getting the pixel format that we will use - let pixel_format = { + // getting the pixel format that we will use and setting it + { let formats = enumerate_native_pixel_formats(dummy_hdc); let (id, _) = builder.choose_pixel_format(formats.into_iter().map(|(a, b)| (b, a))); - - let mut output: winapi::PIXELFORMATDESCRIPTOR = unsafe { mem::zeroed() }; - if unsafe { gdi32::DescribePixelFormat(dummy_hdc, id, - mem::size_of::<winapi::PIXELFORMATDESCRIPTOR>() as winapi::UINT, &mut output) } == 0 - { - let err = Err(OsError(format!("DescribePixelFormat function failed: {}", - os::error_string(os::errno())))); - unsafe { user32::DestroyWindow(dummy_window); } - return err; - } - - output - }; - - // calling SetPixelFormat - unsafe { - if gdi32::SetPixelFormat(dummy_hdc, 1, &pixel_format) == 0 { - let err = Err(OsError(format!("SetPixelFormat function failed: {}", - os::error_string(os::errno())))); - user32::DestroyWindow(dummy_window); - return err; - } + try!(set_pixel_format(dummy_hdc, id)); } // creating the dummy OpenGL context @@ -186,7 +164,7 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O unsafe { user32::DestroyWindow(dummy_window); } // returning the address - (extra_functions, pixel_format) + extra_functions }; // creating the real window this time @@ -233,13 +211,15 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O }; // calling SetPixelFormat - unsafe { - if gdi32::SetPixelFormat(hdc, 1, &pixel_format) == 0 { - let err = Err(OsError(format!("SetPixelFormat function failed: {}", - os::error_string(os::errno())))); - user32::DestroyWindow(real_window); - return err; - } + { + let formats = if extra_functions.GetPixelFormatAttribivARB.is_loaded() { + enumerate_arb_pixel_formats(&extra_functions, hdc) + } else { + enumerate_native_pixel_formats(hdc) + }; + + let (id, _) = builder.choose_pixel_format(formats.into_iter().map(|(a, b)| (b, a))); + try!(set_pixel_format(hdc, id)); } // creating the OpenGL context @@ -512,6 +492,24 @@ fn enumerate_arb_pixel_formats(extra: &gl::wgl_extra::Wgl, hdc: winapi::HDC) result } +fn set_pixel_format(hdc: winapi::HDC, id: libc::c_int) -> Result<(), CreationError> { + let mut output: winapi::PIXELFORMATDESCRIPTOR = unsafe { mem::zeroed() }; + + if unsafe { gdi32::DescribePixelFormat(hdc, id, + mem::size_of::<winapi::PIXELFORMATDESCRIPTOR>() as winapi::UINT, &mut output) } == 0 + { + return Err(OsError(format!("DescribePixelFormat function failed: {}", + os::error_string(os::errno())))); + } + + if unsafe { gdi32::SetPixelFormat(hdc, id, &output) } == 0 { + return Err(OsError(format!("SetPixelFormat function failed: {}", + os::error_string(os::errno())))); + } + + Ok(()) +} + fn load_opengl32_dll() -> Result<winapi::HMODULE, CreationError> { let name = "opengl32.dll".utf16_units().chain(Some(0).into_iter()) .collect::<Vec<u16>>().as_ptr(); |