use std::ptr::null;
use std::ptr::null_mut;
use std::sync::Arc;
use std::sync::Mutex;
use xege_ffi::*;
use crate::RenderMode;
use crate::graphics::DrawableDevice;
use crate::window::Window;
#[bitmask_enum::bitmask(i32)]
pub enum Init {
Default = ege_initmode_flag_INIT_DEFAULT,
TopMost = ege_initmode_flag_INIT_TOPMOST,
NoBorder = ege_initmode_flag_INIT_NOBORDER,
Child = ege_initmode_flag_INIT_CHILD,
RenderManual = ege_initmode_flag_INIT_RENDERMANUAL,
NoForceExit = ege_initmode_flag_INIT_NOFORCEEXIT,
Hide = ege_initmode_flag_INIT_HIDE,
WithLogo = ege_initmode_flag_INIT_WITHLOGO,
Animation = ege_initmode_flag_INIT_ANIMATION,
NoSysDPI = 0x200,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct XEGE {
pub window: Window,
}
#[derive(Debug, thiserror::Error)]
pub enum XEGEError {
#[error("XEGE is already initialized.")]
Initialized,
}
lazy_static::lazy_static! {
static ref INIT_FLAG: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
}
pub fn initgraph(width: i32, height: i32, mode: Init) -> Result<XEGE, XEGEError> {
{
let mut flag = INIT_FLAG.lock().unwrap();
if *flag {
return Err(XEGEError::Initialized);
}
*flag = true;
}
unsafe {
if mode.contains(Init::NoSysDPI) {
SetProcessDPIAware();
}
ege_initgraph(width, height, mode.bits | ege_initmode_flag_INIT_UNICODE);
}
Ok(XEGE {
window: Window(null_mut()),
})
}
impl Drop for XEGE {
fn drop(&mut self) {
{
let mut flag = INIT_FLAG.lock().unwrap();
*flag = false;
}
unsafe { ege_closegraph() };
}
}
impl XEGE {
pub fn closegraph(self) {}
pub fn is_run(&self) -> bool {
unsafe { ege_is_run() }
}
pub fn set_caption(&mut self, caption: &str) {
let wchar_arr: Vec<u16> = caption.encode_utf16().chain(Some(0)).collect();
let wchar_ptr = wchar_arr.as_ptr();
unsafe { ege_setcaption1(wchar_ptr) };
}
pub fn hwnd(&self) -> HWND {
unsafe { ege_getHWnd() }
}
pub fn setrendermode(mode: RenderMode) {
unsafe { ege_setrendermode(mode as i32) };
}
}
impl DrawableDevice for XEGE {
fn mut_ptr(&mut self) -> *mut ege_IMAGE {
null_mut()
}
fn const_ptr(&self) -> *const ege_IMAGE {
null()
}
}