/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   init_tab_item.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <yantoine@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/02/20 20:24:31 by yantoine          #+#    #+#             */
/*   Updated: 2024/02/23 18:16:32 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../so_long.h"

static	int	count_line(char **tab_item_path)
{
	int		line;

	if (!tab_item_path)
	{
		ft_putstr_fd("count_line: error chaine vide\n", 2);
		return (0);
	}
	line = 0;
	while (tab_item_path[line])
	{
		ft_printf("path: %s\n", tab_item_path[line]);
		line++;
	}
	return (line);
}

void	**init_tab_item(t_data *data)
{
	void	**tab_item;
	int		nbr_of_item;
	int		line;
	int		img_height;
	int		img_width;

	nbr_of_item = count_line(data->tile.tab_item_path);
	tab_item = malloc(sizeof(void *) * (nbr_of_item + 1));
	if (!tab_item)
		return (NULL);
	line = 0;
	while (line < nbr_of_item)
	{
		tab_item[line] = mlx_xpm_file_to_image(data->mlx,
				data->tile.tab_item_path[line], &img_width, &img_height);
		if (tab_item[line] == 0)
		{
			data->tile.tab_item = tab_item;
			ft_exit(data);
		}
		line++;
	}
	tab_item[line] = NULL;
	return (tab_item);
}
