Cub3D
Loading...
Searching...
No Matches
parse_fc.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* parse_fc.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/10/07 01:17:05 by kamitsui #+# #+# */
9/* Updated: 2024/12/21 15:36:40 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
20static t_type_fc get_type_of_fc(const char *line, const char *key[])
21{
22 t_type_fc type;
23
24 type = ENUM_F;
25 while (type < ENUM_C)
26 {
27 if (is_key_line(line, key[type]) == true)
28 break ;
29 type++;
30 }
31 return (type);
32}
33
34static void set_fc_color(t_game *game, int *color)
35{
36 game->floor_color = color[ENUM_F];
37 game->ceiling_color = color[ENUM_C];
38 game->frame.ceiling_color = &game->ceiling_color;
39 game->frame.floor_color = &game->floor_color;
40}
41
42static int check_error_rgb_value(t_result result, const char rgb_type)
43{
44 if (result.value >= 0)
45 return (EXIT_SUCCESS);
46 if (result.value == -1)
47 ft_eprintf("%s%c: %s %s\n",
48 ERR_PROMPT, rgb_type, EMSG_RGB_MISS, result.err_msg);
49 if (result.value == -2)
50 {
51 ft_eprintf("%s%c: \"", ERR_PROMPT, rgb_type);
52 print_until_ch(STDERR_FILENO, result.err_msg, ',');
53 ft_eprintf("\" %s\n", EMSG_RGB_RANGE_OUT);
54 }
55 if (result.value == -3)
56 {
57 ft_eprintf("%s%c: \"", ERR_PROMPT, rgb_type);
58 print_until_ch(STDERR_FILENO, result.err_msg, ',');
59 ft_eprintf("\" %s\n", EMSG_RGB_NOT_NUM);
60 }
61 if (result.value == -4)
62 ft_eprintf("%s%c: %s %s\n",
63 ERR_PROMPT, rgb_type, EMSG_UP_TO_THREE_RGB, result.err_msg);
64 return (EXIT_FAILURE);
65}
66
67int get_fc_color(const char *first_word, int *color)
68{
69 const char *next_word;
70 char *str;
71 t_result result;
72
73 next_word = find_next_word(first_word);
74 if (next_word == NULL)
75 {
76 ft_eprintf("%s%c: %s\n",
77 ERR_PROMPT, *first_word, EMSG_RGB_EMPTY);
78 return (EXIT_FAILURE);
79 }
80 str = strdup_trimmed_line(next_word);
81 if (str == NULL)
82 return (EXIT_FAILURE);
83 result = get_rgb_color(str);
84 if (check_error_rgb_value(result, *first_word) != EXIT_SUCCESS)
85 {
86 free(str);
87 return (EXIT_FAILURE);
88 }
89 *color = result.value;
90 free(str);
91 return (EXIT_SUCCESS);
92}
93//Reference error_cub3d.h
94//# define EMSG_RGB_EMPTY "Empty RGB component"
98//
99//# define EMSG_RGB_MISS "Missing RGB component"
103//
104//# define EMSG_RGB_RANGE_OUT "Out of range (0 ~ 255)"
108//
109//# define EMSG_RGB_NOT_NUM "is not a number"
113//
114//# define EMSG_UP_TO_THREE_RGB "Up to three RGB value"
118
119int parse_fc(const char *line, t_parse *parse)
120{
121 t_type_fc type;
122 const char *key[2] = {"F ", "C "};
123 const int bit[2] = {BIT_F, BIT_C};
124 static int color[2] = {-1, -1};
125
126 parse->fc_info = (t_info){.key = key, .bit = bit};
127 type = get_type_of_fc(line, parse->fc_info.key);
128 if (check_duplicate_info(parse->flag, bit[type], line) != EXIT_SUCCESS)
129 return (EXIT_FAILURE);
130 if (get_fc_color(line, &color[type]) != EXIT_SUCCESS)
131 return (EXIT_FAILURE);
132 parse->flag |= parse->fc_info.bit[type];
133 if (check_for_not_matching_bit(parse->flag, BIT_F | BIT_C) == 0x00)
134 {
135 set_fc_color(parse->game, color);
136 parse->flag |= BIT_INIT_FC;
137 }
138 return (EXIT_SUCCESS);
139}
140 //debug_parse_fc(parse->game->debug.fd, color, "parse_fc()");
main game
Definition type_cub3d.h:264