diff --git a/examples/winit.rs b/examples/winit.rs index 7c903e2..192523a 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -23,18 +23,29 @@ fn main() { .unwrap(); } - let context = softbuffer::Context::new(window.clone()).unwrap(); - let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + let mut state = None; event_loop .run(move |event, elwt| { elwt.set_control_flow(ControlFlow::Wait); match event { + Event::Resumed => { + let context = softbuffer::Context::new(window.clone()).unwrap(); + let surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + state = Some((context, surface)); + } + Event::Suspended => { + state = None; + } Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested, } if window_id == window.id() => { + let Some((_, surface)) = state.as_mut() else { + eprintln!("RedrawRequested fired before Resumed or after Suspended"); + return; + }; if let (Some(width), Some(height)) = { let size = window.inner_size(); (NonZeroU32::new(size.width), NonZeroU32::new(size.height))