From c09ba88d9039f84565391b0f831bddfabfb2988d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 5 Aug 2025 13:08:47 +0200 Subject: [PATCH] feat: use t_img_data to store textures insted of void ptr This allows for easier writing of future functions, see next pr comming soon --- src/cub3d_data.h | 10 +++++----- src/utils/frees.c | 12 ++++++++---- src/utils/inits.c | 18 ++++++++++++------ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/cub3d_data.h b/src/cub3d_data.h index dc0aa18..97ffd66 100644 --- a/src/cub3d_data.h +++ b/src/cub3d_data.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ -/* Updated: 2025/07/31 14:17:28 by kcolin ### ########.fr */ +/* Updated: 2025/08/05 13:09:22 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,10 @@ typedef struct s_cub3d_data { void *mlx; void *mlx_win; - void *no_texture; - void *so_texture; - void *we_texture; - void *ea_texture; + t_img_data *no_texture; + t_img_data *so_texture; + t_img_data *we_texture; + t_img_data *ea_texture; t_img_data *img_data; t_mapdata *map; t_player player; diff --git a/src/utils/frees.c b/src/utils/frees.c index 2175263..6319134 100644 --- a/src/utils/frees.c +++ b/src/utils/frees.c @@ -67,13 +67,17 @@ int destroy(t_cub3d_data *data) mlx_destroy_image(data->mlx, data->img_data->img); free(data->img_data); if (data->no_texture) - mlx_destroy_image(data->mlx, data->no_texture); + mlx_destroy_image(data->mlx, data->no_texture->img); + free(data->no_texture); if (data->so_texture) - mlx_destroy_image(data->mlx, data->so_texture); + mlx_destroy_image(data->mlx, data->so_texture->img); + free(data->so_texture); if (data->ea_texture) - mlx_destroy_image(data->mlx, data->ea_texture); + mlx_destroy_image(data->mlx, data->ea_texture->img); + free(data->ea_texture); if (data->we_texture) - mlx_destroy_image(data->mlx, data->we_texture); + mlx_destroy_image(data->mlx, data->we_texture->img); + free(data->we_texture); if (data->mlx) mlx_destroy_display(data->mlx); free(data->mlx); diff --git a/src/utils/inits.c b/src/utils/inits.c index e0d604e..202fc5d 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */ -/* Updated: 2025/07/31 14:27:12 by kcolin ### ########.fr */ +/* Updated: 2025/08/05 13:19:40 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,12 @@ #include "../map/map_checker.h" #include "frees.h" -void *load_single_texture(t_cub3d_data *data, char *path) +t_img_data *load_single_texture(t_cub3d_data *data, char *path) { - int width; - int height; - void *img; + int width; + int height; + void *img; + t_img_data *img_data; img = mlx_xpm_file_to_image(data->mlx, path, &width, &height); if (img == NULL) @@ -35,7 +36,12 @@ void *load_single_texture(t_cub3d_data *data, char *path) destroy(data); } ft_printf("image: %p\n", img); - return (img); + img_data = ft_calloc(sizeof(t_img_data), 1); + img_data->img = img; + img_data->addr = mlx_get_data_addr(img_data->img, + &img_data->bits_per_pixel, &img_data->line_length, + &img_data->endian); + return (img_data); } void load_textures(t_cub3d_data *data)