Cub3D
Loading...
Searching...
No Matches
cub3d.h
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* cub3d.h :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hnagasak <hnagasak@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/08/01 03:19:09 by hnagasak #+# #+# */
9/* Updated: 2024/12/21 12:13:21 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#ifndef CUB3D_H
14# define CUB3D_H
15
16// -------------- include ---------------
17// stadard libraries
18# include <unistd.h>
19# include <stddef.h>
20# include <stdio.h>
21# include <math.h>
22# include <unistd.h>
23# include <fcntl.h>
24
25// project specific header files
26# include "type_cub3d.h"
27# include "error_cub3d.h"
28# include <mlx.h>
29
30// own libraries
31# include "libft.h"
32# include "get_next_line.h"
33# include "ft_printf.h"
34
35# ifdef DEBUG
36# include "debug_cub3d.h"
37# endif
38
39// -------------- define ---------------
40// Cub3d game window
41# define WIN_WIDTH 1024
42# define WIN_HEIGHT 512
43# define MAP_SCALE 4
44# define IMG_3D_WIDTH 1024
45# define IMG_3D_HEIGHT WIN_HEIGHT
46# define IMG_2D_WIDTH 512
47# define IMG_2D_HEIGHT WIN_HEIGHT
48# define DISABLE_2D_MAP 0
49
50// limit of map
51# define MAX_ROWS 1000
52# define MAX_COLS 1000
53
54// Cub3d color
55# define COLOR_GREY 0xAAAAAA
56# define COLOR_WHITE 0xFFFFFF
57# define COLOR_RED 0xFF0000
58
59// key code
60# define KEY_W 119
61# define KEY_A 97
62# define KEY_S 115
63# define KEY_D 100
64# define KEY_LEFT 65361
65# define KEY_RIGHT 65363
66# define KEY_ESC 65307
67
68// ------------- perform_dda ----------------
69// ray_cast->grid_line
70# define START_POINT -1
71# define ON_VERTICAL_LINE 0
72# define ON_HORIZONTAL_LINE 1
73
74// -------------- function prototype ---------------
75
76/******************************
77 * main
78 ******************************/
79
80// srcs/
81// call by main
82int init_game(t_game *game, int argc, char *argv[]);
83int end_game(t_game *game);
84void render_frame(t_game *game);
85int handle_key_press(int keycode, t_game *game);
86int handle_mouse(int button, int x, int y, t_game *game);
87
88/******************************
89 * inittialize
90 ******************************/
91// srcs/
92// │
93// └ init_utils
94int init_mlx_window(t_game *game);
95int init_mlx_image(t_game *game);
96int init_cub_contents(t_game *game, char *filename);
97int parse_cubfile(t_parse *parse, t_game *game, const char *element);
98
99// srcs/init_utils/
100// │
101// └ read_cubfile_utils
102char *ft_strjoin_nullable(char *s1, char *s2);
103
104// srcs/init_utils/
105// │
106// └ parse_cubfile_utils
107
108// call by init_cub_contents()
109const char *find_next_element(const char *line);
110t_type_elem get_type_element(const char *line);
111typedef int (*t_parse_elem)(const char *, t_parse *);
112int parse_tex(const char *line, t_parse *parse);
113int parse_fc(const char *line, t_parse *parse);
114int parse_map(const char *line, t_parse *parse);
115
116// parse_general_func.c
117char *find_next_line(const char *contents);
118const char *find_next_word(const char *s);
119bool is_key_line(const char *line, const char *key);
120char *strdup_trimmed_line(const char *str);
121int print_until_nl(int fd, const char *str);
122int print_until_ch(int fd, const char *str, int c);
123int check_for_not_matching_bit(int variable, int flags_to_check);
124int check_duplicate_info(
125 int value, int flag_to_check, const char *line);
126
127// put_error_msg.c
128void put_error_msg(const char *entry, const char *msg);
129
130// call by parse_tex()
131int create_texture_image(
132 const char *line, t_parse *parse, t_type_wall type);
133
134// call by parse_fc()
135# define CONTINUE 2
136
137int get_fc_color(const char *first_word, int *color);
138t_result get_rgb_color(char *str);
139
140// srcs/init_utils/parse_cubfile_utils/
141// │
142// └ parse_map_utils
143
144// call by parse_map()
145typedef int (*t_parse_map)(const char *, t_parse *parse);
146int check_last_map(const char *line, t_parse *parse);
147int check_range_map(const char *line, t_parse *parse);
148int get_map_data(const char *line, t_parse *parse);
149char **split_lines(const char *str);
150int get_player_info(const char *line, t_parse *parse);
151int check_enclosed_by_walls(const char *line, t_parse *parse);
152
153// call by flood_fill_iterative()
154int push(t_stack *stack, t_point value);
155t_point pop(t_stack *stack);
156bool is_empty(t_stack *stack);
157
158// call by get_player_info()
159int find_player_and_set(
160 char **data, t_point grid, t_parse *parse, t_player *player);
161void set_direction(char direction, t_player *player);
162
163// call by check_enclosed_by_walls()
164t_bool flood_fill(t_map *map, int start_x, int start_y, bool **visited);
165// stack in flood_fill
166# define DFLT_STACK_SIZE 100
167
168// parse->flag
169# define BIT_NORTH 0x01 // 0000 0000 0001
170# define BIT_WEST 0x02 // 0000 0000 0010
171# define BIT_EAST 0x04 // 0000 0000 0100
172# define BIT_SOUTH 0x08 // 0000 0000 1000
173# define BIT_F 0x10 // 0000 0001 0000
174# define BIT_C 0x20 // 0000 0010 0000
175# define BIT_PLAYER 0x40 // 0000 0100 0000
176# define BIT_MAP 0x80 // 0000 1000 0000
177# define BIT_INIT_TEX 0x100 // 0001 0000 0000
178# define BIT_INIT_FC 0x200 // 0010 0000 0000
179# define BIT_INIT_MAP 0x400 // 0100 0000 0000
180
181/******************************
182 * draw_3d
183 ******************************/
184// srcs/
185// │
186// └ draw_3d_utils
187
188// ray cast process for drawing 3D image
189void init_ray(t_frame *frame, int x);
190void perform_dda(t_frame *frame, int x);
191void set_wall_slice(t_frame *frame, int x);
192void set_texture_x_coordinate(t_frame *frame, int x);
193void draw_vertical_line(t_frame *frame, int x);
194typedef void (*t_draw_3d_process)(t_frame *, int);
195
196// call by
197// draw_vertical_line() & debug_texture_y_coordinate_overflow()
198int get_texture_y_coordinate(t_frame *frame, int y);
199
200// call by debug_texture_coordinate()
201t_type_wall get_texture_direction(
202 int type_of_grid_line, t_vector ray_dir);
203
204/******************************
205 * draw_2d
206 ******************************/
207// srcs/
208// │
209// └ draw_2d_utils
210void draw_2d_wall(t_map *map, t_img *img_2d);
211void draw_2d_player(t_img *img_2d, t_player *player);
212
213// srcs/
214// │
215// └ draw_line_utils
216void draw_line(t_img *img, t_line *line);
217void init_plot(t_plot *plot, t_line *line);
218void init_color(t_clr *color, int start_color, int end_color);
219
220/******************************
221 * free
222 ******************************/
223// srcs/
224// │
225// └ free_utils
226void destroy_texture_image(void *mlx, t_texture *texture, int n);
227void *ft_free(void *ptr);
228void free_double_pointer(char **array);
229void free_double_pointer_n(char **array, int n);
230
231/******************************
232 * mlx
233 ******************************/
234// srcs/
235// │
236// └ mlx utils
237void my_mlx_pixel_put(t_img *img, int x, int y, int color);
238
239/******************************
240 * key handle
241 ******************************/
242// srcs/
243// │
244// └ keypress utils
245// call by handle_key_press()
246void quit_game(int keycode, t_game *game);
247void set_move_forward_flag(int keycode, t_game *game);
248void set_move_backward_flag(int keycode, t_game *game);
249void set_strafe_left_flag(int keycode, t_game *game);
250void set_strafe_right_flag(int keycode, t_game *game);
251void set_rotate_left_flag(int keycode, t_game *game);
252void set_rotate_right_flag(int keycode, t_game *game);
253void invalid_key(int keycode, t_game *game);
254typedef void (*t_handle_key_press)(int, t_game *);
255
256/******************************
257 * Update player's position & direction
258 ******************************/
259// update utils
260// srcs/
261// │
262// └ update utils
263# define MOVE_SPEED 0.1
264# define ROTATE_SPEED 0.05
265# define BIT_MOVE_FORWARD 0x01 // 0000 0001
266# define BIT_MOVE_BACKWARD 0x02 // 0000 0010
267# define BIT_STRAFE_LEFT 0x04 // 0000 0100
268# define BIT_STRAFE_RIGHT 0x08 // 0000 1000
269# define BIT_ROTATE_LEFT 0x10 // 0001 0000
270# define BIT_ROTATE_RIGHT 0x20 // 0010 0000
271void move_forward(t_map *map, t_player *player);
272void move_backward(t_map *map, t_player *player);
273void strafe_left(t_map *map, t_player *player);
274void strafe_right(t_map *map, t_player *player);
275void rotate_left(t_map *map, t_player *player);
276void rotate_right(t_map *map, t_player *player);
277typedef void (*t_moving_player)(t_map *, t_player *);
278
279bool is_hit_flag(int flag, int bit);
280bool is_collision_detection_x(
281 char **data, t_vector view_point, double move_amount);
282bool is_collision_detection_y(
283 char **data, t_vector view_point, double move_amount);
284
285#endif
single still image for render_frame
Definition type_cub3d.h:247
main game
Definition type_cub3d.h:264
3D or 2D or Texture image
Definition type_cub3d.h:82
game map
Definition type_cub3d.h:237
keyboard event flag
Definition type_cub3d.h:226
texture
Definition type_cub3d.h:94
vector
Definition type_cub3d.h:74