#![forbid(unsafe_code)] // use vulkanalia::vk::{ DeviceV1_4, EntryV1_4, InstanceV1_4 }; use winit::dpi::LogicalSize; use winit::application::ApplicationHandler; use winit::event::WindowEvent; use winit::event_loop::{ ActiveEventLoop, EventLoop }; use winit::window::{ Window, WindowAttributes, WindowId }; #[derive(Debug)] struct Error { message: String, } type Result = std::result::Result; impl std::fmt::Display for Error { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { fmt.write_str(&self.message) } } impl From for Error { fn from(e: winit::error::EventLoopError) -> Self { match e { winit::error::EventLoopError::NotSupported(e) => Self::from(e), winit::error::EventLoopError::Os(e) => Self::from(e), winit::error::EventLoopError::RecreationAttempt => Error { message: "There may only ever be a single winit event loop.".to_string() }, winit::error::EventLoopError::ExitFailure(code) => Error { message: format!("Clean unhappy exit with code {} via winit event loop", code) } } } } impl From for Error { fn from(e: winit::error::NotSupportedError) -> Self { Error { message: format!("The winit backend does not support an operation: {}", e.to_string()) } } } impl From for Error { fn from(e: winit::error::OsError) -> Self { Error { message: format!("The OS told winit about an error: {}", e.to_string()) } } } struct Surreality { window: Window } impl Surreality { fn new(window: Window) -> Result { Ok(Surreality { window }) } fn render(&mut self, window_id: WindowId) -> Result<()> { if window_id == self.window.id() { println!("render the window"); } else { println!("render something unknown"); } Ok(()) } } impl ApplicationHandler for Surreality { fn resumed(&mut self, event_loop: &ActiveEventLoop) { println!("resumed"); } fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) { println!("window event {:?}", event); match event { WindowEvent::RedrawRequested => { if !event_loop.exiting() { self.render(window_id).unwrap(); // TODO } } WindowEvent::CloseRequested => { event_loop.exit(); } _ => { } } } } fn main() -> std::process::ExitCode { let body: fn() -> Result<()> = || { let event_loop = EventLoop::new()?; let window_attributes = WindowAttributes::default() .with_title("Love, Curiosity, Justice") .with_inner_size(LogicalSize::new(1024, 768)); let window = event_loop.create_window(window_attributes)?; let mut surreality = Surreality::new(window)?; event_loop.run_app(&mut surreality)?; Ok(()) }; println!("Hello!"); match body() { Ok(()) => std::process::ExitCode::SUCCESS, Err(e) => { eprintln!("Error: {}", e); std::process::ExitCode::from(1) } } }