Add hook to make window closeable by close button
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 11 Nov 2023 17:25:16 +0000 (18:25 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 11 Nov 2023 17:25:16 +0000 (18:25 +0100)
main.c

diff --git a/main.c b/main.c
index 2302ee1ffebb4b8605c7afa51ef681cc77bb7da6..cbf446e6b64228b18f7f4fced871f0734d002dde 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/27 14:29:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/10/27 14:31:02 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/11/11 17:31:25 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "color.h"
 #include "vect2.h"
 
+int    close_win(t_session *s)
+{
+       mlx_destroy_window(s->mlx, s->win);
+       return (0);
+}
+
 void   ft_putpx_img(t_img *img, int x, int y, t_color c)
 {
        char    *px_addr;
@@ -133,7 +139,7 @@ void        change_zoom(t_view *view, t_vect2 invariant, double d_zoom)
 int    handle_key_press(int keycode, t_session *s)
 {
        if (keycode == XK_Escape)
-               mlx_destroy_window(s->mlx, s->win);
+               close_win(s);
        return (0);
 }
 
@@ -198,6 +204,7 @@ int main(int argc, char **argv)
        s.img.addr = mlx_get_data_addr(s.img.img, &s.img.bpp, &s.img.bpl, &s.img.endian);
        mlx_hook(s.win, KeyPress, KeyPressMask, handle_key_press, &s);
        mlx_hook(s.win, ButtonPress, ButtonPressMask, handle_mouse_press, &s);
+       mlx_hook(s.win, DestroyNotify, NoEventMask, close_win, &s);
        mlx_loop_hook(s.mlx, no_event_handle, &s);
        draw_fractal(&s);
        mlx_loop(s.mlx);