42_solong/bonus/draw.c
2023-01-19 17:38:47 +01:00

80 lines
2.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
/* Updated: 2023/01/19 15:20:56 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[data->nb_swaps + 1 % NB_COLORS]);
if (c == '1')
return (data->assets[data->nb_swaps + 2 % NB_COLORS]);
if (c == 'E')
return (data->assets[data->nb_swaps + 3 % NB_COLORS]);
if (c == 'P')
return (data->assets[data->nb_swaps + 4 % NB_COLORS]);
if (c == 'B')
return (data->assets[data->nb_swaps + 5 % NB_COLORS]);
return (NULL);
}
static void ft_draw_foots(t_data *data)
{
char *foots;
data->nb_foots++;
foots = ft_itoa(data->nb_foots);
if (foots == NULL)
{
ft_printf("Memory error\n");
ft_exit(data);
}
mlx_string_put(data->mlx, data->window, 10, 10, 255, foots);
ft_printf("\r%s", foots);
free(foots);
}
int ft_draw_map(t_data *data)
{
ssize_t tab[2];
char **patern;
void *img;
ft_fill_pos(data->map);
data->nb_swaps = ft_random_generator(0, NB_COLORS - 6);
patern = ft_get_player_map(*data->map);
if (patern == NULL)
return (1);
ft_draw_img(data, data->assets[data->nb_swaps % NB_COLORS + 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);
ft_draw_foots(data);
return (0);
}