From 58b3bfb4fba22ecdb6f846f88f8a74e13ecae8ec Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 21 Sep 2015 09:33:41 +0200 Subject: [PATCH] Extract PixelFormatRequirements from BuilderAttribs --- src/api/cocoa/mod.rs | 10 +++---- src/lib.rs | 65 +++++++++++++++++++++++++------------------- src/window.rs | 14 +++++----- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index e3feda25..a6824897 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -471,16 +471,16 @@ impl Window { // full color size and hope for the best. Another hiccup is that // `NSOpenGLPFAColorSize` also includes `NSOpenGLPFAAlphaSize`, // so we have to account for that as well. - let alpha_depth = builder.alpha_bits.unwrap_or(8); - let color_depth = builder.color_bits.unwrap_or(24) + alpha_depth; + let alpha_depth = builder.pf_reqs.alpha_bits.unwrap_or(8); + let color_depth = builder.pf_reqs.color_bits.unwrap_or(24) + alpha_depth; let mut attributes = vec![ NSOpenGLPFADoubleBuffer as u32, NSOpenGLPFAClosestPolicy as u32, NSOpenGLPFAColorSize as u32, color_depth as u32, NSOpenGLPFAAlphaSize as u32, alpha_depth as u32, - NSOpenGLPFADepthSize as u32, builder.depth_bits.unwrap_or(24) as u32, - NSOpenGLPFAStencilSize as u32, builder.stencil_bits.unwrap_or(8) as u32, + NSOpenGLPFADepthSize as u32, builder.pf_reqs.depth_bits.unwrap_or(24) as u32, + NSOpenGLPFAStencilSize as u32, builder.pf_reqs.stencil_bits.unwrap_or(8) as u32, NSOpenGLPFAOpenGLProfile as u32, profile, ]; @@ -491,7 +491,7 @@ impl Window { attributes.push(NSOpenGLPFAColorFloat as u32); } - builder.multisampling.map(|samples| { + builder.pf_reqs.multisampling.map(|samples| { attributes.push(NSOpenGLPFAMultisample as u32); attributes.push(NSOpenGLPFASampleBuffers as u32); attributes.push(1); attributes.push(NSOpenGLPFASamples as u32); attributes.push(samples as u32); diff --git a/src/lib.rs b/src/lib.rs index 438938ae..4cf9b0d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,13 +371,7 @@ pub struct BuilderAttribs<'a> { #[allow(dead_code)] headless: bool, strict: bool, - multisampling: Option, - depth_bits: Option, - stencil_bits: Option, - color_bits: Option, - alpha_bits: Option, - stereoscopy: bool, - srgb: Option, + pf_reqs: PixelFormatRequirements, window: WindowAttributes, opengl: GlAttributes<&'a platform::Window>, } @@ -387,13 +381,7 @@ impl BuilderAttribs<'static> { BuilderAttribs { headless: false, strict: false, - multisampling: None, - depth_bits: None, - stencil_bits: None, - color_bits: None, - alpha_bits: None, - stereoscopy: false, - srgb: None, + pf_reqs: Default::default(), window: Default::default(), opengl: Default::default(), } @@ -408,13 +396,7 @@ impl<'a> BuilderAttribs<'a> { let new_attribs = BuilderAttribs { headless: self.headless, strict: self.strict, - multisampling: self.multisampling, - depth_bits: self.depth_bits, - stencil_bits: self.stencil_bits, - color_bits: self.color_bits, - alpha_bits: self.alpha_bits, - stereoscopy: self.stereoscopy, - srgb: self.srgb, + pf_reqs: self.pf_reqs, window: self.window, opengl: GlAttributes { sharing: None, @@ -434,27 +416,27 @@ impl<'a> BuilderAttribs<'a> { { // filtering formats that don't match the requirements let iter = iter.into_iter().filter(|&(_, ref format)| { - if format.color_bits < self.color_bits.unwrap_or(0) { + if format.color_bits < self.pf_reqs.color_bits.unwrap_or(0) { return false; } - if format.alpha_bits < self.alpha_bits.unwrap_or(0) { + if format.alpha_bits < self.pf_reqs.alpha_bits.unwrap_or(0) { return false; } - if format.depth_bits < self.depth_bits.unwrap_or(0) { + if format.depth_bits < self.pf_reqs.depth_bits.unwrap_or(0) { return false; } - if format.stencil_bits < self.stencil_bits.unwrap_or(0) { + if format.stencil_bits < self.pf_reqs.stencil_bits.unwrap_or(0) { return false; } - if !format.stereoscopy && self.stereoscopy { + if !format.stereoscopy && self.pf_reqs.stereoscopy { return false; } - if let Some(req_ms) = self.multisampling { + if let Some(req_ms) = self.pf_reqs.multisampling { match format.multisampling { Some(val) if val >= req_ms => (), _ => return false @@ -465,7 +447,7 @@ impl<'a> BuilderAttribs<'a> { } } - if let Some(srgb) = self.srgb { + if let Some(srgb) = self.pf_reqs.srgb { if srgb != format.srgb { return false; } @@ -529,6 +511,33 @@ impl<'a> BuilderAttribs<'a> { } } +/// VERY UNSTABLE! Describes how the backend should choose a pixel format. +#[derive(Clone, Debug)] +#[allow(missing_docs)] +pub struct PixelFormatRequirements { + pub multisampling: Option, + pub depth_bits: Option, + pub stencil_bits: Option, + pub color_bits: Option, + pub alpha_bits: Option, + pub stereoscopy: bool, + pub srgb: Option, +} + +impl Default for PixelFormatRequirements { + fn default() -> PixelFormatRequirements { + PixelFormatRequirements { + multisampling: None, + depth_bits: None, + stencil_bits: None, + color_bits: None, + alpha_bits: None, + stereoscopy: false, + srgb: None, + } + } +} + /// Attributes to use when creating a window. #[derive(Clone)] pub struct WindowAttributes { diff --git a/src/window.rs b/src/window.rs index 4a45a018..85d3440e 100644 --- a/src/window.rs +++ b/src/window.rs @@ -110,38 +110,38 @@ impl<'a> WindowBuilder<'a> { /// Will panic if `samples` is not a power of two. pub fn with_multisampling(mut self, samples: u16) -> WindowBuilder<'a> { assert!(samples.is_power_of_two()); - self.attribs.multisampling = Some(samples); + self.attribs.pf_reqs.multisampling = Some(samples); self } /// Sets the number of bits in the depth buffer. pub fn with_depth_buffer(mut self, bits: u8) -> WindowBuilder<'a> { - self.attribs.depth_bits = Some(bits); + self.attribs.pf_reqs.depth_bits = Some(bits); self } /// Sets the number of bits in the stencil buffer. pub fn with_stencil_buffer(mut self, bits: u8) -> WindowBuilder<'a> { - self.attribs.stencil_bits = Some(bits); + self.attribs.pf_reqs.stencil_bits = Some(bits); self } /// Sets the number of bits in the color buffer. pub fn with_pixel_format(mut self, color_bits: u8, alpha_bits: u8) -> WindowBuilder<'a> { - self.attribs.color_bits = Some(color_bits); - self.attribs.alpha_bits = Some(alpha_bits); + self.attribs.pf_reqs.color_bits = Some(color_bits); + self.attribs.pf_reqs.alpha_bits = Some(alpha_bits); self } /// Request the backend to be stereoscopic. pub fn with_stereoscopy(mut self) -> WindowBuilder<'a> { - self.attribs.stereoscopy = true; + self.attribs.pf_reqs.stereoscopy = true; self } /// Sets whether sRGB should be enabled on the window. `None` means "I don't care". pub fn with_srgb(mut self, srgb_enabled: Option) -> WindowBuilder<'a> { - self.attribs.srgb = srgb_enabled; + self.attribs.pf_reqs.srgb = srgb_enabled; self }