parsewipper

This commit is contained in:
whaffman 2025-04-19 15:53:14 +02:00
parent ed4695dca4
commit df46797def
9 changed files with 415 additions and 199 deletions

2
.vscode/launch.json vendored
View File

@ -6,7 +6,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/debug/cub3D", // Replace with your executable path
"args": [],
"args": ["test.cub"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],

View File

@ -6,7 +6,7 @@
# By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/04/18 10:53:00 by whaffman ######## odam.nl #
# Updated: 2025/04/19 14:50:25 by whaffman ######## odam.nl #
# #
# **************************************************************************** #
@ -50,6 +50,8 @@ debug_CFLAGS = -Wall -Werror -Werror -g3 -DDEBUG -DDBG='fprintf(stderr, RED "DEB
asan_CFLAGS = -Wall -Werror -Werror -fsanitize=address,leak,undefined -g3
tsan_CFLAGS = -Wall -Werror -Werror -fsanitize=thread -g3
RUN_ARGS=test.cub
# Targets for each build configuration
define BUILD_TARGETS
$(1)_OBJ_PATH = $(BUILD_PATH)/$(1)/obj
@ -63,7 +65,7 @@ $(1): $(BUILD_PATH)/$(1)/$(NAME)
.PHONY: run_$(1)
run_$(1): $(1)
$$(info $$(bold)$$(green)Running $(1)$$(reset))
./$(BUILD_PATH)/$(1)/$(NAME)
./$(BUILD_PATH)/$(1)/$(NAME) $(RUN_ARGS)
$(BUILD_PATH)/$(1)/$(NAME): $(LIBFT) $(MLX42) $$($(1)_OBJECTS) Makefile
$$(info $$(bold)$$(green)Linking $(1) config$$(reset))

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* cub3d.h :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/04/17 20:13:46 by qmennen ### ########.fr */
/* Updated: 2025/04/19 14:46:34 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -45,5 +45,6 @@
# include "hooks.h"
# include "render.h"
# include "player.h"
# include "parser.h"
#endif

29
inc/parser.h Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parser.h :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/19 14:41:55 by whaffman #+# #+# */
/* Updated: 2025/04/19 14:46:21 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#ifndef PARSER_H
# define PARSER_H
# include "cub3d.h"
# include "libft.h"
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);
int parse_config_line(const char *line, t_map *map);
int map_width(char **lines);
int parse_tile(char c);
int parse_map(char **lines, t_map *map);
int parse_file(char *buffer, t_map *map);
void print_map(t_map *map);
#endif

View File

@ -6,37 +6,65 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
/* Updated: 2025/04/18 11:58:31 by whaffman ######## odam.nl */
/* Updated: 2025/04/19 14:47:19 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
# include "cub3d.h"
static int init_game(t_game **game)
{
if (!game_create(game))
return (FAILURE);
if (!player_create(game))
return (FAILURE);
if (!map_create(game))
return (FAILURE);
if (!keyboard_create(game))
return (FAILURE);
screen_center((*game)->screen);
mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
return (SUCCESS);
}
// static int init_game(t_game **game)
// {
// if (!game_create(game))
// return (FAILURE);
// if (!player_create(game))
// return (FAILURE);
// if (!map_create(game))
// return (FAILURE);
// if (!keyboard_create(game))
// return (FAILURE);
// screen_center((*game)->screen);
// mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
// mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
// return (SUCCESS);
// }
int main(void)
{
t_game *game;
// int main(void)
// {
// t_game *game;
errno = 0;
game = NULL;
init_game(&game);
perror("after init");
mlx_loop(game->screen->mlx);
game_terminate(game);
return (EXIT_SUCCESS);
// errno = 0;
// game = NULL;
// init_game(&game);
// perror("after init");
// mlx_loop(game->screen->mlx);
// game_terminate(game);
// return (EXIT_SUCCESS);
// }
int main(int argc, char **argv)
{
t_map map;
char *buffer;
if (argc != 2)
{
ft_putstr_fd("Usage: ./cub3d <map.cub>\n", 2);
return (1);
}
buffer = read_map_file(argv[1]);
if (!buffer)
{
perror("Error reading map file");
return (1);
}
if (parse_file(buffer, &map) == FAILURE)
{
ft_putstr_fd("Error parsing map file\n", 2);
free(buffer);
return (1);
}
print_map(&map);
free(buffer);
return (0);
}

View File

@ -57,22 +57,22 @@ t_map *get_temp_map(void)
return map;
}
void print_map(t_map *map)
{
const int chars[] = {'X', ' ', '.', '#', 'P'};
int i = 0;
while (i < map->height)
{
int j = 0;
while (j < map->width)
{
printf("%c ", chars[map->grid[i][j] + 2]);
j++;
}
printf("\n");
i++;
}
}
// void print_map(t_map *map)
// {
// const int chars[] = {'X', ' ', '.', '#', 'P'};
// int i = 0;
// while (i < map->height)
// {
// int j = 0;
// while (j < map->width)
// {
// printf("%c ", chars[map->grid[i][j] + 2]);
// j++;
// }
// printf("\n");
// i++;
// }
// }
void free_map(t_map **map)
{

View File

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

285
src/parser.c Normal file
View File

@ -0,0 +1,285 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parser.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/18 11:29:58 by whaffman #+# #+# */
/* Updated: 2025/04/19 15:50:18 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_map(t_map *map)
{
int i;
int j;
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");
i = 0;
while (i < map->height)
{
j = 0;
while (j < map->width)
{
if (map->grid[i][j] == TILE_WALL)
ft_putchar_fd('1', 1);
else if (map->grid[i][j] == TILE_EMPTY)
ft_putchar_fd('0', 1);
else if (map->grid[i][j] == TILE_PLAYER)
ft_putchar_fd('P', 1);
else
ft_putchar_fd(' ', 1);
j++;
}
ft_putchar_fd('\n', 1);
i++;
}
}

17
test.cub Normal file
View File

@ -0,0 +1,17 @@
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
10000000001
10000001011
1E000001011
1110000101
10000001 111
100000011101
100000000001
111111111111