Skip to content

Commit

Permalink
Merge pull request #12 from ys-zm/parser
Browse files Browse the repository at this point in the history
added rectangle parser
  • Loading branch information
ys-zm authored Dec 14, 2023
2 parents b9bac48 + 0ebea4f commit 85942bd
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 95 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ RUN_CMD := ./$(NAME) test_maps/simple.cub

# CFLAGS += -Wall -Wextra -Werror
CFLAGS += -Wall -Wextra
# CFLAGS += -g -fsanitize=address
CFLAGS += -g -fsanitize=address
# CFLAGS += -g
# CFLAGS += -Ofast -flto -march=native

LIBFT := libft/build/libft.a
Expand Down
1 change: 1 addition & 0 deletions include/test_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h);
Binary file removed src/.render.c.swp
Binary file not shown.
6 changes: 3 additions & 3 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ t_cell_type MAP[] = {

static void init_map(t_map *m)
{
m->level = MAP;
m->width = MAP_WIDTH;
m->height = MAP_HEIGHT;
// m->level = MAP;
// m->width = MAP_WIDTH;
// m->height = MAP_HEIGHT;
}


Expand Down
34 changes: 33 additions & 1 deletion src/parser/check_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,49 @@ bool is_floor_exposed(t_meta *meta, char *map)
return (false);
}

t_cell_type find_enum_value(char c)
{
if (c == ' ')
{
return (MAP_EMPTY);
}
else if (c == '1')
{
return (MAP_WALL);
}
else
{
return (MAP_SPACE);
}
}

bool save_map(t_meta *meta, char *rect)
{
uint32_t i;

meta->map.level = malloc(sizeof(t_cell_type) * meta->map.width * meta->map.height);
if (!meta->map.level)
return (false);
i = 0;
while (rect[i])
{
meta->map.level[i] = find_enum_value(rect[i]);
i++;
}
return (true);
}
int check_map(t_meta *meta, char *rect)
{
if (!is_map_chars_valid(rect))
return (EXIT_FAILURE);
if (!save_start_pos(meta, rect))
return (pr_err(NO_PLAYER));
rect[find_index(meta, meta->player.position[VEC_X], meta->player.position[VEC_Y])] = '0';

if (flood_fill(meta, rect, meta->player.position[VEC_X], meta->player.position[VEC_Y]))
return (pr_err(INV_WALLS));
if (is_floor_exposed(meta, rect)) // maybe change to a warning?
return (pr_err(OUT_OF_BOUNDS));
if (!save_map(meta, rect))
return(pr_err(MALL_ERR));
return (EXIT_SUCCESS);
}
2 changes: 2 additions & 0 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/* ************************************************************************** */

#include "meta.h"
# include "test_utils.h"

char *read_file(int fd)
{
Expand Down Expand Up @@ -77,6 +78,7 @@ int parser(t_meta *meta, char *map_file)
return(pr_err(MALL_ERR), EXIT_FAILURE);
if (check_map(meta, rect))
return (free(rect), EXIT_FAILURE);
print_map_cell(meta->map.level, meta->map.width, meta->map.height);
free(rect);
return (EXIT_SUCCESS);
}
19 changes: 19 additions & 0 deletions src/test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ void print_map(char *map, uint32_t w, uint32_t h)
}

}

void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h)
{
size_t i = 0;
size_t k = 0;
size_t j;
while (i < h)
{
j = 0;
while (j < w)
{
printf("%d", level[k]);
k++;
j++;
}
printf("\n");
i++;
}
}
1 change: 1 addition & 0 deletions src/utils/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ void free_t_tex(t_tex *tex)
void meta_free(t_meta *meta)
{
free_t_tex(&(meta->tex));
free(meta->map.level);
}
90 changes: 0 additions & 90 deletions test.c

This file was deleted.

0 comments on commit 85942bd

Please sign in to comment.