/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: hexplor <marvin@42.fr>                     +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2023/12/24 17:23:36 by hexplor           #+#    #+#             */
/*   Updated: 2024/02/23 22:10:37 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../so_long.h"

static int	is_win_kill(t_data *data)
{
	ft_exit(data);
	return (0);
}

static int	ft_keyboard(int keycode, t_data *data)
{
	if (keycode == 65307)
		ft_exit(data);
	else if (keycode == 'a' || keycode == 'd' \
	|| keycode == 'w' || keycode == 's')
		ft_move_img(data, keycode);
	refresh_window(data);
	return (0);
}

static t_data	initialisation(char **argv, int argc)
{
	t_data	data;

	if (check_args(argv, argc))
		exit(1);
	data.tile.item_ref_file = ITEM_FILE;
	data.nb_e = 0;
	data.nb_p = 0;
	data.nb_c = 0;
	init_count_item(&data);
	data.map = ft_read_map(argv[1]);
	check_map_g(data.map, &data);
	data.mlx = mlx_init();
	if (init_player(&data) == 1)
		exit(1);
	define_size(&data);
	ft_printf("size etablie\n");
	data.win = mlx_new_window(data.mlx, data.width_screen,
			data.height_screen, "great_asset");
	ft_printf("fenetre etablie\n");
	data.tile.tab_item_path = init_tab_item_path(ITEM_FILE);
	data.tile.tab_item = init_tab_item(&data);
	return (data);
}

static int	launch_game(t_data *data)
{
	refresh_window(data);
	mlx_key_hook(data->win, ft_keyboard, data);
	mlx_hook(data->win, 17, 0, is_win_kill, data);
	mlx_loop(data->mlx);
	return (0);
}

int	main(int argc, char **argv)
{
	t_data	data;

	data = initialisation(argv, argc);
	launch_game(&data);
	return (0);
}
