/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_exit.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/02/01 12:27:16 by yantoine          #+#    #+#             */
/*   Updated: 2024/02/23 21:13:20 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../so_long.h"

void	free_tab(char **tab)
{
	int	i;

	if (!tab)
		return ;
	i = 0;
	while (tab[i])
	{
		free(tab[i]);
		i++;
	}
	free(tab);
	return ;
}

void	free_tab_void(void **tab, t_data *data)
{
	int	i;

	if (!tab)
		return ;
	i = 0;
	while (tab[i])
	{
		mlx_destroy_image(data->mlx, tab[i]);
		i++;
	}
	free(tab);
	return ;
}

void	freelist(t_Liste *liste)
{
	t_Element	*asupprimer;

	if (!liste)
		return ;
	asupprimer = liste->premier;
	while (liste->premier)
	{
		asupprimer = liste->premier;
		liste->premier = liste->premier->suivant;
		free(asupprimer);
	}
	free(liste);
	return ;
}

int	ft_exit(t_data *data)
{
	if (data->player.sprite)
		mlx_destroy_image(data->mlx, data->player.sprite);
	free_tab(data->map);
	free_tab(data->tile.tab_item_path);
	free_tab_void(data->tile.tab_item, data);
	if (data->win)
		mlx_destroy_window(data->mlx, data->win);
	if (data->mlx)
		mlx_destroy_display(data->mlx);
	free(data->mlx);
	freelist(data->path_finder_memory);
	exit(0);
}
