Add minilibx-linux to be able to create GUI
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 2 Apr 2024 21:03:05 +0000 (23:03 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 2 Apr 2024 21:03:05 +0000 (23:03 +0200)
Implement some basic behaviour of the window, such as closing.
So far the window just shows the input argument image.
I use minilibx-linux with a commit from myself, that solves
a memory issue.

.gitmodules
solution/Makefile
solution/main.c
solution/minilibx-linux [new submodule]

index 527c7c494f4fe31350e956c7fea6b9e2839b7ee3..c42374ba96327f090d8e9ef20206f82bae71ae22 100644 (file)
@@ -1,3 +1,6 @@
 [submodule "solution/Libft"]
        path = solution/Libft
        url = git://ljiriste.work/Libft
+[submodule "solution/minilibx-linux"]
+       path = solution/minilibx-linux
+       url = https://github.com/Kycilak/minilibx-linux.git
index 40361147eb1c83c661a15676a2ff79bf86309ed7..eeffa85f7d7756f01b4d74c85e72cc42138c52d8 100644 (file)
@@ -3,15 +3,14 @@ CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic
 
 RM := rm -f
 
-SUBPROJECTS := Libft
+SUBPROJECTS := Libft minilibx-linux
+SUBPROJINC := Libft/inc/ minilibx-linux/
 
 INCDIR := .
-INCDIR += $(addsuffix /inc, $(SUBPROJECTS));
+INCDIR += $(SUBPROJINC)
 INCLUDE := $(addprefix -I, $(INCDIR))
 
-LIBS := jpeg
-
-LIBSFLAGS := $(addprefix -l, $(LIBS))
+LIBSFLAGS := -L/usr/lib -lXext -lX11 -lm -lbsd -LLibft -lft -ljpeg
 
 SRCDIR := .
 
@@ -28,7 +27,7 @@ all : $(NAME)
 debug : CFLAGS += -g
 debug : $(NAME)
 
-$(NAME) : $(OBJECTS) Libft/libft.a
+$(NAME) : $(OBJECTS) Libft/libft.a minilibx-linux/libmlx.a
        $(CC) $(CFLAGS) -o $@ $^ $(LIBSFLAGS)
 
 Libft/libft.a : | Libft/Makefile
@@ -38,6 +37,9 @@ else
        $(MAKE) -C Libft
 endif
 
+minilibx-linux/libmlx.a : | minilibx-linux/Makefile
+       $(MAKE) -C minilibx-linux
+
 %.o : %.c | Libft/Makefile
        $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)
 
index 2743a6f1af950b458d2407add4bef7318d925dd9..350abf9fa624db908d5a76c633d9049a2558366b 100644 (file)
@@ -6,13 +6,16 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/02 08:44:53 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/04/02 11:29:04 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/04/02 22:44:46 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "libft.h"
+#include <mlx.h>
 #include <unistd.h>
-#include <setjmp.h>
+#include <stdlib.h>
+#include <X11/X.h>
+#include <X11/keysym.h>
 
 #include <stdio.h>
 #include <jpeglib.h>
@@ -24,6 +27,21 @@ typedef struct s_rgb
        unsigned char   b;
 }                                      t_rgb;
 
+typedef struct s_mlx_session
+{
+       void    *mlx;
+       void    *mlx_win;
+}                              t_mlx_session;
+
+typedef struct s_mlx_data
+{
+       void    *img;
+       char    *addr;
+       int             bits_per_pixel;
+       int             line_length;
+       int             endian;
+}                              t_mlx_data;
+
 //     Assumes 3 chanels
 int    read_jpeg(char *filename, t_mat *image)
 {
@@ -55,14 +73,99 @@ int read_jpeg(char *filename, t_mat *image)
        return (0);
 }
 
+unsigned int   rgb_to_uint(t_rgb rgb)
+{
+       unsigned int    res;
+
+       res = 0;
+       res |= rgb.r << (8 * 2);
+       res |= rgb.g << (8 * 1);
+       res |= rgb.b << (8 * 0);
+       return (res);
+}
+
+void   copy_mat_to_mlx_image(t_mat *image, t_mlx_data *mlx_image)
+{
+       char    *dest;
+       size_t  x;
+       size_t  y;
+
+       x = 0;
+       while (x < image->cols)
+       {
+               y = 0;
+               while (y < image->rows)
+               {
+                       dest = mlx_image->addr + (y * mlx_image->line_length + x * (mlx_image->bits_per_pixel / 8));
+                       *(unsigned int *)dest = rgb_to_uint(*(t_rgb *)ft_mat_access(image, y, x));
+                       ++y;
+               }
+               ++x;
+       }
+       return ;
+}
+
+int    mlx_close_win(t_mlx_session *s)
+{
+       mlx_destroy_window(s->mlx, s->mlx_win);
+       s->mlx_win = NULL;
+       return (0);
+}
+
+void   free_session(t_mlx_session *s)
+{
+       mlx_destroy_display(s->mlx);
+       free(s->mlx);
+       return ;
+}
+
+void   cleanup(t_mlx_session *s, t_mlx_data *image)
+{
+       mlx_destroy_image(s->mlx, image->img);
+       free_session(s);
+       return ;
+}
+
+int    mlx_handle_key_press(int keycode, t_mlx_session *s)
+{
+       if (keycode == XK_Escape)
+               mlx_close_win(s);
+       return (0);
+}
+
+int    no_event_handle(__attribute__((unused)) t_mlx_session *s)
+{
+       return (0);
+}
+
+void   display(t_mat *image, char *filename)
+{
+       t_mlx_session   s;
+       t_mlx_data              mlx_image;
+
+       s.mlx = mlx_init();
+       s.mlx_win = mlx_new_window(s.mlx, image->cols, image->rows, filename);
+       mlx_image.img = mlx_new_image(s.mlx, image->cols, image->rows);
+       mlx_image.addr = mlx_get_data_addr(mlx_image.img, &mlx_image.bits_per_pixel, &mlx_image.line_length, &mlx_image.endian);
+       copy_mat_to_mlx_image(image, &mlx_image);
+       mlx_hook(s.mlx_win, KeyPress, KeyPressMask, mlx_handle_key_press, &s);
+       mlx_hook(s.mlx_win, DestroyNotify, NoEventMask, mlx_close_win, &s);
+       mlx_loop_hook(s.mlx, no_event_handle, &s);
+       mlx_put_image_to_window(s.mlx, s.mlx_win, mlx_image.img, 0, 0);
+       mlx_loop(s.mlx);
+       cleanup(&s, &mlx_image);
+       return ;
+}
+
 int    main(int argc, char **argv)
 {
-       t_mat   image;
+       t_mat                   image;
 
        if (argc != 2)
                return (1);
        ft_mat_init(&image, sizeof(t_rgb));
        read_jpeg(argv[1], &image);
+       display(&image, argv[1]);
        ft_mat_free(&image, NULL);
        return (0);
 }
diff --git a/solution/minilibx-linux b/solution/minilibx-linux
new file mode 160000 (submodule)
index 0000000..7b689db
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 7b689dbf576eeaafdde87fa2deddd6f616406982