pub mod events;
pub mod prelude {
pub use super::events::{Event, WindowEvent};
pub use super::system::{EventListener, EventListenerHandle};
pub use super::WindowParams;
}
mod backends;
mod system;
use self::ins::{ctx, CTX};
use self::system::{EventListener, EventListenerHandle, WindowSystem};
use crate::errors::*;
use crate::math::prelude::Vector2;
#[derive(Debug, Clone)]
pub struct WindowParams {
pub title: String,
pub size: Vector2<u32>,
pub multisample: u16,
pub vsync: bool,
}
impl Default for WindowParams {
fn default() -> Self {
WindowParams {
title: "Window".to_owned(),
size: Vector2::new(640, 320),
multisample: 2,
vsync: false,
}
}
}
pub(crate) unsafe fn setup(params: WindowParams) -> Result<()> {
debug_assert!(CTX.is_null(), "duplicated setup of window system.");
let ctx = WindowSystem::from(params)?;
CTX = Box::into_raw(Box::new(ctx));
Ok(())
}
pub(crate) unsafe fn headless() {
debug_assert!(CTX.is_null(), "duplicated setup of window system.");
let ctx = WindowSystem::headless();
CTX = Box::into_raw(Box::new(ctx));
}
#[inline]
pub(crate) fn resize(dimensions: Vector2<u32>) {
ctx().resize(dimensions);
}
pub(crate) unsafe fn discard() {
if CTX.is_null() {
return;
}
drop(Box::from_raw(CTX as *mut WindowSystem));
CTX = std::ptr::null();
}
pub fn attach<T: EventListener + 'static>(lis: T) -> EventListenerHandle {
ctx().add_event_listener(lis)
}
pub fn detach(handle: EventListenerHandle) {
ctx().remove_event_listener(handle)
}
#[inline]
pub fn show() {
ctx().show();
}
#[inline]
pub fn hide() {
ctx().hide();
}
#[inline]
pub fn make_current() -> Result<()> {
ctx().make_current()
}
#[inline]
pub fn is_current() -> bool {
ctx().is_current()
}
#[inline]
pub fn position() -> Vector2<i32> {
ctx().position()
}
#[inline]
pub fn dimensions() -> Vector2<u32> {
ctx().dimensions()
}
#[inline]
pub fn device_pixel_ratio() -> f32 {
ctx().device_pixel_ratio()
}
mod ins {
use super::system::WindowSystem;
pub static mut CTX: *const WindowSystem = std::ptr::null();
#[inline]
pub fn ctx() -> &'static WindowSystem {
unsafe {
debug_assert!(
!CTX.is_null(),
"window system has not been initialized properly."
);
&*CTX
}
}
}