From 04c10695da3184d7bd345ad82a6ed743e565309f Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 2 Apr 2024 23:03:05 +0200 Subject: [PATCH] Add minilibx-linux to be able to create GUI 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 | 3 ++ solution/Makefile | 14 +++--- solution/main.c | 109 ++++++++++++++++++++++++++++++++++++++-- solution/minilibx-linux | 1 + 4 files changed, 118 insertions(+), 9 deletions(-) create mode 160000 solution/minilibx-linux diff --git a/.gitmodules b/.gitmodules index 527c7c4..c42374b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/solution/Makefile b/solution/Makefile index 4036114..eeffa85 100644 --- a/solution/Makefile +++ b/solution/Makefile @@ -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) diff --git a/solution/main.c b/solution/main.c index 2743a6f..350abf9 100644 --- a/solution/main.c +++ b/solution/main.c @@ -6,13 +6,16 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include -#include +#include +#include +#include #include #include @@ -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 index 0000000..7b689db --- /dev/null +++ b/solution/minilibx-linux @@ -0,0 +1 @@ +Subproject commit 7b689dbf576eeaafdde87fa2deddd6f616406982 -- 2.30.2