Remove BuilderAttribs

This commit is contained in:
Pierre Krieger 2015-09-21 13:15:43 +02:00
parent a8d3342468
commit 62bafe2130
13 changed files with 138 additions and 134 deletions

View file

@ -14,12 +14,14 @@ use events::MouseButton;
use std::collections::VecDeque; use std::collections::VecDeque;
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use CursorState; use CursorState;
use GlAttributes;
use GlContext; use GlContext;
use GlRequest; use GlRequest;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use WindowAttributes;
use native_monitor::NativeMonitorId; use native_monitor::NativeMonitorId;
use api::egl; use api::egl;
@ -104,15 +106,19 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
} }
impl Window { impl Window {
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> { pub fn new(win_attribs: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
{
use std::{mem, ptr}; use std::{mem, ptr};
let opengl = opengl.clone().map_sharing(|w| &w.context);
let native_window = unsafe { android_glue::get_native_window() }; let native_window = unsafe { android_glue::get_native_window() };
if native_window.is_null() { if native_window.is_null() {
return Err(OsError(format!("Android's 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) egl::NativeDisplay::Android)
.and_then(|p| p.finish(native_window as *const _))); .and_then(|p| p.finish(native_window as *const _)));
@ -255,9 +261,13 @@ pub struct HeadlessContext(EglContext);
impl HeadlessContext { impl HeadlessContext {
/// See the docs in the crate root file. /// See the docs in the crate root file.
pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder, egl::NativeDisplay::Android)); opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
let context = try!(context.finish_pbuffer(builder.window.dimensions.unwrap_or((800, 600)))); // TODO: {
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)) Ok(HeadlessContext(context))
} }
} }

View file

@ -5,14 +5,16 @@ use libc;
use api::osmesa::{OsMesaContext, OsMesaCreationError}; use api::osmesa::{OsMesaContext, OsMesaCreationError};
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use Event; use Event;
use GlAttributes;
use GlContext; use GlContext;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use CursorState; use CursorState;
use MouseCursor; use MouseCursor;
use WindowAttributes;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::path::Path; use std::path::Path;
@ -84,8 +86,14 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
} }
impl Window { impl Window {
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> { pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
let opengl = match OsMesaContext::new(builder) { opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
{
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::NotSupported) => return Err(CreationError::NotSupported),
Err(OsMesaCreationError::CreationError(e)) => return Err(e), Err(OsMesaCreationError::CreationError(e)) => return Err(e),
Ok(c) => c Ok(c) => c

View file

@ -1,8 +1,9 @@
use ContextError; use ContextError;
use CreationError; use CreationError;
use CreationError::OsError; use CreationError::OsError;
use BuilderAttribs; use GlAttributes;
use GlContext; use GlContext;
use PixelFormatRequirements;
use libc; use libc;
use std::ptr; use std::ptr;
@ -27,8 +28,9 @@ pub struct HeadlessContext {
} }
impl HeadlessContext { impl HeadlessContext {
pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements,
let (width, height) = builder.window.dimensions.unwrap_or((1024, 768)); opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
{
let context = unsafe { let context = unsafe {
let attributes = [ let attributes = [
NSOpenGLPFAAccelerated as u32, NSOpenGLPFAAccelerated as u32,

View file

@ -7,7 +7,6 @@ use CreationError::OsError;
use libc; use libc;
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use GlAttributes; use GlAttributes;
use GlContext; use GlContext;
@ -269,12 +268,14 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
impl Window { impl Window {
#[cfg(feature = "window")] #[cfg(feature = "window")]
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> { pub fn new(win_attribs: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
if builder.opengl.sharing.is_some() { opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
{
if opengl.sharing.is_some() {
unimplemented!() unimplemented!()
} }
match builder.opengl.robustness { match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported); return Err(CreationError::RobustnessNotSupported);
}, },
@ -286,7 +287,7 @@ impl Window {
None => { return Err(OsError(format!("Couldn't create NSApplication"))); }, 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, Some(window) => window,
None => { return Err(OsError(format!("Couldn't create NSWindow"))); }, 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 // TODO: perhaps we should return error from create_context so we can
// determine the cause of failure and possibly recover? // 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), Ok((context, pf)) => (context, pf),
Err(e) => { return Err(OsError(format!("Couldn't create OpenGL context: {}", e))); }, Err(e) => { return Err(OsError(format!("Couldn't create OpenGL context: {}", e))); },
}; };
unsafe { unsafe {
if builder.window.transparent { if win_attribs.transparent {
let clear_col = { let clear_col = {
let cls = Class::get("NSColor").unwrap(); let cls = Class::get("NSColor").unwrap();
@ -320,7 +321,7 @@ impl Window {
} }
app.activateIgnoringOtherApps_(YES); app.activateIgnoringOtherApps_(YES);
if builder.window.visible { if win_attribs.visible {
window.makeKeyAndOrderFront_(nil); window.makeKeyAndOrderFront_(nil);
} else { } else {
window.makeKeyWindow(); window.makeKeyWindow();

View file

@ -3,11 +3,12 @@
extern crate osmesa_sys; extern crate osmesa_sys;
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use GlAttributes;
use GlContext; use GlContext;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use Robustness; use Robustness;
use libc; use libc;
use std::{mem, ptr}; use std::{mem, ptr};
@ -32,20 +33,23 @@ impl From<CreationError> for OsMesaCreationError {
} }
impl OsMesaContext { impl OsMesaContext {
pub fn new(builder: BuilderAttribs) -> Result<OsMesaContext, OsMesaCreationError> { pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&OsMesaContext>) -> Result<OsMesaContext, OsMesaCreationError>
{
if let Err(_) = osmesa_sys::OsMesa::try_loading() { if let Err(_) = osmesa_sys::OsMesa::try_loading() {
return Err(OsMesaCreationError::NotSupported); 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 => { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported.into()); return Err(CreationError::RobustnessNotSupported.into());
}, },
_ => () _ => ()
} }
// TODO: use `pf_reqs` for the format
// TODO: check OpenGL version and return `OpenGlVersionNotSupported` if necessary // TODO: check OpenGL version and return `OpenGlVersionNotSupported` if necessary
Ok(OsMesaContext { Ok(OsMesaContext {

View file

@ -11,7 +11,6 @@ use super::WindowWrapper;
use super::Context; use super::Context;
use Api; use Api;
use BuilderAttribs;
use CreationError; use CreationError;
use CreationError::OsError; use CreationError::OsError;
use CursorState; use CursorState;

View file

@ -19,7 +19,6 @@ use GlContext;
use Api; use Api;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements; use PixelFormatRequirements;
use BuilderAttribs;
use WindowAttributes; use WindowAttributes;
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};

View file

@ -1,10 +1,11 @@
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use GlAttributes;
use GlRequest; use GlRequest;
use GlContext; use GlContext;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use Robustness; use Robustness;
use WindowAttributes; use WindowAttributes;
@ -14,28 +15,25 @@ use libc;
use platform; use platform;
/// Object that allows you to build headless contexts. /// Object that allows you to build headless contexts.
pub struct HeadlessRendererBuilder { pub struct HeadlessRendererBuilder<'a> {
attribs: BuilderAttribs<'static>, dimensions: (u32, u32),
pf_reqs: PixelFormatRequirements,
opengl: GlAttributes<&'a platform::HeadlessContext>,
} }
impl HeadlessRendererBuilder { impl<'a> HeadlessRendererBuilder<'a> {
/// Initializes a new `HeadlessRendererBuilder` with default values. /// 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 { HeadlessRendererBuilder {
attribs: BuilderAttribs { dimensions: (width, height),
headless: true, pf_reqs: Default::default(),
window: WindowAttributes { opengl: Default::default(),
dimensions: Some((width, height)),
.. Default::default()
},
.. BuilderAttribs::new()
},
} }
} }
/// Sets how the backend should choose the OpenGL API and version. /// Sets how the backend should choose the OpenGL API and version.
pub fn with_gl(mut self, request: GlRequest) -> HeadlessRendererBuilder { pub fn with_gl(mut self, request: GlRequest) -> HeadlessRendererBuilder<'a> {
self.attribs.opengl.version = request; self.opengl.version = request;
self self
} }
@ -43,14 +41,14 @@ impl HeadlessRendererBuilder {
/// ///
/// The default value for this flag is `cfg!(ndebug)`, which means that it's enabled /// 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`. /// when you run `cargo build` and disabled when you run `cargo build --release`.
pub fn with_gl_debug_flag(mut self, flag: bool) -> HeadlessRendererBuilder { pub fn with_gl_debug_flag(mut self, flag: bool) -> HeadlessRendererBuilder<'a> {
self.attribs.opengl.debug = flag; self.opengl.debug = flag;
self self
} }
/// Sets the robustness of the OpenGL context. See the docs of `Robustness`. /// Sets the robustness of the OpenGL context. See the docs of `Robustness`.
pub fn with_gl_robustness(mut self, robustness: Robustness) -> HeadlessRendererBuilder { pub fn with_gl_robustness(mut self, robustness: Robustness) -> HeadlessRendererBuilder<'a> {
self.attribs.opengl.robustness = robustness; self.opengl.robustness = robustness;
self self
} }
@ -59,15 +57,15 @@ impl HeadlessRendererBuilder {
/// Error should be very rare and only occur in case of permission denied, incompatible system, /// Error should be very rare and only occur in case of permission denied, incompatible system,
/// out of memory, etc. /// out of memory, etc.
pub fn build(self) -> Result<HeadlessContext, CreationError> { pub fn build(self) -> Result<HeadlessContext, CreationError> {
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. /// Builds the headless context.
/// ///
/// The context is build in a *strict* way. That means that if the backend couldn't give /// 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. /// you what you requested, an `Err` will be returned.
pub fn build_strict(mut self) -> Result<HeadlessContext, CreationError> { pub fn build_strict(self) -> Result<HeadlessContext, CreationError> {
self.attribs.strict = true;
self.build() self.build()
} }
} }

View file

@ -362,40 +362,7 @@ pub struct PixelFormat {
pub multisampling: Option<u16>, pub multisampling: Option<u16>,
pub srgb: bool, 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<T, I>(&self, iter: I) -> Result<(T, PixelFormat), CreationError>
where I: IntoIterator<Item=(T, PixelFormat)>, T: Clone
{
self.pf_reqs.choose_pixel_format(iter)
}
}
/// VERY UNSTABLE! Describes how the backend should choose a pixel format. /// VERY UNSTABLE! Describes how the backend should choose a pixel format.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[allow(missing_docs)] #[allow(missing_docs)]

View file

@ -6,14 +6,16 @@ pub use api::x11::{WaitEventsIterator, PollEventsIterator};*/
use std::collections::VecDeque; use std::collections::VecDeque;
use std::sync::Arc; use std::sync::Arc;
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use CursorState; use CursorState;
use Event; use Event;
use GlAttributes;
use GlContext; use GlContext;
use MouseCursor; use MouseCursor;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use WindowAttributes;
use libc; use libc;
use api::wayland; use api::wayland;
@ -161,28 +163,26 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
} }
impl Window { impl Window {
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> { pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
let window = builder.window; opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
let pf_reqs = builder.pf_reqs; {
let opengl = builder.opengl;
match *BACKEND { match *BACKEND {
Backend::Wayland => { Backend::Wayland => {
let opengl = opengl.map_sharing(|w| match w { let opengl = opengl.clone().map_sharing(|w| match w {
&Window::Wayland(ref w) => w, &Window::Wayland(ref w) => w,
_ => panic!() // TODO: return an error _ => 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) => { 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, &Window::X(ref w) => w,
_ => panic!() // TODO: return an error _ => 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()))) Backend::Error(ref error) => Err(CreationError::NoBackendAvailable(Box::new(error.clone())))

View file

@ -1,11 +1,12 @@
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"))] #![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"))]
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use GlAttributes;
use GlContext; use GlContext;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use libc; use libc;
use api::osmesa::{self, OsMesaContext}; use api::osmesa::{self, OsMesaContext};
@ -25,8 +26,12 @@ pub type MonitorID = (); // TODO: hack to make things work
pub struct HeadlessContext(OsMesaContext); pub struct HeadlessContext(OsMesaContext);
impl HeadlessContext { impl HeadlessContext {
pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
match OsMesaContext::new(builder) { opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
{
let opengl = opengl.clone().map_sharing(|c| &c.0);
match OsMesaContext::new(dimensions, pf_reqs, &opengl) {
Ok(c) => return Ok(HeadlessContext(c)), Ok(c) => return Ok(HeadlessContext(c)),
Err(osmesa::OsMesaCreationError::NotSupported) => (), Err(osmesa::OsMesaCreationError::NotSupported) => (),
Err(osmesa::OsMesaCreationError::CreationError(e)) => return Err(e), Err(osmesa::OsMesaCreationError::CreationError(e)) => return Err(e),

View file

@ -7,11 +7,13 @@ pub use api::win32::{WindowProxy, PollEventsIterator, WaitEventsIterator};
use libc; use libc;
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use GlAttributes;
use GlContext; use GlContext;
use WindowAttributes;
use api::egl::ffi::egl::Egl; use api::egl::ffi::egl::Egl;
use api::egl; use api::egl;
@ -57,8 +59,10 @@ pub struct Window(win32::Window);
impl Window { impl Window {
/// See the docs in the crate root file. /// See the docs in the crate root file.
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> { pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
win32::Window::new(&builder.window, &builder.pf_reqs, &builder.opengl.clone().map_sharing(|w| &w.0), opengl: &GlAttributes<&Window>) -> Result<Window, CreationError>
{
win32::Window::new(window, pf_reqs, &opengl.clone().map_sharing(|w| &w.0),
EGL.as_ref().map(|w| &w.0)).map(|w| Window(w)) EGL.as_ref().map(|w| &w.0)).map(|w| Window(w))
} }
} }
@ -86,15 +90,15 @@ pub enum HeadlessContext {
} }
impl HeadlessContext { impl HeadlessContext {
pub fn new(mut builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
builder.window.visible = false; opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
{
// if EGL is available, we try using EGL first // if EGL is available, we try using EGL first
// if EGL returns an error, we try the hidden window method // if EGL returns an error, we try the hidden window method
if let &Some(ref egl) = &*EGL { 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)) 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)); .map(|ctxt| HeadlessContext::EglPbuffer(ctxt));
if let Ok(context) = context { 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))); EGL.as_ref().map(|w| &w.0)));
Ok(HeadlessContext::HiddenWindow(window)) Ok(HeadlessContext::HiddenWindow(window))
} }

View file

@ -2,17 +2,19 @@ use std::collections::vec_deque::IntoIter as VecDequeIter;
use std::default::Default; use std::default::Default;
use Api; use Api;
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use CursorState; use CursorState;
use Event; use Event;
use GlAttributes;
use GlContext; use GlContext;
use GlProfile; use GlProfile;
use GlRequest; use GlRequest;
use MouseCursor; use MouseCursor;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use Robustness; use Robustness;
use WindowAttributes;
use native_monitor::NativeMonitorId; use native_monitor::NativeMonitorId;
use gl_common; use gl_common;
@ -22,14 +24,18 @@ use platform;
/// Object that allows you to build windows. /// Object that allows you to build windows.
pub struct WindowBuilder<'a> { pub struct WindowBuilder<'a> {
attribs: BuilderAttribs<'a> pf_reqs: PixelFormatRequirements,
window: WindowAttributes,
opengl: GlAttributes<&'a platform::Window>,
} }
impl<'a> WindowBuilder<'a> { impl<'a> WindowBuilder<'a> {
/// Initializes a new `WindowBuilder` with default values. /// Initializes a new `WindowBuilder` with default values.
pub fn new() -> WindowBuilder<'a> { pub fn new() -> WindowBuilder<'a> {
WindowBuilder { 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. /// Width and height are in pixels.
pub fn with_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> { 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 self
} }
/// Requests a specific title for the window. /// Requests a specific title for the window.
pub fn with_title(mut self, title: String) -> WindowBuilder<'a> { pub fn with_title(mut self, title: String) -> WindowBuilder<'a> {
self.attribs.window.title = title; self.window.title = title;
self self
} }
@ -52,7 +58,7 @@ impl<'a> WindowBuilder<'a> {
/// If you don't specify dimensions for the window, it will match the monitor's. /// 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> { pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder<'a> {
let MonitorID(monitor) = monitor; let MonitorID(monitor) = monitor;
self.attribs.window.monitor = Some(monitor); self.window.monitor = Some(monitor);
self self
} }
@ -60,19 +66,19 @@ impl<'a> WindowBuilder<'a> {
/// ///
/// There are some exceptions, like FBOs or VAOs. See the OpenGL documentation. /// There are some exceptions, like FBOs or VAOs. See the OpenGL documentation.
pub fn with_shared_lists(mut self, other: &'a Window) -> WindowBuilder<'a> { 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 self
} }
/// Sets how the backend should choose the OpenGL API and version. /// Sets how the backend should choose the OpenGL API and version.
pub fn with_gl(mut self, request: GlRequest) -> WindowBuilder<'a> { pub fn with_gl(mut self, request: GlRequest) -> WindowBuilder<'a> {
self.attribs.opengl.version = request; self.opengl.version = request;
self self
} }
/// Sets the desired OpenGL context profile. /// Sets the desired OpenGL context profile.
pub fn with_gl_profile(mut self, profile: GlProfile) -> WindowBuilder<'a> { pub fn with_gl_profile(mut self, profile: GlProfile) -> WindowBuilder<'a> {
self.attribs.opengl.profile = Some(profile); self.opengl.profile = Some(profile);
self 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 /// 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`. /// 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> { pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder<'a> {
self.attribs.opengl.debug = flag; self.opengl.debug = flag;
self self
} }
/// Sets the robustness of the OpenGL context. See the docs of `Robustness`. /// Sets the robustness of the OpenGL context. See the docs of `Robustness`.
pub fn with_gl_robustness(mut self, robustness: Robustness) -> WindowBuilder<'a> { pub fn with_gl_robustness(mut self, robustness: Robustness) -> WindowBuilder<'a> {
self.attribs.opengl.robustness = robustness; self.opengl.robustness = robustness;
self self
} }
/// Requests that the window has vsync enabled. /// Requests that the window has vsync enabled.
pub fn with_vsync(mut self) -> WindowBuilder<'a> { pub fn with_vsync(mut self) -> WindowBuilder<'a> {
self.attribs.opengl.vsync = true; self.opengl.vsync = true;
self self
} }
/// Sets whether the window will be initially hidden or visible. /// Sets whether the window will be initially hidden or visible.
pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> { pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> {
self.attribs.window.visible = visible; self.window.visible = visible;
self self
} }
@ -110,56 +116,56 @@ impl<'a> WindowBuilder<'a> {
/// Will panic if `samples` is not a power of two. /// Will panic if `samples` is not a power of two.
pub fn with_multisampling(mut self, samples: u16) -> WindowBuilder<'a> { pub fn with_multisampling(mut self, samples: u16) -> WindowBuilder<'a> {
assert!(samples.is_power_of_two()); assert!(samples.is_power_of_two());
self.attribs.pf_reqs.multisampling = Some(samples); self.pf_reqs.multisampling = Some(samples);
self self
} }
/// Sets the number of bits in the depth buffer. /// Sets the number of bits in the depth buffer.
pub fn with_depth_buffer(mut self, bits: u8) -> WindowBuilder<'a> { 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 self
} }
/// Sets the number of bits in the stencil buffer. /// Sets the number of bits in the stencil buffer.
pub fn with_stencil_buffer(mut self, bits: u8) -> WindowBuilder<'a> { 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 self
} }
/// Sets the number of bits in the color buffer. /// Sets the number of bits in the color buffer.
pub fn with_pixel_format(mut self, color_bits: u8, alpha_bits: u8) -> WindowBuilder<'a> { 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.pf_reqs.color_bits = Some(color_bits);
self.attribs.pf_reqs.alpha_bits = Some(alpha_bits); self.pf_reqs.alpha_bits = Some(alpha_bits);
self self
} }
/// Request the backend to be stereoscopic. /// Request the backend to be stereoscopic.
pub fn with_stereoscopy(mut self) -> WindowBuilder<'a> { pub fn with_stereoscopy(mut self) -> WindowBuilder<'a> {
self.attribs.pf_reqs.stereoscopy = true; self.pf_reqs.stereoscopy = true;
self self
} }
/// Sets whether sRGB should be enabled on the window. `None` means "I don't care". /// Sets whether sRGB should be enabled on the window. `None` means "I don't care".
pub fn with_srgb(mut self, srgb_enabled: Option<bool>) -> WindowBuilder<'a> { pub fn with_srgb(mut self, srgb_enabled: Option<bool>) -> WindowBuilder<'a> {
self.attribs.pf_reqs.srgb = srgb_enabled; self.pf_reqs.srgb = srgb_enabled;
self self
} }
/// Sets whether the background of the window should be transparent. /// Sets whether the background of the window should be transparent.
pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder<'a> { pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder<'a> {
self.attribs.window.transparent = transparent; self.window.transparent = transparent;
self self
} }
/// Sets whether the window should have a border, a title bar, etc. /// Sets whether the window should have a border, a title bar, etc.
pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder<'a> { pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder<'a> {
self.attribs.window.decorations = decorations; self.window.decorations = decorations;
self self
} }
/// Enables multitouch /// Enables multitouch
pub fn with_multitouch(mut self) -> WindowBuilder<'a> { pub fn with_multitouch(mut self) -> WindowBuilder<'a> {
self.attribs.window.multitouch = true; self.window.multitouch = true;
self self
} }
@ -169,25 +175,25 @@ impl<'a> WindowBuilder<'a> {
/// out of memory, etc. /// out of memory, etc.
pub fn build(mut self) -> Result<Window, CreationError> { pub fn build(mut self) -> Result<Window, CreationError> {
// resizing the window to the dimensions of the monitor when fullscreen // resizing the window to the dimensions of the monitor when fullscreen
if self.attribs.window.dimensions.is_none() && self.attribs.window.monitor.is_some() { if self.window.dimensions.is_none() && self.window.monitor.is_some() {
self.attribs.window.dimensions = Some(self.attribs.window.monitor.as_ref().unwrap().get_dimensions()) self.window.dimensions = Some(self.window.monitor.as_ref().unwrap().get_dimensions())
} }
// default dimensions // default dimensions
if self.attribs.window.dimensions.is_none() { if self.window.dimensions.is_none() {
self.attribs.window.dimensions = Some((1024, 768)); self.window.dimensions = Some((1024, 768));
} }
// building // 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. /// Builds the window.
/// ///
/// The context is build in a *strict* way. That means that if the backend couldn't give /// 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. /// you what you requested, an `Err` will be returned.
pub fn build_strict(mut self) -> Result<Window, CreationError> { pub fn build_strict(self) -> Result<Window, CreationError> {
self.attribs.strict = true;
self.build() self.build()
} }
} }