cub3d/src/utils/frees.c

97 lines
2.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* frees.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */
/* Updated: 2025/07/22 12:54:01 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../mlx/mlx.h"
#include "../../libft/includes/libft.h"
#include "../map/mapdata.h"
#include "../cub3d_data.h"
#include <stdlib.h>
void free_tab(char **tab)
{
int i;
if (!tab)
return ;
i = 0;
while (tab[i])
{
free(tab[i]);
i++;
}
free(tab);
}
void free_tab_length(char **tab, int length)
{
int i;
if (!tab)
return ;
i = 0;
while (i < length)
{
free(tab[i]);
++i;
}
free(tab);
}
void free_map(t_mapdata *map)
{
if (map->map)
free_tab(map->map);
if (map->mapflood)
free_tab(map->mapflood);
if (map->ea_texture)
free(map->ea_texture);
if (map->no_texture)
free(map->no_texture);
if (map->so_texture)
free(map->so_texture);
if (map->we_texture)
free(map->we_texture);
if (map->filename)
free(map->filename);
free(map);
}
int destroy(t_cub3d_data *data)
{
free_map(data->map);
if (data->mlx_win)
mlx_destroy_window(data->mlx, data->mlx_win);
if (data->img_data)
mlx_destroy_image(data->mlx, data->img_data->img);
free(data->img_data);
if (data->mlx)
mlx_destroy_display(data->mlx);
free(data->mlx);
exit(0);
return (0);
}
/*
Calls get_next_line in a loop until NULL is returned, freeing all data returned.
Ensures that all memory allocated by get_next_line is properly freed.
*/
void gnl_exhaust(int fd)
{
char *line;
line = get_next_line(fd);
while (line != NULL)
{
free(line);
line = get_next_line(fd);
}
}