use std::sync::Arc; use glam::uvec2; use photon_light::{Core, init_gpu_inner}; use winit::{ application::ApplicationHandler, event::WindowEvent, event_loop::{ActiveEventLoop, ControlFlow, EventLoop}, window::Window, }; const TITLE: &str = "WGPU example"; struct MainWindow { window: Arc, core: Core, } impl MainWindow { fn new(event_loop: &ActiveEventLoop) -> Self { let window = event_loop .create_window(Window::default_attributes().with_title(TITLE)) .unwrap(); let window = Arc::new(window); let gpu = pollster::block_on(init_gpu_inner(|instance| { instance.create_surface(Arc::clone(&window)) })) .unwrap(); let mut core = Core::new(gpu); core.configure(uvec2(1, 1)); Self { window, core } } fn event(&mut self, event_loop: &ActiveEventLoop, event: WindowEvent) { match event { WindowEvent::CloseRequested => event_loop.exit(), WindowEvent::Resized(physical_size) => self .core .configure(uvec2(physical_size.width, physical_size.height)), WindowEvent::RedrawRequested => self.core.redraw(), _ => {} } } } struct Application { main_window: Option, } impl Application { fn new() -> Self { Self { main_window: None } } } impl ApplicationHandler for Application { fn resumed(&mut self, event_loop: &ActiveEventLoop) { self.main_window = Some(MainWindow::new(event_loop)); } fn window_event( &mut self, event_loop: &ActiveEventLoop, window_id: winit::window::WindowId, event: WindowEvent, ) { let window = self .main_window .as_mut() .expect("window must exist to recieve events"); assert_eq!(window.window.id(), window_id); window.event(event_loop, event); } } fn main() { let event_loop = EventLoop::new().unwrap(); event_loop.set_control_flow(ControlFlow::Wait); let mut app = Application::new(); event_loop.run_app(&mut app).unwrap(); }