extern crate glfw;
#[macro_use]
extern crate log;
use std::sync::mpsc::Receiver;
use glfw::Glfw;
use glfw::WindowMode;
use glfw::WindowHint;
use glfw::Window;
use glfw::WindowEvent;
use glfw::WindowMode::Windowed;
use glfw::WindowHint::ContextVersion;
use glfw::WindowHint::OpenglForwardCompat;
use glfw::WindowHint::OpenglProfile;
use glfw::OpenGlProfileHint;
pub struct WindowBuilder<'glfw, 'title, 'monitor> {
glfw: &'glfw mut Glfw,
size: Option<(u32, u32)>,
title: Option<&'title str>,
mode: Option<WindowMode<'monitor>>,
common_hints: Vec<WindowHint>,
try_hints: Vec<Vec<WindowHint>>,
}
impl<'glfw, 'title, 'monitor> WindowBuilder<'glfw, 'title, 'monitor> {
pub fn new(glfw: &'glfw mut Glfw) -> WindowBuilder<'glfw, 'title, 'monitor> {
WindowBuilder {
glfw: glfw,
size: None,
title: None,
mode: None,
try_hints: vec![],
common_hints: vec![],
}
}
}
impl<'glfw, 'title, 'monitor, 'hints> WindowBuilder<'glfw, 'title, 'monitor> {
pub fn size(mut self, width: u32, height: u32)
-> WindowBuilder<'glfw, 'title, 'monitor> {
self.size = Some((width, height));
self
}
pub fn title(mut self, title: &'title str)
-> WindowBuilder<'glfw, 'title, 'monitor> {
self.title = Some(title);
self
}
pub fn mode(mut self, mode: WindowMode<'monitor>)
-> WindowBuilder<'glfw, 'title, 'monitor> {
self.mode = Some(mode);
self
}
pub fn try_hints(mut self, hints: &[WindowHint])
-> WindowBuilder<'glfw, 'title, 'monitor> {
self.try_hints.push(hints.iter().map(|&x| x).collect());
self
}
pub fn common_hints(mut self, hints: &[WindowHint])
-> WindowBuilder<'glfw, 'title, 'monitor> {
self.common_hints.extend(hints.iter().map(|&x| x));
self
}
pub fn try_modern_context_hints(self)
-> WindowBuilder<'glfw, 'title, 'monitor> {
self.try_hints(&[
WindowHint::ContextVersion(4, 5),
WindowHint::OpenglForwardCompat(true),
WindowHint::OpenglProfile(OpenGlProfileHint::Core)
])
.try_hints(&[
WindowHint::ContextVersion(4, 4),
WindowHint::OpenglForwardCompat(true),
WindowHint::OpenglProfile(OpenGlProfileHint::Core)
])
.try_hints(&[
WindowHint::ContextVersion(4, 3),
WindowHint::OpenglForwardCompat(true),
WindowHint::OpenglProfile(OpenGlProfileHint::Core)
])
.try_hints(&[
WindowHint::ContextVersion(4, 2),
WindowHint::OpenglForwardCompat(true),
WindowHint::OpenglProfile(OpenGlProfileHint::Core)
])
.try_hints(&[
WindowHint::ContextVersion(4, 1),
WindowHint::OpenglForwardCompat(true),
WindowHint::OpenglProfile(OpenGlProfileHint::Core)
])
.try_hints(&[
WindowHint::ContextVersion(4, 0),
WindowHint::OpenglForwardCompat(true),
WindowHint::OpenglProfile(OpenGlProfileHint::Core)
])
.try_hints(&[
WindowHint::ContextVersion(3, 2),
WindowHint::OpenglForwardCompat(true),
WindowHint::OpenglProfile(OpenGlProfileHint::Core),
])
.try_hints(&[
WindowHint::ContextVersion(3, 1),
WindowHint::OpenglForwardCompat(true),
])
.try_hints(&[
WindowHint::ContextVersion(3, 1),
])
.try_hints(&[
WindowHint::ContextVersion(3, 0),
WindowHint::OpenglForwardCompat(true),
])
.try_hints(&[
WindowHint::ContextVersion(3, 0),
])
.try_hints(&[
WindowHint::ContextVersion(2, 1),
])
.try_hints(&[
WindowHint::ContextVersion(2, 0),
])
}
pub fn create(self) -> Option<(Window, Receiver<(f64, WindowEvent)>)> {
let WindowBuilder { glfw, common_hints, try_hints, size, title, mode } = self;
let (width, height) = size.unwrap_or((640, 480));
let title = title.unwrap_or("GLFW Window");
let mode = mode.unwrap_or(WindowMode::Windowed);
glfw.set_error_callback::<()>(None);
for setup in try_hints.iter() {
glfw.default_window_hints();
for hint in common_hints.iter() {
glfw.window_hint(*hint);
}
for hint in setup.iter() {
glfw.window_hint(*hint);
}
let r = glfw.create_window(width, height, title, mode);
match r {
Some((window, events)) => {
info!("Created GLFW window with GL context hints {:?} and {:?}", common_hints, setup);
return Some((window, events));
},
None => {
debug!("Couldn't create a context for hints {:?} and {:?}", common_hints, setup);
}
}
}
None
}
}