#![deny(missing_docs)]
#![deny(missing_copy_implementations)]
extern crate input;
extern crate shader_version;
use shader_version::OpenGL;
use input::Input;
pub type ProcAddress = *const ();
#[derive(Debug, Copy, Clone)]
pub struct Size {
pub width: u32,
pub height: u32,
}
impl From<[u32; 2]> for Size {
#[inline(always)]
fn from(value: [u32; 2]) -> Size {
Size { width: value[0], height: value[1] }
}
}
impl From<(u32, u32)> for Size {
#[inline(always)]
fn from(value: (u32, u32)) -> Size {
Size { width: value.0, height: value.1 }
}
}
pub trait BuildFromWindowSettings: Sized {
fn build_from_window_settings(settings: WindowSettings)
-> Result<Self, String>;
}
pub trait Window {
type Event;
fn set_should_close(&mut self, value: bool);
fn should_close(&self) -> bool;
fn size(&self) -> Size;
fn swap_buffers(&mut self);
fn poll_event(&mut self) -> Option<Self::Event>;
fn draw_size(&self) -> Size;
}
pub trait AdvancedWindow: Window + Sized {
fn get_title(&self) -> String;
fn set_title(&mut self, value: String);
fn title(mut self, value: String) -> Self {
self.set_title(value);
self
}
fn get_exit_on_esc(&self) -> bool;
fn set_exit_on_esc(&mut self, value: bool);
fn exit_on_esc(mut self, value: bool) -> Self {
self.set_exit_on_esc(value);
self
}
fn set_capture_cursor(&mut self, value: bool);
fn capture_cursor(mut self, value: bool) -> Self {
self.set_capture_cursor(value);
self
}
}
pub trait OpenGLWindow: Window {
fn get_proc_address(&mut self, proc_name: &str) -> ProcAddress;
fn is_current(&self) -> bool;
fn make_current(&mut self);
}
pub struct WindowSettings {
title: String,
size: Size,
samples: u8,
fullscreen: bool,
exit_on_esc: bool,
vsync: bool,
opengl: Option<OpenGL>,
srgb: bool,
}
impl WindowSettings {
pub fn new<T: Into<String>, S: Into<Size>>(
title: T, size: S) -> WindowSettings
{
WindowSettings {
title: title.into(),
size: size.into(),
samples: 0,
fullscreen: false,
exit_on_esc: false,
vsync: false,
opengl: None,
srgb: true,
}
}
pub fn build<W: BuildFromWindowSettings>(self) -> Result<W, String> {
BuildFromWindowSettings::build_from_window_settings(self)
}
pub fn get_title(&self) -> String { self.title.clone() }
pub fn title(mut self, value: String) -> Self {
self.title = value;
self
}
pub fn get_size(&self) -> Size { self.size }
pub fn size(mut self, value: Size) -> Self {
self.size = value;
self
}
pub fn get_fullscreen(&self) -> bool { self.fullscreen }
pub fn fullscreen(mut self, value: bool) -> Self {
self.fullscreen = value;
self
}
pub fn get_exit_on_esc(&self) -> bool { self.exit_on_esc }
pub fn exit_on_esc(mut self, value: bool) -> Self {
self.exit_on_esc = value;
self
}
pub fn get_samples(&self) -> u8 { self.samples }
pub fn samples(mut self, value: u8) -> Self {
self.samples = value;
self
}
pub fn get_vsync(&self) -> bool { self.vsync }
pub fn vsync(mut self, value: bool) -> Self {
self.vsync = value;
self
}
pub fn get_maybe_opengl(&self) -> Option<OpenGL> { self.opengl }
pub fn maybe_opengl(mut self, value: Option<OpenGL>) -> Self {
self.opengl = value;
self
}
pub fn opengl(mut self, value: OpenGL) -> Self {
self.opengl = Some(value);
self
}
pub fn get_srgb(&self) -> bool { self.srgb }
pub fn srgb(mut self, value: bool) -> Self {
self.srgb = value;
self
}
}
pub struct NoWindow {
should_close: bool,
title: String,
size: Size
}
impl NoWindow {
pub fn new(settings: WindowSettings) -> NoWindow {
NoWindow {
should_close: false,
title: settings.title,
size: settings.size,
}
}
}
impl Window for NoWindow {
type Event = Input;
fn should_close(&self) -> bool { self.should_close }
fn set_should_close(&mut self, value: bool) { self.should_close = value; }
fn size(&self) -> Size { self.size }
fn swap_buffers(&mut self) {}
fn poll_event(&mut self) -> Option<Input> { None }
fn draw_size(&self) -> Size { self.size() }
}
impl BuildFromWindowSettings for NoWindow {
fn build_from_window_settings(settings: WindowSettings)
-> Result<Self, String> {
Ok(NoWindow::new(settings))
}
}
impl AdvancedWindow for NoWindow {
fn get_title(&self) -> String { self.title.clone() }
fn set_title(&mut self, value: String) { self.title = value; }
fn get_exit_on_esc(&self) -> bool { false }
fn set_exit_on_esc(&mut self, _value: bool) {}
fn set_capture_cursor(&mut self, _value: bool) {}
}