bonus and mandatory separation
This commit is contained in:
55
bonus/asset.c
Normal file
55
bonus/asset.c
Normal file
@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* asset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/06 14:23:00 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 16:03:54 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static void *ft_gen_asset(char *color, size_t case_size, t_data *data)
|
||||
{
|
||||
t_square square;
|
||||
char *name;
|
||||
int dimmension;
|
||||
void *img;
|
||||
|
||||
square.color = color;
|
||||
square.size = case_size;
|
||||
name = ft_xpm_gen_file(square);
|
||||
if (name == NULL)
|
||||
return (NULL);
|
||||
img = mlx_xpm_file_to_image(data->mlx,
|
||||
name, &dimmension, &dimmension);
|
||||
free(name);
|
||||
return (img);
|
||||
}
|
||||
|
||||
int ft_gen_assets(t_data *data)
|
||||
{
|
||||
char **colors;
|
||||
size_t i;
|
||||
|
||||
colors = ft_split(COLORS, '|');
|
||||
if (colors == NULL)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (i < NB_COLORS)
|
||||
{
|
||||
data->assets[i] = ft_gen_asset(colors[i], CASE_SIZE, data);
|
||||
if (data->assets[i] == NULL)
|
||||
return (1);
|
||||
data->assets[i + NB_COLORS] = ft_gen_asset(colors[i],
|
||||
WINDOW_SIZE, data);
|
||||
if (data->assets[i + NB_COLORS] == NULL)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, colors);
|
||||
return (0);
|
||||
}
|
82
bonus/draw.c
Normal file
82
bonus/draw.c
Normal file
@ -0,0 +1,82 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 12:52:30 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' && BONUS)
|
||||
return (data->assets[data->nb_swaps + 5 % NB_COLORS]);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static void ft_draw_foots(t_data *data)
|
||||
{
|
||||
char *foots;
|
||||
|
||||
if (BONUS)
|
||||
{
|
||||
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);
|
||||
if (BONUS)
|
||||
data->nb_swaps = ++data->nb_swaps % NB_COLORS;
|
||||
patern = ft_get_player_map(*data->map);
|
||||
if (patern == NULL)
|
||||
return (1);
|
||||
ft_draw_img(data, data->assets[data->nb_swaps + 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);
|
||||
}
|
77
bonus/key.c
Normal file
77
bonus/key.c
Normal file
@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* key.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 12:52:02 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static void ft_case_update(char *new_pos, char *player_pos)
|
||||
{
|
||||
*new_pos = 'P';
|
||||
*player_pos = '0';
|
||||
}
|
||||
|
||||
static void ft_epileptic_mod(t_data *data)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < NB_COLORS * 100)
|
||||
{
|
||||
ft_draw_img(data, data->assets[(i % NB_COLORS) + NB_COLORS], 0, 0);
|
||||
write(1, "\a\a\a\a\a", 5);
|
||||
i++;
|
||||
}
|
||||
ft_exit(data);
|
||||
}
|
||||
|
||||
static void ft_move(t_data *data, char direction)
|
||||
{
|
||||
size_t tab[2];
|
||||
char *new_pos;
|
||||
|
||||
tab[0] = data->map->player_pos[0];
|
||||
tab[1] = data->map->player_pos[1];
|
||||
if (direction == 'L')
|
||||
new_pos = &data->map->patern[tab[1]][tab[0] - 1];
|
||||
if (direction == 'U')
|
||||
new_pos = &data->map->patern[tab[1] - 1][tab[0]];
|
||||
if (direction == 'D')
|
||||
new_pos = &data->map->patern[tab[1] + 1][tab[0]];
|
||||
if (direction == 'R')
|
||||
new_pos = &data->map->patern[tab[1]][tab[0] + 1];
|
||||
if (*new_pos == 'C')
|
||||
data->map->nb_collectable--;
|
||||
if (*new_pos == '0' || *new_pos == 'C')
|
||||
ft_case_update(new_pos, &data->map->patern[tab[1]][tab[0]]);
|
||||
if (*new_pos == 'E')
|
||||
{
|
||||
if (data->map->nb_collectable == 0)
|
||||
ft_exit(data);
|
||||
}
|
||||
if (*new_pos == 'B')
|
||||
ft_epileptic_mod(data);
|
||||
ft_draw_map(data);
|
||||
}
|
||||
|
||||
int ft_key(int keycode, t_data *data)
|
||||
{
|
||||
if (keycode == 65361)
|
||||
ft_move(data, 'L');
|
||||
else if (keycode == 65363)
|
||||
ft_move(data, 'R');
|
||||
else if (keycode == 65362)
|
||||
ft_move(data, 'U');
|
||||
else if (keycode == 65364)
|
||||
ft_move(data, 'D');
|
||||
else if (keycode == 65307)
|
||||
ft_exit(data);
|
||||
return (0);
|
||||
}
|
80
bonus/main.c
Normal file
80
bonus/main.c
Normal file
@ -0,0 +1,80 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/12 17:20:17 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 18:50:00 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
int ft_initialised(char *path, t_data *data)
|
||||
{
|
||||
t_map *map;
|
||||
|
||||
map = ft_getmap(path);
|
||||
if (map == NULL)
|
||||
{
|
||||
ft_printf("Map error\n");
|
||||
exit(1);
|
||||
}
|
||||
data->mlx = mlx_init();
|
||||
ft_printf("Generating assets ...");
|
||||
ft_gen_assets(data);
|
||||
ft_printf("\rGenerating assets [FINISHED]\n");
|
||||
data->map = map;
|
||||
data->window = mlx_new_window(data->mlx, WINDOW_SIZE, WINDOW_SIZE, "long");
|
||||
data->nb_foots = 0;
|
||||
data->nb_swaps = 35;
|
||||
if (BONUS)
|
||||
data->nb_swaps = ft_random_generator(0, NB_COLORS);
|
||||
ft_draw_map(data);
|
||||
mlx_hook(data->window, 17, (0L), ft_exit, data);
|
||||
mlx_hook(data->window, 2, (1L << 0), ft_key, data);
|
||||
mlx_loop(data->mlx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_exit(t_data *data)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < NB_COLORS * 2)
|
||||
{
|
||||
if (data->assets[i] != NULL)
|
||||
mlx_destroy_image(data->mlx, data->assets[i]);
|
||||
i++;
|
||||
}
|
||||
if (data->map != NULL && data->map->patern != NULL)
|
||||
ft_freer_tab_ultimate(1, data->map->patern);
|
||||
if (data->window != NULL && data->mlx != NULL)
|
||||
mlx_destroy_window(data->mlx, data->window);
|
||||
if (data->mlx != NULL)
|
||||
mlx_destroy_display(data->mlx);
|
||||
if (data->mlx != NULL)
|
||||
free(data->mlx);
|
||||
if (data->map != NULL)
|
||||
free(data->map);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_data data;
|
||||
|
||||
if (ac != 2 || ft_strcmp(av[1] + ft_strlen(av[1]) - 4, ".ber"))
|
||||
{
|
||||
ft_printf("Map error\n");
|
||||
return (1);
|
||||
}
|
||||
data.map = NULL;
|
||||
data.window = NULL;
|
||||
if (ft_initialised(av[1], &data))
|
||||
ft_printf("Memory error");
|
||||
return (1);
|
||||
}
|
139
bonus/map.c
Normal file
139
bonus/map.c
Normal file
@ -0,0 +1,139 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 13:51:49 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 13:56:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
char **ft_readfile(char *path)
|
||||
{
|
||||
char **map;
|
||||
int fd;
|
||||
size_t nb_line;
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
nb_line = 1;
|
||||
map = ft_calloc(sizeof(char *), 2);
|
||||
if (map == NULL)
|
||||
return (NULL);
|
||||
map[0] = get_next_line(fd);
|
||||
while (map[nb_line - 1] != NULL)
|
||||
{
|
||||
ft_strchr(map[nb_line - 1], '\n')[0] = '\0';
|
||||
map[nb_line] = get_next_line(fd);
|
||||
map = ft_tabrealloc(map, nb_line + 1, nb_line + 2);
|
||||
if (map == NULL)
|
||||
return (NULL);
|
||||
nb_line++;
|
||||
}
|
||||
return (map);
|
||||
}
|
||||
|
||||
void ft_fill_pos(t_map *map)
|
||||
{
|
||||
size_t x;
|
||||
size_t y;
|
||||
|
||||
y = 1;
|
||||
while (map->patern[y + 1] != NULL)
|
||||
{
|
||||
x = 0;
|
||||
while (x < ft_strlen(map->patern[y]))
|
||||
{
|
||||
if (map->patern[y][x] == 'P')
|
||||
{
|
||||
map->player_pos[0] = x;
|
||||
map->player_pos[1] = y;
|
||||
}
|
||||
if (map->patern[y][x] == 'E')
|
||||
{
|
||||
map->exit_pos[0] = x;
|
||||
map->exit_pos[1] = y;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
t_map *ft_getmap(char *path)
|
||||
{
|
||||
t_map *map;
|
||||
|
||||
map = ft_calloc(sizeof(t_map), 1);
|
||||
if (map == NULL)
|
||||
return (NULL);
|
||||
map->patern = ft_readfile(path);
|
||||
if (ft_map_is_correct(map) == 0)
|
||||
{
|
||||
ft_freer_tab_ultimate(1, map->patern);
|
||||
free(map);
|
||||
return (NULL);
|
||||
}
|
||||
return (map);
|
||||
}
|
||||
|
||||
static char *ft_get_line(const char *line, ssize_t player_x)
|
||||
{
|
||||
char *out;
|
||||
ssize_t start;
|
||||
ssize_t stop;
|
||||
ssize_t i;
|
||||
ssize_t y;
|
||||
|
||||
out = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
start = player_x - (RENDER_DISTANCE);
|
||||
if (start < 0)
|
||||
start = 0;
|
||||
stop = player_x + RENDER_DISTANCE + 1;
|
||||
if (stop > (ssize_t) ft_strlen(line))
|
||||
stop = ft_strlen(line);
|
||||
i = 0;
|
||||
y = player_x - RENDER_DISTANCE - start;
|
||||
if (y < 0)
|
||||
y = -y;
|
||||
while (start + i < stop)
|
||||
{
|
||||
out[y + i] = line[i + start];
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
char **ft_get_player_map(t_map map)
|
||||
{
|
||||
char **player_map;
|
||||
ssize_t y;
|
||||
ssize_t i;
|
||||
|
||||
player_map = ft_calloc(RENDER_DISTANCE * 2 + 2, sizeof(char *));
|
||||
if (player_map == NULL)
|
||||
return (NULL);
|
||||
i = map.player_pos[1] - RENDER_DISTANCE;
|
||||
if (i < 0)
|
||||
i = 0;
|
||||
y = -1;
|
||||
while (++y < RENDER_DISTANCE * 2 + 1)
|
||||
{
|
||||
if (map.player_pos[1] + y >= RENDER_DISTANCE && map.patern[i] != NULL)
|
||||
player_map[y] = ft_get_line(map.patern[i++], map.player_pos[0]);
|
||||
else
|
||||
player_map[y] = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
|
||||
if (player_map[y] == NULL)
|
||||
{
|
||||
ft_cancel(player_map, y);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
player_map[y] = NULL;
|
||||
return (player_map);
|
||||
}
|
123
bonus/parsing.c
Normal file
123
bonus/parsing.c
Normal file
@ -0,0 +1,123 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 20:14:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 15:47:42 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
int ft_test_map_dimmention(t_map *map)
|
||||
{
|
||||
size_t i;
|
||||
size_t len;
|
||||
|
||||
len = ft_strlen(map->patern[0]);
|
||||
i = 1;
|
||||
while (map->patern[i] != NULL)
|
||||
{
|
||||
if (ft_strlen(map->patern[i]) != len)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
map->x_len = len;
|
||||
map->y_len = i;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_test_map_content1(t_map *map)
|
||||
{
|
||||
size_t x;
|
||||
size_t y;
|
||||
size_t nb_collectable;
|
||||
|
||||
nb_collectable = 0;
|
||||
y = 0;
|
||||
while (y < map->y_len)
|
||||
{
|
||||
x = 0;
|
||||
while (x < map->x_len)
|
||||
{
|
||||
if (!BONUS && ft_is_in("01CEP", map->patern[y][x]) == 0)
|
||||
return (0);
|
||||
if (BONUS && ft_is_in("01CEPB", map->patern[y][x]) == 0)
|
||||
return (0);
|
||||
if (map->patern[y][x] == 'C')
|
||||
nb_collectable++;
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
if (nb_collectable == 0)
|
||||
return (0);
|
||||
map->nb_collectable = nb_collectable;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_test_map_content2(char **patern)
|
||||
{
|
||||
size_t x;
|
||||
size_t y;
|
||||
size_t test1;
|
||||
size_t test2;
|
||||
|
||||
test1 = 0;
|
||||
test2 = 0;
|
||||
y = 0;
|
||||
while (patern[y + 1] != NULL)
|
||||
{
|
||||
x = 0;
|
||||
while (x < ft_strlen(patern[y]))
|
||||
{
|
||||
if (patern[y][x] == 'E')
|
||||
test1++;
|
||||
else if (patern[y][x] == 'P')
|
||||
test2++;
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
return (test1 == 1 && test2 == 1);
|
||||
}
|
||||
|
||||
int ft_test_map_is_surronded(t_map map)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!ft_contain_only(map.patern[0], '1'))
|
||||
return (0);
|
||||
i = 1;
|
||||
while (map.patern[i + 1] != NULL)
|
||||
{
|
||||
if (map.patern[i][0] != '1')
|
||||
return (0);
|
||||
if (map.patern[i][ft_strlen(map.patern[i]) - 1] != '1')
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
if (!ft_contain_only(map.patern[i], '1'))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_map_is_correct(t_map *map)
|
||||
{
|
||||
if (!ft_test_map_dimmention(map))
|
||||
return (0);
|
||||
if (!ft_test_map_content1(map))
|
||||
return (0);
|
||||
if (!ft_test_map_content2(map->patern))
|
||||
return (0);
|
||||
if (!ft_test_map_dimmention(map))
|
||||
return (0);
|
||||
if (!ft_test_map_is_surronded(*map))
|
||||
return (0);
|
||||
if (!ft_test_map_is_finishable(map))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
79
bonus/parsing2.c
Normal file
79
bonus/parsing2.c
Normal file
@ -0,0 +1,79 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/12 16:17:20 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/13 16:28:34 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static void ft_collector(char **patern, size_t x, size_t y)
|
||||
{
|
||||
if (BONUS)
|
||||
{
|
||||
if (ft_is_in("1B", patern[y][x]))
|
||||
return ;
|
||||
}
|
||||
else
|
||||
if (patern[y][x] == '1')
|
||||
return ;
|
||||
patern[y][x] = '1';
|
||||
ft_collector(patern, x, y - 1);
|
||||
ft_collector(patern, x + 1, y);
|
||||
ft_collector(patern, x, y + 1);
|
||||
ft_collector(patern, x - 1, y);
|
||||
}
|
||||
|
||||
static char **ft_patern_dup(char **patern)
|
||||
{
|
||||
char **patern2;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (patern[i] != NULL)
|
||||
i++;
|
||||
patern2 = ft_calloc(i + 1, sizeof(char *));
|
||||
if (patern2 == NULL)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (patern[i] != NULL)
|
||||
{
|
||||
patern2[i] = ft_strdup(patern[i]);
|
||||
if (patern2[i] == NULL)
|
||||
{
|
||||
ft_cancel(patern2, i);
|
||||
return (NULL);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
patern2[i] = NULL;
|
||||
return (patern2);
|
||||
}
|
||||
|
||||
int ft_test_map_is_finishable(t_map *map)
|
||||
{
|
||||
size_t i;
|
||||
int ok;
|
||||
char **patern;
|
||||
|
||||
patern = ft_patern_dup(map->patern);
|
||||
if (patern == NULL)
|
||||
return (1);
|
||||
ft_fill_pos(map);
|
||||
ft_collector(patern, map->player_pos[0], map->player_pos[1]);
|
||||
i = 1;
|
||||
ok = 1;
|
||||
while (patern[i + 1] != NULL)
|
||||
{
|
||||
if (ft_is_in(patern[i], 'C') || ft_is_in(patern[i], 'E'))
|
||||
ok = 0;
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, patern);
|
||||
return (ok);
|
||||
}
|
67
bonus/solong.h
Normal file
67
bonus/solong.h
Normal file
@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* solong.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 16:20:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 18:36:10 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SOLONG_H
|
||||
# define SOLONG_H
|
||||
# include "../libftx/libftx.h"
|
||||
# define XPM_PATH "./textures/"
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include "../minilibx-linux/mlx.h"
|
||||
# define CASE_SIZE 64
|
||||
# define XPM_HEADER "\nstatic char * XFACE[] = {\""
|
||||
# define COLORS "silver|gray|white|maroon|red|purple|fuchsia|green|lime|olive|yellow|navy|blue|teal|aqua|aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|lime|limegreen|linen|magenta|maroon|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|navy|oldlace|olive|olivedrab|orange|orangered|orchid|palegoldenrod|palegreen|paleturquoise|palevioletred|papayawhip|peachpuff|peru|pink|plum|powderblue|purple|red|rosybrown|royalblue|saddlebrown|salmon|sandybrown|seagreen|seashell|sienna|silver|skyblue|slateblue|slategray|slategrey|snow|springgreen|steelblue|tan|teal|thistle|tomato|turquoise|violet|wheat|white|whitesmoke|yellow|yellowgreen"
|
||||
# define NB_COLORS 93
|
||||
# define RENDER_DISTANCE 5
|
||||
# define WINDOW_SIZE (RENDER_DISTANCE * 2 + 1) * CASE_SIZE
|
||||
|
||||
typedef struct s_square
|
||||
{
|
||||
char *color;
|
||||
size_t size;
|
||||
} t_square;
|
||||
|
||||
typedef struct s_map
|
||||
{
|
||||
size_t x_len;
|
||||
size_t y_len;
|
||||
size_t nb_collectable;
|
||||
char **patern;
|
||||
size_t player_pos[2];
|
||||
size_t exit_pos[2];
|
||||
} t_map;
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
void *mlx;
|
||||
void *window;
|
||||
void *assets[NB_COLORS * 2];
|
||||
t_map *map;
|
||||
size_t nb_swaps;
|
||||
size_t nb_foots;
|
||||
} t_data;
|
||||
|
||||
int ft_test_map_is_finishable(t_map *map);
|
||||
int ft_exit(t_data *data);
|
||||
int ft_test_map_content2(char **patern);
|
||||
int ft_key(int key, t_data *data);
|
||||
void ft_fill_pos(t_map *map);
|
||||
char *name_generator(t_square square);
|
||||
t_map *ft_getmap(char *path);
|
||||
char *ft_xpm_gen_file(t_square square);
|
||||
int ft_gen_assets(t_data *data);
|
||||
char **ft_get_player_map(t_map map);
|
||||
int ft_draw_map(t_data *data);
|
||||
char *name_generator(t_square square);
|
||||
int ft_color_changer(t_data *data);
|
||||
void ft_draw_img(t_data *data, void *img, size_t x, size_t y);
|
||||
#endif
|
115
bonus/xpm.c
Normal file
115
bonus/xpm.c
Normal file
@ -0,0 +1,115 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* xpm.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 13:53:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/11 16:53:45 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static char *ft_square(size_t size)
|
||||
{
|
||||
char *map;
|
||||
size_t x;
|
||||
size_t y;
|
||||
|
||||
map = malloc(((size + 4) * size) * sizeof(char));
|
||||
if (map == NULL)
|
||||
return (map);
|
||||
y = 0;
|
||||
while (y < size)
|
||||
{
|
||||
map[(size + 4) * y] = '"';
|
||||
x = 0;
|
||||
while (x < size)
|
||||
{
|
||||
map[y * (size + 4) + x + 1] = 'a';
|
||||
x++;
|
||||
}
|
||||
map[(size + 4) * y + size + 1] = '"';
|
||||
map[(size + 4) * y + size + 2] = ',';
|
||||
map[(size + 4) * y + size + 3] = '\n';
|
||||
y++;
|
||||
}
|
||||
map[(size + 4) * size - 2] = '\0';
|
||||
return (map);
|
||||
}
|
||||
|
||||
char *name_generator(t_square square)
|
||||
{
|
||||
char *out;
|
||||
char *size;
|
||||
char *temp;
|
||||
|
||||
size = ft_itoa(square.size);
|
||||
if (size == NULL)
|
||||
return (NULL);
|
||||
temp = ft_strmerger(3, XPM_PATH, square.color, "_");
|
||||
if (temp == NULL)
|
||||
{
|
||||
free(size);
|
||||
return (NULL);
|
||||
}
|
||||
out = ft_strmerger(5, temp, size, "x", size, ".xpm");
|
||||
free(size);
|
||||
free(temp);
|
||||
return (out);
|
||||
}
|
||||
|
||||
static char *ft_gen_xpm_content(t_square square)
|
||||
{
|
||||
char *content;
|
||||
char *temp;
|
||||
char *map;
|
||||
|
||||
temp = ft_itoa(square.size);
|
||||
if (temp == NULL)
|
||||
return (NULL);
|
||||
content = ft_strmerger(5, XPM_HEADER, temp, " ", temp, " 1 1\",\n\"");
|
||||
free(temp);
|
||||
if (content == NULL)
|
||||
return (NULL);
|
||||
temp = ft_strmerger(5, "/* XPM */", content, "a c ", square.color, "\",\n");
|
||||
free(content);
|
||||
if (temp == NULL)
|
||||
return (NULL);
|
||||
map = ft_square(square.size);
|
||||
if (map == NULL)
|
||||
{
|
||||
free(temp);
|
||||
return (NULL);
|
||||
}
|
||||
content = ft_strmerger(3, temp, map, "};");
|
||||
free(temp);
|
||||
free(map);
|
||||
return (content);
|
||||
}
|
||||
|
||||
char *ft_xpm_gen_file(t_square square)
|
||||
{
|
||||
char *file_content;
|
||||
char *path;
|
||||
int fd;
|
||||
|
||||
path = name_generator(square);
|
||||
if (path == NULL)
|
||||
return (NULL);
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
file_content = ft_gen_xpm_content(square);
|
||||
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
||||
write(fd, file_content, ft_strlen(file_content));
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
free(file_content);
|
||||
}
|
||||
else
|
||||
close(fd);
|
||||
return (path);
|
||||
}
|
Reference in New Issue
Block a user