42_solong/draw.c

69 lines
2.1 KiB
C
Raw Normal View History

2022-12-22 11:41:31 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
2023-01-10 13:04:46 -05:00
/* Updated: 2023/01/10 18:07:06 by cchauvet ### ########.fr */
2022-12-22 11:41:31 -05:00
/* */
/* ************************************************************************** */
#include "solong.h"
static void ft_draw_xpm(t_data *data, char *img_path, size_t x, size_t y)
2022-12-22 11:41:31 -05:00
{
int bozo;
2023-01-06 13:37:36 -05:00
void *img;
2022-12-22 11:41:31 -05:00
2023-01-09 14:41:06 -05:00
bozo = CASE_SIZE;
img = mlx_xpm_file_to_image(data->mlx, img_path, &bozo, &bozo);
if (img == NULL)
return ;
mlx_put_image_to_window(data->mlx, data->window, img, x, y);
mlx_destroy_image(data->mlx, img);
2022-12-22 11:41:31 -05:00
}
2023-01-06 13:37:36 -05:00
static char *ft_char2xpm(t_data data, char c)
{
if (c == 'C')
return (data.ccolor);
if (c == '1')
return (data.wcolor);
if (c == 'E')
return (data.ecolor);
if (c == 'P')
return (data.pcolor);
return (NULL);
}
2023-01-09 14:41:06 -05:00
int ft_draw_map(t_data *data)
2023-01-06 13:37:36 -05:00
{
2023-01-10 13:04:46 -05:00
ssize_t tab[2];
char **patern;
char *path;
2023-01-06 13:37:36 -05:00
2023-01-09 14:41:06 -05:00
ft_fill_pos(data);
ft_color_changer(data);
patern = ft_get_player_map(*data->map);
if (patern == NULL)
return (1);
ft_draw_xpm(data, data->bcolor, 0, 0);
2023-01-10 13:04:46 -05:00
tab[1] = -1;
while (patern[++tab[1]] != NULL)
2023-01-06 13:37:36 -05:00
{
2023-01-10 13:04:46 -05:00
tab[0] = -1;
while (patern[tab[1]][++tab[0]] != '\0')
{
2023-01-10 13:04:46 -05:00
path = ft_char2xpm(*data, patern[tab[1]][tab[0]]);
if (path != NULL)
2023-01-10 13:04:46 -05:00
ft_draw_xpm(data, path, tab[0] * CASE_SIZE, tab[1] * CASE_SIZE);
}
2023-01-06 13:37:36 -05:00
}
2023-01-10 13:04:46 -05:00
ft_freer_ultimate(3, data->bcolor, data->pcolor, data->ccolor);
ft_freer_ultimate(2, data->wcolor, data->ecolor);
2023-01-09 14:41:06 -05:00
ft_freer_tab_ultimate(1, patern);
return (0);
2023-01-06 13:37:36 -05:00
}