loop_hook¶
MiniLibX: mlx_loop_hook
Sets a function to call at the start of every frame of loop.
Do your per-frame updating and drawing here.
Parameters¶
| Name | Type | Description |
|---|---|---|
mlx |
Mlx |
The connection from init |
f |
Callable[[T], Any] |
Called as f(param) once per frame. Its return value is ignored. |
param |
T |
Passed to f each time. Defaults to None. |
Returns¶
None.
Notes¶
- There's one loop hook per
mlx. Callingloop_hookagain replaces it. - The hook runs before the windows are shown, so anything it draws appears on screen in the same frame.
- Windows aren't cleared between frames. Call
clear_window, or draw a full-window background, if things move. - An exception in
fstops the loop and propagates out ofloop. fisn't checked when you register it. If it isn't callable, or takes the wrong number of arguments, you get aTypeErrorfromloopon the first frame.
Example¶
import malibx
mlx = malibx.init()
win = malibx.new_window(mlx, 320, 100, "counter")
def frame(st):
malibx.clear_window(mlx, win)
malibx.string_put(mlx, win, 10, 20, 0xFFFFFF, f"frame {st['n']}")
st["n"] += 1
malibx.loop_hook(mlx, frame, {"n": 0})
malibx.loop(mlx)