ftsplit eruit en parse in losse files

This commit is contained in:
whaffman 2025-04-22 13:42:51 +02:00
parent 8ff1559ee1
commit 567fda00fb
19 changed files with 581 additions and 366 deletions

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* game.h :+: :+: :+: */ /* game.h :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */ /* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */
/* Updated: 2025/04/15 17:58:22 by qmennen ### ########.fr */ /* Updated: 2025/04/22 12:59:11 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,5 +18,6 @@
int game_create(t_game **game); int game_create(t_game **game);
void game_loop(void *param); void game_loop(void *param);
void game_terminate(t_game *game); void game_terminate(t_game *game);
void free_game(t_game **game);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/19 14:41:55 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 "cub3d.h"
# include "libft.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); char *read_map_file(const char *filename);
int is_map_line(const char *line); int is_map_line(const char *line);
unsigned int parse_color(const char *color_str); 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_map(char **lines, t_map *map);
int parse_file(char *buffer, t_map *map); int parse_file(char *buffer, t_map *map);
void print_config(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 #endif

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 15:52:44 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* game.c :+: :+: :+: */ /* game.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/04/17 20:01:19 by qmennen ### ########.fr */ /* Updated: 2025/04/22 12:58:50 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,6 +28,20 @@ int game_create(t_game **game)
return (SUCCESS); 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) void game_loop(void *param)
{ {
t_game *game; t_game *game;

171
src/map.c
View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* map.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:20:51 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:20:52 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -8,55 +20,6 @@
#define SUCCESS 1 #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) void print_map(t_map *map)
{ {
const int chars[] = {'X', ' ', '.', '#', 'P'}; const int chars[] = {'X', ' ', '.', '#', 'P'};
@ -137,6 +100,7 @@ int enclosed_map(t_map *map)
int x; int x;
int y; int y;
if (!map) if (!map)
return FAILURE; return FAILURE;
if (!find_player_or_empty(map, &x, &y)) 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) static int parse_args(int argc, char **argv, t_map *map)
{ {
char *buffer; char *buffer;
@ -193,11 +137,73 @@ static int parse_args(int argc, char **argv, t_map *map)
free(buffer); free(buffer);
return (SUCCESS); 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) int map_create(t_game **game, int argc, char **argv)
{ {
t_map *map; t_map *map;
t_tile **grid;
if (!game || !*game) if (!game || !*game)
return (FAILURE); return (FAILURE);
@ -209,30 +215,35 @@ int map_create(t_game **game, int argc, char **argv)
} }
parse_args(argc, argv, map); parse_args(argc, argv, map);
print_map(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)) if(!enclosed_map(map))
{ {
fprintf(stderr, "NOT GOOD MAP FRIEND\n"); fprintf(stderr, "NOT GOOD MAP FRIEND\n");
map_free(map); map_free(map);
return (FAILURE); return (FAILURE);
} }
grid_free(map->grid, map->height);
map->grid = grid;
print_config(map); print_config(map);
(*game)->map = map; (*game)->map = map;
return (SUCCESS); return (SUCCESS);
} }
void map_free(t_map *map) void map_free(t_map *map)
{ {
int i; grid_free(map->grid, map->height);
int j; //free(map->grid);
free(map->NO_texture);
i = 0; free(map->SO_texture);
while (i < map->height) free(map->WE_texture);
{ free(map->EA_texture);
j = 0;
free(map->grid[i]);
i++;
}
free(map->grid);
free(map); free(map);
} }

View File

@ -1,264 +0,0 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parser.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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 <fcntl.h>
#include <unistd.h>
#include <limits.h>
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);
}

27
src/parser/count_chars.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* count_chars.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:02:31 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:03:03 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
size_t count_chars(const char *str, char c)
{
size_t count;
count = 0;
while (*str)
{
if (*str == c)
count++;
str++;
}
return (count);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_file_size.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:04:07 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:06:41 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <fcntl.h>
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);
}

30
src/parser/is_map_line.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* is_map_line.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

29
src/parser/map_width.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* map_width.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

37
src/parser/parse_color.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_color.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_config_line.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

35
src/parser/parse_file.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_file.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

60
src/parser/parse_map.c Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_map.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

27
src/parser/parse_tile.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_tile.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* pointer_lines.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:03:24 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:17:28 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#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);
}

29
src/parser/print_config.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_config.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* read_map_file.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:07:09 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:07:56 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <unistd.h>
#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);
}

View File

@ -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 C 100,100,10
11111111111 11111111111