From ed4695dca46e5256b9fdce2820ee317fe115d9e8 Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 18 Apr 2025 16:18:24 +0200 Subject: [PATCH] Parse WIP --- inc/types.h | 21 +++++--- src/map.c | 53 +++++++++---------- src/parse.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+), 35 deletions(-) create mode 100644 src/parse.c diff --git a/inc/types.h b/inc/types.h index b15e898..968b234 100644 --- a/inc/types.h +++ b/inc/types.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* types.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/04/17 19:38:44 by qmennen ### ########.fr */ +/* :::::::: */ +/* types.h :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ +/* Updated: 2025/04/18 12:15:00 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ typedef enum TILE { + TILE_VISITED = -2, TILE_VOID = -1, TILE_EMPTY = 0, TILE_WALL = 1, @@ -42,6 +43,12 @@ typedef struct s_map unsigned int width; unsigned int height; t_tile **grid; + char *NO_texture; + char *SO_texture; + char *WE_texture; + char *EA_texture; + int floor_color; + int ceiling_color; } t_map; typedef struct s_keyboard diff --git a/src/map.c b/src/map.c index ba5446c..6b7eb02 100644 --- a/src/map.c +++ b/src/map.c @@ -7,6 +7,8 @@ #define FAILURE 0 #define SUCCESS 1 + +t_map *get_temp_map(void) { const t_tile const_map[10][10] = { @@ -146,41 +148,36 @@ int enclosed_map(t_map *map) return SUCCESS; } -int main(void) -{ - t_map *map; +// int main(void) +// { +// t_map *map; - map = get_temp_map(); - if (!map) - { - fprintf(stderr, "Failed to allocate memory for map\n"); - return 1; - } - print_map(map); - if(!enclosed_map(map)) - fprintf(stderr, "NOT GOOD MAP FRIEND\n"); - else - fprintf(stderr, "YES, GOOD MAP FRIEND\n"); - free_map(&map); - return 0; -} +// map = get_temp_map(); +// if (!map) +// { +// fprintf(stderr, "Failed to allocate memory for map\n"); +// return 1; +// } +// print_map(map); +// if(!enclosed_map(map)) +// fprintf(stderr, "NOT GOOD MAP FRIEND\n"); +// else +// fprintf(stderr, "YES, GOOD MAP FRIEND\n"); +// free_map(&map); +// return 0; +// } int map_create(t_game **game) { t_map *map; - map = malloc(sizeof(t_map)); - if (!map) - return (FAILURE); - /** - * - * TEMP MAP - * - */ - map->width = 10; - map->height = 10; - map->grid = get_temp_map(); + map = get_temp_map(); + print_map(map); + if(!enclosed_map(map)) + fprintf(stderr, "NOT GOOD MAP FRIEND\n"); + else + fprintf(stderr, "YES, GOOD MAP FRIEND\n"); (*game)->map = map; return (SUCCESS); } diff --git a/src/parse.c b/src/parse.c new file mode 100644 index 0000000..9b7b7bf --- /dev/null +++ b/src/parse.c @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/18 11:29:58 by whaffman #+# #+# */ +/* Updated: 2025/04/18 15:20:37 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "libft.h" + +#include +#include +#include + +ssize_t get_file_size(const char *filename) +{ + int fd; + ssize_t size; + + fd = open(filename, O_RDONLY); + if (fd == -1) + return (-1); + size = read(fd, NULL, SIZE_MAX); + if (size == -1) + { + close(fd); + return (-1); + } + close(fd); + return (size); +} + +char *read_map_file(const char *filename) +{ + int fd; + ssize_t size; + ssize_t bytes_read; + char *buffer; + + fd = open(filename, O_RDONLY); + if (fd == -1) + return (NULL); + size = get_file_size(filename); + if (size == -1) + return (close(fd), NULL); + buffer = malloc(size + 1); + if (!buffer) + return (close(fd), NULL); + bytes_read = read(fd, buffer, size); + if (bytes_read == -1) + { + free(buffer); + close(fd); + return (NULL); + } + buffer[bytes_read] = '\0'; + close(fd); + return (buffer); +} + +int is_map_line(const char *line) +{ + if (!line || !*line) + return (FAILURE); + while (*line && ft_isspace(*line)) + line++; + if (!*line) + return (FAILURE); + if (ft_strchr(line, '1')) + return (SUCCESS); + return (FAILURE); +} + +int parse_color(const char *color_str) +{ + +} + +int valid_color(char *str) +{ + char **tokens; + int i; + + tokens = ft_split(str, ','); + if (!tokens || !tokens[0] || !tokens[1] || tokens[2]) + return (FAILURE); + i = 0; + while (tokens[i]) + { + if (!ft_isdigit(tokens[i][0])) + return (FAILURE); + i++; + } +} + + +int parse_config_line(const char *line, t_map *map) +{ + char **tokens; + + tokens = ft_split(line, ' '); + if (!tokens || !tokens[0] || !tokens[1] || tokens[2]) + return (FAILURE); + if (ft_strncmp(tokens[0], "NO", 2) == 0) + map->NO_texture = ft_strdup(tokens[1]); + else if (ft_strncmp(tokens[0], "SO", 2) == 0) + map->SO_texture = ft_strdup(tokens[1]); + else if (ft_strncmp(tokens[0], "WE", 2) == 0) + map->WE_texture = ft_strdup(tokens[1]); + else if (ft_strncmp(tokens[0], "EA", 2) == 0) + map->EA_texture = ft_strdup(tokens[1]); + else if (ft_strncmp(tokens[0], "F", 1) == 0) + map->floor_color = parse_color(tokens[1]); + else if (ft_strncmp(tokens[0], "C", 1) == 0) + map->ceiling_color = parse_color(tokens[1]); + else + { + ft_free_arr(tokens); + return (FAILURE); + } + ft_free_arr(tokens); + return (SUCCESS); +} + +int parse_file(char *buffer, t_map *map) +{ + char **lines; + + lines = ft_split(buffer, '\n'); + if (!lines) + return (FAILURE); + while (*lines && !is_map_line(*lines)) + { + if (**lines && !parse_config_line(*lines, map)) + return (free(lines), FAILURE); + lines++; + } + parse_map(lines, map); + free(lines); + return (SUCCESS); +}