Skip to main content

Module main_loop

Module main_loop 

Source
Expand description

Games that keep their own main loop (loop { update(); draw(); wait_frame(); }) instead of implementing Game. Same mechanism as the C SDK’s gasm_loop.h: main_loop! exports the entry points and runs your function on the first frame; main_loop::wait_frame suspends it until the next frame: with Binaryen’s Asyncify inside the module (gasm_frame, any runner), or by the runner itself when it switches stacks (gasm_run). Build, then post-process the .wasm:

wasm-opt game.wasm --asyncify --pass-arg=asyncify-removelist@gasm_frame,gasm_loop_frame -O2 -o game.wasm
wasm-opt game.wasm -O2 -o game-run.wasm    # runners with stack switching only: smaller, faster
ⓘ
fn run() -> i32 {
    loop {
        if gasm::pad(0).held(gasm::Buttons::START) { return 0; }
        draw();
        gasm::main_loop::wait_frame();
    }
}
gasm::main_loop!(run);

main_loop!(run, on_exit) also exports gasm_exit (the player is quitting: flush saves). wasm32 only: native builds of such a game run run straight through.

Functions§

frames
Frames waited so far.
wait_frame
Return to the runner; continues in the next frame (input and time are per frame).