Cub3D
Loading...
Searching...
No Matches
parse_cubfile.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* parse_cubfile.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hnagasak <hnagasak@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/11/30 16:00:10 by kamitsui #+# #+# */
9/* Updated: 2024/12/21 15:42:51 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15static bool is_find_element(const char *element)
16{
17 if (element == NULL)
18 {
19 ft_eprintf("%s%s\n", ERR_PROMPT, EMSG_EMPTY_CUB);
20 return (false);
21 }
22 return (true);
23}
24
25static int parse_element(
26 const char *element, t_parse *parse, t_type_elem type, t_game *game)
27{
28 static t_parse_elem func[3] = {parse_tex, parse_fc, parse_map};
29 static const int bit[3] = {BIT_INIT_TEX, BIT_INIT_FC, BIT_INIT_MAP};
30 const int tex_bit = BIT_NORTH | BIT_WEST | BIT_EAST | BIT_SOUTH;
31
32 if (check_duplicate_info(parse->flag, bit[type], element) != EXIT_SUCCESS)
33 return (EXIT_FAILURE);
34 if (func[type](element, parse) != EXIT_SUCCESS)
35 {
36 if (check_for_not_matching_bit(parse->flag, tex_bit) != 0x00)
37 destroy_texture_image(game->mlx, game->texture, parse->flag);
38 if (is_hit_flag(parse->flag, BIT_INIT_MAP) == true)
39 free_double_pointer(game->map.data);
40 return (EXIT_FAILURE);
41 }
42 return (EXIT_SUCCESS);
43}
44
45static int check_missing_cub_contents(int flag)
46{
47 int missing_bit;
48 const int success_bit = BIT_NORTH | BIT_WEST | BIT_EAST | BIT_SOUTH
49 | BIT_F | BIT_C | BIT_MAP;
50 const char *key[8] = {"NO", "WE", "EA", "SO", "F", "C", NULL, "MAP"};
51 int i;
52 int bit;
53
54 missing_bit = check_for_not_matching_bit(flag, success_bit);
55 if (missing_bit != 0x00)
56 {
57 i = 0;
58 bit = 0x01;
59 while (bit <= BIT_MAP)
60 {
61 if (missing_bit & bit)
62 ft_eprintf("%s%s: %s\n", ERR_PROMPT, key[i], EMSG_ENTRY_MISS);
63 i++;
64 bit <<= 1;
65 }
66 return (EXIT_FAILURE);
67 }
68 return (EXIT_SUCCESS);
69}
70
74int parse_cubfile(t_parse *parse, t_game *game, const char *element)
75{
76 t_type_elem type;
77
78 if (*element == '\n')
79 element = find_next_element(element);
80 if (is_find_element(element) == false)
81 return (EXIT_FAILURE);
82 while (element != NULL)
83 {
84 type = get_type_element(element);
85 if (type == ENUM_ELEMENT_ERR)
86 {
87 put_error_msg(element, EMSG_ENTRY_INVAL);
88 return (EXIT_FAILURE);
89 }
90 if (parse_element(element, parse, type, game) != EXIT_SUCCESS)
91 return (EXIT_FAILURE);
92 if (type == ENUM_MAP)
93 break ;
94 element = find_next_element(element);
95 }
96 if (check_missing_cub_contents(parse->flag) != EXIT_SUCCESS)
97 return (EXIT_FAILURE);
98 return (EXIT_SUCCESS);
99}
100 //debug_element_type(game->debug.fd, element, "parse_cubfile()");
main game
Definition type_cub3d.h:264