diff --git a/inc/game.h b/inc/game.h index dc5aa90..4b57704 100644 --- a/inc/game.h +++ b/inc/game.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* game.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */ -/* Updated: 2025/04/15 17:58:22 by qmennen ### ########.fr */ +/* :::::::: */ +/* game.h :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */ +/* Updated: 2025/04/22 12:59:11 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,5 +18,6 @@ int game_create(t_game **game); void game_loop(void *param); void game_terminate(t_game *game); +void free_game(t_game **game); #endif diff --git a/inc/parser.h b/inc/parser.h index 49326f5..11b59bc 100644 --- a/inc/parser.h +++ b/inc/parser.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/19 14:41:55 by whaffman #+# #+# */ -/* Updated: 2025/04/20 13:50:42 by whaffman ######## odam.nl */ +/* Updated: 2025/04/22 13:18:20 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,7 +15,9 @@ # include "cub3d.h" # include "libft.h" -ssize_t get_file_size(const char *filename); +size_t count_chars(const char *str, char c); + +ssize_t get_file_size(const char *filename); char *read_map_file(const char *filename); int is_map_line(const char *line); unsigned int parse_color(const char *color_str); @@ -25,5 +27,8 @@ int parse_tile(char c); int parse_map(char **lines, t_map *map); int parse_file(char *buffer, t_map *map); void print_config(t_map *map); +t_tile **copy_map(t_tile **grid, int width, int height); +char **pointer_lines(char *buffer, char c); + #endif \ No newline at end of file diff --git a/inc/types.h b/inc/types.h index 968b234..5be3803 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/04/18 12:15:00 by whaffman ######## odam.nl */ +/* Updated: 2025/04/22 11:28:34 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/game.c b/src/game.c index d74a9ea..81039f6 100644 --- a/src/game.c +++ b/src/game.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* game.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ -/* Updated: 2025/04/17 20:01:19 by qmennen ### ########.fr */ +/* :::::::: */ +/* game.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ +/* Updated: 2025/04/22 12:58:50 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,6 +28,20 @@ int game_create(t_game **game) return (SUCCESS); } +void free_game(t_game **game) +{ + if (game && *game) + { + if ((*game)->screen) + free((*game)->screen); + if ((*game)->player) + free((*game)->player); + if ((*game)->map) + free((*game)->map); + free(*game); + } +} + void game_loop(void *param) { t_game *game; diff --git a/src/map.c b/src/map.c index 51350bb..cf79e22 100644 --- a/src/map.c +++ b/src/map.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* map.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:20:51 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:20:52 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + #include #include #include @@ -8,55 +20,6 @@ #define SUCCESS 1 -// t_map *get_temp_map(void) -// { -// const t_tile const_map[10][10] = -// { -// {-1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, -// {1, 1, 0, 0, 0, 0, 0, 0, 0, 1}, -// {1, 0, 0, 0, 0, 0, 0, 1, 1, 1}, -// {1, 0, 0, 2, 0, 0, 0, 1, -1, 1}, -// {1, 1, 1, 1, 0, 1, 0, 1, -1, -1 }, -// {1, 1, -1, 1, 0, 1, 1, 1, -1, 1}, -// {1, 1, 1, 1, 0, 0, 0, 1, 1, 1}, -// {1, 0, 0, 0, 1, 0, 0, 1, 0, 1}, -// {1, 0, 0, 0, 1, 0, 0, 1, 0, 1}, -// {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} -// }; - -// t_tile **grid = malloc(10 * sizeof(int *)); -// if (!grid) -// return NULL; - -// for (int i = 0; i < 10; i++) -// { -// grid[i] = malloc(10 * sizeof(int)); -// if (!grid[i]) -// { -// for (int j = 0; j < i; j++) -// free(grid[j]); -// free(grid); -// return NULL; -// } -// for (int j = 0; j < 10; j++) -// { -// grid[i][j] = const_map[i][j]; -// } -// } -// t_map *map = malloc(sizeof(t_map)); -// if (!map) -// { -// for (int i = 0; i < 10; i++) -// free(grid[i]); -// free(grid); -// return NULL; -// } -// map->width = 10; -// map->height = 10; -// map->grid = grid; -// return map; -// } - void print_map(t_map *map) { const int chars[] = {'X', ' ', '.', '#', 'P'}; @@ -137,6 +100,7 @@ int enclosed_map(t_map *map) int x; int y; + if (!map) return FAILURE; if (!find_player_or_empty(map, &x, &y)) @@ -149,26 +113,6 @@ int enclosed_map(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; -// } - - static int parse_args(int argc, char **argv, t_map *map) { char *buffer; @@ -193,11 +137,73 @@ static int parse_args(int argc, char **argv, t_map *map) free(buffer); return (SUCCESS); } +t_tile **create_grid(int width, int height) +{ + t_tile **grid; + int i; + + grid = malloc(sizeof(t_tile *) * height); + if (!grid) + return (NULL); + i = 0; + while (i < height) + { + grid[i] = malloc(sizeof(t_tile) * width); + if (!grid[i]) + { + while (--i >= 0) + free(grid[i]); + free(grid); + return (NULL); + } + i++; + } + return (grid); +} + +t_tile **copy_map(t_tile** grid, int width, int height) +{ + t_tile **copy; + int i; + int j; + + copy = create_grid(width, height); + if (!copy) + return (NULL); + i = 0; + while (i < height) + { + j = 0; + while (j < width) + { + copy[i][j] = grid[i][j]; + j++; + } + i++; + } + return (copy); +} + +void grid_free(t_tile **grid, int height) +{ + int i; + + if (!grid) + return; + i = 0; + while (i < height) + { + free(grid[i]); + i++; + } + free(grid); +} int map_create(t_game **game, int argc, char **argv) { t_map *map; + t_tile **grid; if (!game || !*game) return (FAILURE); @@ -209,30 +215,35 @@ int map_create(t_game **game, int argc, char **argv) } parse_args(argc, argv, map); print_map(map); + grid = copy_map(map->grid, map->width, map->height); + if (!grid) + { + perror("Error copying map"); + free(map); + return (FAILURE); + } if(!enclosed_map(map)) { fprintf(stderr, "NOT GOOD MAP FRIEND\n"); map_free(map); return (FAILURE); } + grid_free(map->grid, map->height); + map->grid = grid; print_config(map); (*game)->map = map; return (SUCCESS); } + void map_free(t_map *map) { - int i; - int j; - - i = 0; - while (i < map->height) - { - j = 0; - free(map->grid[i]); - i++; - } - free(map->grid); + grid_free(map->grid, map->height); + //free(map->grid); + free(map->NO_texture); + free(map->SO_texture); + free(map->WE_texture); + free(map->EA_texture); free(map); } diff --git a/src/parser.c b/src/parser.c deleted file mode 100644 index 7655e2b..0000000 --- a/src/parser.c +++ /dev/null @@ -1,264 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* parser.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/04/18 11:29:58 by whaffman #+# #+# */ -/* Updated: 2025/04/20 13:51: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; - ssize_t read_size; - char buf[4096]; - - fd = open(filename, O_RDONLY); - if (fd == -1) - return (-1); - size = 0; - while ((read_size = read(fd, buf, sizeof(buf))) > 0) - { - size += read_size; - if (read_size == -1) - break; - } - 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 * sizeof(char) + 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); - while (*line) - { - if (!ft_strchr("1 ", *line)) - return (FAILURE); - line++; - } - return (SUCCESS); -} - -unsigned int parse_color(const char *color_str) -{ - char **tokens; - int r; - int g; - int b; - - tokens = ft_split(color_str, ','); - if (!tokens - || !(tokens[0] && ft_strlen(tokens[0]) <=3) - || !(tokens[1] && ft_strlen(tokens[1]) <=3) - || !(tokens[2] && ft_strlen(tokens[2]) <=3) - || tokens[3]) - return (FAILURE); - - r = ft_atoi(tokens[0]); - g = ft_atoi(tokens[1]); - b = ft_atoi(tokens[2]); - ft_free_arr(tokens); - if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) - return (FAILURE); - return ((r << 16) | (g << 8) | b); -} - -// 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 map_width(char **lines) -{ - int i; - int width; - - i = 0; - width = 0; - while (lines[i]) - { - if (ft_strlen(lines[i]) > width) - width = ft_strlen(lines[i]); - i++; - } - return (width); -} - - -int parse_tile(char c) -{ - if (c == '1') - return (TILE_WALL); - else if (c == '0') - return (TILE_EMPTY); - else if (c == ' ') - return (TILE_VOID); - else if (c == 'N' || c == 'S' || c == 'E' || c == 'W') - return (TILE_PLAYER); - else - return (-2); -} - -int parse_map(char **lines, t_map *map) -{ - int i; - int j; - - i = 0; - while (lines[i]) - { - i++; - } - map->height = i; - map->width = map_width(lines); - map->grid = malloc(sizeof(t_tile *) * (map->height + 1)); - if (!map->grid) - return (FAILURE); - i = 0; - while (i < map->height) - { - map->grid[i] = malloc(sizeof(t_tile) * (map->width + 1)); - if (!map->grid[i]) - return (FAILURE); - j = 0; - while (j < map->width) - { - if (j >= ft_strlen(lines[i])) - map->grid[i][j] = TILE_VOID; - else - map->grid[i][j] = parse_tile(lines[i][j]); - if (map->grid[i][j] == -2) - return (FAILURE); - j++; - } - i++; - } - map->grid[i] = NULL; - return (SUCCESS); -} - -int parse_file(char *buffer, t_map *map) -{ - char **lines; - int i; - - - lines = ft_split(buffer, '\n'); - if (!lines) - return (FAILURE); - i = 0; - while (lines[i] && !is_map_line(lines[i])) - { - printf("Parsing line: %s\n", lines[i]); - if (*lines[i] && !parse_config_line(lines[i], map)) - return (free(lines), FAILURE); - i++; - } - if (!parse_map(&lines[i], map)) - return (free(lines), FAILURE); - free(lines); - return (SUCCESS); -} - - -void print_config(t_map *map) -{ - printf("Map:\n"); - printf("Width: %d, Height: %d\n", map->width, map->height); - printf("Textures:\n"); - printf("NO: %s\n", map->NO_texture); - printf("SO: %s\n", map->SO_texture); - printf("WE: %s\n", map->WE_texture); - printf("EA: %s\n", map->EA_texture); - printf("Floor color: %u\n", map->floor_color); - printf("Ceiling color: %u\n", map->ceiling_color); - printf("Grid:\n"); - - print_map(map); -} diff --git a/src/parser/count_chars.c b/src/parser/count_chars.c new file mode 100644 index 0000000..8f11b08 --- /dev/null +++ b/src/parser/count_chars.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* count_chars.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:02:31 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:03:03 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +size_t count_chars(const char *str, char c) +{ + size_t count; + + count = 0; + while (*str) + { + if (*str == c) + count++; + str++; + } + return (count); +} \ No newline at end of file diff --git a/src/parser/get_file_size.c b/src/parser/get_file_size.c new file mode 100644 index 0000000..8f36bb8 --- /dev/null +++ b/src/parser/get_file_size.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* get_file_size.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:04:07 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:06:41 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include + +ssize_t get_file_size(const char *filename) +{ + int fd; + ssize_t size; + ssize_t read_size; + char buf[4096]; + + fd = open(filename, O_RDONLY); + if (fd == -1) + return (-1); + size = 0; + read_size = read(fd, buf, sizeof(buf)); + while (read_size > 0) + { + size += read_size; + if (read_size == -1) + break ; + read_size = read(fd, buf, sizeof(buf)); + } + close(fd); + return (size); +} \ No newline at end of file diff --git a/src/parser/is_map_line.c b/src/parser/is_map_line.c new file mode 100644 index 0000000..d95c0dd --- /dev/null +++ b/src/parser/is_map_line.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* is_map_line.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:08:26 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:08:37 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int is_map_line(const char *line) +{ + if (!line || !*line) + return (FAILURE); + while (*line && ft_isspace(*line)) + line++; + if (!*line) + return (FAILURE); + while (*line) + { + if (!ft_strchr("1 ", *line)) + return (FAILURE); + line++; + } + return (SUCCESS); +} \ No newline at end of file diff --git a/src/parser/map_width.c b/src/parser/map_width.c new file mode 100644 index 0000000..d9f2ebd --- /dev/null +++ b/src/parser/map_width.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* map_width.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:13:07 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:19:40 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int map_width(char **lines) +{ + int i; + int width; + + i = 0; + width = 0; + while (lines[i]) + { + if (ft_strlen(lines[i]) > width) + width = ft_strlen(lines[i]); + i++; + } + return (width); +} \ No newline at end of file diff --git a/src/parser/parse_color.c b/src/parser/parse_color.c new file mode 100644 index 0000000..0639f5c --- /dev/null +++ b/src/parser/parse_color.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_color.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:08:52 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:09:33 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +unsigned int parse_color(const char *color_str) +{ + char **tokens; + int r; + int g; + int b; + + tokens = ft_split(color_str, ','); + if (!tokens + || !(tokens[0] && ft_strlen(tokens[0]) <= 3) + || !(tokens[1] && ft_strlen(tokens[1]) <= 3) + || !(tokens[2] && ft_strlen(tokens[2]) <= 3) + || tokens[3]) + return (FAILURE); + + r = ft_atoi(tokens[0]); + g = ft_atoi(tokens[1]); + b = ft_atoi(tokens[2]); + ft_free_arr(tokens); + if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) + return (FAILURE); + return ((r << 16) | (g << 8) | b); +} \ No newline at end of file diff --git a/src/parser/parse_config_line.c b/src/parser/parse_config_line.c new file mode 100644 index 0000000..fce9e38 --- /dev/null +++ b/src/parser/parse_config_line.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_config_line.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:10:06 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:10:23 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +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); +} \ No newline at end of file diff --git a/src/parser/parse_file.c b/src/parser/parse_file.c new file mode 100644 index 0000000..c316932 --- /dev/null +++ b/src/parser/parse_file.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_file.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:11:37 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:11:46 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int parse_file(char *buffer, t_map *map) +{ + char **lines; + int i; + + lines = pointer_lines(buffer, '\n'); + if (!lines) + return (FAILURE); + i = 0; + while (lines[i] && !is_map_line(lines[i])) + { + printf("Parsing line: %s\n", lines[i]); + if (*lines[i] && !parse_config_line(lines[i], map)) + return (free(lines), FAILURE); + i++; + } + if (!parse_map(&lines[i], map)) + return (free(lines), FAILURE); + free(lines); + return (SUCCESS); +} \ No newline at end of file diff --git a/src/parser/parse_map.c b/src/parser/parse_map.c new file mode 100644 index 0000000..d316a4b --- /dev/null +++ b/src/parser/parse_map.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_map.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:19:21 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int parse_map_line(char **lines, t_map *map, int i) +{ + int j; + + map->grid[i] = malloc(sizeof(t_tile) * (map->width + 1)); + if (!map->grid[i]) + return (FAILURE); + j = 0; + while (j < map->width) + { + if (j >= ft_strlen(lines[i])) + map->grid[i][j] = TILE_VOID; + else + map->grid[i][j] = parse_tile(lines[i][j]); + if (map->grid[i][j] == -2) + return (FAILURE); + j++; + } + return (SUCCESS); +} + +int parse_map(char **lines, t_map *map) +{ + int i; + + i = 0; + while (lines[i]) + i++; + map->height = i; + map->width = map_width(lines); + map->grid = malloc(sizeof(t_tile *) * (map->height + 1)); + if (!map->grid) + return (FAILURE); + i = 0; + while (i < map->height) + { + if (parse_map_line(lines, map, i) == FAILURE) + { + map_free(map); + return (FAILURE); + } + i++; + } + map->grid[i] = NULL; + return (SUCCESS); +} diff --git a/src/parser/parse_tile.c b/src/parser/parse_tile.c new file mode 100644 index 0000000..425a029 --- /dev/null +++ b/src/parser/parse_tile.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_tile.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:12:31 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:12:56 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int parse_tile(char c) +{ + if (c == '1') + return (TILE_WALL); + else if (c == '0') + return (TILE_EMPTY); + else if (c == ' ') + return (TILE_VOID); + else if (c == 'N' || c == 'S' || c == 'E' || c == 'W') + return (TILE_PLAYER); + else + return (-2); +} \ No newline at end of file diff --git a/src/parser/pointer_lines.c b/src/parser/pointer_lines.c new file mode 100644 index 0000000..fcb9746 --- /dev/null +++ b/src/parser/pointer_lines.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* pointer_lines.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:03:24 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:17:28 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "cub3d.h" + +char **pointer_lines(char *buffer, char c) +{ + char **lines; + size_t count; + size_t i; + + count = count_chars(buffer, c) + 1; + lines = malloc(sizeof(char *) * (count + 1)); + if (!lines) + return (NULL); + lines[0] = buffer; + i = 1; + while (i < count) + { + lines[i] = ft_strchr(buffer, c); + if (lines[i]) + { + *lines[i] = '\0'; + lines[i] += 1; + buffer = lines[i]; + } + i++; + } + lines[i] = NULL; + return (lines); +} \ No newline at end of file diff --git a/src/parser/print_config.c b/src/parser/print_config.c new file mode 100644 index 0000000..8d37df4 --- /dev/null +++ b/src/parser/print_config.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* print_config.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:11:18 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:11:28 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +void print_config(t_map *map) +{ + printf("Map:\n"); + printf("Width: %d, Height: %d\n", map->width, map->height); + printf("Textures:\n"); + printf("NO: %s\n", map->NO_texture); + printf("SO: %s\n", map->SO_texture); + printf("WE: %s\n", map->WE_texture); + printf("EA: %s\n", map->EA_texture); + printf("Floor color: %u\n", map->floor_color); + printf("Ceiling color: %u\n", map->ceiling_color); + printf("Grid:\n"); + + print_map(map); +} \ No newline at end of file diff --git a/src/parser/read_map_file.c b/src/parser/read_map_file.c new file mode 100644 index 0000000..1c89215 --- /dev/null +++ b/src/parser/read_map_file.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* read_map_file.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:07:09 by whaffman #+# #+# */ +/* Updated: 2025/04/22 13:07:56 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + + +#include +#include + +#include "cub3d.h" + +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 * sizeof(char) + 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); +} \ No newline at end of file diff --git a/test.cub b/test.cub index fe1a819..3642e9a 100644 --- a/test.cub +++ b/test.cub @@ -1,9 +1,19 @@ -NO ./path/file.png -WE ./path/file.png -SO ./path/file.png -EA ./path/file.png -F 200,200,200 + +NO ./path/file.png + +WE ./path/file.png + +SO ./path/file.png + +EA ./path/file.png + +F 200,200,200 + + + + + C 100,100,10 11111111111