Function set_main_loop_with_arg

Source
pub fn set_main_loop_with_arg<F, T>(
    func: F,
    arg: T,
    fps: c_int,
    simulate_infinite_loop: bool,
)
where F: 'static + FnMut(&mut T), T: 'static,
Expand description

Sets the given function as the main loop of the calling thread, using the emscripten-defined emscripten_set_main_loop. The given function accepts a mutable reference (argument arg) to the variable that will contain the loop state and whatever else is needed for it to run.

If you don’t need that state argument, check out set_main_loop.

The main loop can be cancelled using the cancel_main_loop function.

§Arguments

  • func - The function to be set as main event loop for the calling thread.
  • arg - The variable that represents the state that the main event loop ought to interact with. It will be consumed so that it can be kept alive during the loop. It must be 'static, that means arg’s type should only contain owned data and 'static references, if any.
  • fps - The number of calls of the function per second. If set to a value <= 0, the browser’s requestAnimationFrame() function will be used (recommended when using the main function for rendering) instead of a fixed rate.
  • simulate_infinite_loop - If true, no code after the function call will be executed, otherwise the code after the function call will be executed.

§Examples

struct GameData {
    level: u32,
    score: u32
}
let mut game_data = GameData {
    level: 1,
    score: 0
}

set_main_loop_with_arg(|data| {
    if data.score < data.level {
        data.score += 1;
    } else {
        data.score = 0;
        data.level += 1;
    }

    // Here you call your display to screen functions.
    // For demonstration purposes I chose `println!`.
    println!("Score {}, level {}", data.score, data.level);
}, game_data, 0, true);