From c4a867b05438fa52df5e722acf4113cb6c178afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 31 Jul 2025 13:43:08 +0200 Subject: [PATCH] feat: load and destroy wall textures --- src/cub3d_data.h | 6 +++++- src/map/setters.c | 3 ++- src/utils/frees.c | 10 +++++++++- src/utils/inits.c | 37 +++++++++++++++++++++++++++++++++---- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/cub3d_data.h b/src/cub3d_data.h index 4cdc95f..dc0aa18 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 13:55:08 by tchampio ### ########.fr */ +/* Updated: 2025/07/31 14:17:28 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +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 *img_data; t_mapdata *map; t_player player; diff --git a/src/map/setters.c b/src/map/setters.c index f52a84b..0a82082 100644 --- a/src/map/setters.c +++ b/src/map/setters.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */ -/* Updated: 2025/07/23 12:20:51 by tchampio ### ########.fr */ +/* Updated: 2025/07/31 14:08:27 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,6 +81,7 @@ int try_set_texture(t_mapdata *map, char **texture, char *texture_name) return (2); } *texture = ft_strdup(texture_name); + (*texture)[ft_strlen(*texture) - 1] = '\0'; return (0); } diff --git a/src/utils/frees.c b/src/utils/frees.c index 233884b..2175263 100644 --- a/src/utils/frees.c +++ b/src/utils/frees.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */ -/* Updated: 2025/07/31 13:26:06 by kcolin ### ########.fr */ +/* Updated: 2025/07/31 14:25:10 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,6 +66,14 @@ int destroy(t_cub3d_data *data) if (data->img_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); + if (data->so_texture) + mlx_destroy_image(data->mlx, data->so_texture); + if (data->ea_texture) + mlx_destroy_image(data->mlx, data->ea_texture); + if (data->we_texture) + mlx_destroy_image(data->mlx, 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 48343e5..e0d604e 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* inits.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/07/31 13:26:53 by tchampio #+# #+# */ -/* Updated: 2025/07/31 14:01:39 by tchampio ### ########.fr */ +/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */ +/* Updated: 2025/07/31 14:27:12 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,35 @@ #include "../map/map_checker.h" #include "frees.h" +void *load_single_texture(t_cub3d_data *data, char *path) +{ + int width; + int height; + void *img; + + img = mlx_xpm_file_to_image(data->mlx, path, &width, &height); + if (img == NULL) + { + ft_printf("Error: failed to open image at %s\n", path); + destroy(data); + } + if (width != height || width != TEXTURE_SIZE) + { + ft_printf("Error: textures are not the right size\n"); + destroy(data); + } + ft_printf("image: %p\n", img); + return (img); +} + +void load_textures(t_cub3d_data *data) +{ + data->no_texture = load_single_texture(data, data->map->no_texture); + data->so_texture = load_single_texture(data, data->map->so_texture); + data->we_texture = load_single_texture(data, data->map->we_texture); + data->ea_texture = load_single_texture(data, data->map->ea_texture); +} + void init_cub3d_data(t_cub3d_data *data, char **argv) { ft_bzero(data, sizeof(*data)); @@ -36,5 +65,5 @@ void init_cub3d_data(t_cub3d_data *data, char **argv) &data->img_data->endian); init_player(&data->player, data->map); data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT); - data->delta = get_milliseconds(); + load_textures(data); }