pub fn run<F>(self, mut event_handler: F) -> !
    where
        F: 'static
            + FnMut(
                Event<'_, ()>,
                &Self,
                &winit::event_loop::EventLoopWindowTarget<()>,
                &mut winit::event_loop::ControlFlow,
            ),
    {
        self.event_loop
            .take()
            .unwrap()
            .run(move |event, event_loop, control_flow| {
                match event {
                    // Exit when close is requested
                    // This might not always be desired but it's fine for now.
                    winit::event::Event::WindowEvent {
                        event: winit::event::WindowEvent::CloseRequested,
                        ..
                    } => *control_flow = ControlFlow::Exit,

                    // Resize the viewport when the window is resized
                    winit::event::Event::WindowEvent {
                        event: winit::event::WindowEvent::Resized(new_size),
                        ..
                    } => {
                        let (width, height) = new_size.into();
                        self.viewport.resize(width, height);
                        let mut on_resize = self.on_resize.borrow_mut();
                        if let Some(handler) = on_resize.as_mut() {
                            handler(&self, width, height);
                        }
                    }

                    winit::event::Event::RedrawEventsCleared => {
                        // Clamp to some max framerate if the target framerate is set
                        if let Some(program_time) = &self.time {
                            if program_time.should_draw() {
                                self.viewport.request_redraw()
                            }
                        }
                    }

                    // Handle redrawing
                    // We manually call the event handler. This returns out of the closure iteration
                    winit::event::Event::RedrawRequested(w) => {
                        let resized_to = if let Some(new_size) = *self.viewport.resize_to.borrow() {
                            event_handler(Event::Resize(new_size), &self, event_loop, control_flow);
                            Some(new_size)
                        } else {
                            None
                        };

                        // We first call the event handler with the original event,
                        // in case the user wants to perform some operations before creating the Frame
                        event_handler(
                            Event::Winit(winit::event::Event::RedrawRequested(w)),
                            &self,
                            event_loop,
                            control_flow,
                        );

                        // Then we create a new Frame and pass it to the event handler
                        let mut frame = match self.viewport.begin_frame() {
                            Ok(mut frame) => {
                                frame.delta_time = self
                                    .time
                                    .as_ref()
                                    .map(|time| time.delta_time.get().as_secs_f32());
                                frame
                            }
                            Err(e) => {
                                // Rudimentary error handling. Just logs and continues
                                tracing::warn!("Requested frame but {}. Redraw is suppressed", e);
                                return;
                            }
                        };
                        frame.resized_to = resized_to;

                        // Then we call the event handler with the frame
                        event_handler(Event::RedrawFrame(frame), &self, event_loop, control_flow);

                        // Return early so we don't call the event handler twice (once from 2 above)
                        return;
                    }
                    _ => {}
                }
                let event = Event::Winit(event);
                event_handler(event, &self, event_loop, control_flow);
            })
    }
/// Run the program
/// This is a wrapper over Run that does some stuff automatically like resizing
/// and closing the window
/// # Panics
/// Panics if run() was already used, as the event_loop is consumed on run
/// This uses custom Event enum to allow for pass frame to redraw directly
Run the program
This is a wrapper over Run that does some stuff automatically like resizing
and closing the window
Run the program This is a wrapper over Run that does some stuff automatically like resizing and closing the window
pub fn run<F>(self, mut event_handler: F) -> !
    where
        F: 'static
            + FnMut(
                Event<'_, ()>,
                &Self,
                &winit::event_loop::EventLoopWindowTarget<()>,
                &mut winit::event_loop::ControlFlow,
            ),
    {
        self.event_loop
            .take()
            .unwrap()
            .run(move |event, event_loop, control_flow| {
                match event {
                    // Exit when close is requested
                    // This might not always be desired but it's fine for now.
                    winit::event::Event::WindowEvent {
                        event: winit::event::WindowEvent::CloseRequested,
                        ..
                    } => *control_flow = ControlFlow::Exit,
                    // Resize the viewport when the window is resized
                    winit::event::Event::WindowEvent {
                        event: winit::event::WindowEvent::Resized(new_size),
                        ..
                    } => {
                        let (width, height) = new_size.into();
                        self.viewport.resize(width, height);
                        let mut on_resize = self.on_resize.borrow_mut();
                        if let Some(handler) = on_resize.as_mut() {
                            handler(&self, width, height);
                        }
                    }
                    winit::event::Event::RedrawEventsCleared => {
                        // Clamp to some max framerate if the target framerate is set
                        if let Some(program_time) = &self.time {
                            if program_time.should_draw() {
                                self.viewport.request_redraw()
                            }
                        }
                    }
                    // Handle redrawing
                    // We manually call the event handler. This returns out of the closure iteration
                    winit::event::Event::RedrawRequested(w) => {
                        let resized_to = if let Some(new_size) = *self.viewport.resize_to.borrow() {
                            event_handler(Event::Resize(new_size), &self, event_loop, control_flow);
                            Some(new_size)
                        } else {
                            None
                        };
                        // We first call the event handler with the original event,
                        // in case the user wants to perform some operations before creating the Frame
                        event_handler(
                            Event::Winit(winit::event::Event::RedrawRequested(w)),
                            &self,
                            event_loop,
                            control_flow,
                        );
                        // Then we create a new Frame and pass it to the event handler
                        let mut frame = match self.viewport.begin_frame() {
                            Ok(mut frame) => {
                                frame.delta_time = self
                                    .time
                                    .as_ref()
                                    .map(|time| time.delta_time.get().as_secs_f32());
                                frame
                            }
                            Err(e) => {
                                // Rudimentary error handling. Just logs and continues
                                tracing::warn!("Requested frame but {}. Redraw is suppressed", e);
                                return;
                            }
                        };
                        frame.resized_to = resized_to;
                        // Then we call the event handler with the frame
                        event_handler(Event::RedrawFrame(frame), &self, event_loop, control_flow);
                        // Return early so we don't call the event handler twice (once from 2 above)
                        return;
                    }
                    _ => {}
                }
                let event = Event::Winit(event);
                event_handler(event, &self, event_loop, control_flow);
            })
    }
pub fn run < F > ( self , mut event_handler : F ) -> ! where F : ' static + FnMut ( Event < ' _ , ( ) > , & Self , & winit :: event_loop :: EventLoopWindowTarget < ( ) > , & mut winit :: event_loop :: ControlFlow , ) , { self . event_loop . take ( ) . unwrap ( ) . run ( move | event , event_loop , control_flow | { match event { // Exit when close is requested // This might not always be desired but it's fine for now. winit :: event :: Event :: WindowEvent { event : winit :: event :: WindowEvent :: CloseRequested , .. } => * control_flow = ControlFlow :: Exit , // Resize the viewport when the window is resized winit :: event :: Event :: WindowEvent { event : winit :: event :: WindowEvent :: Resized ( new_size ) , .. } => { let ( width , height ) = new_size . into ( ) ; self . viewport . resize ( width , height ) ; let mut on_resize = self . on_resize . borrow_mut ( ) ; if let Some ( handler ) = on_resize . as_mut ( ) { handler ( & self , width , height ) ; } } winit :: event :: Event :: RedrawEventsCleared => { // Clamp to some max framerate if the target framerate is set if let Some ( program_time ) = & self . time { if program_time . should_draw ( ) { self . viewport . request_redraw ( ) } } } // Handle redrawing // We manually call the event handler. This returns out of the closure iteration winit :: event :: Event :: RedrawRequested ( w ) => { let resized_to = if let Some ( new_size ) = * self . viewport . resize_to . borrow ( ) { event_handler ( Event :: Resize ( new_size ) , & self , event_loop , control_flow ) ; Some ( new_size ) } else { None } ; // We first call the event handler with the original event, // in case the user wants to perform some operations before creating the Frame event_handler ( Event :: Winit ( winit :: event :: Event :: RedrawRequested ( w ) ) , & self , event_loop , control_flow , ) ; // Then we create a new Frame and pass it to the event handler let mut frame = match self . viewport . begin_frame ( ) { Ok ( mut frame ) => { frame . delta_time = self . time . as_ref ( ) . map ( | time | time . delta_time . get ( ) . as_secs_f32 ( ) ) ; frame } Err ( e ) => { // Rudimentary error handling. Just logs and continues tracing :: warn ! ( " " e ) ; return ; } } ; frame . resized_to = resized_to ; // Then we call the event handler with the frame event_handler ( Event :: RedrawFrame ( frame ) , & self , event_loop , control_flow ) ; // Return early so we don't call the event handler twice (once from 2 above) return ; } _ => { } } let event = Event :: Winit ( event ) ; event_handler ( event , & self , event_loop , control_flow ) ; } ) }
Run the program
This is a wrapper over Run that does some stuff automatically like resizing
and closing the window
Run the program This is a wrapper over Run that does some stuff automatically like resizing and closing the window
self
event_handler
F
self
event_handler
F
panics
Panics if run() was already used, as the event_loop is consumed on run
This uses custom Event enum to allow for pass frame to redraw directly
Panics if run () was already used as the event_loop is consumed on run This uses custom Event enum to allow for pass frame to redraw directly