Skip to content

Commit

Permalink
not working
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Jan 29, 2024
1 parent 7c4c0f0 commit 44486cf
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 18 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ SRCS = parser/check_elements.c \
set_textures.c \
game/floorcaster.c \
game/sprite.c \
game/sprite_utils.c
game/sprite_utils.c \
parser/lexer.c \
parser/lexer_utils.c

HEADER_DIR := include
HEADERS := meta.h \
Expand Down Expand Up @@ -116,6 +118,7 @@ fclean: clean tclean
$(MAKE) -C MLX42/build clean
$(MAKE) -C libft fclean
rm -f $(NAME)
rm -rf $(NAME).dSYM

re: fclean all

Expand Down
41 changes: 37 additions & 4 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jboeve <[email protected]>+#+ */
/* +#+ */
/* Created: 2023/11/01 20:07:37 by jboeve#+##+# */
/* Updated: 2024/01/25 16:46:54 by jboeve ######## odam.nl */
/* Updated: 2024/01/29 15:27:32 by yesimzaim ######## odam.nl */
/**/
/* ************************************************************************** */

Expand Down Expand Up @@ -171,13 +171,31 @@ typedef struct s_player {


typedef struct s_map {
char *map_element;
t_cell_type *level;
uint32_t width;
uint32_t height;
t_vec2u player_start;
char player_start_dir;
} t_map;

typedef struct s_flag
{
char *flag;
char *content;
struct s_flag *next;
} t_flag;


typedef struct s_lex {
t_flag n;
t_flag s;
t_flag e;
t_flag w;
t_flag c;
t_flag f;
} t_lex;

typedef struct s_sprite {
t_vec2d pos;
mlx_texture_t *tex;
Expand All @@ -202,13 +220,12 @@ typedef struct s_attr {
t_sprite sprites[SPRITE_COUNT];
} t_attr;


typedef struct s_minimap {
mlx_image_t *minimap_image;
mlx_image_t *ppos_image;
mlx_image_t *fps_image;
mlx_image_t *info_image;
} t_minimap;
} t_minimap;

typedef struct s_meta {
mlx_t *mlx;
Expand All @@ -220,8 +237,10 @@ typedef struct s_meta {
uint32_t fps;
t_map map;
t_attr attributes;
const char *scene_name;
const char *scene_name;
char *map_element;
t_lex lexer;
t_flag *extras;
} t_meta;


Expand Down Expand Up @@ -297,4 +316,18 @@ void print_ints_array(char *msg, int32_t *arr, uint32_t size);

void sprite_sort(double *sprite_dist, int32_t *sprite_order);


// lexer.c

int lexer(t_meta *meta, char *map_file);

// lexer_utils.c

void print_lexer_mandatory(t_lex *lexer);
void print_lexer_map(t_map *map);
void print_lexer_extras(t_flag *extras);

// parser.c


#endif
5 changes: 4 additions & 1 deletion include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# define NO_MAP "Map element is missing\n"
# define INV_COLOR_CODE "Please check color codes for F and C elements\nRGB values should be between 0-255\n"
# define MLX_ERR "MLX error occured"
# define INVALID_ELEMENT "There are invalid elements in the input file\n"
# include "meta.h"

typedef enum e_err {
Expand All @@ -48,6 +49,7 @@ MISSING_ELEMENTS,
MISSING_MAP,
COLOR_CODE_WRONG,
MLX_ERROR,
INV_ELE,
} t_err;


Expand All @@ -63,8 +65,9 @@ int find_index(t_meta *meta, uint32_t x, uint32_t y);


// parser.c
char *file_to_string(int fd);
char *read_file(int fd);
int map_ext(char *file);
int map_extension(char *file);
int parser(t_meta *meta, char *map_file);

// parse_map.c
Expand Down
7 changes: 7 additions & 0 deletions src/cub3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ int cub3d(int argc, char **argv)
if (argc != 2)
return (pr_err(ARG_ERR));
ft_bzero(&meta, sizeof(t_meta));

if (lexer(&meta, argv[1]))
return (EXIT_FAILURE);
print_lexer_mandatory(&meta.lexer);
print_lexer_map(&meta.map);
print_lexer_extras(meta.extras);
return 0;
if (parser(&meta, argv[1]))
return(meta_free(&meta), EXIT_FAILURE);
if (set_textures(&meta.attributes))
Expand Down
236 changes: 236 additions & 0 deletions src/parser/lexer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* lexer.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:30:18 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:18:50 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"
#include "parser.h"

char *extract_file(char *map_file)
{
int fd;
char *file = NULL;
if (map_extension(map_file)) // check map ext
return(pr_err(INV_EXT), NULL);
fd = open(map_file, O_RDONLY); // open file
if (fd == -1)
return (pr_err(INV_FILE), NULL);
file = file_to_string(fd);
close(fd);
if (!file)
return(pr_err(MALL_ERR), NULL);
return (file);
}

// mallocs the paths to NO, SO, WE, EA elements
char *get_val(char *file)
{
int i;
char *val;

i = 0;
skip_spaces(&file);
while (file[i] && file[i] != '\n')
i++;
if (i)
{
val = ft_substr(file, 0, i);
if (!val)
return (NULL);
return (val);
}
return (ft_strdup(""));
}

int input_lexer(t_lex *lex, char *file, int *skip)
{
char element[6] = {'N', 'S', 'W', 'E', 'F', 'C'};
char** content[6] = {&lex->n.content, &lex->s.content, &lex->w.content, &lex->e.content, &lex->c.content, &lex->f.content};
int i;

i = 0;
*skip = 1;
skip_spaces(&file);
while (i < 6)
{
if (*file && *file == element[i])
{
if (*content[i])
return (pr_err(DUP_ELEMENTS));
*content[i] = get_val(file + 2);
if (!(content[i]))
return (pr_err(MALL_ERR), EXIT_FAILURE);
if (!ft_strncmp(*content[i], "", 1))
return (pr_err(M_PATH), EXIT_FAILURE);
}
i++;
}
return (EXIT_SUCCESS);
}

bool mandatory_elements(t_lex *lex)
{
int i;
char* flags[6] = {lex->n.content, lex->s.content, lex->w.content, lex->e.content, lex->c.content, lex->f.content};

i = 0;
while (i < 6)
{
if (flags[i] == NULL)
return (false);
i++;
}
return (true);
}

int end_of_map(char *file)
{
int i;

i = 0;
while (file[i])
{
if (valid_map_char(file[i]) || file[i] == '\n')
i++;
if (only_spaces(file))
break ;
}
return (i);
}

int input_map_lexer(char *file, t_map *map)
{
int i = 0;

if (*file)
{
i = end_of_map(file);
map->map_element = ft_substr(file, 0, i);
if (!map->map_element)
return (pr_err(MALL_ERR));
return (EXIT_SUCCESS);
}

return (pr_err(MISSING_MAP));
}

void skip_map_element(char **file, int *skip)
{
*skip = 0;
while (*file)
{
if (is_map_element(*file))
skip_line(file);
if (only_spaces(*file) || !is_map_element(*file))
break ;
}
}

bool is_valid_extra(char *file)
{
if (*file && !ft_strncmp(file, "SP", 2))
return (true);
return (false);
}

int lexer_input_extra(t_flag *extras, char *file, int *skip)
{
t_flag *new_node;
new_node = malloc(sizeof(t_flag) * 1);
if (!new_node)
return (pr_err(MALL_ERR), EXIT_FAILURE);
new_node->content = get_val(file);
if (extras == NULL)
extras = new_node;
else
{
while (extras->next == NULL)
extras = extras->next;
extras->next = new_node;
}
*skip = 1;
return (EXIT_SUCCESS);
}

int lex(char *file, t_lex *lexer, t_map *map, t_flag *extras)
{
int exit_code;
int skip;

skip = 1;
exit_code = 0;
while (*file)
{
skip_spaces(&file);
if (is_valid_element(file))
{
exit_code = input_lexer(lexer, file, &skip);
if (exit_code)
printf("1\n");
}
else if (!only_spaces(file) && is_map_element(file))
{
if (map->map_element != NULL)
{
exit_code = pr_err(DUP_ELEMENTS);
if (exit_code)
printf("2.1\n");
}
else if (mandatory_elements(lexer))
{
exit_code = input_map_lexer(file, map);
if (exit_code)
printf("2.2\n");
}
else
{
exit_code = pr_err(FILE_ORDER);
if (exit_code)
printf("2.3\n");
}
if (exit_code)
printf("2\n");
skip_map_element(&file, &skip);
}
else if (is_valid_extra(file))
{
exit_code = lexer_input_extra(extras, file, &skip);
if (exit_code)
printf("3\n");
}
else if (!only_spaces(file))
{
exit_code = pr_err(INV_ELE);
if (exit_code)
printf("4\n");
}
else
skip = 1;
if (exit_code)
return (EXIT_FAILURE);
if (skip)
skip_line(&file);
}
return (EXIT_SUCCESS);
}

// read the file into one string and lexes the elements into a linked list
int lexer(t_meta *meta, char *map_file)
{
char *file = NULL;

meta->scene_name = map_file;
file = extract_file(map_file);
if (!file)
return(EXIT_FAILURE);
if (lex(file, &meta->lexer, &meta->map, meta->extras))
return (EXIT_FAILURE); //also free everything in case of error!
return (EXIT_SUCCESS);
}
Loading

0 comments on commit 44486cf

Please sign in to comment.