bonus and mandatory separation
This commit is contained in:
55
mandatory/asset.c
Normal file
55
mandatory/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/16 18:27:18 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);
|
||||
i++;
|
||||
}
|
||||
data->assets[NB_COLORS] = ft_gen_asset(colors[i],
|
||||
WINDOW_SIZE, data);
|
||||
if (data->assets[NB_COLORS] == NULL)
|
||||
return (1);
|
||||
ft_freer_tab_ultimate(1, colors);
|
||||
return (0);
|
||||
}
|
58
mandatory/draw.c
Normal file
58
mandatory/draw.c
Normal file
@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:38:37 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[0]);
|
||||
if (c == '1')
|
||||
return (data->assets[1]);
|
||||
if (c == 'E')
|
||||
return (data->assets[2]);
|
||||
if (c == 'P')
|
||||
return (data->assets[3]);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int ft_draw_map(t_data *data)
|
||||
{
|
||||
ssize_t tab[2];
|
||||
char **patern;
|
||||
void *img;
|
||||
|
||||
ft_fill_pos(data->map);
|
||||
patern = ft_get_player_map(*data->map);
|
||||
if (patern == NULL)
|
||||
return (1);
|
||||
ft_draw_img(data, data->assets[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);
|
||||
return (0);
|
||||
}
|
61
mandatory/key.c
Normal file
61
mandatory/key.c
Normal file
@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* key.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:39:43 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_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);
|
||||
}
|
||||
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
mandatory/main.c
Normal file
80
mandatory/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/16 18:23:34 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 ...");
|
||||
if (ft_gen_assets(data) == 1)
|
||||
{
|
||||
ft_printf("\rGenerating assets [ERROR]");
|
||||
ft_exit(data);
|
||||
}
|
||||
ft_printf("\rGenerating assets [FINISHED]\n");
|
||||
data->map = map;
|
||||
data->window = mlx_new_window(data->mlx, WINDOW_SIZE, WINDOW_SIZE, "long");
|
||||
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 + 1)
|
||||
{
|
||||
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
mandatory/map.c
Normal file
139
mandatory/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);
|
||||
}
|
121
mandatory/parsing.c
Normal file
121
mandatory/parsing.c
Normal file
@ -0,0 +1,121 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 20:14:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:40:12 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 (ft_is_in("01CEP", 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);
|
||||
}
|
73
mandatory/parsing2.c
Normal file
73
mandatory/parsing2.c
Normal file
@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/12 16:17:20 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 15:38:59 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solong.h"
|
||||
|
||||
static void ft_collector(char **patern, size_t x, size_t y)
|
||||
{
|
||||
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
mandatory/solong.h
Normal file
67
mandatory/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:30 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 "gold|white|pink|red|gray"
|
||||
# define NB_COLORS 4
|
||||
# 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 + 1];
|
||||
t_map *map;
|
||||
} t_data;
|
||||
|
||||
//void ft_ultimate_image_destroyer(t_data *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);
|
||||
char **ft_readfile(char *path);
|
||||
int ft_map_is_correct(t_map *map);
|
||||
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);
|
||||
void ft_draw_img(t_data *data, void *img, size_t x, size_t y);
|
||||
#endif
|
120
mandatory/xpm.c
Normal file
120
mandatory/xpm.c
Normal file
@ -0,0 +1,120 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* xpm.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 13:53:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/16 18:14:56 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);
|
||||
if (fd == -1)
|
||||
{
|
||||
free(path);
|
||||
return (NULL);
|
||||
}
|
||||
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