/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   asset.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2023/01/06 14:23:00 by cchauvet          #+#    #+#             */
/*   Updated: 2023/01/18 17:32:05 by cchauvet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "solong.h"

static void	*ft_gen_asset(char *color, size_t case_size, t_data *data)
{
	t_square	square;
	char		*name;
	int			dimmension;
	void		*img;

	square.color = color;
	square.size = case_size;
	name = ft_xpm_gen_file(square);
	if (name == NULL)
		return (NULL);
	img = mlx_xpm_file_to_image(data->mlx,
			name, &dimmension, &dimmension);
	free(name);
	return (img);
}

int	ft_gen_assets(t_data *data)
{
	char		**colors;
	size_t		i;

	colors = ft_split(COLORS, '|');
	if (colors == NULL)
		return (1);
	i = 0;
	while (i < NB_COLORS)
	{
		data->assets[i] = ft_gen_asset(colors[i], CASE_SIZE, data);
		if (data->assets[i] == NULL)
			return (1);
		i++;
	}
	data->assets[NB_COLORS] = ft_gen_asset(colors[i],
			(RENDER_DISTANCE * 2 + 1) * CASE_SIZE, data);
	if (data->assets[NB_COLORS] == NULL)
		return (1);
	ft_freer_tab_ultimate(1, colors);
	return (0);
}
