42_solong/draw.c

81 lines
2.4 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-13 10:10:11 -05:00
/* Updated: 2023/01/13 16:04:19 by cchauvet ### ########.fr */
2022-12-22 11:41:31 -05:00
/* */
/* ************************************************************************** */
#include "solong.h"
2023-01-13 09:10:27 -05:00
void ft_draw_img(t_data *data, void *img, size_t x, size_t y)
2022-12-22 11:41:31 -05:00
{
2023-01-11 13:10:28 -05:00
if (img != NULL)
mlx_put_image_to_window(data->mlx, data->window, img, x, y);
2022-12-22 11:41:31 -05:00
}
2023-01-06 13:37:36 -05:00
2023-01-13 10:10:11 -05:00
static void *ft_char2img(t_data *data, char c)
{
if (c == 'C')
2023-01-11 13:10:28 -05:00
return (data->assets[data->nb_swaps + 1 % NB_COLORS]);
if (c == '1')
2023-01-11 13:10:28 -05:00
return (data->assets[data->nb_swaps + 2 % NB_COLORS]);
if (c == 'E')
2023-01-11 13:10:28 -05:00
return (data->assets[data->nb_swaps + 3 % NB_COLORS]);
if (c == 'P')
2023-01-11 13:10:28 -05:00
return (data->assets[data->nb_swaps + 4 % NB_COLORS]);
if (c == 'B')
return (data->assets[data->nb_swaps + 5 % NB_COLORS]);
return (NULL);
}
2023-01-13 09:10:27 -05:00
static void ft_draw_foots(t_data *data)
{
char *foots;
2023-01-13 10:10:11 -05:00
if (BONUS)
2023-01-13 09:10:27 -05:00
{
2023-01-13 10:10:11 -05:00
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);
2023-01-13 09:10:27 -05:00
}
}
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;
2023-01-11 13:10:28 -05:00
void *img;
2023-01-06 13:37:36 -05:00
2023-01-13 09:14:11 -05:00
ft_fill_pos(data->map);
2023-01-13 09:10:27 -05:00
data->nb_swaps = ++data->nb_swaps % (NB_COLORS - 6);
2023-01-09 14:41:06 -05:00
patern = ft_get_player_map(*data->map);
if (patern == NULL)
return (1);
2023-01-11 13:10:28 -05:00
ft_draw_img(data, data->assets[data->nb_swaps + NB_COLORS], 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-11 13:10:28 -05:00
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);
}
2023-01-06 13:37:36 -05:00
}
2023-01-09 14:41:06 -05:00
ft_freer_tab_ultimate(1, patern);
2023-01-13 09:10:27 -05:00
ft_draw_foots(data);
return (0);
2023-01-06 13:37:36 -05:00
}