👨‍💻
Guide
Laura's GithubSimon's Github
  • What is this gitbook for?
  • 🛠️Useful tools
    • 🏁Header files
    • 🧱C Structures
    • 🔗Linked Lists (todo)
    • 📄Makefiles
    • 🔄Switch statement
    • 🗃️File descriptors (FD)
  • 🖌️MiniLibX
    • MiniLibX Helper Function
    • MiniLibX Hook Examples
  • 0️⃣ Rank 00
    • Libft
      • 📑LIBC functions
        • ft_isalpha
        • ft_isdigit
        • ft_isalnum
        • ft_isascii
        • ft_isprint
        • ft_strlen
        • ft_memset
        • ft_bzero
        • ft_memcpy
        • ft_memmove
        • ft_strlcpy
        • ft_strlcat
        • ft_toupper
        • ft_tolower
        • ft_strchr
        • ft_strrchr
        • ft_strncmp
        • ft_memchr
        • ft_memcmp
        • ft_strnstr
        • ft_atoi
        • ft_calloc
        • ft_strdup
      • 📑Additional functions
        • ft_substr
        • ft_strjoin
        • ft_strtrim
        • ft_split
        • ft_itoa
        • ft_strmapi
        • ft_striteri
        • ft_putchar_fd
        • ft_putstr_fd
        • ft_putendl_fd
        • ft_putnbr_fd
      • 📑Bonus functions
        • ft_lstnew
        • ft_lstadd_front
        • ft_lstsize
        • ft_lstlast
        • ft_lstadd_back
        • ft_lstdelone
        • ft_lstclear
        • ft_lstiter
        • ft_lstmap
  • 1️⃣ Rank 01
    • Born2beRoot
      • 📠What's a virtual machine ?
      • 📠Install your virtual machine
      • 📠P2P Evaluation - Questions
    • ft_printf
      • ▪️Variadic functions
      • ▪️Building the thing
    • get_next_line
      • ▪️open() & read()
      • ▪️Static variables
      • ▪️Building the thing
      • ▪️Commented solution
  • 2️⃣ Rank 02
    • so_long
      • ▪️Understand so_long
      • ▪️Core concepts
      • ▪️Building the thing
    • pipex
      • ▪️Understand pipex
      • ▪️Functions used
      • ▪️Building the thing
    • minitalk
      • ▪️Understand minitalk
      • ▪️Functions used
      • ▪️Building the thing
    • push_swap
      • ▪️Algorithms
      • ◾Building the thing
    • FdF
      • 🗡️Understand FdF
      • 🗡️Graphics programming
      • 🗡️Building the thing
  • 3️⃣ RANK 03
    • Philosophers
      • ▪️Understand Philosophers
      • ▪️Functions used
      • ▪️Building the thing
    • Minishell
      • ▪️Understand Minishell
      • ▪️Functions
      • ◾Building the thing
  • 4️⃣ RANK 04
    • CPP (00 - 04) (doing)
      • CPP00
      • CPP01
      • CPP02
      • CPP03
      • CPP04 (doing)
    • NetPractice
      • Theory
      • Level 1 & 2
    • MiniRT
      • Understand MiniRT
      • Building the thing
  • 5️⃣ RANK 05
    • CPP (05-09) (to-do)
      • CPP05
      • CPP06 (to-do)
      • CPP07
      • CPP08 (to-do)
      • CPP09 (to-do)
    • Inception (doing)
      • 🕓The basics (Docker, Images, etc...)
      • Project Files
    • webserv (to-do)
  • 6️⃣ rank 06
    • ft_transcendence (to-do)
  • 🛂Exams
    • Exam Rank 02
      • Level 1
        • first_word
        • fizz_buzz
        • *ft_putstr
        • *ft_strcpy
        • *ft_strlen
        • ft_swap
        • repeat_alpha
        • rev_print
        • rot_13
        • rotone
        • search_and_replace
        • ulstr
      • Level 2
        • alpha_mirror
        • camel_to_snake
        • do_op
        • *ft_atoi
        • *ft_strcmp
        • ft_strcspn
        • ft_strspn
        • *ft_strdup
        • ft_strpbrk
        • ft_strrev
        • inter
        • last_word
        • ft_is_power_2
        • max
        • print_bits
        • reverse_bits
        • wdmatch
        • swap_bits
        • union
        • snake_to_camel
      • Level 3
        • add_prime_sum
        • epur_str
        • expand_str
        • ft_atoi_base
        • ft_list_size
        • ft_range
        • ft_rrange
        • hidenp
        • lcm
        • paramsum
        • pgcd
        • print_hex
        • rstr_capitalizer
        • str_capitalizer
        • tab_mult
      • Level 4
        • flood_fil
        • fprime
        • ft_split
        • ft_itoa
        • ft_list_foreach
        • ft_list_remove
        • rev_wstr
        • rotstring
        • sort_int_tab
        • sort_list
    • Exam Rank 03
    • Exam Rank 04
    • Exam Rank 05
      • Module 0
  • 👥Team
Powered by GitBook
On this page
  • Simple key_pressed_handler
  • Simple mouse_handler
  • mlx_loop_hook

Was this helpful?

  1. MiniLibX

MiniLibX Hook Examples

This page will showcase some examples hooks that you can use with MiniLibX.

PreviousMiniLibX Helper FunctionNextLibft

Last updated 2 years ago

Was this helpful?

keycode and mousecode here are the MacOS ones, on Linux they are different (the escape key on Mac is 53, it's 65307 on Linux) and probably even different on Windows.

If you need to find what are hooks for MiniLibX, you can fin more information in the and chapter.

Here under I'll show you some examples hooks showing the most important values and some values that are not described in the documentation.

#include "mlx.h"
#define WINDOW_HEIGHT 720
#define WINDOW_WIDTH 1280
int main(void)
{
/* 
 * this is the MLX initialisation, I only put it in the example so you can
 * better see what I am doing below
 */
    t_env *env;

    env.mlx = mlx_init();
    env.win = mlx_new_window(env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_NAME);
    env.img = mlx_new_image(env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT);
    env.addr = mlx_get_data_addr(env.img, &env.bits_per_pixel, &env.line_length, &env.endian);
     /* end of MLX initialisation */
     /* Here I will be declaring the hooks, see below for their implementation.
     */
     mlx_hook(env.win, 4, 0, mouse_handler, &env);
     // mouse_handler will be called everytime a mouse down event is emitted
     mlx_hook(env.win, 2, 1L << 0, key_handler, &env);
     // key_handler will be called everytime a key is pressed
     mlx_hook(env.win, 17, 1L << 0, close_window, &env);
     // close_window is called when we click on the red cross to close the window
     mlx_loop_hook(env.mlx, render, &env);
     // Since MXL loops over and over again, we can use the mlx_loop_hook
     // to execute a function everytime MLX loops over.
     
     mlx_loop(env.mlx);
}

Simple key_pressed_handler

Since you can filter if you you want to listen to the keypress or keyrelease event, you can have two different key handlers, you might want to start doing something when we press the space key and stop it only when you release it.

int key_handler(int keycode, t_env *env)
{
    if (keycode == 53)
        ft_printf("ESCAPE");
    else if (keycode == 0 || keycode == 123)
        ft_printf("LEFT (A / ARROW_LEFT)");
    else if (keycode == 2 || keycode == 124)
        ft_printf("RIGHT (D / ARROW_RIGHT)");
    else if (keycode == 1 || keycode == 125)
        ft_printf("DOWN (S / ARROW_DOWN)");
    else if (keycode == 13 || keycode == 126)
        ft_printf("LEFT (A / ARROW_LEFT)");
    else
        ft_printf("%d\n", keycode);
    return (0);
}

I think here you can see how you could find what keycode corresponds to any key on your keyboard by looking at this function.

Simple mouse_handler

int mouse_handler(int mousecode, int x, int y, t_env *env)
{
    /* x and y parameters are the pixel coordinates of the mouse
     * in the window when the event was emitted
     * you can use them to check that the user clicked in a specific region
     * of the window
     */
     if (mousecode == 1)
         ft_printf("Left Click");
     else if (mousecode == 2)
         ft_printf("Right Click");
     else if (mousecode == 3)
         ft_printf("Middle Click");
     else if (mousecode == 4)
         ft_printf("Scroll UP");
     else if (mousecode == 5)
         ft_printf("Scroll DOWN");
     else if (mousecode == 6)
         ft_printf("Scroll right");
     else if (mousecode == 7)
         ft_printf("Scroll left");
     else
         ft_printf("%d\n", mousecode);
}

mlx_loop_hook

This hook will call a function every time through the MLX loop, we can use this to update what we show on screen. That's why I called it render or draw most of the time.

int render(t_env *env)
{
    draw_background(env);
    draw_map(env);
    draw_player(env);
    draw_moves(env);
}

These are just examples and simple handlers so that you can better understand how this works and adapt it based on your needs.

For example, in so-long I use this hook to draw the map based on what changed. I draw the background completely again, to overwrite what was there, and then I redraw the walls and the player, but the player position can change based on keypressed events so the player moves along. The same way for collectibles, I don't want to draw them again if I already collected them (you can see the example I described ).

🖌️
Events
Hooks
here
key code