ca marche

This commit is contained in:
Camille Chauvet 2023-01-09 20:41:06 +01:00
parent b5315e3a0a
commit 195f7bc66a
19 changed files with 221 additions and 91 deletions

View File

@ -1,4 +1,4 @@
SRCS = asset.c color.c draw.c ft_freer.c main.c map.c parsing.c xpm.c
SRCS = asset.c color.c draw.c key.c main.c map.c parsing.c xpm.c
OBJS = ${SRCS:.c=.o}
@ -30,6 +30,7 @@ fclean: clean
make -C libftx fclean
make -C minilibx-linux clean
re: fclean all
re: fclean
make all
.PHONY: all clean fclean re

17
color.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 15:56:19 by cchauvet #+# #+# */
/* Updated: 2023/01/08 17:51:40 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 19:17:12 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,19 +16,18 @@ static int ft_textures_set(t_data *data, char **colors)
{
t_square square;
if (data->nb_color_swap + 5 > NB_COLORS)
data->nb_color_swap = 0;
square.color = colors[data->nb_color_swap++];
square.size = CASE_SIZE;
square.color = colors[ft_random_generator(0, 100)];
data->ecolor = name_generator(square);
square.color = colors[data->nb_color_swap++];
square.color = colors[ft_random_generator(0, 100)];
data->pcolor = name_generator(square);
square.color = colors[data->nb_color_swap++];
square.color = colors[ft_random_generator(0, 100)];
data->wcolor = name_generator(square);
square.color = colors[data->nb_color_swap++];
square.color = colors[ft_random_generator(0, 100)];
data->ccolor = name_generator(square);
square.color = colors[data->nb_color_swap++];
square.color = colors[ft_random_generator(0, 100)];
square.size = WINDOW_SIZE;
square.color = colors[data->nb_color_swap++];
square.color = colors[ft_random_generator(0, 100)];
data->bcolor = name_generator(square);
if (data->bcolor == NULL || data->wcolor == NULL
|| data->pcolor == NULL || data->ecolor == NULL || data->ccolor == NULL)

Binary file not shown.

11
draw.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 15:49:23 by cchauvet #+# #+# */
/* Updated: 2023/01/08 17:57:57 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 20:13:40 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@ static void ft_draw_xpm(t_data *data, char *img_path, size_t x, size_t y)
int bozo;
void *img;
bozo = CASE_SIZE;
img = mlx_xpm_file_to_image(data->mlx, img_path, &bozo, &bozo);
if (img == NULL)
return ;
@ -37,14 +38,16 @@ static char *ft_char2xpm(t_data data, char c)
return (NULL);
}
int ft_draw_map(t_data *data, t_map *map)
int ft_draw_map(t_data *data)
{
size_t x;
size_t y;
char **patern;
char *path;
patern = ft_get_player_map(*map);
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);
@ -61,6 +64,6 @@ int ft_draw_map(t_data *data, t_map *map)
}
y++;
}
//ft_freer_tab_ultimate(1, patern);
ft_freer_tab_ultimate(1, patern);
return (0);
}

57
key.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
/* Updated: 2023/01/09 20:38:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static void ft_case_update(char *new_pos, char *player_pos)
{
*new_pos = 'P';
*player_pos = '0';
}
void ft_move(t_data *data, char direction)
{
size_t x;
size_t y;
char *new_pos;
x = data->map->player_pos[0];
y = data->map->player_pos[1];
if (direction == 'L')
new_pos = &data->map->patern[x - 1][y];
if (direction == 'U')
new_pos = &data->map->patern[x][y + 1];
if (direction == 'D')
new_pos = &data->map->patern[x][y - 1];
if (direction == 'R')
new_pos = &data->map->patern[x + 1][y];
if (*new_pos == 'C')
data->map->nb_collectable++;
if (*new_pos == '0' || *new_pos == 'C')
ft_case_update(new_pos, &data->map->patern[y][x]);
ft_draw_map(data);
}
int ft_key(int keycode, t_data *data)
{
ft_printf("%d\n", keycode);
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');
return (0);
}

View File

@ -6,13 +6,13 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2023/01/08 18:08:14 by cchauvet ### ########.fr #
# Updated: 2023/01/09 19:04:49 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = clang
SRCS = ft_freer.c ft_is_in.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c
SRCS = ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c
OBJS = $(SRCS:.c=.o)

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:03:10 by cchauvet #+# #+# */
/* Updated: 2023/01/08 18:08:06 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 19:05:08 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,8 +14,12 @@
# define EXTRA_H
# include <stdarg.h>
# include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
# include "../libft/libft.h"
char *get_next_line(int fd);
size_t ft_random_generator(size_t start, size_t stop);
void ft_freer_tab_ultimate(size_t len, ...);
void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);

View File

@ -6,15 +6,14 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 15:01:40 by cchauvet #+# #+# */
/* Updated: 2023/01/08 15:39:31 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 17:57:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
#include "extra.h"
void ft_freer_ultimate(size_t len, ...)
{
va_list va;
size_t i;
@ -32,9 +31,9 @@ void ft_freer_tab(char **tab)
{
size_t i;
i = 0;
if (tab == NULL)
return ;
i = 0;
while (tab[i] != NULL)
{
free(tab[i]);

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_random_generator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 18:43:27 by cchauvet #+# #+# */
/* Updated: 2023/01/09 20:09:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
size_t ft_random_generator(size_t start, size_t stop)
{
int fd;
char *line;
int sum;
size_t i;
fd = open("/dev/random", O_RDONLY);
if (fd == -1)
return (0);
line = ft_calloc(sizeof(char), 10001);
if (line == NULL)
return (0);
read(fd, line, 10000);
sum = 0;
i = 0;
while (i < ft_strlen(line))
{
sum += (unsigned int) line[i];
i++;
}
free(line);
return (sum % (stop - start));
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:06:04 by cchauvet #+# #+# */
/* Updated: 2023/01/08 11:13:17 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 17:46:11 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@ char *ft_strfjoin(char *s1, char *s2)
size_t y;
char *out;
out = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 12:32:52 by cchauvet #+# #+# */
/* Updated: 2023/01/08 14:06:32 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 18:04:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@ char *ft_strgen(char c, size_t len)
char *out;
size_t i;
out = malloc((len + 1) * sizeof(char));
out = ft_calloc((len + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 18:58:48 by cchauvet #+# #+# */
/* Updated: 2023/01/05 18:58:59 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 17:45:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@ char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size)
char **new;
size_t i;
new = malloc(new_size * sizeof(char *));
new = ft_calloc(new_size, sizeof(char *));
if (new == NULL)
return (NULL);
i = 0;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/09/29 18:07:15 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 13:51:09 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/01/08 18:07:49 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 19:05:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@
int ft_printf(const char *format, ...);
char *get_next_line(int fd);
size_t ft_random_generator(size_t start, size_t stop);
void ft_freer_tab_ultimate(size_t len, ...);
void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);

33
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/12 17:20:17 by cchauvet #+# #+# */
/* Updated: 2023/01/08 18:00:03 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 20:39:28 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,28 +14,31 @@
int ft_initialised(char *path)
{
t_data data;
t_data *data;
t_map *map;
data = ft_calloc(1, sizeof(t_data));
if (data == NULL)
return (1);
map = ft_getmap(path);
if (map == NULL)
{
ft_printf("Map error\n");
return (1);
}
if (ft_color_changer(&data))
{
ft_printf("Memory error");
ft_freer_tab_ultimate(1, map);
return (1);
}
data.mlx = mlx_init();
data.window = mlx_new_window(data.mlx, WINDOW_SIZE, WINDOW_SIZE, "");
ft_draw_map(&data, map);
mlx_loop(data.mlx);
mlx_destroy_window(data.mlx, data.window);
mlx_destroy_display(data.mlx);
free(data.mlx);
data->map = map;
data->mlx = mlx_init();
data->window = mlx_new_window(data->mlx, WINDOW_SIZE, WINDOW_SIZE, "solong");
ft_draw_map(data);
mlx_key_hook(data->window, ft_key, data);
mlx_loop(data->mlx);
ft_freer_tab_ultimate(1, map->patern);
mlx_destroy_window(data->mlx, data->window);
mlx_destroy_display(data->mlx);
free(data->mlx);
ft_freer_ultimate(5, data->bcolor, data->pcolor, data->ccolor, data->wcolor, data->ecolor);
free(data);
free(map);
return (0);
}

38
map.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 13:51:49 by cchauvet #+# #+# */
/* Updated: 2023/01/08 14:48:51 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 19:56:25 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@ char **ft_readfile(char *path)
fd = open(path, O_RDONLY);
nb_line = 1;
map = malloc(sizeof(char *) * 2);
map = ft_calloc(sizeof(char *), 2);
if (map == NULL)
return (NULL);
map[0] = get_next_line(fd);
@ -36,15 +36,40 @@ char **ft_readfile(char *path)
return (map);
}
void ft_fill_pos(t_data *data)
{
size_t x;
size_t y;
y = 0;
while (data->map->patern[y] != NULL)
{
x = 0;
while (x < ft_strlen(data->map->patern[y]))
{
if (data->map->patern[y][x] == 'P')
{
data->map->player_pos[0] = x;
data->map->player_pos[1] = y;
}
if (data->map->patern[y][x] == 'E')
{
data->map->exit_pos[0] = x;
data->map->exit_pos[1] = y;
}
x++;
}
y++;
}
}
t_map *ft_getmap(char *path)
{
t_map *map;
map = malloc(sizeof(t_map));
map = ft_calloc(sizeof(t_map), 1);
if (map == NULL)
return (NULL);
map->exit[2] = 0;
map->player_pos[2] = 0;
map->patern = ft_readfile(path);
if (ft_map_is_correct(map) == 0)
return (NULL);
@ -90,7 +115,7 @@ char **ft_get_player_map(t_map map)
size_t y;
size_t i;
player_map = malloc(RENDER_DISTANCE * 2 + 2);
player_map = ft_calloc(RENDER_DISTANCE * 2 + 2, sizeof(char *));
if (player_map == NULL)
return (NULL);
i = 0;
@ -100,6 +125,7 @@ char **ft_get_player_map(t_map map)
player_map[y] = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
if (map.player_pos[1] + y >= RENDER_DISTANCE && map.patern[i] != NULL)
{
free(player_map[y]);
player_map[y] = ft_get_line(map.patern[i], map.player_pos[0]);
i++;
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 20:14:44 by cchauvet #+# #+# */
/* Updated: 2023/01/08 12:18:52 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 19:52:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -59,29 +59,28 @@ int ft_test_map_content1(t_map *map)
int ft_test_map_content2(t_map *map)
{
ssize_t cord[2];
size_t x;
size_t y;
size_t test1;
size_t test2;
cord[1] = -1;
while ((size_t) ++cord[1] < map->y_len)
test1 = 0;
test2 = 0;
y = 0;
while (y < map->y_len)
{
cord[0] = -1;
while ((size_t) ++cord[0] < map->x_len)
x = 0;
while (x < map->x_len)
{
if (map->patern[cord[1]][cord[0]] == 'E')
{
map->exit[0] = cord[0];
map->exit[1] = cord[1];
map->exit[2]++;
if (map->patern[y][x] == 'E')
test1++;
else if (map->patern[y][x] == 'P')
test2++;
x++;
}
else if (map->patern[cord[1]][cord[0]] == 'P')
{
map->player_pos[0] = cord[0];
map->player_pos[1] = cord[1];
map->player_pos[2]++;
y++;
}
}
}
if (map->player_pos[2] == 1 && map->exit[2] == 1)
if (test1 == 1 && test2 == 1)
return (1);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 16:20:03 by cchauvet #+# #+# */
/* Updated: 2023/01/08 18:07:30 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 20:36:28 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,8 +37,8 @@ typedef struct s_map
size_t y_len;
size_t nb_collectable;
char **patern;
size_t player_pos[3];
size_t exit[3];
size_t player_pos[2];
size_t exit_pos[2];
} t_map;
typedef struct s_data
@ -50,10 +50,11 @@ typedef struct s_data
char *ccolor;
char *wcolor;
char *ecolor;
size_t size;
size_t nb_color_swap;
t_map *map;
} t_data;
int ft_key(int key, t_data *data);
void ft_fill_pos(t_data *data);
char *name_generator(t_square square);
t_map *ft_getmap(char *path);
char *ft_gen_xpm_file(t_square);
@ -62,7 +63,7 @@ char **ft_readfile(char *path);
int ft_map_is_correct(t_map *map);
int ft_gen_assets(void);
char **ft_get_player_map(t_map map);
int ft_draw_map(t_data *data, t_map *map);
int ft_draw_map(t_data *data);
char *name_generator(t_square square);
int ft_color_changer(t_data *data);
#endif

24
xpm.c
View File

@ -6,37 +6,37 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 13:53:03 by cchauvet #+# #+# */
/* Updated: 2023/01/08 17:06:34 by cchauvet ### ########.fr */
/* Updated: 2023/01/09 13:14:06 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static char *ft_square(void)
static char *ft_square(size_t size)
{
char *map;
size_t x;
size_t y;
map = malloc(((CASE_SIZE + 4) * CASE_SIZE) * sizeof(char));
map = malloc(((size + 4) * size) * sizeof(char));
if (map == NULL)
return (map);
y = 0;
while (y < CASE_SIZE)
while (y < size)
{
map[(CASE_SIZE + 4) * y] = '"';
map[(size + 4) * y] = '"';
x = 0;
while (x < CASE_SIZE)
while (x < size)
{
map[y * (CASE_SIZE + 4) + x + 1] = 'a';
map[y * (size + 4) + x + 1] = 'a';
x++;
}
map[(CASE_SIZE + 4) * y + CASE_SIZE + 1] = '"';
map[(CASE_SIZE + 4) * y + CASE_SIZE + 2] = ',';
map[(CASE_SIZE + 4) * y + CASE_SIZE + 3] = '\n';
map[(size + 4) * y + size + 1] = '"';
map[(size + 4) * y + size + 2] = ',';
map[(size + 4) * y + size + 3] = '\n';
y++;
}
map[(CASE_SIZE + 4) * CASE_SIZE - 2] = '\0';
map[(size + 4) * size - 2] = '\0';
return (map);
}
@ -78,7 +78,7 @@ static char *ft_gen_xpm_content(t_square square)
free(content);
if (temp == NULL)
return (NULL);
map = ft_square();
map = ft_square(square.size);
if (map == NULL)
{
free(temp);