Skip to content

loop_hook

malibx.loop_hook(mlx: Mlx, f: Callable[[T], Any], param: T = None) -> None

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. Calling loop_hook again 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 f stops the loop and propagates out of loop.
  • f isn't checked when you register it. If it isn't callable, or takes the wrong number of arguments, you get a TypeError from loop on 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)

See also

loop · hook · Passing state with param