/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   init_player.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <yantoine@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/02/20 21:50:30 by yantoine          #+#    #+#             */
/*   Updated: 2024/02/21 23:56:37 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../so_long.h"

static int	get_base_player_position(t_data *data)
{
	int	x;
	int	y;

	y = 0;
	while (data->map[y])
	{
		x = 0;
		while (data->map[y][x])
		{
			if (data->map[y][x] == '4')
			{
				data->player.position.y = y * 64;
				data->player.position.x = x * 64;
				ft_printf("position trouvé:\nx = %d\ny = %d\n", x, y);
				return (0);
			}
			x++;
		}
		y++;
	}
	ft_printf("ERRREUR: position non trouve\n");
	return (1);
}

int	init_player(t_data *data)
{
	int	img_width;
	int	img_height;

	get_base_player_position(data);
	data->player.sprite_path = PLAYER_SPRITE_PATH;
	data->player.sprite = mlx_xpm_file_to_image(data->mlx,
			data->player.sprite_path, &img_width, &img_height);
	if (data->player.sprite == NULL)
	{
		ft_printf("init_player: Erreur chargement du sprite player\n");
		return (1);
	}
	return (0);
}
