feat: load and destroy wall textures

This commit is contained in:
Khaïs COLIN 2025-07-31 13:43:08 +02:00
parent 184ecc7eb3
commit c4a867b054
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 49 additions and 7 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 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;
void *mlx_win; void *mlx_win;
void *no_texture;
void *so_texture;
void *we_texture;
void *ea_texture;
t_img_data *img_data; t_img_data *img_data;
t_mapdata *map; t_mapdata *map;
t_player player; t_player player;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */ /* 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); return (2);
} }
*texture = ft_strdup(texture_name); *texture = ft_strdup(texture_name);
(*texture)[ft_strlen(*texture) - 1] = '\0';
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 13:59:27 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) if (data->img_data)
mlx_destroy_image(data->mlx, data->img_data->img); mlx_destroy_image(data->mlx, data->img_data->img);
free(data->img_data); 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) if (data->mlx)
mlx_destroy_display(data->mlx); mlx_destroy_display(data->mlx);
free(data->mlx); free(data->mlx);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* inits.c :+: :+: :+: */ /* inits.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:26:53 by tchampio #+# #+# */ /* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/07/31 14:01:39 by tchampio ### ########.fr */ /* Updated: 2025/07/31 14:27:12 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,35 @@
#include "../map/map_checker.h" #include "../map/map_checker.h"
#include "frees.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) void init_cub3d_data(t_cub3d_data *data, char **argv)
{ {
ft_bzero(data, sizeof(*data)); ft_bzero(data, sizeof(*data));
@ -36,5 +65,5 @@ void init_cub3d_data(t_cub3d_data *data, char **argv)
&data->img_data->endian); &data->img_data->endian);
init_player(&data->player, data->map); init_player(&data->player, data->map);
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT); data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
data->delta = get_milliseconds(); load_textures(data);
} }