Parse WIP

This commit is contained in:
whaffman 2025-04-18 16:18:24 +02:00
parent 433c3b7d89
commit ed4695dca4
3 changed files with 185 additions and 35 deletions

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* types.h :+: :+: :+: */ /* types.h :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* 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/17 19:38:44 by qmennen ### ########.fr */ /* Updated: 2025/04/18 12:15:00 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,7 @@
typedef enum TILE typedef enum TILE
{ {
TILE_VISITED = -2,
TILE_VOID = -1, TILE_VOID = -1,
TILE_EMPTY = 0, TILE_EMPTY = 0,
TILE_WALL = 1, TILE_WALL = 1,
@ -42,6 +43,12 @@ typedef struct s_map
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
t_tile **grid; t_tile **grid;
char *NO_texture;
char *SO_texture;
char *WE_texture;
char *EA_texture;
int floor_color;
int ceiling_color;
} t_map; } t_map;
typedef struct s_keyboard typedef struct s_keyboard

View File

@ -7,6 +7,8 @@
#define FAILURE 0 #define FAILURE 0
#define SUCCESS 1 #define SUCCESS 1
t_map *get_temp_map(void)
{ {
const t_tile const_map[10][10] = const t_tile const_map[10][10] =
{ {
@ -146,41 +148,36 @@ int enclosed_map(t_map *map)
return SUCCESS; return SUCCESS;
} }
int main(void) // int main(void)
{ // {
t_map *map; // t_map *map;
map = get_temp_map(); // map = get_temp_map();
if (!map) // if (!map)
{ // {
fprintf(stderr, "Failed to allocate memory for map\n"); // fprintf(stderr, "Failed to allocate memory for map\n");
return 1; // return 1;
} // }
print_map(map); // print_map(map);
if(!enclosed_map(map)) // if(!enclosed_map(map))
fprintf(stderr, "NOT GOOD MAP FRIEND\n"); // fprintf(stderr, "NOT GOOD MAP FRIEND\n");
else // else
fprintf(stderr, "YES, GOOD MAP FRIEND\n"); // fprintf(stderr, "YES, GOOD MAP FRIEND\n");
free_map(&map); // free_map(&map);
return 0; // return 0;
} // }
int map_create(t_game **game) int map_create(t_game **game)
{ {
t_map *map; t_map *map;
map = malloc(sizeof(t_map)); map = get_temp_map();
if (!map) print_map(map);
return (FAILURE); if(!enclosed_map(map))
/** fprintf(stderr, "NOT GOOD MAP FRIEND\n");
* else
* TEMP MAP fprintf(stderr, "YES, GOOD MAP FRIEND\n");
*
*/
map->width = 10;
map->height = 10;
map->grid = get_temp_map();
(*game)->map = map; (*game)->map = map;
return (SUCCESS); return (SUCCESS);
} }

146
src/parse.c Normal file
View File

@ -0,0 +1,146 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* 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);
}