/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   draw.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/12/13 15:49:23 by cchauvet          #+#    #+#             */
/*   Updated: 2023/01/16 15:38:37 by cchauvet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "solong.h"

void	ft_draw_img(t_data *data, void *img, size_t x, size_t y)
{
	if (img != NULL)
		mlx_put_image_to_window(data->mlx, data->window, img, x, y);
}

static void	*ft_char2img(t_data *data, char c)
{
	if (c == 'C')
		return (data->assets[0]);
	if (c == '1')
		return (data->assets[1]);
	if (c == 'E')
		return (data->assets[2]);
	if (c == 'P')
		return (data->assets[3]);
	return (NULL);
}

int	ft_draw_map(t_data *data)
{
	ssize_t	tab[2];
	char	**patern;
	void	*img;

	ft_fill_pos(data->map);
	patern = ft_get_player_map(*data->map);
	if (patern == NULL)
		return (1);
	ft_draw_img(data, data->assets[NB_COLORS], 0, 0);
	tab[1] = -1;
	while (patern[++tab[1]] != NULL)
	{
		tab[0] = -1;
		while (patern[tab[1]][++tab[0]] != '\0')
		{
			img = ft_char2img(data, patern[tab[1]][tab[0]]);
			if (img != NULL)
				ft_draw_img(data, img, tab[0] * CASE_SIZE, tab[1] * CASE_SIZE);
		}
	}
	ft_freer_tab_ultimate(1, patern);
	return (0);
}
