From c244f8c033f100a6e0f3e0b2b408f6ddc1006d47 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Sun, 20 Sep 2015 09:29:22 +0200 Subject: Extract GlAttributes from BuilderAttribs --- src/api/cocoa/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/api/cocoa') diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 259f847..e259f39 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -267,11 +267,11 @@ impl<'a> Iterator for WaitEventsIterator<'a> { impl Window { #[cfg(feature = "window")] pub fn new(builder: BuilderAttribs) -> Result { - if builder.sharing.is_some() { + if builder.opengl.sharing.is_some() { unimplemented!() } - match builder.gl_robustness { + match builder.opengl.robustness { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { return Err(CreationError::RobustnessNotSupported); }, @@ -438,7 +438,7 @@ impl Window { } fn create_context(view: id, builder: &BuilderAttribs) -> Result<(IdRef, PixelFormat), CreationError> { - let profile = match (builder.gl_version, builder.gl_version.to_gl_version(), builder.gl_profile) { + let profile = match (builder.opengl.version, builder.opengl.version.to_gl_version(), builder.opengl.profile) { // Note: we are not using ranges because of a rust bug that should be fixed here: // https://github.com/rust-lang/rust/pull/27050 @@ -540,7 +540,7 @@ impl Window { }; cxt.setView_(view); - let value = if builder.vsync { 1 } else { 0 }; + let value = if builder.opengl.vsync { 1 } else { 0 }; cxt.setValues_forParameter_(&value, NSOpenGLContextParameter::NSOpenGLCPSwapInterval); CGLEnable(cxt.CGLContextObj(), kCGLCECrashOnRemovedFunctions); -- cgit v1.2.3 From 48fe9b26442662517b1590cb98ab81d79b059953 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 21 Sep 2015 09:15:53 +0200 Subject: Extract WindowAttributes from the BuilderAttribs --- src/api/cocoa/headless.rs | 2 +- src/api/cocoa/mod.rs | 12 +++---- src/api/egl/mod.rs | 2 +- src/api/ios/mod.rs | 2 +- src/api/osmesa/mod.rs | 2 +- src/api/wayland/mod.rs | 6 ++-- src/api/win32/init.rs | 22 ++++++------- src/api/x11/window.rs | 18 +++++------ src/headless.rs | 6 +++- src/lib.rs | 77 ++++++++++++++++++++++++++++++++------------- src/platform/windows/mod.rs | 2 +- src/window.rs | 22 ++++++------- 12 files changed, 106 insertions(+), 67 deletions(-) (limited to 'src/api/cocoa') diff --git a/src/api/cocoa/headless.rs b/src/api/cocoa/headless.rs index 7cd0f88..ad1a464 100644 --- a/src/api/cocoa/headless.rs +++ b/src/api/cocoa/headless.rs @@ -28,7 +28,7 @@ pub struct HeadlessContext { impl HeadlessContext { pub fn new(builder: BuilderAttribs) -> Result { - let (width, height) = builder.dimensions.unwrap_or((1024, 768)); + let (width, height) = builder.window.dimensions.unwrap_or((1024, 768)); let context = unsafe { let attributes = [ NSOpenGLPFAAccelerated as u32, diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index e259f39..e3feda2 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -301,7 +301,7 @@ impl Window { }; unsafe { - if builder.transparent { + if builder.window.transparent { let clear_col = { let cls = Class::get("NSColor").unwrap(); @@ -317,7 +317,7 @@ impl Window { } app.activateIgnoringOtherApps_(YES); - if builder.visible { + if builder.window.visible { window.makeKeyAndOrderFront_(nil); } else { window.makeKeyWindow(); @@ -358,7 +358,7 @@ impl Window { fn create_window(builder: &BuilderAttribs) -> Option { unsafe { - let screen = match builder.monitor { + let screen = match builder.window.monitor { Some(ref monitor_id) => { let native_id = match monitor_id.get_native_identifier() { NativeMonitorId::Numeric(num) => num, @@ -390,12 +390,12 @@ impl Window { let frame = match screen { Some(screen) => NSScreen::frame(screen), None => { - let (width, height) = builder.dimensions.unwrap_or((800, 600)); + let (width, height) = builder.window.dimensions.unwrap_or((800, 600)); NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64)) } }; - let masks = if screen.is_some() || !builder.decorations { + let masks = if screen.is_some() || !builder.window.decorations { NSBorderlessWindowMask as NSUInteger | NSResizableWindowMask as NSUInteger } else { @@ -412,7 +412,7 @@ impl Window { NO, )); window.non_nil().map(|window| { - let title = IdRef::new(NSString::alloc(nil).init_str(&builder.title)); + let title = IdRef::new(NSString::alloc(nil).init_str(&builder.window.title)); window.setTitle_(*title); window.setAcceptsMouseMovedEvents_(YES); if screen.is_some() { diff --git a/src/api/egl/mod.rs b/src/api/egl/mod.rs index c06c8ef..32ab71e 100644 --- a/src/api/egl/mod.rs +++ b/src/api/egl/mod.rs @@ -367,7 +367,7 @@ impl<'a> ContextPrototype<'a> { } pub fn finish_pbuffer(self) -> Result { - let dimensions = self.builder.dimensions.unwrap_or((800, 600)); + let dimensions = self.builder.window.dimensions.unwrap_or((800, 600)); let attrs = &[ ffi::egl::WIDTH as libc::c_int, dimensions.0 as libc::c_int, diff --git a/src/api/ios/mod.rs b/src/api/ios/mod.rs index 9d1b527..0163f45 100644 --- a/src/api/ios/mod.rs +++ b/src/api/ios/mod.rs @@ -219,7 +219,7 @@ impl Window { let state = &mut *self.delegate_state; - if builder.multitouch { + if builder.window.multitouch { let _: () = msg_send![state.view, setMultipleTouchEnabled:YES]; } diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index 2bfc8f5..2debc7e 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -37,7 +37,7 @@ impl OsMesaContext { return Err(OsMesaCreationError::NotSupported); } - let dimensions = builder.dimensions.unwrap(); + let dimensions = builder.window.dimensions.unwrap(); match builder.opengl.robustness { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { diff --git a/src/api/wayland/mod.rs b/src/api/wayland/mod.rs index b46e2d1..7bc5798 100644 --- a/src/api/wayland/mod.rs +++ b/src/api/wayland/mod.rs @@ -251,7 +251,7 @@ impl Window { if !is_egl_available() { return Err(CreationError::NotSupported) } - let (w, h) = builder.dimensions.unwrap_or((800, 600)); + let (w, h) = builder.window.dimensions.unwrap_or((800, 600)); let surface = EGLSurface::new( wayland_context.compositor.create_surface(), @@ -259,12 +259,12 @@ impl Window { h as i32 ); - let mut shell_window = if let Some(PlatformMonitorID::Wayland(ref monitor)) = builder.monitor { + let mut shell_window = if let Some(PlatformMonitorID::Wayland(ref monitor)) = builder.window.monitor { let shell_surface = wayland_context.shell.get_shell_surface(surface); shell_surface.set_fullscreen(ShellFullscreenMethod::Default, Some(&monitor.output)); ShellWindow::Plain(shell_surface) } else { - if builder.decorations { + if builder.window.decorations { ShellWindow::Decorated(match DecoratedSurface::new( surface, w as i32, diff --git a/src/api/win32/init.rs b/src/api/win32/init.rs index daa6b5a..8c2d86c 100644 --- a/src/api/win32/init.rs +++ b/src/api/win32/init.rs @@ -47,7 +47,7 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option>(); let (tx, rx) = channel(); @@ -92,20 +92,20 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, // building a RECT object with coordinates let mut rect = winapi::RECT { - left: 0, right: builder.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG, - top: 0, bottom: builder.dimensions.unwrap_or((1024, 768)).1 as winapi::LONG, + left: 0, right: builder.window.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG, + top: 0, bottom: builder.window.dimensions.unwrap_or((1024, 768)).1 as winapi::LONG, }; // switching to fullscreen if necessary // this means adjusting the window's position so that it overlaps the right monitor, // and change the monitor's resolution if necessary - if builder.monitor.is_some() { - let monitor = builder.monitor.as_ref().unwrap(); + if builder.window.monitor.is_some() { + let monitor = builder.window.monitor.as_ref().unwrap(); try!(switch_to_fullscreen(&mut rect, monitor)); } // computing the style and extended style of the window - let (ex_style, style) = if builder.monitor.is_some() || builder.decorations == false { + let (ex_style, style) = if builder.window.monitor.is_some() || builder.window.decorations == false { (winapi::WS_EX_APPWINDOW, winapi::WS_POPUP | winapi::WS_CLIPSIBLINGS | winapi::WS_CLIPCHILDREN) } else { (winapi::WS_EX_APPWINDOW | winapi::WS_EX_WINDOWEDGE, @@ -117,19 +117,19 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, // creating the real window this time, by using the functions in `extra_functions` let real_window = { - let (width, height) = if builder.monitor.is_some() || builder.dimensions.is_some() { + let (width, height) = if builder.window.monitor.is_some() || builder.window.dimensions.is_some() { (Some(rect.right - rect.left), Some(rect.bottom - rect.top)) } else { (None, None) }; - let (x, y) = if builder.monitor.is_some() { + let (x, y) = if builder.window.monitor.is_some() { (Some(rect.left), Some(rect.top)) } else { (None, None) }; - let style = if !builder.visible || builder.headless { + let style = if !builder.window.visible || builder.headless { style } else { style | winapi::WS_VISIBLE @@ -203,7 +203,7 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, }; // making the window transparent - if builder.transparent { + if builder.window.transparent { let bb = winapi::DWM_BLURBEHIND { dwFlags: 0x1, // FIXME: DWM_BB_ENABLE; fEnable: 1, @@ -215,7 +215,7 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, } // calling SetForegroundWindow if fullscreen - if builder.monitor.is_some() { + if builder.window.monitor.is_some() { user32::SetForegroundWindow(real_window.0); } diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs index 8e2bdb5..d39e701 100644 --- a/src/api/x11/window.rs +++ b/src/api/x11/window.rs @@ -296,9 +296,9 @@ pub struct Window { impl Window { pub fn new(display: &Arc, builder: BuilderAttribs) -> Result { - let dimensions = builder.dimensions.unwrap_or((800, 600)); + let dimensions = builder.window.dimensions.unwrap_or((800, 600)); - let screen_id = match builder.monitor { + let screen_id = match builder.window.monitor { Some(PlatformMonitorID::X(MonitorID(_, monitor))) => monitor as i32, _ => unsafe { (display.xlib.XDefaultScreen)(display.display) }, }; @@ -316,7 +316,7 @@ impl Window { // FIXME: `XF86VidModeModeInfo` is missing its `hskew` field. Therefore we point to // `vsyncstart` instead of `vdisplay` as a temporary hack. - let mode_to_switch_to = if builder.monitor.is_some() { + let mode_to_switch_to = if builder.window.monitor.is_some() { let matching_mode = (0 .. mode_num).map(|i| { let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m }).find(|m| m.hdisplay == dimensions.0 as u16 && m.vsyncstart == dimensions.1 as u16); @@ -415,7 +415,7 @@ impl Window { ffi::KeyReleaseMask | ffi::ButtonPressMask | ffi::ButtonReleaseMask | ffi::KeymapStateMask; swa.border_pixel = 0; - if builder.transparent { + if builder.window.transparent { swa.background_pixel = 0; } swa.override_redirect = 0; @@ -424,7 +424,7 @@ impl Window { let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi::CWEventMask; - if builder.transparent { + if builder.window.transparent { window_attributes |= ffi::CWBackPixel; } @@ -448,7 +448,7 @@ impl Window { }; // set visibility - if builder.visible { + if builder.window.visible { unsafe { (display.xlib.XMapRaised)(display.display, window); (display.xlib.XFlush)(display.display); @@ -461,7 +461,7 @@ impl Window { (display.xlib.XInternAtom)(display.display, delete_window, 0) ); (display.xlib.XSetWMProtocols)(display.display, window, &mut wm_delete_window, 1); - with_c_str(&*builder.title, |title| {; + with_c_str(&*builder.window.title, |title| {; (display.xlib.XStoreName)(display.display, window, title); }); (display.xlib.XFlush)(display.display); @@ -509,7 +509,7 @@ impl Window { // Set ICCCM WM_CLASS property based on initial window title unsafe { - with_c_str(&*builder.title, |c_name| { + with_c_str(&*builder.window.title, |c_name| { let hint = (display.xlib.XAllocClassHint)(); (*hint).res_name = c_name as *mut libc::c_char; (*hint).res_class = c_name as *mut libc::c_char; @@ -518,7 +518,7 @@ impl Window { }); } - let is_fullscreen = builder.monitor.is_some(); + let is_fullscreen = builder.window.monitor.is_some(); // finish creating the OpenGL context let context = match context { diff --git a/src/headless.rs b/src/headless.rs index 1d14a4c..83e35b3 100644 --- a/src/headless.rs +++ b/src/headless.rs @@ -6,6 +6,7 @@ use GlRequest; use GlContext; use PixelFormat; use Robustness; +use WindowAttributes; use gl_common; use libc; @@ -23,7 +24,10 @@ impl HeadlessRendererBuilder { HeadlessRendererBuilder { attribs: BuilderAttribs { headless: true, - dimensions: Some((width, height)), + window: WindowAttributes { + dimensions: Some((width, height)), + .. Default::default() + }, .. BuilderAttribs::new() }, } diff --git a/src/lib.rs b/src/lib.rs index 82d0f3b..438938a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,10 +371,6 @@ pub struct BuilderAttribs<'a> { #[allow(dead_code)] headless: bool, strict: bool, - dimensions: Option<(u32, u32)>, - title: String, - monitor: Option, - visible: bool, multisampling: Option, depth_bits: Option, stencil_bits: Option, @@ -382,9 +378,7 @@ pub struct BuilderAttribs<'a> { alpha_bits: Option, stereoscopy: bool, srgb: Option, - transparent: bool, - decorations: bool, - multitouch: bool, + window: WindowAttributes, opengl: GlAttributes<&'a platform::Window>, } @@ -393,10 +387,6 @@ impl BuilderAttribs<'static> { BuilderAttribs { headless: false, strict: false, - dimensions: None, - title: "glutin window".to_string(), - monitor: None, - visible: true, multisampling: None, depth_bits: None, stencil_bits: None, @@ -404,9 +394,7 @@ impl BuilderAttribs<'static> { alpha_bits: None, stereoscopy: false, srgb: None, - transparent: false, - decorations: true, - multitouch: false, + window: Default::default(), opengl: Default::default(), } } @@ -420,10 +408,6 @@ impl<'a> BuilderAttribs<'a> { let new_attribs = BuilderAttribs { headless: self.headless, strict: self.strict, - dimensions: self.dimensions, - title: self.title, - monitor: self.monitor, - visible: self.visible, multisampling: self.multisampling, depth_bits: self.depth_bits, stencil_bits: self.stencil_bits, @@ -431,9 +415,7 @@ impl<'a> BuilderAttribs<'a> { alpha_bits: self.alpha_bits, stereoscopy: self.stereoscopy, srgb: self.srgb, - transparent: self.transparent, - decorations: self.decorations, - multitouch: self.multitouch, + window: self.window, opengl: GlAttributes { sharing: None, version: self.opengl.version, @@ -547,6 +529,59 @@ impl<'a> BuilderAttribs<'a> { } } +/// Attributes to use when creating a window. +#[derive(Clone)] +pub struct WindowAttributes { + /// The dimensions of the window. If this is `None`, some platform-specific dimensions will be + /// used. + /// + /// The default is `None`. + pub dimensions: Option<(u32, u32)>, + + /// If `Some`, the window will be in fullscreen mode with the given monitor. + /// + /// The default is `None`. + pub monitor: Option, + + /// The title of the window in the title bar. + /// + /// The default is `"glutin window"`. + pub title: String, + + /// Whether the window should be immediately visible upon creation. + /// + /// The default is `true`. + pub visible: bool, + + /// Whether the the window should be transparent. If this is true, writing colors + /// with alpha values different than `1.0` will produce a transparent window. + /// + /// The default is `false`. + pub transparent: bool, + + /// Whether the window should have borders and bars. + /// + /// The default is `true`. + pub decorations: bool, + + /// ??? TODO: document me + pub multitouch: bool, +} + +impl Default for WindowAttributes { + fn default() -> WindowAttributes { + WindowAttributes { + dimensions: None, + monitor: None, + title: "glutin window".to_owned(), + visible: true, + transparent: false, + decorations: true, + multitouch: false, + } + } +} + /// Attributes to use when creating an OpenGL context. #[derive(Clone)] pub struct GlAttributes { diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index 7087a23..521939c 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -86,7 +86,7 @@ pub enum HeadlessContext { impl HeadlessContext { pub fn new(mut builder: BuilderAttribs) -> Result { - builder.visible = false; + builder.window.visible = false; // if EGL is available, we try using EGL first // if EGL returns an error, we try the hidden window method diff --git a/src/window.rs b/src/window.rs index afca319..4a45a01 100644 --- a/src/window.rs +++ b/src/window.rs @@ -37,13 +37,13 @@ impl<'a> WindowBuilder<'a> { /// /// Width and height are in pixels. pub fn with_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> { - self.attribs.dimensions = Some((width, height)); + self.attribs.window.dimensions = Some((width, height)); self } /// Requests a specific title for the window. pub fn with_title(mut self, title: String) -> WindowBuilder<'a> { - self.attribs.title = title; + self.attribs.window.title = title; self } @@ -52,7 +52,7 @@ impl<'a> WindowBuilder<'a> { /// If you don't specify dimensions for the window, it will match the monitor's. pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder<'a> { let MonitorID(monitor) = monitor; - self.attribs.monitor = Some(monitor); + self.attribs.window.monitor = Some(monitor); self } @@ -99,7 +99,7 @@ impl<'a> WindowBuilder<'a> { /// Sets whether the window will be initially hidden or visible. pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> { - self.attribs.visible = visible; + self.attribs.window.visible = visible; self } @@ -147,19 +147,19 @@ impl<'a> WindowBuilder<'a> { /// Sets whether the background of the window should be transparent. pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder<'a> { - self.attribs.transparent = transparent; + self.attribs.window.transparent = transparent; self } /// Sets whether the window should have a border, a title bar, etc. pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder<'a> { - self.attribs.decorations = decorations; + self.attribs.window.decorations = decorations; self } /// Enables multitouch pub fn with_multitouch(mut self) -> WindowBuilder<'a> { - self.attribs.multitouch = true; + self.attribs.window.multitouch = true; self } @@ -169,13 +169,13 @@ impl<'a> WindowBuilder<'a> { /// out of memory, etc. pub fn build(mut self) -> Result { // resizing the window to the dimensions of the monitor when fullscreen - if self.attribs.dimensions.is_none() && self.attribs.monitor.is_some() { - self.attribs.dimensions = Some(self.attribs.monitor.as_ref().unwrap().get_dimensions()) + if self.attribs.window.dimensions.is_none() && self.attribs.window.monitor.is_some() { + self.attribs.window.dimensions = Some(self.attribs.window.monitor.as_ref().unwrap().get_dimensions()) } // default dimensions - if self.attribs.dimensions.is_none() { - self.attribs.dimensions = Some((1024, 768)); + if self.attribs.window.dimensions.is_none() { + self.attribs.window.dimensions = Some((1024, 768)); } // building -- cgit v1.2.3 From 58b3bfb4fba22ecdb6f846f88f8a74e13ecae8ec Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 21 Sep 2015 09:33:41 +0200 Subject: 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(-) (limited to 'src/api/cocoa') diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index e3feda2..a682489 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 438938a..4cf9b0d 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 4a45a01..85d3440 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 } -- cgit v1.2.3 From a8d3342468cc1f490fddb361d54215d9e889271f Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 21 Sep 2015 12:39:48 +0200 Subject: Switch OS/X to new design --- src/api/cocoa/mod.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'src/api/cocoa') diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index a682489..35b7046 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -9,11 +9,14 @@ use libc; use Api; use BuilderAttribs; use ContextError; +use GlAttributes; use GlContext; use GlProfile; use GlRequest; use PixelFormat; +use PixelFormatRequirements; use Robustness; +use WindowAttributes; use native_monitor::NativeMonitorId; use objc::runtime::{Class, Object, Sel, BOOL, YES, NO}; @@ -283,7 +286,7 @@ impl Window { None => { return Err(OsError(format!("Couldn't create NSApplication"))); }, }; - let window = match Window::create_window(&builder) + let window = match Window::create_window(&builder.window) { Some(window) => window, None => { return Err(OsError(format!("Couldn't create NSWindow"))); }, @@ -295,7 +298,7 @@ impl Window { // TODO: perhaps we should return error from create_context so we can // determine the cause of failure and possibly recover? - let (context, pf) = match Window::create_context(*view, &builder) { + let (context, pf) = match Window::create_context(*view, &builder.pf_reqs, &builder.opengl) { Ok((context, pf)) => (context, pf), Err(e) => { return Err(OsError(format!("Couldn't create OpenGL context: {}", e))); }, }; @@ -356,9 +359,9 @@ impl Window { } } - fn create_window(builder: &BuilderAttribs) -> Option { + fn create_window(attrs: &WindowAttributes) -> Option { unsafe { - let screen = match builder.window.monitor { + let screen = match attrs.monitor { Some(ref monitor_id) => { let native_id = match monitor_id.get_native_identifier() { NativeMonitorId::Numeric(num) => num, @@ -390,12 +393,12 @@ impl Window { let frame = match screen { Some(screen) => NSScreen::frame(screen), None => { - let (width, height) = builder.window.dimensions.unwrap_or((800, 600)); + let (width, height) = attrs.dimensions.unwrap_or((800, 600)); NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64)) } }; - let masks = if screen.is_some() || !builder.window.decorations { + let masks = if screen.is_some() || !attrs.decorations { NSBorderlessWindowMask as NSUInteger | NSResizableWindowMask as NSUInteger } else { @@ -412,7 +415,7 @@ impl Window { NO, )); window.non_nil().map(|window| { - let title = IdRef::new(NSString::alloc(nil).init_str(&builder.window.title)); + let title = IdRef::new(NSString::alloc(nil).init_str(&attrs.title)); window.setTitle_(*title); window.setAcceptsMouseMovedEvents_(YES); if screen.is_some() { @@ -437,8 +440,10 @@ impl Window { } } - fn create_context(view: id, builder: &BuilderAttribs) -> Result<(IdRef, PixelFormat), CreationError> { - let profile = match (builder.opengl.version, builder.opengl.version.to_gl_version(), builder.opengl.profile) { + fn create_context(view: id, pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&Window>) + -> Result<(IdRef, PixelFormat), CreationError> + { + let profile = match (opengl.version, opengl.version.to_gl_version(), opengl.profile) { // Note: we are not using ranges because of a rust bug that should be fixed here: // https://github.com/rust-lang/rust/pull/27050 @@ -471,16 +476,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.pf_reqs.alpha_bits.unwrap_or(8); - let color_depth = builder.pf_reqs.color_bits.unwrap_or(24) + alpha_depth; + let alpha_depth = pf_reqs.alpha_bits.unwrap_or(8); + let color_depth = 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.pf_reqs.depth_bits.unwrap_or(24) as u32, - NSOpenGLPFAStencilSize as u32, builder.pf_reqs.stencil_bits.unwrap_or(8) as u32, + NSOpenGLPFADepthSize as u32, pf_reqs.depth_bits.unwrap_or(24) as u32, + NSOpenGLPFAStencilSize as u32, pf_reqs.stencil_bits.unwrap_or(8) as u32, NSOpenGLPFAOpenGLProfile as u32, profile, ]; @@ -491,7 +496,7 @@ impl Window { attributes.push(NSOpenGLPFAColorFloat as u32); } - builder.pf_reqs.multisampling.map(|samples| { + 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); @@ -540,7 +545,7 @@ impl Window { }; cxt.setView_(view); - let value = if builder.opengl.vsync { 1 } else { 0 }; + let value = if opengl.vsync { 1 } else { 0 }; cxt.setValues_forParameter_(&value, NSOpenGLContextParameter::NSOpenGLCPSwapInterval); CGLEnable(cxt.CGLContextObj(), kCGLCECrashOnRemovedFunctions); -- cgit v1.2.3 From 62bafe2130db5aaf32bd46091581086ae435e4cf Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 21 Sep 2015 13:15:43 +0200 Subject: Remove BuilderAttribs --- src/api/android/mod.rs | 22 +++++++++---- src/api/caca/mod.rs | 14 ++++++-- src/api/cocoa/headless.rs | 8 +++-- src/api/cocoa/mod.rs | 17 +++++----- src/api/osmesa/mod.rs | 12 ++++--- src/api/win32/init.rs | 1 - src/api/win32/mod.rs | 1 - src/headless.rs | 42 ++++++++++++------------ src/lib.rs | 35 +------------------- src/platform/linux/api_dispatch.rs | 20 ++++++------ src/platform/linux/mod.rs | 11 +++++-- src/platform/windows/mod.rs | 23 +++++++------ src/window.rs | 66 +++++++++++++++++++++----------------- 13 files changed, 138 insertions(+), 134 deletions(-) (limited to 'src/api/cocoa') diff --git a/src/api/android/mod.rs b/src/api/android/mod.rs index ecea9b3..be5a79f 100644 --- a/src/api/android/mod.rs +++ b/src/api/android/mod.rs @@ -14,12 +14,14 @@ use events::MouseButton; use std::collections::VecDeque; use Api; -use BuilderAttribs; use ContextError; use CursorState; +use GlAttributes; use GlContext; use GlRequest; use PixelFormat; +use PixelFormatRequirements; +use WindowAttributes; use native_monitor::NativeMonitorId; use api::egl; @@ -104,15 +106,19 @@ impl<'a> Iterator for WaitEventsIterator<'a> { } impl Window { - pub fn new(builder: BuilderAttribs) -> Result { + pub fn new(win_attribs: &WindowAttributes, pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&Window>) -> Result + { use std::{mem, ptr}; + let opengl = opengl.clone().map_sharing(|w| &w.context); + let native_window = unsafe { android_glue::get_native_window() }; if native_window.is_null() { return Err(OsError(format!("Android's native window is null"))); } - let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder.pf_reqs, &builder.opengl, + let context = try!(EglContext::new(egl::ffi::egl::Egl, pf_reqs, &opengl, egl::NativeDisplay::Android) .and_then(|p| p.finish(native_window as *const _))); @@ -255,9 +261,13 @@ pub struct HeadlessContext(EglContext); impl HeadlessContext { /// See the docs in the crate root file. - pub fn new(builder: BuilderAttribs) -> Result { - let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder, egl::NativeDisplay::Android)); - let context = try!(context.finish_pbuffer(builder.window.dimensions.unwrap_or((800, 600)))); // TODO: + pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&HeadlessContext>) -> Result + { + let opengl = opengl.clone().map_sharing(|c| &c.0); + let context = try!(EglContext::new(egl::ffi::egl::Egl, pf_reqs, &opengl, + egl::NativeDisplay::Android)); + let context = try!(context.finish_pbuffer(dimensions)); // TODO: Ok(HeadlessContext(context)) } } diff --git a/src/api/caca/mod.rs b/src/api/caca/mod.rs index 3b4018e..c1a19c7 100644 --- a/src/api/caca/mod.rs +++ b/src/api/caca/mod.rs @@ -5,14 +5,16 @@ use libc; use api::osmesa::{OsMesaContext, OsMesaCreationError}; use Api; -use BuilderAttribs; use ContextError; use CreationError; use Event; +use GlAttributes; use GlContext; use PixelFormat; +use PixelFormatRequirements; use CursorState; use MouseCursor; +use WindowAttributes; use std::collections::VecDeque; use std::path::Path; @@ -84,8 +86,14 @@ impl<'a> Iterator for WaitEventsIterator<'a> { } impl Window { - pub fn new(builder: BuilderAttribs) -> Result { - let opengl = match OsMesaContext::new(builder) { + pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&Window>) -> Result + { + let opengl = opengl.clone().map_sharing(|w| &w.opengl); + + let opengl = match OsMesaContext::new(window.dimensions.unwrap_or((800, 600)), pf_reqs, + &opengl) + { Err(OsMesaCreationError::NotSupported) => return Err(CreationError::NotSupported), Err(OsMesaCreationError::CreationError(e)) => return Err(e), Ok(c) => c diff --git a/src/api/cocoa/headless.rs b/src/api/cocoa/headless.rs index ad1a464..f9bbffd 100644 --- a/src/api/cocoa/headless.rs +++ b/src/api/cocoa/headless.rs @@ -1,8 +1,9 @@ use ContextError; use CreationError; use CreationError::OsError; -use BuilderAttribs; +use GlAttributes; use GlContext; +use PixelFormatRequirements; use libc; use std::ptr; @@ -27,8 +28,9 @@ pub struct HeadlessContext { } impl HeadlessContext { - pub fn new(builder: BuilderAttribs) -> Result { - let (width, height) = builder.window.dimensions.unwrap_or((1024, 768)); + pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&HeadlessContext>) -> Result + { let context = unsafe { let attributes = [ NSOpenGLPFAAccelerated as u32, diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 35b7046..539545c 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -7,7 +7,6 @@ use CreationError::OsError; use libc; use Api; -use BuilderAttribs; use ContextError; use GlAttributes; use GlContext; @@ -269,12 +268,14 @@ impl<'a> Iterator for WaitEventsIterator<'a> { impl Window { #[cfg(feature = "window")] - pub fn new(builder: BuilderAttribs) -> Result { - if builder.opengl.sharing.is_some() { + pub fn new(win_attribs: &WindowAttributes, pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&Window>) -> Result + { + if opengl.sharing.is_some() { unimplemented!() } - match builder.opengl.robustness { + match opengl.robustness { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { return Err(CreationError::RobustnessNotSupported); }, @@ -286,7 +287,7 @@ impl Window { None => { return Err(OsError(format!("Couldn't create NSApplication"))); }, }; - let window = match Window::create_window(&builder.window) + let window = match Window::create_window(win_attribs) { Some(window) => window, None => { return Err(OsError(format!("Couldn't create NSWindow"))); }, @@ -298,13 +299,13 @@ impl Window { // TODO: perhaps we should return error from create_context so we can // determine the cause of failure and possibly recover? - let (context, pf) = match Window::create_context(*view, &builder.pf_reqs, &builder.opengl) { + let (context, pf) = match Window::create_context(*view, pf_reqs, opengl) { Ok((context, pf)) => (context, pf), Err(e) => { return Err(OsError(format!("Couldn't create OpenGL context: {}", e))); }, }; unsafe { - if builder.window.transparent { + if win_attribs.transparent { let clear_col = { let cls = Class::get("NSColor").unwrap(); @@ -320,7 +321,7 @@ impl Window { } app.activateIgnoringOtherApps_(YES); - if builder.window.visible { + if win_attribs.visible { window.makeKeyAndOrderFront_(nil); } else { window.makeKeyWindow(); diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index 2debc7e..ca34e93 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -3,11 +3,12 @@ extern crate osmesa_sys; use Api; -use BuilderAttribs; use ContextError; use CreationError; +use GlAttributes; use GlContext; use PixelFormat; +use PixelFormatRequirements; use Robustness; use libc; use std::{mem, ptr}; @@ -32,20 +33,23 @@ impl From for OsMesaCreationError { } impl OsMesaContext { - pub fn new(builder: BuilderAttribs) -> Result { + pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&OsMesaContext>) -> Result + { if let Err(_) = osmesa_sys::OsMesa::try_loading() { return Err(OsMesaCreationError::NotSupported); } - let dimensions = builder.window.dimensions.unwrap(); + if opengl.sharing.is_some() { unimplemented!() } // TODO: proper error - match builder.opengl.robustness { + match opengl.robustness { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { return Err(CreationError::RobustnessNotSupported.into()); }, _ => () } + // TODO: use `pf_reqs` for the format // TODO: check OpenGL version and return `OpenGlVersionNotSupported` if necessary Ok(OsMesaContext { diff --git a/src/api/win32/init.rs b/src/api/win32/init.rs index 5628a98..7c7eb83 100644 --- a/src/api/win32/init.rs +++ b/src/api/win32/init.rs @@ -11,7 +11,6 @@ use super::WindowWrapper; use super::Context; use Api; -use BuilderAttribs; use CreationError; use CreationError::OsError; use CursorState; diff --git a/src/api/win32/mod.rs b/src/api/win32/mod.rs index eb31873..f580950 100644 --- a/src/api/win32/mod.rs +++ b/src/api/win32/mod.rs @@ -19,7 +19,6 @@ use GlContext; use Api; use PixelFormat; use PixelFormatRequirements; -use BuilderAttribs; use WindowAttributes; pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; diff --git a/src/headless.rs b/src/headless.rs index 83e35b3..4730eb4 100644 --- a/src/headless.rs +++ b/src/headless.rs @@ -1,10 +1,11 @@ use Api; -use BuilderAttribs; use ContextError; use CreationError; +use GlAttributes; use GlRequest; use GlContext; use PixelFormat; +use PixelFormatRequirements; use Robustness; use WindowAttributes; @@ -14,28 +15,25 @@ use libc; use platform; /// Object that allows you to build headless contexts. -pub struct HeadlessRendererBuilder { - attribs: BuilderAttribs<'static>, +pub struct HeadlessRendererBuilder<'a> { + dimensions: (u32, u32), + pf_reqs: PixelFormatRequirements, + opengl: GlAttributes<&'a platform::HeadlessContext>, } -impl HeadlessRendererBuilder { +impl<'a> HeadlessRendererBuilder<'a> { /// Initializes a new `HeadlessRendererBuilder` with default values. - pub fn new(width: u32, height: u32) -> HeadlessRendererBuilder { + pub fn new(width: u32, height: u32) -> HeadlessRendererBuilder<'a> { HeadlessRendererBuilder { - attribs: BuilderAttribs { - headless: true, - window: WindowAttributes { - dimensions: Some((width, height)), - .. Default::default() - }, - .. BuilderAttribs::new() - }, + dimensions: (width, height), + pf_reqs: Default::default(), + opengl: Default::default(), } } /// Sets how the backend should choose the OpenGL API and version. - pub fn with_gl(mut self, request: GlRequest) -> HeadlessRendererBuilder { - self.attribs.opengl.version = request; + pub fn with_gl(mut self, request: GlRequest) -> HeadlessRendererBuilder<'a> { + self.opengl.version = request; self } @@ -43,14 +41,14 @@ impl HeadlessRendererBuilder { /// /// The default value for this flag is `cfg!(ndebug)`, which means that it's enabled /// when you run `cargo build` and disabled when you run `cargo build --release`. - pub fn with_gl_debug_flag(mut self, flag: bool) -> HeadlessRendererBuilder { - self.attribs.opengl.debug = flag; + pub fn with_gl_debug_flag(mut self, flag: bool) -> HeadlessRendererBuilder<'a> { + self.opengl.debug = flag; self } /// Sets the robustness of the OpenGL context. See the docs of `Robustness`. - pub fn with_gl_robustness(mut self, robustness: Robustness) -> HeadlessRendererBuilder { - self.attribs.opengl.robustness = robustness; + pub fn with_gl_robustness(mut self, robustness: Robustness) -> HeadlessRendererBuilder<'a> { + self.opengl.robustness = robustness; self } @@ -59,15 +57,15 @@ impl HeadlessRendererBuilder { /// Error should be very rare and only occur in case of permission denied, incompatible system, /// out of memory, etc. pub fn build(self) -> Result { - platform::HeadlessContext::new(self.attribs).map(|w| HeadlessContext { context: w }) + platform::HeadlessContext::new(self.dimensions, &self.pf_reqs, &self.opengl) + .map(|w| HeadlessContext { context: w }) } /// Builds the headless context. /// /// The context is build in a *strict* way. That means that if the backend couldn't give /// you what you requested, an `Err` will be returned. - pub fn build_strict(mut self) -> Result { - self.attribs.strict = true; + pub fn build_strict(self) -> Result { self.build() } } diff --git a/src/lib.rs b/src/lib.rs index aa51d5a..a75a47a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -362,40 +362,7 @@ pub struct PixelFormat { pub multisampling: Option, pub srgb: bool, } - -/// Attributes -// FIXME: remove `pub` (https://github.com/rust-lang/rust/issues/23585) -#[derive(Clone)] -#[doc(hidden)] -pub struct BuilderAttribs<'a> { - #[allow(dead_code)] - headless: bool, - strict: bool, - pf_reqs: PixelFormatRequirements, - window: WindowAttributes, - opengl: GlAttributes<&'a platform::Window>, -} - -impl BuilderAttribs<'static> { - fn new() -> BuilderAttribs<'static> { - BuilderAttribs { - headless: false, - strict: false, - pf_reqs: Default::default(), - window: Default::default(), - opengl: Default::default(), - } - } -} - -impl<'a> BuilderAttribs<'a> { - fn choose_pixel_format(&self, iter: I) -> Result<(T, PixelFormat), CreationError> - where I: IntoIterator, T: Clone - { - self.pf_reqs.choose_pixel_format(iter) - } -} - + /// VERY UNSTABLE! Describes how the backend should choose a pixel format. #[derive(Clone, Debug)] #[allow(missing_docs)] diff --git a/src/platform/linux/api_dispatch.rs b/src/platform/linux/api_dispatch.rs index 0fae6a0..46c58bb 100644 --- a/src/platform/linux/api_dispatch.rs +++ b/src/platform/linux/api_dispatch.rs @@ -6,14 +6,16 @@ pub use api::x11::{WaitEventsIterator, PollEventsIterator};*/ use std::collections::VecDeque; use std::sync::Arc; -use BuilderAttribs; use ContextError; use CreationError; use CursorState; use Event; +use GlAttributes; use GlContext; use MouseCursor; use PixelFormat; +use PixelFormatRequirements; +use WindowAttributes; use libc; use api::wayland; @@ -161,28 +163,26 @@ impl<'a> Iterator for WaitEventsIterator<'a> { } impl Window { - pub fn new(builder: BuilderAttribs) -> Result { - let window = builder.window; - let pf_reqs = builder.pf_reqs; - let opengl = builder.opengl; - + pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&Window>) -> Result + { match *BACKEND { Backend::Wayland => { - let opengl = opengl.map_sharing(|w| match w { + let opengl = opengl.clone().map_sharing(|w| match w { &Window::Wayland(ref w) => w, _ => panic!() // TODO: return an error }); - wayland::Window::new(&window, &pf_reqs, &opengl).map(Window::Wayland) + wayland::Window::new(window, pf_reqs, &opengl).map(Window::Wayland) }, Backend::X(ref connec) => { - let opengl = opengl.map_sharing(|w| match w { + let opengl = opengl.clone().map_sharing(|w| match w { &Window::X(ref w) => w, _ => panic!() // TODO: return an error }); - x11::Window::new(connec, &window, &pf_reqs, &opengl).map(Window::X) + x11::Window::new(connec, window, pf_reqs, &opengl).map(Window::X) }, Backend::Error(ref error) => Err(CreationError::NoBackendAvailable(Box::new(error.clone()))) diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 3525ce5..f5d663b 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,11 +1,12 @@ #![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"))] use Api; -use BuilderAttribs; use ContextError; use CreationError; +use GlAttributes; use GlContext; use PixelFormat; +use PixelFormatRequirements; use libc; use api::osmesa::{self, OsMesaContext}; @@ -25,8 +26,12 @@ pub type MonitorID = (); // TODO: hack to make things work pub struct HeadlessContext(OsMesaContext); impl HeadlessContext { - pub fn new(builder: BuilderAttribs) -> Result { - match OsMesaContext::new(builder) { + pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&HeadlessContext>) -> Result + { + let opengl = opengl.clone().map_sharing(|c| &c.0); + + match OsMesaContext::new(dimensions, pf_reqs, &opengl) { Ok(c) => return Ok(HeadlessContext(c)), Err(osmesa::OsMesaCreationError::NotSupported) => (), Err(osmesa::OsMesaCreationError::CreationError(e)) => return Err(e), diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index 51f7567..af9261f 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -7,11 +7,13 @@ pub use api::win32::{WindowProxy, PollEventsIterator, WaitEventsIterator}; use libc; use Api; -use BuilderAttribs; use ContextError; use CreationError; use PixelFormat; +use PixelFormatRequirements; +use GlAttributes; use GlContext; +use WindowAttributes; use api::egl::ffi::egl::Egl; use api::egl; @@ -57,8 +59,10 @@ pub struct Window(win32::Window); impl Window { /// See the docs in the crate root file. - pub fn new(builder: BuilderAttribs) -> Result { - win32::Window::new(&builder.window, &builder.pf_reqs, &builder.opengl.clone().map_sharing(|w| &w.0), + pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&Window>) -> Result + { + win32::Window::new(window, pf_reqs, &opengl.clone().map_sharing(|w| &w.0), EGL.as_ref().map(|w| &w.0)).map(|w| Window(w)) } } @@ -86,15 +90,15 @@ pub enum HeadlessContext { } impl HeadlessContext { - pub fn new(mut builder: BuilderAttribs) -> Result { - builder.window.visible = false; - + pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&HeadlessContext>) -> Result + { // if EGL is available, we try using EGL first // if EGL returns an error, we try the hidden window method if let &Some(ref egl) = &*EGL { - let context = EglContext::new(egl.0.clone(), &builder.pf_reqs, &builder.opengl.clone().map_sharing(|_| unimplemented!()), // TODO: + let context = EglContext::new(egl.0.clone(), pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), // TODO: egl::NativeDisplay::Other(None)) - .and_then(|prototype| prototype.finish_pbuffer(builder.window.dimensions.unwrap_or((800, 600)))) // TODO: + .and_then(|prototype| prototype.finish_pbuffer(dimensions)) .map(|ctxt| HeadlessContext::EglPbuffer(ctxt)); if let Ok(context) = context { @@ -102,7 +106,8 @@ impl HeadlessContext { } } - let window = try!(win32::Window::new(&builder.window, &builder.pf_reqs, &builder.opengl.clone().map_sharing(|w| &w.0), + let window = try!(win32::Window::new(&WindowAttributes { visible: false, .. Default::default() }, + pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), //TODO: EGL.as_ref().map(|w| &w.0))); Ok(HeadlessContext::HiddenWindow(window)) } diff --git a/src/window.rs b/src/window.rs index 85d3440..246f5c9 100644 --- a/src/window.rs +++ b/src/window.rs @@ -2,17 +2,19 @@ use std::collections::vec_deque::IntoIter as VecDequeIter; use std::default::Default; use Api; -use BuilderAttribs; use ContextError; use CreationError; use CursorState; use Event; +use GlAttributes; use GlContext; use GlProfile; use GlRequest; use MouseCursor; use PixelFormat; +use PixelFormatRequirements; use Robustness; +use WindowAttributes; use native_monitor::NativeMonitorId; use gl_common; @@ -22,14 +24,18 @@ use platform; /// Object that allows you to build windows. pub struct WindowBuilder<'a> { - attribs: BuilderAttribs<'a> + pf_reqs: PixelFormatRequirements, + window: WindowAttributes, + opengl: GlAttributes<&'a platform::Window>, } impl<'a> WindowBuilder<'a> { /// Initializes a new `WindowBuilder` with default values. pub fn new() -> WindowBuilder<'a> { WindowBuilder { - attribs: BuilderAttribs::new(), + pf_reqs: Default::default(), + window: Default::default(), + opengl: Default::default(), } } @@ -37,13 +43,13 @@ impl<'a> WindowBuilder<'a> { /// /// Width and height are in pixels. pub fn with_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> { - self.attribs.window.dimensions = Some((width, height)); + self.window.dimensions = Some((width, height)); self } /// Requests a specific title for the window. pub fn with_title(mut self, title: String) -> WindowBuilder<'a> { - self.attribs.window.title = title; + self.window.title = title; self } @@ -52,7 +58,7 @@ impl<'a> WindowBuilder<'a> { /// If you don't specify dimensions for the window, it will match the monitor's. pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder<'a> { let MonitorID(monitor) = monitor; - self.attribs.window.monitor = Some(monitor); + self.window.monitor = Some(monitor); self } @@ -60,19 +66,19 @@ impl<'a> WindowBuilder<'a> { /// /// There are some exceptions, like FBOs or VAOs. See the OpenGL documentation. pub fn with_shared_lists(mut self, other: &'a Window) -> WindowBuilder<'a> { - self.attribs.opengl.sharing = Some(&other.window); + self.opengl.sharing = Some(&other.window); self } /// Sets how the backend should choose the OpenGL API and version. pub fn with_gl(mut self, request: GlRequest) -> WindowBuilder<'a> { - self.attribs.opengl.version = request; + self.opengl.version = request; self } /// Sets the desired OpenGL context profile. pub fn with_gl_profile(mut self, profile: GlProfile) -> WindowBuilder<'a> { - self.attribs.opengl.profile = Some(profile); + self.opengl.profile = Some(profile); self } @@ -81,25 +87,25 @@ impl<'a> WindowBuilder<'a> { /// The default value for this flag is `cfg!(debug_assertions)`, which means that it's enabled /// when you run `cargo build` and disabled when you run `cargo build --release`. pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder<'a> { - self.attribs.opengl.debug = flag; + self.opengl.debug = flag; self } /// Sets the robustness of the OpenGL context. See the docs of `Robustness`. pub fn with_gl_robustness(mut self, robustness: Robustness) -> WindowBuilder<'a> { - self.attribs.opengl.robustness = robustness; + self.opengl.robustness = robustness; self } /// Requests that the window has vsync enabled. pub fn with_vsync(mut self) -> WindowBuilder<'a> { - self.attribs.opengl.vsync = true; + self.opengl.vsync = true; self } /// Sets whether the window will be initially hidden or visible. pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> { - self.attribs.window.visible = visible; + self.window.visible = visible; self } @@ -110,56 +116,56 @@ 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.pf_reqs.multisampling = Some(samples); + self.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.pf_reqs.depth_bits = Some(bits); + self.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.pf_reqs.stencil_bits = Some(bits); + self.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.pf_reqs.color_bits = Some(color_bits); - self.attribs.pf_reqs.alpha_bits = Some(alpha_bits); + self.pf_reqs.color_bits = Some(color_bits); + self.pf_reqs.alpha_bits = Some(alpha_bits); self } /// Request the backend to be stereoscopic. pub fn with_stereoscopy(mut self) -> WindowBuilder<'a> { - self.attribs.pf_reqs.stereoscopy = true; + self.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.pf_reqs.srgb = srgb_enabled; + self.pf_reqs.srgb = srgb_enabled; self } /// Sets whether the background of the window should be transparent. pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder<'a> { - self.attribs.window.transparent = transparent; + self.window.transparent = transparent; self } /// Sets whether the window should have a border, a title bar, etc. pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder<'a> { - self.attribs.window.decorations = decorations; + self.window.decorations = decorations; self } /// Enables multitouch pub fn with_multitouch(mut self) -> WindowBuilder<'a> { - self.attribs.window.multitouch = true; + self.window.multitouch = true; self } @@ -169,25 +175,25 @@ impl<'a> WindowBuilder<'a> { /// out of memory, etc. pub fn build(mut self) -> Result { // resizing the window to the dimensions of the monitor when fullscreen - if self.attribs.window.dimensions.is_none() && self.attribs.window.monitor.is_some() { - self.attribs.window.dimensions = Some(self.attribs.window.monitor.as_ref().unwrap().get_dimensions()) + if self.window.dimensions.is_none() && self.window.monitor.is_some() { + self.window.dimensions = Some(self.window.monitor.as_ref().unwrap().get_dimensions()) } // default dimensions - if self.attribs.window.dimensions.is_none() { - self.attribs.window.dimensions = Some((1024, 768)); + if self.window.dimensions.is_none() { + self.window.dimensions = Some((1024, 768)); } // building - platform::Window::new(self.attribs).map(|w| Window { window: w }) + platform::Window::new(&self.window, &self.pf_reqs, &self.opengl) + .map(|w| Window { window: w }) } /// Builds the window. /// /// The context is build in a *strict* way. That means that if the backend couldn't give /// you what you requested, an `Err` will be returned. - pub fn build_strict(mut self) -> Result { - self.attribs.strict = true; + pub fn build_strict(self) -> Result { self.build() } } -- cgit v1.2.3