// Application Loop.
//
// Beyond this point lies the master function for running the main application loop!
//
// This is undoubtedly the hairiest part of nannou's code base. This is largely due to the fact
// that it is the part of nannou where we marry application and user input events, loop timing,
// updating the model, platform-specific quirks and warts, the various possible `LoopMode`s and
// Vulkan interop.
//
// If you would like to contribute but are unsure about any of the following, feel free to open an
// issue and ask!
Application Loop.
Beyond this point lies the master function for running the main application loop!

This is undoubtedly the hairiest part of nannou's code base. This is largely due to the fact
that it is the part of nannou where we marry application and user input events, loop timing,
updating the model, platform-specific quirks and warts, the various possible `LoopMode`s and
Vulkan interop.

If you would like to contribute but are unsure about any of the following, feel free to open an
issue and ask!
Application Loop . Beyond this point lies the master function for running the main application loop! This is undoubtedly the hairiest part of nannou ' s code base . This is largely due to the fact that it is the part of nannou where we marry application and user input events loop timing updating the model platform - specific quirks and warts the various possible ` LoopMode ` s and Vulkan interop . If you would like to contribute but are unsure about any of the following feel free to open an issue and ask!
fn run_loop<M, E>(
    mut app: App,
    mut model: M,
    event_fn: Option<EventFn<M, E>>,
    update_fn: Option<UpdateFn<M>>,
    default_view: Option<View<M>>,
    exit_fn: Option<ExitFn<M>>,
) where
    M: 'static,
    E: LoopEvent,
{
    let loop_start = Instant::now();
    // Initialise the loop context.
    let mut loop_ctxt = LoopContext {
        event_fn,
        update_fn,
        default_view,
        loop_start,
        winit_events: vec![],
        last_update: loop_start,
        last_loop_end: loop_start,
        updates_remaining: LoopMode::DEFAULT_UPDATES_FOLLOWING_EVENT,
    };
    let mut loop_mode = app.loop_mode();
    // Begin running the application loop based on the current `LoopMode`.
    'mode: loop {
        let Break {
            model: new_model,
            reason,
        } = match loop_mode {
            LoopMode::Rate { update_interval } => {
                run_loop_mode_rate(&mut app, model, &mut loop_ctxt, update_interval)
            }
            LoopMode::Wait {
                updates_following_event,
                update_interval,
            } => {
                loop_ctxt.updates_remaining = updates_following_event;
                run_loop_mode_wait(
                    &mut app,
                    model,
                    &mut loop_ctxt,
                    updates_following_event,
                    update_interval,
                )
            }
            LoopMode::NTimes {
                number_of_updates,
                update_interval,
            } => {
                loop_ctxt.updates_remaining = number_of_updates;
                run_loop_mode_ntimes(&mut app, model, &mut loop_ctxt, update_interval)
            }
            LoopMode::RefreshSync {
                minimum_update_interval,
                windows,
            } => run_loop_mode_refresh_sync(
                &mut app,
                model,
                &mut loop_ctxt,
                minimum_update_interval,
                windows,
            ),
        };
        model = new_model;
        match reason {
            // If the break reason was due to the `LoopMode` changing, switch to the new loop mode
            // and continue.
            BreakReason::NewLoopMode(new_loop_mode) => {
                loop_mode = new_loop_mode;
                change_loop_mode(&app, &loop_mode);
            }
            // If the loop broke due to the application exiting, we're done!
            BreakReason::Exit => {
                if let Some(exit_fn) = exit_fn {
                    exit_fn(&app, model);
                }
                return;
            }
        }
    }
}
fn run_loop < M , E > ( mut app : App , mut model : M , event_fn : Option < EventFn < M , E > > , update_fn : Option < UpdateFn < M > > , default_view : Option < View < M > > , exit_fn : Option < ExitFn < M > > , ) where M : ' static , E : LoopEvent , { let loop_start = Instant :: now ( ) ; // Initialise the loop context. let mut loop_ctxt = LoopContext { event_fn , update_fn , default_view , loop_start , winit_events : vec ! [ ] , last_update : loop_start , last_loop_end : loop_start , updates_remaining : LoopMode :: DEFAULT_UPDATES_FOLLOWING_EVENT , } ; let mut loop_mode = app . loop_mode ( ) ; // Begin running the application loop based on the current `LoopMode`. ' mode : loop { let Break { model : new_model , reason , } = match loop_mode { LoopMode :: Rate { update_interval } => { run_loop_mode_rate ( & mut app , model , & mut loop_ctxt , update_interval ) } LoopMode :: Wait { updates_following_event , update_interval , } => { loop_ctxt . updates_remaining = updates_following_event ; run_loop_mode_wait ( & mut app , model , & mut loop_ctxt , updates_following_event , update_interval , ) } LoopMode :: NTimes { number_of_updates , update_interval , } => { loop_ctxt . updates_remaining = number_of_updates ; run_loop_mode_ntimes ( & mut app , model , & mut loop_ctxt , update_interval ) } LoopMode :: RefreshSync { minimum_update_interval , windows , } => run_loop_mode_refresh_sync ( & mut app , model , & mut loop_ctxt , minimum_update_interval , windows , ) , } ; model = new_model ; match reason { // If the break reason was due to the `LoopMode` changing, switch to the new loop mode // and continue. BreakReason :: NewLoopMode ( new_loop_mode ) => { loop_mode = new_loop_mode ; change_loop_mode ( & app , & loop_mode ) ; } // If the loop broke due to the application exiting, we're done! BreakReason :: Exit => { if let Some ( exit_fn ) = exit_fn { exit_fn ( & app , model ) ; } return ; } } } }
Application Loop.
Application Loop .
app
App
model
M
event_fn
Option<EventFn<M, E>>
update_fn
Option<UpdateFn<M>>
default_view
Option<View<M>>
exit_fn
Option<ExitFn<M>>