/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   quit.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/12/29 18:06:50 by erey-bet          #+#    #+#             */
/*   Updated: 2023/01/09 17:52:53 by erey-bet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../so_long_bonus.h"

int	end(t_data *data)
{
	if (get_position(data->map, 'C') == NULL)
		quit(data);
	return (1);
}

void	all_free(t_data *data)
{
	(void)data;
}

void	free_img(t_data *data)
{
	int	i;

	i = 0;
	while (i < 8)
		mlx_destroy_image(data->mlx, data->imgs.ply[i++]);
	free(data->imgs.ply);
	i = 0;
	while (i < 3)
		mlx_destroy_image(data->mlx, data->imgs.wall[i++]);
	free(data->imgs.wall);
	i = 0;
	while (i < 3)
		mlx_destroy_image(data->mlx, data->imgs.col[i++]);
	free(data->imgs.col);
	i = 0;
	while (i < 3)
		mlx_destroy_image(data->mlx, data->imgs.exit[i++]);
	free(data->imgs.exit);
	mlx_destroy_image(data->mlx, data->imgs.ene);
	mlx_destroy_image(data->mlx, data->imgs.finish);
}

int	quit(t_data *data)
{
	int	i;

	mlx_destroy_image(data->mlx, data->imgs.bg);
	free_img(data);
	if (data->mlx_win)
	{
		mlx_clear_window(data->mlx, data->mlx_win);
		mlx_destroy_window(data->mlx, data->mlx_win);
	}
	mlx_destroy_display(data->mlx);
	mlx_loop_end(data->mlx);
	i = 0;
	if (data->map)
	{
		while (data->map[i])
			free(data->map[i++]);
		free(data->map);
	}
	free(data->mlx);
	exit(0);
	return (0);
}
