Skip to content
W2.Wizard edited this page Feb 15, 2022 · 23 revisions

Hooks in MLX42

Hooks allow you to add your own functions to the main loop execution of the program, aka these functions get executed every frame. They also serve to intercept certain key presses such as scrolling or pressing enter.

Scroll hook

/**
 * Callback function used to handle scrolling.
 * 
 * @param[in] x The mouse x delta.
 * @param[in] y The mouse y delta.
 * @param[in] param Additional parameter to pass onto the function.
 */
typedef void (*	t_mlx_scrollfunc)(double xdelta, double ydelta, void *param);

/**
 * This function sets the scroll callback, which is called when a scrolling 
 * device is used, such as a mouse wheel.
 * 
 * @param[in] mlx The MLX instance handle.
 * @param[in] func The scroll wheel callback function.
 * @param[in] param An additional optional parameter.
 */
void mlx_scroll_hook(t_mlx *mlx, t_mlx_scrollfunc func, void *param);

Key hook

If you get any compiler warnings from the keyhook function you can safely ignore those, this is caused by norme...

/**
 * Callback function used to handle key presses.
 * 
 * @param[in] key The key/keycode that was pressed.
 * @param[in] action The action is either MLX_PRESS, MLX_REPEAT or MLX_RELEASE. 
 * @param[in] param Additional parameter to pass onto the function.
 */
typedef void (*	t_mlx_keyfunc)(t_keys key, t_action action, void *param);

/**
 * This function sets the key callback, which is called when a key is pressed
 * on the keyboard. Useful for single key press detection.
 * 
 * @param[in] mlx The MLX instance handle.
 * @param[in] func The key press callback function.
 * @param[in] param An additional optional parameter.
 */
void mlx_key_hook(t_mlx *mlx, t_mlx_keyfunc func, void *param);

Generic Hook

/**
 * Adds a function hook to the main loop. Aka, executes a function per frame.
 * 
 * @param[in] mlx The MLX instance handle.
 * @param[in] f The function.
 * @param[in] param The parameter to pass onto the function.
 * @returns Wether the hook was added successfuly. 
 */
bool mlx_loop_hook(t_mlx *mlx, void (*f)(void *), void *param);
Clone this wiki locally