1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! Cross-platform game loop helper.
//!
//! On native, [`run`] drives a standard
//! `while !rl.window_should_close()` loop. On
//! `wasm32-unknown-emscripten`, it hands the per-frame closure to
//! `emscripten_set_main_loop_arg`, so you don't need `-sASYNCIFY=1` just
//! to keep the loop responsive. Same source on both:
//!
//! ```no_run
//! use sola_raylib::prelude::*;
//!
//! fn main() {
//! let (rl, thread) = sola_raylib::init()
//! .size(640, 480)
//! .title("Hello")
//! .build();
//!
//! sola_raylib::core::game_loop::run(rl, thread, 60, |rl, thread| {
//! let mut d = rl.begin_drawing(thread);
//! d.clear_background(Color::WHITE);
//! d.draw_text("Hello", 12, 12, 20, Color::BLACK);
//! });
//! }
//! ```
//!
//! ## Tradeoff vs Asyncify
//!
//! The classic on-web pattern is to link with `-sASYNCIFY=1`, which
//! instruments every C function so blocking calls can yield to the
//! browser between frames. It works but adds 10 to 30 percent wasm size
//! and a per-call overhead. `run()` skips it by registering the closure
//! with emscripten's event loop directly.
//!
//! The cost is a `'static` closure: no borrowed locals, so `move` your
//! game state in. On emscripten `run` returns immediately while
//! emscripten keeps calling the closure each frame.
use crate;
use c_void;
extern "C"
/// Run `callback` per frame until the window closes (native) or while
/// the page is open (emscripten).
///
/// `fps` sets the target frame rate. On native it forwards to
/// `RaylibHandle::set_target_fps`; on emscripten it's passed to
/// `emscripten_set_main_loop_arg`. Pass `0` for no cap on native or to
/// use `requestAnimationFrame` cadence on emscripten.
///
/// On emscripten, `run` returns as soon as the callback is registered
/// (emscripten owns the event loop), which is why the closure has to be
/// `'static`. On native it returns when the user closes the window.