├── Makefile ├── cub3d_lect.pdf ├── includes ├── cub3d.h ├── hyochoi_cub3d.h ├── hyochoi_macros.h ├── minckim_cub3d.h ├── minckim_macros.h ├── mlx.h └── utils.h ├── map └── map.cub ├── mlx ├── Makefile ├── font.c ├── font.xcf ├── libmlx.a ├── mlx_init_loop.m ├── mlx_init_loop.o ├── mlx_int.h ├── mlx_int_str_to_wordtab.c ├── mlx_int_str_to_wordtab.o ├── mlx_mouse.m ├── mlx_mouse.o ├── mlx_new_image.m ├── mlx_new_image.o ├── mlx_new_window.h ├── mlx_new_window.m ├── mlx_new_window.o ├── mlx_opengl.h ├── mlx_opengl.m ├── mlx_png.c ├── mlx_png.h ├── mlx_png.o ├── mlx_rgb.c ├── mlx_shaders.c ├── mlx_shaders.o ├── mlx_xpm.c └── mlx_xpm.o ├── srcs ├── cub3d.c ├── draw_frame.c ├── init_all.c ├── parse_all.c ├── parse_tool.c ├── print_msg.c └── save_bmp.c ├── srcs_minckim ├── convert.c ├── equation.c ├── init_pixel.c ├── make_entity.c ├── play.c ├── player_move.c ├── raycast.c └── vector.c ├── textures ├── blue.xpm ├── brick.xpm ├── grey.xpm ├── tree.xpm └── wood.xpm └── utils ├── etc.c ├── ft_strdup.c ├── get_next_line.c ├── list.c └── str.c /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: hyochoi +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2020/09/10 13:28:31 by hyochoi #+# #+# # 9 | # Updated: 2020/09/27 18:24:24 by hyochoi ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = cub3D 14 | CC = gcc 15 | IMG = img.bmp 16 | LIBMLX_DIR = mlx 17 | MLX_FLAGS = -framework OpenGL -framework AppKit 18 | CFLAGS = -Wall -Wextra -Werror -g -O3 19 | INCS = includes 20 | SRCS_DIR = ./srcs/ 21 | SRCS_FILE = \ 22 | cub3d.c init_all.c parse_all.c \ 23 | parse_tool.c print_msg.c save_bmp.c draw_frame.c 24 | 25 | SRCS_MINCKIM_DIR = ./srcs_minckim/ 26 | SRCS_MINCKIM_FILE = \ 27 | convert.c vector.c make_entity.c init_pixel.c\ 28 | raycast.c equation.c play.c player_move.c 29 | 30 | SRCS = $(addprefix $(SRCS_DIR),$(SRCS_FILE)) \ 31 | $(addprefix $(SRCS_MINCKIM_DIR),$(SRCS_MINCKIM_FILE)) 32 | 33 | 34 | OBJS = $(SRCS:.c=.o) 35 | UTIL = ./utils/get_next_line.c ./utils/etc.c ./utils/list.c ./utils/str.c \ 36 | ./utils/ft_strdup.c 37 | UTIL_O = $(UTIL:.c=.o) 38 | HEADER = ./includes/cub3d.h ./includes/hyochoi_cub3d.h ./includes/utils.h \ 39 | ./includes/hyochoi_macros.h 40 | 41 | .PHONY: all clean fclean re norm 42 | 43 | all: $(NAME) 44 | 45 | %.o: %.c 46 | @$(CC) $(CFLAGS) -I$(INCS) -c $< -o $(<:.c=.o) 47 | 48 | $(NAME): $(OBJS) $(UTIL_O) 49 | @echo "\x1b[33m\nmaking libmlx.a ...\x1b[0m" 50 | @make all -s -C $(LIBMLX_DIR)/ 51 | @echo "\x1b[33m\ncompling srcs ...\x1b[0m" 52 | @$(CC) $(CFLAGS) $(OBJS) $(UTIL_O) -o $(NAME) -l$(LIBMLX_DIR) -L $(LIBMLX_DIR) $(MLX_FLAGS) 53 | @echo "\x1b[32m\ndone!\x1b[0m" 54 | @echo "\x1b[32m\nIf you want to check invalid maps simply, just\n\x1b[0m" 55 | @echo "\x1b[32mgit clone https://github.com/humblEgo/cub3D_map_tester.git\n\x1b[0m" 56 | @echo "\x1b[32mThis 'cub3D_map_tester' (by.iwoo) will help you.\n\x1b[0m" 57 | @echo "\x1b[32m\nUse map/map.cub to run cub3D (mandatory part)\n\x1b[0m" 58 | 59 | 60 | run: $(NAME) 61 | ./$(NAME) map/map.cub 62 | 63 | clean: 64 | @echo "\x1b[33m\ncleaning object files...\x1b[0m" 65 | @/bin/rm -f $(OBJS) $(UTIL_O) 66 | @echo "\x1b[32m\ndone!\n\x1b[0m" 67 | 68 | # @make clean -s -C $(LIBMLX_DIR)/ 69 | 70 | fclean: clean 71 | @echo "\x1b[33m\ncleaning libmlx and cub3D...\x1b[0m" 72 | @/bin/rm -f $(NAME) $(IMG) 73 | @echo "\x1b[32m\ndone!\n\x1b[0m" 74 | 75 | # @make fclean -s -C $(LIBMLX_DIR)/ 76 | 77 | re: fclean all 78 | 79 | norm: 80 | norminette $(SRCS) $(UTIL) $(HEADER) 81 | -------------------------------------------------------------------------------- /cub3d_lect.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/cub3d_lect.pdf -------------------------------------------------------------------------------- /includes/cub3d.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cub3d.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/17 23:04:50 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/27 18:06:55 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef CUB3D_H 14 | # define CUB3D_H 15 | 16 | # include "hyochoi_cub3d.h" 17 | # include "minckim_cub3d.h" 18 | 19 | #endif -------------------------------------------------------------------------------- /includes/hyochoi_cub3d.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* hyochoi_cub3d.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/17 23:06:43 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/27 18:19:32 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef HYOCHOI_CUB3D_H 14 | # define HYOCHOI_CUB3D_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include "utils.h" 25 | # include "mlx.h" 26 | 27 | #define TEXNUM_MAX 5 28 | 29 | /* 30 | ** ============================================================================ 31 | ** structures 32 | ** ============================================================================ 33 | */ 34 | typedef struct s_mlx 35 | { 36 | void *win; 37 | void *mlx; 38 | } t_mlx; 39 | 40 | typedef struct s_img 41 | { 42 | int w; 43 | int h; 44 | int bits_per_pixel; 45 | int size_line; 46 | int endian; 47 | void *ptr; 48 | unsigned int *addr; 49 | } t_img; 50 | 51 | typedef struct s_map 52 | { 53 | char info_num; 54 | char dir_init; 55 | int h; 56 | int color[2]; 57 | int color_flag[2]; 58 | int pl_num; 59 | t_list *list; 60 | char **map; 61 | } t_map; 62 | 63 | typedef struct s_player 64 | { 65 | double pos_x; 66 | double pos_y; 67 | } t_player; 68 | 69 | typedef struct s_key 70 | { 71 | char w; 72 | char s; 73 | char a; 74 | char d; 75 | char arr_l; 76 | char arr_r; 77 | } t_key; 78 | 79 | typedef struct s_all 80 | { 81 | t_key key; 82 | t_player pl; 83 | t_mlx mlx; 84 | t_map map; 85 | t_img img; 86 | t_img tex[TEXNUM_MAX]; 87 | } t_all; 88 | 89 | typedef struct s_mapcheck 90 | { 91 | int err_cnt; 92 | int len_for; 93 | int len_back; 94 | int len_cur; 95 | int n; 96 | } t_mapcheck; 97 | 98 | /* 99 | ** =========================================================================== 100 | ** init_all.c 101 | */ 102 | void init_all(t_all *a); 103 | /* 104 | ** =========================================================================== 105 | ** parse_all.c 106 | */ 107 | int parse_map(char *filename, t_all *a); 108 | /* 109 | ** =========================================================================== 110 | ** parse_tool.c 111 | */ 112 | int parse_tex(char *line, int num, t_all *a); 113 | int parse_resol(char *line, t_all *a); 114 | int parse_color(char *line, int n, t_all *a); 115 | int map_valid_check(t_all *a); 116 | /* 117 | ** =========================================================================== 118 | ** print_msg.c 119 | */ 120 | int error_msg(int num); 121 | /* 122 | ** =========================================================================== 123 | ** save_bmp.c 124 | */ 125 | # ifndef MINCKIM_STRUCT 126 | # define MINCKIM_STRUCT 127 | typedef struct s_vec{ 128 | double x; 129 | double y; 130 | } t_vec; 131 | 132 | typedef struct s_ray{ 133 | t_vec dir; 134 | double distance; 135 | } t_ray; 136 | 137 | typedef struct s_pixel{ 138 | double distance; 139 | unsigned int *color; 140 | } t_pixel; 141 | 142 | typedef struct s_screen{ 143 | void *mlx; 144 | void *win; 145 | t_img img; 146 | t_pixel **pixel; 147 | t_vec origin; 148 | t_vec dir; 149 | t_vec plane; 150 | double sin_unit; 151 | double cos_unit; 152 | double distance; 153 | t_ray *ray; 154 | int w; 155 | int h; 156 | } t_screen; 157 | 158 | typedef struct s_entity{ 159 | t_vec a; 160 | t_vec b; 161 | t_img *texture; 162 | } t_entity; 163 | 164 | typedef struct s_runtime{ 165 | t_screen screen; 166 | t_vec player_origin; 167 | t_vec player_dir; 168 | t_vec player_plane; 169 | t_img texture[TEXNUM_MAX]; 170 | t_entity *wall; 171 | t_entity *sprite; 172 | t_key key; 173 | int color_floor; 174 | int color_ceiling; 175 | } t_runtime; 176 | # endif 177 | t_screen *draw_frame(t_runtime *r); 178 | int save_bmp(t_runtime *r); 179 | 180 | #endif 181 | -------------------------------------------------------------------------------- /includes/hyochoi_macros.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* hyochoi_macros.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/08/16 17:16:42 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/24 23:35:36 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef HYOCHOI_MACROS_H 14 | # define HYOCHOI_MACROS_H 15 | 16 | # define WIN_W_MAX 2560 17 | # define WIN_H_MAX 1400 18 | # define WIN_W_MIN 640 19 | # define WIN_H_MIN 480 20 | 21 | # ifndef TEXNUM_MAX 22 | # define TEXNUM_MAX 5 23 | # endif 24 | # define INFO_PARSED 8 25 | # define SPTEX_MAX 'S' 26 | 27 | # define EAST 0 28 | # define NORTH 1 29 | # define SOUTH 2 30 | # define WEST 3 31 | # define SPRITE 4 32 | 33 | # define KEY_A 0 34 | # define KEY_S 1 35 | # define KEY_D 2 36 | # define KEY_W 13 37 | # define KEY_ESC 53 38 | # define KEY_ARR_L 123 39 | # define KEY_ARR_R 124 40 | 41 | # define SPACE 0 42 | # define NUM 1 43 | 44 | # define GAME 0 45 | # define SAVE 1 46 | 47 | # define INVALID_ARG -1 48 | # define OPEN_ERROR -2 49 | # define GNL_ERROR -3 50 | # define INVALID_MAP -4 51 | # define MALLOC_FAILED -5 52 | # define MULTIPLE_INFO -6 53 | # define LACKING_INFO -7 54 | # define MLX_ERROR -8 55 | # define INAPP_VALUE -9 56 | # define TOO_SMALL_RES -10 57 | # define NOT_SURROUNDED -11 58 | # define MULTI_PLAYER -12 59 | # define INVALID_COMP -13 60 | # define NO_PLAYER -14 61 | 62 | # define FLOOR 0 63 | # define CEIL 1 64 | 65 | # define PRINT_COLOR_RED "\x1b[31m" 66 | # define PRINT_COLOR_GREEN "\x1b[32m" 67 | # define PRINT_COLOR_YELLOW "\x1b[33m" 68 | # define PRINT_COLOR_BLUE "\x1b[34m" 69 | # define PRINT_COLOR_RESET "\x1b[0m" 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /includes/minckim_cub3d.h: -------------------------------------------------------------------------------- 1 | #ifndef MINCKIM_CUB3D_H 2 | # define MINCKIM_CUB3D_H 3 | # include 4 | # include 5 | # include "../includes/mlx.h" 6 | # include "hyochoi_cub3d.h" 7 | 8 | /* 9 | ** ============================================================================ 10 | ** structures 11 | ** ============================================================================ 12 | */ 13 | 14 | # ifndef MINCKIM_STRUCT 15 | # define MINCKIM_STRUCT 16 | typedef struct s_vec{ 17 | double x; 18 | double y; 19 | } t_vec; 20 | 21 | typedef struct s_ray{ 22 | t_vec dir; 23 | double distance; 24 | } t_ray; 25 | 26 | typedef struct s_pixel{ 27 | double distance; 28 | unsigned int *color; 29 | } t_pixel; 30 | 31 | typedef struct s_screen{ 32 | void *mlx; 33 | void *win; 34 | t_img img; 35 | t_pixel **pixel; 36 | t_vec origin; 37 | t_vec dir; 38 | t_vec plane; 39 | double sin_unit; 40 | double cos_unit; 41 | double distance; 42 | t_ray *ray; 43 | int w; 44 | int h; 45 | } t_screen; 46 | 47 | typedef struct s_entity{ 48 | t_vec a; 49 | t_vec b; 50 | t_img *texture; 51 | } t_entity; 52 | 53 | typedef struct s_runtime{ 54 | t_screen screen; 55 | t_vec player_origin; 56 | t_vec player_dir; 57 | t_vec player_plane; 58 | t_img texture[TEXNUM_MAX]; 59 | t_entity *wall; 60 | t_entity *sprite; 61 | t_key key; 62 | int color_floor; 63 | int color_ceiling; 64 | } t_runtime; 65 | # endif 66 | 67 | /* 68 | ** =========================================================================== 69 | ** m_convert.c 70 | */ 71 | void convert_data(); 72 | /* 73 | ** =========================================================================== 74 | ** m_init_pixel.c 75 | */ 76 | void img_put_color(); 77 | unsigned int img_pick_color(t_img *img, int x, int y); 78 | t_pixel **init_pixel(); 79 | /* 80 | ** =========================================================================== 81 | ** m_equation.c 82 | */ 83 | t_vec equation_solver(t_vec *coeff0, t_vec *coeff1, t_vec *constant); 84 | /* 85 | ** =========================================================================== 86 | ** m_make_entity.c 87 | */ 88 | t_entity *make_wall(); 89 | t_entity *make_sprite(); 90 | /* 91 | ** =========================================================================== 92 | ** m_play.c 93 | */ 94 | int play(t_runtime *r); 95 | /* 96 | ** =========================================================================== 97 | ** m_player_move.c 98 | */ 99 | int key_press_manager(int key, t_key *key_storage); 100 | int key_release_manager(int key, t_key *key_storage); 101 | int player_move(t_runtime *r); 102 | int cub_close(void); 103 | /* 104 | ** =========================================================================== 105 | ** m_raycast.c 106 | */ 107 | void draw_wall(t_screen *screen, t_entity *wall); 108 | void draw_sprite(t_screen *screen, t_entity *sprite); 109 | void draw_floor(t_screen *screen, int color_floor, int color_ceiling); 110 | /* 111 | ** =========================================================================== 112 | ** m_vector.c 113 | */ 114 | t_vec vec_new(double a, double b); 115 | t_vec vec_add(t_vec a, t_vec b); 116 | t_vec vec_sub(t_vec a, t_vec b); 117 | t_vec vec_rot_min_cw(t_vec a); 118 | t_vec vec_rot_min_ccw(t_vec a); 119 | t_vec vec_mul(t_vec a, double b); 120 | #endif -------------------------------------------------------------------------------- /includes/minckim_macros.h: -------------------------------------------------------------------------------- 1 | #include 2 | #define FOV M_PI_2 3 | 4 | # define EAST 0 5 | # define NORTH 1 6 | # define SOUTH 2 7 | # define WEST 3 8 | # define SPRITE 4 9 | 10 | # define WALL_W 1000 11 | # define WALL_H 1000 12 | 13 | 14 | # define FLOOR 0 15 | # define CEILING 1 16 | 17 | # define ANGLE_MIN 1 18 | # define MOVE_MIN 100 19 | 20 | 21 | # define COLOR_DEFAULT 0 22 | 23 | # define EYE_LEVEL 500 -------------------------------------------------------------------------------- /includes/mlx.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mlx.h for MinilibX in 3 | ** 4 | ** Made by Charlie Root 5 | ** Login 6 | ** 7 | ** Started on Mon Jul 31 16:37:50 2000 Charlie Root 8 | ** Last update Tue Oct 01 16:23:28 2014 Olivier Crouzet 9 | */ 10 | 11 | /* 12 | ** MinilibX - Please report bugs 13 | */ 14 | 15 | 16 | /* 17 | ** FR msg - FR msg - FR msg 18 | ** 19 | ** MacOSX 20 | ** La MinilibX utilise 2 frameworks Mac : OpenGL et AppKit 21 | ** qu'il faut ajouter a la compilation : 22 | ** -framework OpenGL -framework AppKit 23 | ** 24 | ** UNIX / Linux 25 | ** La MinilibX utilise 2 librairies supplementaires qu'il 26 | ** est necessaire de rajouter a la compilation : 27 | ** -lmlx -lXext -lX11 28 | ** 29 | ** La MinilibX permet le chargement des images de type Xpm. 30 | ** Notez que cette implementation est incomplete. 31 | ** 32 | ** Il y a des differences entre X11 et MacOS. 33 | ** les numeros des touches ne sont pas les memes, 34 | ** les numeros des boutons souris ne sont pas les memes. 35 | ** Egalement l'expose est gere differemment, et sous MacOS 36 | ** il est preferable d'entrer le plus tot possible dans mlx_loop, 37 | ** il est normal que les fenetres n'apparaissent pas avant mlx_loop 38 | ** (ou bien forcez avec mlx_do_sync mais c'est pas genial). 39 | ** Sous MacOS, l'octet Alpha est pris en compte dans toutes les 40 | ** images, et represente la transparence et non l'opacite comme 41 | ** c'est normalement le cas. 42 | */ 43 | 44 | 45 | #ifndef MLX_H 46 | 47 | #define MLX_H 48 | 49 | 50 | void *mlx_init(); 51 | /* 52 | ** needed before everything else. 53 | ** return (void *)0 if failed 54 | */ 55 | 56 | 57 | /* 58 | ** Basic actions 59 | */ 60 | 61 | void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title); 62 | /* 63 | ** return void *0 if failed 64 | */ 65 | int mlx_clear_window(void *mlx_ptr, void *win_ptr); 66 | int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color); 67 | /* 68 | ** origin for x & y is top left corner of the window 69 | ** y down is positive 70 | ** color is 0x00RRGGBB 71 | */ 72 | 73 | 74 | /* 75 | ** Image stuff 76 | */ 77 | 78 | void *mlx_new_image(void *mlx_ptr,int width,int height); 79 | /* 80 | ** return void *0 if failed 81 | */ 82 | char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel, 83 | int *size_line, int *endian); 84 | /* 85 | ** endian : 0 = sever X is little endian, 1 = big endian 86 | ** endian : useless on macos, client and graphical framework have the same endian 87 | */ 88 | int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr, 89 | int x, int y); 90 | unsigned int mlx_get_color_value(void *mlx_ptr, int color); 91 | 92 | 93 | /* 94 | ** dealing with Events 95 | */ 96 | 97 | int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param); 98 | int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param); 99 | int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param); 100 | 101 | int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param); 102 | int mlx_loop (void *mlx_ptr); 103 | 104 | 105 | /* 106 | ** hook funct are called as follow : 107 | ** 108 | ** expose_hook(void *param); 109 | ** key_hook(int keycode, void *param); 110 | ** mouse_hook(int button, int x,int y, void *param); 111 | ** loop_hook(void *param); 112 | ** 113 | */ 114 | 115 | 116 | /* 117 | ** Usually asked... 118 | */ 119 | 120 | int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color, 121 | char *string); 122 | void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data, 123 | int *width, int *height); 124 | void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename, 125 | int *width, int *height); 126 | void *mlx_png_file_to_image(void *mlx_ptr, char *file, int *width, int *height); 127 | 128 | int mlx_destroy_window(void *mlx_ptr, void *win_ptr); 129 | 130 | int mlx_destroy_image(void *mlx_ptr, void *img_ptr); 131 | 132 | /* 133 | ** generic hook system for all events, and minilibX functions that 134 | ** can be hooked. Some macro and defines from X11/X.h are needed here. 135 | */ 136 | 137 | int mlx_hook(void *win_ptr, int x_event, int x_mask, 138 | int (*funct)(), void *param); 139 | 140 | int mlx_mouse_hide(); 141 | int mlx_mouse_show(); 142 | int mlx_mouse_move(void *win_ptr, int x, int y); 143 | int mlx_mouse_get_pos(void *win_ptr, int *x, int *y); 144 | 145 | int mlx_do_key_autorepeatoff(void *mlx_ptr); 146 | int mlx_do_key_autorepeaton(void *mlx_ptr); 147 | int mlx_do_sync(void *mlx_ptr); 148 | 149 | #endif /* MLX_H */ 150 | -------------------------------------------------------------------------------- /includes/utils.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/08/18 15:49:23 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/18 00:09:03 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef UTILS_H 14 | # define UTILS_H 15 | 16 | # include 17 | 18 | typedef struct s_list 19 | { 20 | char *content; 21 | struct s_list *next; 22 | } t_list; 23 | 24 | int get_next_line(int fd, char **line); 25 | 26 | int ft_isblank(const char c); 27 | int is_blank_line(const char *s); 28 | int ft_atoi(const char *nptr); 29 | char *skip_(int flag, char *str); 30 | 31 | size_t ft_strlen(const char *s); 32 | int ft_strcmp(const char *s1, const char *s2); 33 | char *ft_strchr(const char *s, int c); 34 | char *ft_strndup(char *str, size_t len); 35 | char *ft_strjoin(char const *s1, char const *s2); 36 | 37 | int ft_lstsize(t_list *lst); 38 | void ft_lstadd_back(t_list **lst, t_list *new); 39 | t_list *ft_lstnew(char *content); 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /map/map.cub: -------------------------------------------------------------------------------- 1 | R 1280 720 2 | NO ./textures/brick.xpm 3 | SO ./textures/blue.xpm 4 | WE ./textures/wood.xpm 5 | EA ./textures/grey.xpm 6 | 7 | S ./textures/tree.xpm 8 | F 224,172,105 9 | C 205,249,255 10 | 11 | 11111111111111111111111111111 12 | 10000000001100000000000000001 13 | 10110000011100020000W00010001 14 | 1110120000000000000000000010001 15 | 1011000001110000000000021000111 16 | 10000000001100000111011110001 17 | 11110111111111011100000010001 18 | 11110111111111011101010000001 19 | 11000000110101011000000000001 20 | 10002000000000000000000000001 21 | 10000000000000001101010010001 22 | 11000000110101011111011110001 23 | 11110111111101011111011110001 24 | 11111111111111111111111111111 -------------------------------------------------------------------------------- /mlx/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: hyochoi +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2020/09/10 16:24:23 by hyochoi #+# #+# # 9 | # Updated: 2020/09/10 17:02:46 by hyochoi ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NOM=libmlx.a 14 | SRC= mlx_shaders.c mlx_new_window.m mlx_init_loop.m mlx_new_image.m mlx_xpm.c mlx_int_str_to_wordtab.c 15 | SRC+= mlx_png.c mlx_mouse.m 16 | OBJ1=$(SRC:.c=.o) 17 | OBJ=$(OBJ1:.m=.o) 18 | CFLAGS+=-O2 19 | 20 | # add to match string put with X11 in size and position 21 | CFLAGS+= -DSTRINGPUTX11 22 | 23 | .PHONY: all re fclean clean 24 | 25 | all: $(NOM) 26 | 27 | $(NOM): $(OBJ) 28 | @ar -r $(NOM) $(OBJ) 29 | @ranlib $(NOM) 30 | 31 | clean: 32 | @rm -f $(OBJ) *~ 33 | @rm -f mlx_app 34 | 35 | fclean: clean 36 | @rm -f $(NOM) 37 | 38 | re: fclean all 39 | -------------------------------------------------------------------------------- /mlx/font.xcf: -------------------------------------------------------------------------------- 1 | MX����@��� �!�L�!Th -------------------------------------------------------------------------------- /mlx/libmlx.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/libmlx.a -------------------------------------------------------------------------------- /mlx/mlx_init_loop.m: -------------------------------------------------------------------------------- 1 | // mlx_init_loop.m 2 | // By Ol 3 | 4 | #import 5 | #import 6 | #import 7 | 8 | #include "mlx_int.h" 9 | #include "mlx_new_window.h" 10 | 11 | #include "font.c" 12 | 13 | 14 | void do_loop_hook2(CFRunLoopTimerRef observer, void * info) 15 | { 16 | ((mlx_ptr_t *)info)->loop_hook(((mlx_ptr_t *)info)->loop_hook_data); 17 | } 18 | 19 | 20 | void do_loop_flush(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void * info) 21 | { 22 | mlx_ptr_t *mlx_ptr; 23 | mlx_win_list_t *win; 24 | 25 | mlx_ptr = (mlx_ptr_t *)info; 26 | win = mlx_ptr->win_list; 27 | while (win) 28 | { 29 | if (win->nb_flush > 0 && win->pixmgt) 30 | { 31 | [(id)win->winid selectGLContext]; 32 | [(id)win->winid mlx_gl_draw]; 33 | glFlush(); 34 | win->nb_flush = 0; 35 | } 36 | win = win->next; 37 | } 38 | } 39 | 40 | 41 | 42 | 43 | void *mlx_init() 44 | { 45 | mlx_ptr_t *new_mlx; 46 | int bidon; 47 | int i; 48 | 49 | if ((new_mlx = malloc(sizeof(*new_mlx))) == NULL) 50 | return ((void *)0); 51 | new_mlx->win_list = NULL; 52 | new_mlx->img_list = NULL; 53 | new_mlx->loop_hook = NULL; 54 | new_mlx->loop_hook_data = NULL; 55 | new_mlx->main_loop_active = 0; 56 | 57 | new_mlx->appid = [NSApplication sharedApplication]; 58 | 59 | // super magic trick to detach app from terminal, get menubar & key input events 60 | for (NSRunningApplication * app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"]) 61 | { 62 | [app activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 63 | break; 64 | } 65 | usleep(100000); 66 | ProcessSerialNumber psn = { 0, kCurrentProcess }; 67 | (void) TransformProcessType(&psn, kProcessTransformToForegroundApplication); 68 | usleep(100000); 69 | [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 70 | 71 | // load font 72 | new_mlx->font = mlx_new_image(new_mlx, (FONT_WIDTH+2)*95, FONT_HEIGHT); 73 | i = 0; 74 | while (i < 4*(FONT_WIDTH+2)*95*FONT_HEIGHT) 75 | { 76 | new_mlx->font->buffer[i+0] = font_atlas.pixel_data[i+2]; 77 | new_mlx->font->buffer[i+1] = font_atlas.pixel_data[i+1]; 78 | new_mlx->font->buffer[i+2] = font_atlas.pixel_data[i+0]; 79 | ((unsigned char *)new_mlx->font->buffer)[i+3] = 0xFF-font_atlas.pixel_data[i+3]; 80 | i += 4; 81 | } 82 | 83 | 84 | #ifdef STRINGPUTX11 85 | new_mlx->font->vertexes[2] = FONT_WIDTH/1.4; 86 | new_mlx->font->vertexes[4] = FONT_WIDTH/1.4; 87 | new_mlx->font->vertexes[5] = (-FONT_HEIGHT-1)/1.4; 88 | new_mlx->font->vertexes[7] = (-FONT_HEIGHT-1)/1.4; 89 | #else 90 | new_mlx->font->vertexes[2] = FONT_WIDTH; 91 | new_mlx->font->vertexes[4] = FONT_WIDTH; 92 | new_mlx->font->vertexes[5] = -FONT_HEIGHT-1; 93 | new_mlx->font->vertexes[7] = -FONT_HEIGHT-1; 94 | #endif 95 | 96 | return ((void *)new_mlx); 97 | } 98 | 99 | 100 | void mlx_loop(mlx_ptr_t *mlx_ptr) 101 | { 102 | CFRunLoopObserverRef observer; 103 | CFRunLoopObserverContext ocontext = {.version = 0, .info = mlx_ptr, .retain = NULL, .release = NULL, .copyDescription = NULL}; 104 | 105 | mlx_ptr->main_loop_active = 1; 106 | 107 | observer = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeTimers, true, 0, do_loop_flush, &ocontext); 108 | CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes); 109 | 110 | // [[[MlxLoopHookObj alloc] initWithPtr:mlx_ptr] performSelector:@selector(do_loop_hook) withObject:nil afterDelay:0.0]; 111 | 112 | [NSApp run]; 113 | } 114 | 115 | 116 | void mlx_pixel_put(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr, int x, int y, int color) 117 | { 118 | if (!win_ptr->pixmgt) 119 | return ; 120 | [(id)(win_ptr->winid) selectGLContext]; 121 | [(id)(win_ptr->winid) pixelPutColor:color X:x Y:y]; 122 | win_ptr->nb_flush ++; 123 | } 124 | 125 | 126 | void mlx_int_loop_once() 127 | { 128 | NSEvent *event; 129 | NSDate *thedate; 130 | 131 | thedate = [NSDate dateWithTimeIntervalSinceNow:0.1]; 132 | while (42) 133 | { 134 | event = [NSApp nextEventMatchingMask:NSEventMaskAny 135 | untilDate:thedate 136 | inMode:NSDefaultRunLoopMode 137 | dequeue:YES]; 138 | if (event == nil) 139 | { 140 | [thedate release]; 141 | return ; 142 | } 143 | [NSApp sendEvent:event]; 144 | [NSApp updateWindows]; 145 | } 146 | } 147 | 148 | 149 | int mlx_do_sync(mlx_ptr_t *mlx_ptr) 150 | { 151 | mlx_win_list_t *win; 152 | 153 | win = mlx_ptr->win_list; 154 | while (win) 155 | { 156 | if (win->pixmgt) 157 | { 158 | [(id)(win->winid) selectGLContext]; 159 | [(id)(win->winid) mlx_gl_draw]; 160 | glFlush(); 161 | if (!mlx_ptr->main_loop_active) 162 | mlx_int_loop_once(); 163 | } 164 | win = win->next; 165 | } 166 | return (0); 167 | } 168 | 169 | 170 | int mlx_loop_hook(mlx_ptr_t *mlx_ptr, void (*fct)(void *), void *param) 171 | { 172 | CFRunLoopTimerContext tcontext = {0, mlx_ptr, NULL, NULL, NULL}; 173 | CFRunLoopTimerRef timer; 174 | 175 | if (mlx_ptr->loop_hook != NULL) 176 | { 177 | CFRunLoopTimerInvalidate(mlx_ptr->loop_timer); 178 | [(id)(mlx_ptr->loop_timer) release]; 179 | } 180 | 181 | mlx_ptr->loop_hook = fct; 182 | mlx_ptr->loop_hook_data = param; 183 | 184 | if (fct) 185 | { 186 | timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.0, 0.0001, 0, 0, &do_loop_hook2, &tcontext); 187 | mlx_ptr->loop_timer = timer; 188 | CFRunLoopAddTimer(CFRunLoopGetMain(), timer, kCFRunLoopCommonModes); 189 | } 190 | 191 | return (0); 192 | } 193 | -------------------------------------------------------------------------------- /mlx/mlx_init_loop.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/mlx_init_loop.o -------------------------------------------------------------------------------- /mlx/mlx_int.h: -------------------------------------------------------------------------------- 1 | // 2 | // mlx_int.h for minilibx 3 | // 4 | // ol@staff.42.fr 5 | // 6 | // include opengl needed before mlx_int.h 7 | // 8 | 9 | 10 | #define MAX_EVENT 32 11 | #define MAX_PIXEL_NB 200000 12 | #define UNIQ_BPP 4 13 | 14 | #define FONT_WIDTH 10 15 | #define FONT_HEIGHT 20 16 | 17 | 18 | typedef int (*func_t)(); 19 | 20 | /* structs */ 21 | 22 | typedef struct glsl_info_s 23 | { 24 | GLuint pixel_vshader; 25 | GLuint pixel_fshader; 26 | GLuint pixel_program; 27 | GLint loc_pixel_position; 28 | GLint loc_pixel_texture; 29 | GLint loc_pixel_winhalfsize; 30 | 31 | GLuint image_vshader; 32 | GLuint image_fshader; 33 | GLuint image_program; 34 | GLint loc_image_position; 35 | GLint loc_image_winhalfsize; 36 | GLint loc_image_texture; 37 | GLint loc_image_pos; 38 | GLint loc_image_size; 39 | 40 | GLuint font_vshader; 41 | GLuint font_fshader; 42 | GLuint font_program; 43 | GLint loc_font_position; 44 | GLint loc_font_winhalfsize; 45 | GLint loc_font_texture; 46 | GLint loc_font_color; 47 | GLint loc_font_posinwin; 48 | GLint loc_font_posinatlas; 49 | GLint loc_font_atlassize; 50 | } glsl_info_t; 51 | 52 | 53 | typedef struct mlx_img_list_s 54 | { 55 | int width; 56 | int height; 57 | char *buffer; 58 | GLfloat vertexes[8]; 59 | struct mlx_img_list_s *next; 60 | } mlx_img_list_t; 61 | 62 | 63 | typedef struct mlx_img_ctx_s 64 | { 65 | GLuint texture; 66 | GLuint vbuffer; 67 | mlx_img_list_t *img; 68 | struct mlx_img_ctx_s *next; 69 | } mlx_img_ctx_t; 70 | 71 | typedef struct mlx_win_list_s 72 | { 73 | void *winid; 74 | mlx_img_ctx_t *img_list; 75 | int nb_flush; 76 | int pixmgt; 77 | struct mlx_win_list_s *next; 78 | } mlx_win_list_t; 79 | 80 | 81 | typedef struct mlx_ptr_s 82 | { 83 | void *appid; 84 | mlx_win_list_t *win_list; 85 | mlx_img_list_t *img_list; 86 | void (*loop_hook)(void *); 87 | void *loop_hook_data; 88 | void *loop_timer; 89 | mlx_img_list_t *font; 90 | int main_loop_active; 91 | } mlx_ptr_t; 92 | 93 | // proto 94 | 95 | int mlx_shaders(glsl_info_t *glsl); 96 | char **mlx_int_str_to_wordtab(char *str); 97 | int mlx_int_str_str(char *str,char *find,int len); 98 | int mlx_int_str_str_cote(char *str,char *find,int len); 99 | int mlx_destroy_image(mlx_ptr_t *mlx_ptr, mlx_img_list_t *img_ptr); 100 | void *mlx_new_image(); 101 | void *mlx_xpm_to_image(mlx_ptr_t *xvar,char **xpm_data,int *width,int *height); 102 | int mlx_do_sync(mlx_ptr_t *mlx_ptr); 103 | -------------------------------------------------------------------------------- /mlx/mlx_int_str_to_wordtab.c: -------------------------------------------------------------------------------- 1 | // 2 | // str 2 wordtab & co 3 | // by ol 4 | 5 | 6 | #include 7 | #include 8 | 9 | int mlx_int_str_str(char *str,char *find,int len) 10 | { 11 | int len_f; 12 | int pos; 13 | char *s; 14 | char *f; 15 | 16 | len_f = strlen(find); 17 | if (len_f>len) 18 | return (-1); 19 | pos = 0; 20 | while (*(str+len_f-1)) 21 | { 22 | s = str; 23 | f = find; 24 | while (*(f++) == *(s++)) 25 | if (!*f) 26 | return (pos); 27 | str ++; 28 | pos ++; 29 | } 30 | return (-1); 31 | } 32 | 33 | 34 | 35 | int mlx_int_str_str_cote(char *str,char *find,int len) 36 | { 37 | int len_f; 38 | int pos; 39 | char *s; 40 | char *f; 41 | int cote; 42 | 43 | len_f = strlen(find); 44 | if (len_f>len) 45 | return (-1); 46 | cote = 0; 47 | pos = 0; 48 | while (*(str+len_f-1)) 49 | { 50 | if (*str=='"') 51 | cote = 1-cote; 52 | if (!cote) 53 | { 54 | s = str; 55 | f = find; 56 | while (*(f++) == *(s++)) 57 | if (!*f) 58 | return (pos); 59 | } 60 | str ++; 61 | pos ++; 62 | } 63 | return (-1); 64 | } 65 | 66 | 67 | char **mlx_int_str_to_wordtab(char *str) 68 | { 69 | char **tab; 70 | int pos; 71 | int nb_word; 72 | int len; 73 | 74 | len = strlen(str); 75 | nb_word = 0; 76 | pos = 0; 77 | while (pos 2 | 3 | #import 4 | #import 5 | 6 | #include "mlx_int.h" 7 | #include "mlx_new_window.h" 8 | 9 | int mlx_mouse_hide() 10 | { 11 | // CGDisplayHideCursor(kCGDirectMainDisplay); 12 | [NSCursor hide]; 13 | return (0); 14 | } 15 | 16 | int mlx_mouse_show() 17 | { 18 | // CGDisplayShowCursor(kCGDirectMainDisplay); 19 | [NSCursor unhide]; 20 | return (0); 21 | } 22 | 23 | int mlx_mouse_move(mlx_win_list_t *win, int x, int y) 24 | { 25 | CGPoint point; 26 | NSRect pos; 27 | id thewin; 28 | 29 | thewin = [(id)(win->winid) win]; 30 | pos = [thewin frame]; 31 | // printf("got win pos %f %f\n", pos.origin.x, pos.origin.y); 32 | point.x = pos.origin.x + x; 33 | point.y = NSHeight([[thewin screen] frame]) - NSHeight([(id)(win->winid) frame]) - pos.origin.y + 1 + y; 34 | CGWarpMouseCursorPosition(point); 35 | CGAssociateMouseAndMouseCursorPosition(true); 36 | return (0); 37 | } 38 | 39 | 40 | int mlx_mouse_get_pos(mlx_win_list_t *win, int *x, int *y) 41 | { 42 | CGPoint point; 43 | id thewin; 44 | NSRect pos; 45 | 46 | thewin = [(id)(win->winid) win]; 47 | pos = [(id)(win->winid) frame]; 48 | point = [thewin mouseLocationOutsideOfEventStream]; 49 | *x = point.x; 50 | *y = NSHeight(pos) - 1 - point.y; 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /mlx/mlx_mouse.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/mlx_mouse.o -------------------------------------------------------------------------------- /mlx/mlx_new_image.m: -------------------------------------------------------------------------------- 1 | // mlx_new_image 2 | // 3 | // by Ol 4 | // 5 | 6 | 7 | #import 8 | #import 9 | 10 | #include "mlx_int.h" 11 | #include "mlx_new_window.h" 12 | 13 | 14 | 15 | void *mlx_new_image(mlx_ptr_t *mlx_ptr, int width, int height) 16 | { 17 | mlx_img_list_t *newimg; 18 | 19 | // if (mlx_ptr->win_list == NULL) 20 | // return (NULL); // need at leat one window created to have openGL context and create texture 21 | if ((newimg = malloc(sizeof(*newimg))) == NULL) 22 | return ((void *)0); 23 | newimg->next = mlx_ptr->img_list; 24 | mlx_ptr->img_list = newimg; 25 | newimg->width = width; 26 | newimg->height = height; 27 | newimg->vertexes[0] = 0.0; newimg->vertexes[1] = 0.0; 28 | newimg->vertexes[2] = width; newimg->vertexes[3] = 0.0; 29 | newimg->vertexes[4] = width; newimg->vertexes[5] = -height; 30 | newimg->vertexes[6] = 0.0; newimg->vertexes[7] = -height; 31 | newimg->buffer = malloc(UNIQ_BPP*width*height); 32 | bzero(newimg->buffer, UNIQ_BPP*width*height); 33 | 34 | return (newimg); 35 | } 36 | 37 | mlx_img_ctx_t *add_img_to_ctx(mlx_img_list_t *img, mlx_win_list_t *win) 38 | { 39 | mlx_img_ctx_t *imgctx; 40 | 41 | imgctx = win->img_list; 42 | while (imgctx) 43 | { 44 | if (imgctx->img == img) 45 | return (imgctx); 46 | imgctx = imgctx->next; 47 | } 48 | 49 | imgctx = malloc(sizeof(*imgctx)); 50 | imgctx->img = img; 51 | imgctx->next = win->img_list; 52 | win->img_list = imgctx; 53 | 54 | glGenTextures(1, &(imgctx->texture)); 55 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 57 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 58 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 59 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 60 | glTexImage2D( 61 | GL_TEXTURE_2D, 0, /* target, level of detail */ 62 | GL_RGBA8, /* internal format */ 63 | img->width, img->height, 0, /* width, height, border */ 64 | GL_BGRA, GL_UNSIGNED_BYTE, /* external format, type */ 65 | img->buffer /* pixels */ 66 | ); 67 | 68 | glGenBuffers(1, &(imgctx->vbuffer)); 69 | glBindBuffer(GL_ARRAY_BUFFER, imgctx->vbuffer); 70 | glBufferData(GL_ARRAY_BUFFER, sizeof(img->vertexes), img->vertexes, GL_DYNAMIC_DRAW); // 4 points buff 71 | 72 | return (imgctx); 73 | } 74 | 75 | 76 | void mlx_put_image_to_window(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr, mlx_img_list_t *img_ptr, int x, int y) 77 | { 78 | mlx_img_ctx_t *imgctx; 79 | 80 | if (!win_ptr->pixmgt) 81 | return ; 82 | 83 | [(id)(win_ptr->winid) selectGLContext]; 84 | imgctx = add_img_to_ctx(img_ptr, win_ptr); 85 | 86 | // update texture 87 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 88 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img_ptr->width, img_ptr->height, 0, 89 | GL_BGRA, GL_UNSIGNED_BYTE, img_ptr->buffer); 90 | 91 | [(id)(win_ptr->winid) mlx_gl_draw_img:img_ptr andCtx:imgctx andX:x andY:y]; 92 | 93 | win_ptr->nb_flush ++; 94 | } 95 | 96 | // assume here 32bpp little endian 97 | 98 | char *mlx_get_data_addr(mlx_img_list_t *img_ptr, int *bits_per_pixel, int *size_line, int *endian) 99 | { 100 | *bits_per_pixel = UNIQ_BPP*8; 101 | *size_line = img_ptr->width*UNIQ_BPP; 102 | *endian = 0; // little endian for now on mac-intel 103 | return (img_ptr->buffer); 104 | } 105 | 106 | unsigned int mlx_get_color_value(mlx_ptr_t *mlx_ptr, int color) 107 | { 108 | return (color); 109 | } 110 | 111 | int mlx_string_put(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr, int x, int y, int color, unsigned char *string) 112 | { 113 | mlx_img_ctx_t *imgctx; 114 | int gX; 115 | int gY; 116 | 117 | if (!win_ptr->pixmgt) 118 | return(0); 119 | 120 | #ifdef STRINGPUTX11 121 | y -= (FONT_HEIGHT * 2)/3; 122 | #endif 123 | 124 | [(id)(win_ptr->winid) selectGLContext]; 125 | 126 | imgctx = add_img_to_ctx(mlx_ptr->font, win_ptr); 127 | 128 | while (*string) 129 | { 130 | if (*string >= 32 && *string <= 127) 131 | { 132 | gX = (FONT_WIDTH+2)*(*string-32); 133 | gY = 0; 134 | // printf("put char %c pos %d %d\n", *string, gX, gY); 135 | [(id)(win_ptr->winid) mlx_gl_draw_font:mlx_ptr->font andCtx:imgctx andX:x andY:y andColor:color glyphX:gX glyphY:gY]; 136 | #ifdef STRINGPUTX11 137 | x += FONT_WIDTH/1.4; 138 | #else 139 | x += FONT_WIDTH; 140 | #endif 141 | } 142 | string ++; 143 | } 144 | 145 | win_ptr->nb_flush ++; 146 | 147 | return (0); 148 | } 149 | 150 | int mlx_destroy_image(mlx_ptr_t *mlx_ptr, mlx_img_list_t *img_todel) 151 | { 152 | mlx_img_ctx_t ctx_first; 153 | mlx_img_ctx_t *ctx; 154 | mlx_img_ctx_t *ctx_to_del; 155 | mlx_img_list_t img_first; 156 | mlx_img_list_t *img; 157 | mlx_win_list_t *win; 158 | 159 | img_first.next = mlx_ptr->img_list; 160 | img = &img_first; 161 | while (img && img->next) 162 | { 163 | if (img->next == img_todel) 164 | img->next = img->next->next; 165 | img = img->next; 166 | } 167 | mlx_ptr->img_list = img_first.next; 168 | 169 | 170 | win = mlx_ptr->win_list; 171 | while (win) 172 | { 173 | ctx_first.next = win->img_list; 174 | ctx = &ctx_first; 175 | while (ctx && ctx->next) 176 | { 177 | if (ctx->next->img == img_todel) 178 | { 179 | [(id)(win->winid) selectGLContext]; 180 | glDeleteBuffers(1, &(ctx->next->vbuffer)); 181 | glDeleteTextures(1, &(ctx->next->texture)); 182 | ctx_to_del = ctx->next; 183 | ctx->next = ctx->next->next; 184 | free(ctx_to_del); 185 | } 186 | ctx = ctx->next; 187 | } 188 | win->img_list = ctx_first.next; 189 | win = win->next; 190 | } 191 | 192 | 193 | free(img_todel->buffer); 194 | free(img_todel); 195 | 196 | // printf("destroy image done.\n"); 197 | return (0); 198 | } 199 | -------------------------------------------------------------------------------- /mlx/mlx_new_image.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/mlx_new_image.o -------------------------------------------------------------------------------- /mlx/mlx_new_window.h: -------------------------------------------------------------------------------- 1 | // 2 | // mlx_int.h for minilibx 3 | // 4 | // ol@staff.42.fr 5 | // 6 | // include opengl needed before mlx_int.h 7 | // 8 | 9 | #import 10 | #import "mlx_int.h" 11 | 12 | @interface NSWindowEvent : NSWindow 13 | { 14 | func_t event_funct[MAX_EVENT]; 15 | void *(event_param[MAX_EVENT]); 16 | int keyrepeat; 17 | int keyflag; 18 | int size_x; 19 | int size_y; 20 | } 21 | - (NSWindowEvent *) initWithContentRect:(NSRect)rect styleMask:(NSUInteger)winstyle backing:(NSBackingStoreType)bck defer:(BOOL) dfr; 22 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param; 23 | - (void) setKeyRepeat:(int)mode; 24 | - (void) exposeNotification:(NSNotification *)note; 25 | - (void) closeNotification:(NSNotification *)note; 26 | @end 27 | 28 | 29 | @interface MlxWin : NSOpenGLView 30 | { 31 | NSWindowEvent *win; 32 | NSOpenGLContext *ctx; 33 | glsl_info_t glsl; 34 | int openglwin; 35 | 36 | int size_x; 37 | int size_y; 38 | 39 | int pixel_nb; 40 | GLuint pixel_vbuffer; 41 | GLuint pixel_texture; 42 | unsigned int *pixtexbuff; 43 | } 44 | 45 | - (id) initWithRect: (NSRect)rect andTitle: (NSString *)title pfaAttrs: (NSOpenGLPixelFormatAttribute *)attrs; 46 | - (void) selectGLContext; 47 | - (void) flushGLContext; 48 | - (void) pixelPutColor: (int)color X:(int)x Y:(int)y; 49 | - (void) mlx_gl_draw; 50 | - (void) mlx_gl_draw_img:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y; 51 | - (void) mlx_gl_draw_font:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y andColor:(int)color glyphX:(int)gx glyphY:(int)gy; 52 | - (NSOpenGLContext *) ctx; 53 | - (NSWindowEvent *) win; 54 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param; 55 | - (void) setKeyRepeat:(int)mode; 56 | - (void) ctxNeedsUpdate; 57 | @end 58 | -------------------------------------------------------------------------------- /mlx/mlx_new_window.m: -------------------------------------------------------------------------------- 1 | // mlx_new_window.m 2 | 3 | #import 4 | #import 5 | #import 6 | 7 | #include 8 | #include 9 | 10 | #include "mlx_int.h" 11 | #include "mlx_new_window.h" 12 | 13 | 14 | NSOpenGLPixelFormatAttribute pfa_attrs[] = 15 | { 16 | NSOpenGLPFADepthSize, 32, 17 | NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy, 18 | 0 19 | }; 20 | 21 | static const GLfloat pixel_vertexes[8] = 22 | { 23 | -1.0 , -1.0, 24 | 1.0, -1.0, 25 | 1.0, 1.0, 26 | -1.0, 1.0 27 | }; 28 | 29 | 30 | 31 | int get_mouse_button(NSEventType eventtype) 32 | { 33 | switch (eventtype) { 34 | case NSEventTypeLeftMouseDown: 35 | case NSEventTypeLeftMouseUp: 36 | case NSEventTypeLeftMouseDragged: 37 | return 1; 38 | case NSEventTypeRightMouseDown: 39 | case NSEventTypeRightMouseUp: 40 | case NSEventTypeRightMouseDragged: 41 | return 2; 42 | case NSEventTypeOtherMouseDown: 43 | case NSEventTypeOtherMouseUp: 44 | case NSEventTypeOtherMouseDragged: 45 | return 3; 46 | default: 47 | return 0; 48 | } 49 | } 50 | 51 | 52 | // classes for window & events 53 | 54 | 55 | 56 | 57 | @implementation NSWindowEvent 58 | 59 | - (NSWindowEvent *) initWithContentRect:(NSRect)rect styleMask:(NSUInteger)winstyle backing:(NSBackingStoreType)bck defer:(BOOL) dfr 60 | { 61 | int i; 62 | 63 | if ((self = [super initWithContentRect:rect 64 | styleMask:winstyle 65 | backing:bck 66 | defer:dfr])) 67 | { 68 | i = MAX_EVENT; 69 | while (i--) 70 | { 71 | event_funct[i] = NULL; 72 | event_param[i] = NULL; 73 | } 74 | keyrepeat = 0; 75 | keyflag = 0; 76 | size_x = rect.size.width; 77 | size_y = rect.size.height; 78 | } 79 | return (self); 80 | } 81 | 82 | 83 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param 84 | { 85 | event_funct[event] = func; 86 | event_param[event] = param; 87 | if (event == 6 || event == 32) // motion notify && high precision motion notify 88 | { 89 | if (func == NULL) 90 | [self setAcceptsMouseMovedEvents:NO]; 91 | else 92 | [self setAcceptsMouseMovedEvents:YES]; 93 | } 94 | } 95 | 96 | 97 | - (void) setKeyRepeat:(int)mode 98 | { 99 | keyrepeat = mode; 100 | } 101 | 102 | - (BOOL) acceptsFirstResponder 103 | { 104 | return (YES); 105 | } 106 | 107 | - (void) flagsChanged:(NSEvent *)theEvent 108 | { 109 | unsigned int flag; 110 | int the_key; 111 | unsigned int val; 112 | 113 | flag = [theEvent modifierFlags]; 114 | // printf("Key flag changed: %x => %x\n", keyflag, flag); 115 | // printf("**mlx flag low part : %d - %x\n", flag&0xFFFF, flag&0xFFFF); 116 | 117 | if (!(val = (keyflag|flag)&(~(keyflag&flag)))) 118 | return ; // no change - can happen when loosing focus on special key pressed, then re-pressed later 119 | the_key = 1; 120 | while (((val >> (the_key-1)) & 0x01)==0) 121 | the_key ++; 122 | if (flag > keyflag && event_funct[2] != NULL) 123 | event_funct[2](0xFF+the_key, event_param[2]); 124 | if (flag < keyflag && event_funct[3] != NULL) 125 | event_funct[3](0xFF+the_key, event_param[3]); 126 | /* 127 | if (event_funct[2] != NULL) 128 | { 129 | if (!(keyflag & NSAlphaShiftKeyMask) && (flag&NSAlphaShiftKeyMask)) event_funct[2](0xFF+1, event_param[2]); 130 | if (!(keyflag & NSShiftKeyMask) && (flag&NSShiftKeyMask)) event_funct[2](0xFF+2, event_param[2]); 131 | if (!(keyflag & NSControlKeyMask) && (flag&NSControlKeyMask)) event_funct[2](0xFF+3, event_param[2]); 132 | if (!(keyflag & NSAlternateKeyMask) && (flag&NSAlternateKeyMask)) event_funct[2](0xFF+4, event_param[2]); 133 | if (!(keyflag & NSCommandKeyMask) && (flag&NSCommandKeyMask)) event_funct[2](0xFF+5, event_param[2]); 134 | if (!(keyflag & NSNumericPadKeyMask) && (flag&NSNumericPadKeyMask)) event_funct[2](0xFF+6, event_param[2]); 135 | if (!(keyflag & NSHelpKeyMask) && (flag&NSHelpKeyMask)) event_funct[2](0xFF+7, event_param[2]); 136 | if (!(keyflag & NSFunctionKeyMask) && (flag&NSFunctionKeyMask)) event_funct[2](0xFF+8, event_param[2]); 137 | } 138 | if (event_funct[3] != NULL) 139 | { 140 | if ((keyflag & NSShiftKeyMask) && !(flag&NSShiftKeyMask)) event_funct[3](NSShiftKeyMask, event_param[3]); 141 | 142 | if ((keyflag & NSAlphaShiftKeyMask) && !(flag&NSAlphaShiftKeyMask)) event_funct[3](0xFF+1, event_param[3]); 143 | if ((keyflag & NSShiftKeyMask) && !(flag&NSShiftKeyMask)) event_funct[3](0xFF+2, event_param[3]); 144 | if ((keyflag & NSControlKeyMask) && !(flag&NSControlKeyMask)) event_funct[3](0xFF+3, event_param[3]); 145 | if ((keyflag & NSAlternateKeyMask) && !(flag&NSAlternateKeyMask)) event_funct[3](0xFF+4, event_param[3]); 146 | if ((keyflag & NSCommandKeyMask) && !(flag&NSCommandKeyMask)) event_funct[3](0xFF+5, event_param[3]); 147 | if ((keyflag & NSNumericPadKeyMask) && !(flag&NSNumericPadKeyMask)) event_funct[3](0xFF+6, event_param[3]); 148 | if ((keyflag & NSHelpKeyMask) && !(flag&NSHelpKeyMask)) event_funct[3](0xFF+7, event_param[3]); 149 | if ((keyflag & NSFunctionKeyMask) && !(flag&NSFunctionKeyMask)) event_funct[3](0xFF+8, event_param[3]); 150 | } 151 | */ 152 | keyflag = flag; 153 | } 154 | 155 | - (void) keyDown:(NSEvent *)theEvent 156 | { 157 | if (keyrepeat==0 && [theEvent isARepeat]) 158 | return ; 159 | // printf("Key Down: %d\n", [theEvent keyCode]); 160 | if (event_funct[2] != NULL) 161 | event_funct[2]([theEvent keyCode], event_param[2]); 162 | // else [super keyDown: theEvent]; 163 | } 164 | 165 | - (void) keyUp:(NSEvent *)theEvent 166 | { 167 | // printf("Key Up: %d\n", [theEvent keyCode]); 168 | if (event_funct[3] != NULL) 169 | event_funct[3]([theEvent keyCode], event_param[3]); 170 | // else [super keyUp: theEvent]; 171 | 172 | } 173 | 174 | - (void) mouseDown:(NSEvent *)theEvent 175 | { 176 | NSPoint thepoint; 177 | int button; 178 | 179 | thepoint = [theEvent locationInWindow]; 180 | button = get_mouse_button([theEvent type]); 181 | // printf("Mouse pressed bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 182 | if (event_funct[4] != NULL) 183 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 184 | } 185 | 186 | - (void) rightMouseDown:(NSEvent *)theEvent 187 | { 188 | NSPoint thepoint; 189 | int button; 190 | 191 | thepoint = [theEvent locationInWindow]; 192 | button = get_mouse_button([theEvent type]); 193 | // printf("Mouse pressed bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 194 | if (event_funct[4] != NULL) 195 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 196 | } 197 | 198 | - (void) otherMouseDown:(NSEvent *)theEvent 199 | { 200 | NSPoint thepoint; 201 | int button; 202 | 203 | thepoint = [theEvent locationInWindow]; 204 | button = get_mouse_button([theEvent type]); 205 | // printf("Mouse pressed bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 206 | if (event_funct[4] != NULL) 207 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 208 | } 209 | 210 | - (void) mouseUp:(NSEvent *)theEvent 211 | { 212 | NSPoint thepoint; 213 | int button; 214 | 215 | thepoint = [theEvent locationInWindow]; 216 | button = get_mouse_button([theEvent type]); 217 | // printf("Mouse release bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 218 | if (event_funct[5] != NULL) 219 | event_funct[5](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[5]); 220 | } 221 | 222 | - (void) rightMouseUp:(NSEvent *)theEvent 223 | { 224 | NSPoint thepoint; 225 | int button; 226 | 227 | thepoint = [theEvent locationInWindow]; 228 | button = get_mouse_button([theEvent type]); 229 | // printf("Mouse release bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 230 | if (event_funct[5] != NULL) 231 | event_funct[5](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[5]); 232 | } 233 | 234 | - (void) otherMouseUp:(NSEvent *)theEvent 235 | { 236 | NSPoint thepoint; 237 | int button; 238 | 239 | thepoint = [theEvent locationInWindow]; 240 | button = get_mouse_button([theEvent type]); 241 | // printf("Mouse release bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 242 | if (event_funct[5] != NULL) 243 | event_funct[5](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[5]); 244 | } 245 | 246 | - (void) mouseMoved:(NSEvent *)theEvent 247 | { 248 | NSPoint thepoint; 249 | 250 | thepoint = [theEvent locationInWindow]; 251 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 252 | if (event_funct[6] != NULL) 253 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 254 | } 255 | 256 | 257 | - (void) mouseDragged:(NSEvent *)theEvent 258 | { 259 | NSPoint thepoint; 260 | 261 | thepoint = [theEvent locationInWindow]; 262 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 263 | if (event_funct[6] != NULL) 264 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 265 | } 266 | 267 | 268 | - (void) rightMouseDragged:(NSEvent *)theEvent 269 | { 270 | NSPoint thepoint; 271 | 272 | thepoint = [theEvent locationInWindow]; 273 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 274 | if (event_funct[6] != NULL) 275 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 276 | } 277 | 278 | 279 | - (void) otherMouseDragged:(NSEvent *)theEvent 280 | { 281 | NSPoint thepoint; 282 | 283 | thepoint = [theEvent locationInWindow]; 284 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 285 | if (event_funct[6] != NULL) 286 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 287 | } 288 | 289 | 290 | - (void) scrollWheel:(NSEvent *)theEvent 291 | { 292 | NSPoint thepoint; 293 | int button; 294 | float sens; 295 | 296 | if (event_funct[4] == NULL) 297 | return ; 298 | 299 | button = 0; 300 | thepoint = [theEvent locationInWindow]; 301 | sens = [theEvent deltaY]; 302 | if (sens > 0.2) 303 | button = 4; 304 | if (sens < -0.2) 305 | button = 5; 306 | sens = [theEvent deltaX]; 307 | if (sens > 0.2) 308 | button = 6; 309 | if (sens < -0.2) 310 | button = 7; 311 | if (button != 0) 312 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 313 | } 314 | 315 | 316 | - (void) exposeNotification:(NSNotification *)note 317 | { 318 | // printf("Expose...\n"); 319 | if (event_funct[12] != NULL) 320 | event_funct[12](event_param[12]); 321 | // printf("Expose done.\n"); 322 | } 323 | 324 | - (void) closeNotification:(NSNotification *)note 325 | { 326 | if (event_funct[17] != NULL) 327 | event_funct[17](event_param[17]); 328 | } 329 | 330 | - (void) deminiaturizeNotification:(NSNotification *)note 331 | { 332 | // if (event_funct[??] != NULL) 333 | // event_funct[??](event_param[??]); 334 | [self exposeNotification:note]; 335 | } 336 | @end 337 | 338 | 339 | @implementation MlxWin 340 | 341 | - (id) initWithRect: (NSRect)rect andTitle: (NSString *)title pfaAttrs: (NSOpenGLPixelFormatAttribute *)attrs 342 | { 343 | NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; 344 | 345 | if ((self = [super initWithFrame:rect pixelFormat:pixFmt]) != nil) 346 | { 347 | NSUInteger windowStyle = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable; 348 | 349 | win = [[NSWindowEvent alloc] initWithContentRect:rect 350 | styleMask:windowStyle 351 | backing:NSBackingStoreBuffered // NSBackingStoreNonretained 352 | defer:NO]; 353 | [win setContentView:self]; 354 | [win setTitle:title]; 355 | [win setKeyRepeat:1]; 356 | [win makeKeyAndOrderFront:self]; 357 | 358 | // printf("init ctx: current %p ", [NSOpenGLContext currentContext]); 359 | 360 | // ctx = [[NSOpenGLContext alloc] initWithFormat:pixFmt shareContext:[NSOpenGLContext currentContext]]; //other_context]; 361 | // [ctx setView:self]; 362 | // [ctx makeCurrentContext]; 363 | 364 | [[self openGLContext] makeCurrentContext]; 365 | [[self openGLContext] setView:self]; 366 | [self prepareOpenGL]; 367 | 368 | [self setNextKeyView:self]; 369 | 370 | // [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(exposeNotification:) name:@"NSWindowDidExposeNotification" object:nil]; 371 | [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(exposeNotification:) name:@"NSWindowDidBecomeKeyNotification" object:win]; 372 | [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(deminiaturizeNotification:) name:@"NSWindowDidDeminiaturizeNotification" object:win]; 373 | [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(closeNotification:) name:@"NSWindowWillCloseNotification" object:win]; 374 | // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ctxNeedsUpdate:) 375 | // name:NSViewGlobalFrameDidChangeNotification 376 | // object:nil]; 377 | 378 | size_x = rect.size.width; 379 | size_y = rect.size.height; 380 | 381 | glClearColor(0, 0, 0, 0); 382 | glClear(GL_COLOR_BUFFER_BIT); 383 | glFlush(); 384 | 385 | //[win makeKeyAndOrderFront:nil]; 386 | // BOOL r = [win isKeyWindow]; 387 | // if (r==YES) printf("keywindow ok\n"); else printf("keywindow KO\n"); 388 | 389 | // Window controller subclass to set title 390 | // NSWindowController* windowController = [[NSWindowController alloc] initWithWindow:win]; 391 | // [windowController windowTitleForDocumentDisplayName:title]; 392 | // [windowController showWindow:nil]; 393 | // MlxWinController *mlxWinCont = [[MlxWinController alloc] initWin:win andTitle:title]; 394 | 395 | // after nswindowcontroller who will retake first responder 396 | // BOOL r = [win makeFirstResponder:nil]; 397 | // if (r==YES) printf("responder ok\n"); else printf("responder KO\n"); 398 | 399 | [pixFmt release]; 400 | } 401 | return (self); 402 | } 403 | 404 | - (int) pixel_management 405 | { 406 | bzero(&glsl, sizeof(glsl)); // so gldelete[shader/program] go silent on error. 407 | 408 | glDisable(GL_DEPTH_TEST); 409 | glGenBuffers(1, &pixel_vbuffer); 410 | glBindBuffer(GL_ARRAY_BUFFER, pixel_vbuffer); 411 | glBufferData(GL_ARRAY_BUFFER, sizeof(pixel_vertexes), pixel_vertexes, GL_DYNAMIC_DRAW); // 4 points buff 412 | // pixel_ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); 413 | 414 | glGenTextures(1, &pixel_texture); 415 | glBindTexture(GL_TEXTURE_2D, pixel_texture); 416 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 417 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 418 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 419 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 420 | pixtexbuff = malloc(sizeof(unsigned int)*size_x*size_y); 421 | pixel_nb = size_x*size_y; 422 | while (pixel_nb--) pixtexbuff[pixel_nb] = 0xFF000000; 423 | pixel_nb = 0; 424 | glTexImage2D( 425 | GL_TEXTURE_2D, 0, /* target, level of detail */ 426 | GL_RGBA8, /* internal format */ 427 | size_x, size_y, 0, /* width, height, border */ 428 | GL_BGRA, GL_UNSIGNED_BYTE, /* external format, type */ 429 | pixtexbuff /* pixels */ 430 | ); 431 | // printf("pix tex err? 0x%x\n", glGetError()); 432 | 433 | if (mlx_shaders(&glsl)) 434 | return (0); 435 | 436 | glUseProgram(glsl.pixel_program); 437 | glsl.loc_pixel_texture = glGetUniformLocation(glsl.pixel_program, "texture"); 438 | //glsl.loc_pixel_winhalfsize = glGetUniformLocation(glsl.pixel_program, "winhalfsize"); 439 | glsl.loc_pixel_position = glGetAttribLocation(glsl.pixel_program, "position"); 440 | // printf("err? 0x%x\n", glGetError()); 441 | 442 | glUseProgram(glsl.image_program); 443 | glsl.loc_image_texture = glGetUniformLocation(glsl.image_program, "texture"); 444 | glsl.loc_image_pos = glGetUniformLocation(glsl.image_program, "imagepos"); 445 | glsl.loc_image_size = glGetUniformLocation(glsl.image_program, "imagesize"); 446 | glsl.loc_image_winhalfsize = glGetUniformLocation(glsl.image_program, "winhalfsize"); 447 | glsl.loc_image_position = glGetAttribLocation(glsl.image_program, "position"); 448 | // printf("err? 0x%x\n", glGetError()); 449 | 450 | glUseProgram(glsl.font_program); 451 | glsl.loc_font_texture = glGetUniformLocation(glsl.font_program, "texture"); 452 | glsl.loc_font_color = glGetUniformLocation(glsl.font_program, "color"); 453 | glsl.loc_font_posinwin = glGetUniformLocation(glsl.font_program, "fontposinwin"); 454 | glsl.loc_font_posinatlas = glGetUniformLocation(glsl.font_program, "fontposinatlas"); 455 | glsl.loc_font_atlassize = glGetUniformLocation(glsl.font_program, "fontatlassize"); 456 | glsl.loc_font_winhalfsize = glGetUniformLocation(glsl.font_program, "winhalfsize"); 457 | glsl.loc_font_position = glGetAttribLocation(glsl.font_program, "position"); 458 | // printf("err? 0x%x\n", glGetError()); 459 | 460 | glFlush(); 461 | return (1); 462 | } 463 | 464 | - (void) ctxNeedsUpdate 465 | { 466 | // printf("Context update\n"); 467 | [ctx update]; 468 | } 469 | 470 | - (void) selectGLContext 471 | { 472 | if ([NSOpenGLContext currentContext] != [self openGLContext]) 473 | { 474 | // printf("ctx: %p => %p\n", [NSOpenGLContext currentContext], [self openGLContext]); 475 | [[self openGLContext] makeCurrentContext]; 476 | } 477 | } 478 | 479 | - (void) flushGLContext 480 | { 481 | [[self openGLContext] flushBuffer]; 482 | } 483 | 484 | - (NSOpenGLContext *) ctx 485 | { 486 | return (ctx); 487 | } 488 | 489 | - (NSWindowEvent *) win 490 | { 491 | return (win); 492 | } 493 | 494 | 495 | - (void) pixelPutColor: (int)color X:(int)x Y:(int)y 496 | { 497 | pixel_nb ++; 498 | 499 | glBindTexture(GL_TEXTURE_2D, pixel_vbuffer); 500 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, 1, 1, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid *)(&color)); 501 | 502 | if (pixel_nb >= MAX_PIXEL_NB) 503 | [self mlx_gl_draw]; 504 | } 505 | 506 | - (void) destroyPixelManagement 507 | { 508 | free(pixtexbuff); 509 | [self selectGLContext]; 510 | glDeleteBuffers(1, &pixel_vbuffer); 511 | glDeleteTextures(1, &pixel_texture); 512 | glDeleteProgram(glsl.pixel_program); 513 | glDeleteProgram(glsl.image_program); 514 | glDeleteShader(glsl.pixel_vshader); 515 | glDeleteShader(glsl.pixel_fshader); 516 | glDeleteShader(glsl.image_vshader); 517 | glDeleteShader(glsl.image_fshader); 518 | } 519 | 520 | 521 | - (void) destroyMe 522 | { 523 | [[NSNotificationCenter defaultCenter] removeObserver:win]; 524 | [[NSNotificationCenter defaultCenter] removeObserver:self]; 525 | // [ctx release]; 526 | [win close]; 527 | [self release]; 528 | } 529 | 530 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param 531 | { 532 | [win setEvent:event andFunc:func andParam:param]; 533 | } 534 | 535 | - (void) setKeyRepeat:(int)mode 536 | { 537 | [win setKeyRepeat:mode]; 538 | } 539 | 540 | - (void) clearWin 541 | { 542 | glClearColor(0, 0, 0, 0); 543 | glClear(GL_COLOR_BUFFER_BIT); 544 | } 545 | 546 | - (void) mlx_gl_draw_img:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y 547 | { 548 | 549 | if (pixel_nb >0) 550 | [self mlx_gl_draw]; 551 | 552 | glUseProgram(glsl.image_program); 553 | 554 | glActiveTexture(GL_TEXTURE0); 555 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 556 | glUniform1i(glsl.loc_image_texture, 0); 557 | 558 | glUniform2f(glsl.loc_image_winhalfsize, size_x/2, size_y/2); 559 | glUniform2f(glsl.loc_image_pos, x, size_y - y); 560 | glUniform2f(glsl.loc_image_size, img->width, -img->height); 561 | 562 | glBindBuffer(GL_ARRAY_BUFFER, imgctx->vbuffer); 563 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), (void*)0); 564 | glEnableVertexAttribArray(0); 565 | 566 | glEnable(GL_BLEND); 567 | glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); // src alpha 0xFF : keep dst 568 | glBlendEquation(GL_FUNC_ADD); 569 | 570 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 571 | glDisableVertexAttribArray(0); 572 | 573 | } 574 | 575 | 576 | - (void) mlx_gl_draw_font:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y andColor:(int)color glyphX:(int)gx glyphY:(int)gy 577 | { 578 | GLfloat color_tab[4]; 579 | 580 | if (pixel_nb >0) 581 | [self mlx_gl_draw]; 582 | 583 | color_tab[0] = ((float)((color&0xFF0000)>>16))/255.0; 584 | color_tab[1] = ((float)((color&0xFF00)>>8))/255.0; 585 | color_tab[2] = ((float)((color&0xFF)>>0))/255.0; 586 | color_tab[3] = 1.0; 587 | glUseProgram(glsl.font_program); 588 | 589 | glActiveTexture(GL_TEXTURE0); 590 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 591 | glUniform1i(glsl.loc_font_texture, 0); 592 | glUniform4fv(glsl.loc_font_color, 1, color_tab); 593 | 594 | glUniform2f(glsl.loc_font_winhalfsize, size_x/2, size_y/2); 595 | glUniform2f(glsl.loc_font_posinwin, x, size_y - 1 - y); 596 | glUniform2f(glsl.loc_font_posinatlas, gx, gy); 597 | glUniform2f(glsl.loc_font_atlassize, img->width, img->height); 598 | 599 | glBindBuffer(GL_ARRAY_BUFFER, imgctx->vbuffer); 600 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), (void*)0); 601 | glEnableVertexAttribArray(0); 602 | 603 | glEnable(GL_BLEND); 604 | glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); // src alpha 0xFF : keep dst 605 | glBlendEquation(GL_FUNC_ADD); 606 | 607 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 608 | glDisableVertexAttribArray(0); 609 | 610 | } 611 | 612 | 613 | - (void) mlx_gl_draw 614 | { 615 | if (pixel_nb <= 0) 616 | return ; 617 | 618 | glUseProgram(glsl.pixel_program); 619 | 620 | glActiveTexture(GL_TEXTURE0); 621 | glBindTexture(GL_TEXTURE_2D, pixel_vbuffer); 622 | glUniform1i(glsl.loc_pixel_texture, 0); 623 | 624 | glBindBuffer(GL_ARRAY_BUFFER, pixel_vbuffer); 625 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), (void*)0); 626 | glEnableVertexAttribArray(0); 627 | 628 | glEnable(GL_BLEND); 629 | glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); // src alpha 0xFF : keep dst 630 | glBlendEquation(GL_FUNC_ADD); 631 | 632 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 633 | glDisableVertexAttribArray(0); 634 | 635 | pixel_nb = size_x*size_y; 636 | while (pixel_nb--) pixtexbuff[pixel_nb] = 0xFF000000; 637 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size_x, size_y, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixtexbuff); 638 | pixel_nb = 0; 639 | 640 | } 641 | 642 | @end 643 | 644 | 645 | // mlx API 646 | 647 | 648 | void *mlx_new_window(mlx_ptr_t *mlx_ptr, int size_x, int size_y, char *title) 649 | { 650 | mlx_win_list_t *newwin; 651 | NSString *str; 652 | 653 | if ((newwin = malloc(sizeof(*newwin))) == NULL) 654 | return ((void *)0); 655 | newwin->img_list = NULL; 656 | newwin->next = mlx_ptr->win_list; 657 | newwin->nb_flush = 0; 658 | newwin->pixmgt = 1; 659 | mlx_ptr->win_list = newwin; 660 | 661 | NSRect windowRect = NSMakeRect(100, 100, size_x, size_y); 662 | str = [NSString stringWithCString:title encoding:NSASCIIStringEncoding]; 663 | newwin->winid = [[MlxWin alloc] initWithRect:windowRect andTitle:str pfaAttrs:pfa_attrs]; 664 | if (newwin->winid) 665 | if (![(id)(newwin->winid) pixel_management]) 666 | { 667 | [(id)(newwin->winid) destroyPixelManagement]; 668 | [(id)(newwin->winid) destroyMe]; 669 | } 670 | return ((void *)newwin); 671 | } 672 | 673 | 674 | void mlx_clear_window(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr) 675 | { 676 | [(id)(win_ptr->winid) selectGLContext]; 677 | [(id)(win_ptr->winid) clearWin]; 678 | win_ptr->nb_flush ++; 679 | } 680 | 681 | 682 | void mlx_expose_hook(mlx_win_list_t *win_ptr, int (*funct_ptr)(), void *param) 683 | { 684 | [(id)(win_ptr->winid) setEvent:12 andFunc:funct_ptr andParam:param]; 685 | } 686 | 687 | void mlx_key_hook(mlx_win_list_t *win_ptr, int (*funct_ptr)(), void *param) 688 | { 689 | [(id)(win_ptr->winid) setEvent:3 andFunc:funct_ptr andParam:param]; 690 | } 691 | 692 | void mlx_mouse_hook(mlx_win_list_t *win_ptr, int (*funct_ptr)(), void *param) 693 | { 694 | [(id)(win_ptr->winid) setEvent:4 andFunc:funct_ptr andParam:param]; 695 | } 696 | 697 | void mlx_hook(mlx_win_list_t *win_ptr, int x_event, int x_mask, int (*funct_ptr)(), void *param) 698 | { 699 | [(id)(win_ptr->winid) setEvent:x_event andFunc:funct_ptr andParam:param]; 700 | } 701 | 702 | int mlx_do_key_autorepeatoff(mlx_ptr_t *mlx_ptr) 703 | { 704 | mlx_win_list_t *win; 705 | 706 | win = mlx_ptr->win_list; 707 | while (win) 708 | { 709 | [(id)(win->winid) setKeyRepeat:0]; 710 | win = win->next; 711 | } 712 | return (0); 713 | } 714 | 715 | int mlx_do_key_autorepeaton(mlx_ptr_t *mlx_ptr) 716 | { 717 | mlx_win_list_t *win; 718 | 719 | win = mlx_ptr->win_list; 720 | while (win) 721 | { 722 | [(id)(win->winid) setKeyRepeat:1]; 723 | win = win->next; 724 | } 725 | return (0); 726 | } 727 | 728 | 729 | int mlx_destroy_window(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_to_del) 730 | { 731 | mlx_win_list_t first; 732 | mlx_win_list_t *win; 733 | mlx_img_ctx_t *ctx; 734 | mlx_img_ctx_t *ctx2; 735 | 736 | first.next = mlx_ptr->win_list; 737 | win = &first; 738 | while (win && win->next) 739 | { 740 | if (win->next == win_to_del) 741 | win->next = win->next->next; 742 | win = win->next; 743 | } 744 | mlx_ptr->win_list = first.next; 745 | 746 | if (win_to_del->pixmgt) 747 | { 748 | [(id)(win_to_del->winid) selectGLContext]; 749 | ctx = win_to_del->img_list; // should be null anyway if no pixel management 750 | while (ctx) 751 | { 752 | glDeleteBuffers(1, &(ctx->vbuffer)); 753 | glDeleteTextures(1, &(ctx->texture)); 754 | ctx2 = ctx; 755 | ctx = ctx->next; 756 | free(ctx2); 757 | } 758 | [(id)(win_to_del->winid) destroyPixelManagement]; 759 | } 760 | [(id)(win_to_del->winid) destroyMe]; 761 | free(win_to_del); 762 | 763 | // printf("destroy window done.\n"); 764 | mlx_do_sync(mlx_ptr); 765 | return (0); 766 | } 767 | -------------------------------------------------------------------------------- /mlx/mlx_new_window.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/mlx_new_window.o -------------------------------------------------------------------------------- /mlx/mlx_opengl.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** 3 | ** mlx_opengl.h 4 | ** 5 | ** public include, use it after mlx.h 6 | ** designed only for minilibx_macos 7 | ** 8 | */ 9 | 10 | void *mlx_new_opengl_window(void *mlx_ptr, int size_x, int size_y, char *title); 11 | 12 | /* create an opengl window. put_image & pixel_put & string_put do not work there. */ 13 | 14 | int mlx_opengl_swap_buffers(void *win_ptr); 15 | 16 | /* the created window is double buffered. Use this funct to swap buffers */ 17 | /* this funct will call glFlush(). Don't call it. */ 18 | 19 | int mlx_opengl_window_set_context(void *win_ptr); 20 | 21 | /* in case multiple opengl windows are present, change opengl active context */ 22 | -------------------------------------------------------------------------------- /mlx/mlx_opengl.m: -------------------------------------------------------------------------------- 1 | // mlx_opengl.m 2 | 3 | #import 4 | #import 5 | #import 6 | 7 | #include 8 | 9 | #include "mlx_int.h" 10 | #include "mlx_new_window.h" 11 | 12 | 13 | 14 | 15 | 16 | NSOpenGLPixelFormatAttribute pfa_attrs_opengl[] = 17 | { 18 | NSOpenGLPFADepthSize, 32, 19 | NSOpenGLPFADoubleBuffer, 20 | NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core, 21 | 0 22 | }; 23 | 24 | 25 | 26 | void *mlx_new_opengl_window(mlx_ptr_t *mlx_ptr, int size_x, int size_y, char *title) 27 | { 28 | mlx_win_list_t *newwin; 29 | NSString *str; 30 | 31 | if ((newwin = malloc(sizeof(*newwin))) == NULL) 32 | return ((void *)0); 33 | newwin->img_list = NULL; 34 | newwin->next = mlx_ptr->win_list; 35 | newwin->nb_flush = 0; 36 | newwin->pixmgt = 0; 37 | mlx_ptr->win_list = newwin; 38 | 39 | NSRect windowRect = NSMakeRect(100, 100, size_x, size_y); 40 | str = [NSString stringWithCString:title encoding:NSASCIIStringEncoding]; 41 | newwin->winid = [[MlxWin alloc] initWithRect:windowRect andTitle:str pfaAttrs:pfa_attrs_opengl]; 42 | 43 | return ((void *)newwin); 44 | } 45 | 46 | 47 | int mlx_opengl_swap_buffers(mlx_win_list_t *win_ptr) 48 | { 49 | [(id)(win_ptr->winid) flushGLContext]; 50 | return (0); 51 | } 52 | 53 | int mlx_opengl_window_set_context(mlx_win_list_t *win_ptr) 54 | { 55 | [(id)(win_ptr->winid) selectGLContext]; 56 | return (0); 57 | } 58 | -------------------------------------------------------------------------------- /mlx/mlx_png.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "zlib.h" 13 | 14 | #include 15 | #include "mlx_int.h" 16 | 17 | 18 | #define PNG_MAGIC_SIZE 8 19 | unsigned char magic[PNG_MAGIC_SIZE] = {137, 80, 78, 71, 13, 10, 26, 10}; 20 | #define PNG_HDR_SIZE 13 21 | 22 | #define Z_CHUNK 32768 23 | 24 | #define ERR_MAGIC_SIZE 1 25 | #define ERR_MAGIC_WRONG 2 26 | #define ERR_STRUCT_INCOMPLETE 3 27 | #define ERR_STRUCT_HDR 4 28 | #define ERR_STRUCT_END 5 29 | #define ERR_STRUCT_CRC 6 30 | #define ERR_STRUCT_INCIMPL 7 31 | #define ERR_STRUCT_DAT 8 32 | #define ERR_STRUCT_MISSCHK 9 33 | #define ERR_ZLIB 10 34 | #define ERR_DATA_MISMATCH 11 35 | #define ERR_DATA_FILTER 12 36 | char *(mipng_err[]) = 37 | { 38 | "No error", 39 | "Not enough size for magic", 40 | "Wrong magic", 41 | "Incomplete chunk structure", 42 | "Duplicate or incorrect header", 43 | "Duplicate or incorrect end", 44 | "Invalid CRC in chunk", 45 | "Incorrect header or configuration not implemented", 46 | "Non consecutive dat chunks", 47 | "Missing header/dat/end chunk(s)", 48 | "Zlib inflate error", 49 | "Inflated data size mismatch", 50 | "Unknown scanline filter" 51 | }; 52 | 53 | typedef struct png_info_s 54 | { 55 | unsigned int width; 56 | unsigned int height; 57 | int depth; 58 | int color; 59 | int interlace; 60 | int bpp; 61 | } png_info_t; 62 | 63 | 64 | int mipng_is_type(unsigned char *ptr, char *type) 65 | { 66 | if (ptr[4] == type[0] && ptr[5] == type[1] && ptr[6] == type[2] && ptr[7] == type[3]) 67 | return (1); 68 | return (0); 69 | } 70 | 71 | 72 | unsigned char mipng_defilter_none(unsigned char *buff, int pos, int a, int b, int c) 73 | { return (buff[pos]); } 74 | unsigned char mipng_defilter_sub(unsigned char *buff, int pos, int a, int b, int c) 75 | { return (buff[pos]+(unsigned int)a); } 76 | unsigned char mipng_defilter_up(unsigned char *buff, int pos, int a, int b, int c) 77 | { return (buff[pos]+(unsigned int)b); } 78 | unsigned char mipng_defilter_average(unsigned char *buff, int pos, int a, int b, int c) 79 | { return (buff[pos]+((unsigned int)a+(unsigned int)b)/2); } 80 | unsigned char mipng_defilter_paeth(unsigned char *buff, int pos, int a, int b, int c) 81 | { 82 | int p; 83 | int result; 84 | 85 | p = a + b - c; 86 | if (abs(b - c) <= abs(a - c) && abs(b - c) <= abs(a + b - c - c)) 87 | result = a; 88 | else 89 | if (abs(a - c) <= abs(a + b - c - c)) 90 | result = b; 91 | else 92 | result = c; 93 | return (buff[pos]+result); 94 | } 95 | 96 | 97 | 98 | unsigned char (*(mipng_defilter[]))(unsigned char *buff, int pos, int a, int b, int c) = 99 | { 100 | mipng_defilter_none, 101 | mipng_defilter_sub, 102 | mipng_defilter_up, 103 | mipng_defilter_average, 104 | mipng_defilter_paeth 105 | }; 106 | 107 | // only work for mlx mac or img 32bpp 108 | int mipng_fill_img(mlx_img_list_t *img, unsigned char *buf, png_info_t *pi) 109 | { 110 | unsigned int current_filter; 111 | int ipos; 112 | int bpos; 113 | int ilen; 114 | int iline; 115 | int blen; 116 | unsigned char tmp; 117 | unsigned char *ibuf; 118 | 119 | ibuf = (unsigned char *)img->buffer; 120 | iline = img->width * UNIQ_BPP; 121 | ilen = img->width * img->height * UNIQ_BPP; 122 | blen = img->width * img->height * pi->bpp + img->height; 123 | ipos = 0; 124 | bpos = 0; 125 | while (ipos < ilen && bpos < blen) 126 | { 127 | if ((ipos % iline) == 0) 128 | { 129 | if ((current_filter = buf[bpos++]) > 4) 130 | return (ERR_DATA_FILTER); 131 | } 132 | ibuf[ipos] = mipng_defilter[current_filter](buf, bpos, 133 | ipos%iline>3?ibuf[ipos-UNIQ_BPP]:0, 134 | (ipos>=iline)?ibuf[ipos-iline]:0, 135 | (ipos>=iline && ipos%iline>3)?ibuf[ipos-iline-UNIQ_BPP]:0); 136 | ipos ++; 137 | bpos ++; 138 | if (pi->depth == 16) 139 | bpos ++; 140 | if (ipos % 4 == 3 && pi->color == 2) // no alpha 141 | img->buffer[ipos++] = 0xFF; 142 | } 143 | if (ipos != ilen || bpos != blen) 144 | { 145 | // printf("fill err ipos %d vs %d, bpos %d vs %d\n", ipos, ilen, bpos, blen); 146 | return (ERR_DATA_MISMATCH); 147 | } 148 | ipos = 0; 149 | while (ipos < ilen) 150 | { 151 | tmp = ibuf[ipos]; 152 | ibuf[ipos] = ibuf[ipos+2]; 153 | ibuf[ipos+2] = tmp; 154 | ibuf[ipos+3] = 0xFF - ibuf[ipos+3]; 155 | ipos += UNIQ_BPP; 156 | } 157 | return (0); 158 | } 159 | 160 | 161 | int mipng_data(mlx_img_list_t *img, unsigned char *dat, png_info_t *pi) 162 | { 163 | unsigned int len; 164 | int b_pos; 165 | unsigned char *buffer; 166 | int ret; 167 | int z_ret; 168 | unsigned z_have; 169 | z_stream z_strm; 170 | unsigned char z_out[Z_CHUNK]; 171 | 172 | b_pos = 0; 173 | if (!(buffer = malloc((long long)img->width*(long long)img->height*(long long)pi->bpp + img->height))) 174 | err(1, "Can't malloc"); 175 | z_strm.zalloc = Z_NULL; 176 | z_strm.zfree = Z_NULL; 177 | z_strm.opaque = Z_NULL; 178 | z_strm.avail_in = 0; 179 | z_strm.next_in = Z_NULL; 180 | z_ret = inflateInit(&z_strm); 181 | if (z_ret != Z_OK) 182 | return (ERR_ZLIB); 183 | 184 | while (mipng_is_type(dat, "IDAT")) 185 | { 186 | len = *((unsigned int *)dat); 187 | len = ntohl(len); 188 | z_strm.avail_in = len; 189 | z_strm.next_in = dat + 8; 190 | z_strm.avail_out = 0; 191 | while (z_strm.avail_out == 0) 192 | { 193 | z_strm.avail_out = Z_CHUNK; 194 | z_strm.next_out = z_out; 195 | z_ret = inflate(&z_strm, Z_NO_FLUSH); 196 | // printf("inflate ret %d avail_out %d\n", z_ret, z_strm.avail_out); 197 | if (z_ret != Z_OK && z_ret != Z_STREAM_END) 198 | { 199 | inflateEnd(&z_strm); 200 | return (ERR_ZLIB); 201 | } 202 | if (b_pos + Z_CHUNK - z_strm.avail_out > img->width*img->height*pi->bpp+img->height) 203 | { 204 | inflateEnd(&z_strm); 205 | return (ERR_DATA_MISMATCH); 206 | } 207 | bcopy(z_out, buffer+b_pos, Z_CHUNK - z_strm.avail_out); 208 | b_pos += Z_CHUNK - z_strm.avail_out; 209 | } 210 | dat += len + 4 + 4 + 4; 211 | } 212 | inflateEnd(&z_strm); 213 | if (b_pos != img->width*img->height*pi->bpp+img->height) 214 | { 215 | // printf("pb : bpos %d vs expected %d\n", b_pos, img->width*img->height*pi->bpp+img->height); 216 | return (ERR_DATA_MISMATCH); 217 | } 218 | if ((ret = mipng_fill_img(img, buffer, pi))) 219 | return (ret); 220 | return (0); 221 | } 222 | 223 | 224 | 225 | int mipng_magic(unsigned char *ptr, int size) 226 | { 227 | int i; 228 | 229 | if (size < PNG_MAGIC_SIZE) 230 | return (ERR_MAGIC_SIZE); 231 | i = 0; 232 | while (i < PNG_MAGIC_SIZE) 233 | if (*(ptr++) != magic[i++]) 234 | return (ERR_MAGIC_WRONG); 235 | return (0); 236 | } 237 | 238 | 239 | unsigned long crc_table[256] = { 0, 0x77073096, 0xee0e612c, 0x990951ba, 0x76dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0xedb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x9b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x1db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x6b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0xf00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x86d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x3b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x4db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0xd6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0xa00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x26d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x5005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0xcb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0xbdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; 240 | 241 | // From http://www.w3.org/TR/PNG/#D-CRCAppendix 242 | int mipng_crc(unsigned char *ptr, int len) 243 | { 244 | unsigned int file_crc; 245 | unsigned long crc; 246 | int i; 247 | 248 | file_crc = *((unsigned int *)(ptr+4+4+len)); 249 | file_crc = ntohl(file_crc); 250 | 251 | crc = 0xffffffffL; 252 | i = 0; 253 | while (i < len+4) 254 | crc = crc_table[(crc ^ ptr[(i++)+4]) & 0xff] ^ (crc >> 8); 255 | crc ^= 0xffffffffL; 256 | 257 | if (file_crc != crc) 258 | return (1); 259 | return (0); 260 | } 261 | 262 | 263 | int mipng_structure(unsigned char *ptr, int size, unsigned char **hdr, unsigned char **dat) 264 | { 265 | unsigned int len; 266 | int dat_state; 267 | int end; 268 | 269 | dat_state = 0; 270 | *hdr = NULL; 271 | *dat = NULL; 272 | end = 0; 273 | while (size) 274 | { 275 | if (size >= 4) // length present 276 | { 277 | len = *((unsigned int *)ptr); 278 | len = ntohl(len); 279 | if (size < 4 + 4 + 4 + len) 280 | return (ERR_STRUCT_INCOMPLETE); 281 | if (mipng_crc(ptr, len)) 282 | return (ERR_STRUCT_CRC); 283 | // printf("found chunk len %d type %c%c%c%c\n", len, *(ptr+4), *(ptr+5), *(ptr+6), *(ptr+7)); 284 | if (mipng_is_type(ptr, "IHDR")) 285 | { 286 | if (*hdr || len != PNG_HDR_SIZE) 287 | return (ERR_STRUCT_HDR); 288 | *hdr = ptr; 289 | } 290 | if (mipng_is_type(ptr, "IEND")) 291 | { 292 | if (len != 0 || size != 4+4+4) 293 | return (ERR_STRUCT_END); 294 | end = 1; 295 | } 296 | if (mipng_is_type(ptr, "IDAT")) 297 | { 298 | if (dat_state == 0) 299 | { 300 | dat_state = 1; 301 | *dat = ptr; 302 | } 303 | if (dat_state == 2) 304 | return (ERR_STRUCT_DAT); 305 | } 306 | else 307 | if (dat_state == 1) 308 | dat_state = 2; 309 | size -= 4+4+4+len; 310 | ptr += 4+4+4+len; 311 | } 312 | else 313 | return (ERR_STRUCT_INCOMPLETE); 314 | } 315 | if (*hdr == 0 || *dat == 0 || end == 0) 316 | return (ERR_STRUCT_MISSCHK); 317 | return (0); 318 | } 319 | 320 | 321 | int mipng_verif_hdr(unsigned char *hdr, png_info_t *pi) 322 | { 323 | unsigned int compress; 324 | unsigned int filter; 325 | 326 | hdr += 8; 327 | pi->width = ntohl(*((unsigned long *)hdr)); 328 | pi->height = ntohl(*((unsigned long *)(hdr+4))); 329 | pi->depth = *(hdr+8); 330 | pi->color = *(hdr+9); 331 | compress = *(hdr+10); 332 | filter = *(hdr+11); 333 | pi->interlace = *(hdr+12); 334 | if (pi->width <= 0 || pi->height <= 0 || (pi->depth != 8 && pi->depth != 16) 335 | || (pi->color != 2 && pi->color != 6) || compress != 0 || filter != 0 || pi->interlace != 0) 336 | return (ERR_STRUCT_INCIMPL); 337 | pi->bpp = pi->depth / 8; 338 | if (pi->color == 2) 339 | pi->bpp *= 3; 340 | if (pi->color == 6) 341 | pi->bpp *= 4; 342 | // printf("hdr info : %d x %d, depth %d, col type %d, comp %d, filter %d, interlace %d\nbpp is %d\n", 343 | // pi->width, pi->height, pi->depth, pi->color, compress, filter, pi->interlace, pi->bpp); 344 | return (0); 345 | } 346 | 347 | 348 | mlx_img_list_t *mlx_int_parse_png(mlx_ptr_t *xvar, unsigned char *fptr, int size) 349 | { 350 | int err; 351 | unsigned char *hdr; 352 | unsigned char *dat; 353 | png_info_t pi; 354 | mlx_img_list_t *img; 355 | 356 | if ((err = mipng_magic(fptr, size))) 357 | { 358 | warnx("mlx PNG error : %s", mipng_err[err]); 359 | return ((mlx_img_list_t *)0); 360 | } 361 | fptr += PNG_MAGIC_SIZE; 362 | size -= PNG_MAGIC_SIZE; 363 | if ((err = mipng_structure(fptr, size, &hdr, &dat))) 364 | { 365 | warnx("mlx PNG error : %s", mipng_err[err]); 366 | return ((mlx_img_list_t *)0); 367 | } 368 | if ((err = mipng_verif_hdr(hdr, &pi))) 369 | { 370 | warnx("mlx PNG error : %s", mipng_err[err]); 371 | return ((mlx_img_list_t *)0); 372 | } 373 | if (!(img = mlx_new_image(xvar, pi.width, pi.height))) 374 | { 375 | warnx("mlx PNG error : Can't create mlx image"); 376 | return ((mlx_img_list_t *)0); 377 | } 378 | if ((err = mipng_data(img, dat, &pi))) 379 | { 380 | mlx_destroy_image(xvar, img); 381 | warnx("mlx PNG error : %s", mipng_err[err]); 382 | return ((mlx_img_list_t *)0); 383 | } 384 | return (img); 385 | } 386 | 387 | 388 | 389 | 390 | void *mlx_png_file_to_image(mlx_ptr_t *xvar, char *file, int *width, int *height) 391 | { 392 | int fd; 393 | int size; 394 | unsigned char *ptr; 395 | mlx_img_list_t *img; 396 | 397 | if ((fd = open(file, O_RDONLY)) == -1 || (size = lseek(fd, 0, SEEK_END)) == -1 || 398 | (ptr = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)MAP_FAILED) 399 | { 400 | if (fd >= 0) 401 | close(fd); 402 | warnx("Can't map png file '%s'", file); 403 | return ((void *)0); 404 | } 405 | if ((img = mlx_int_parse_png(xvar, ptr, size))) 406 | { 407 | *width = img->width; 408 | *height = img->height; 409 | } 410 | else 411 | { 412 | *width = 0; 413 | *height = 0; 414 | } 415 | munmap(ptr,size); 416 | close(fd); 417 | return (img); 418 | } 419 | -------------------------------------------------------------------------------- /mlx/mlx_png.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | void *mlx_png_file_to_image(void *xvar, char *file, int *width, int *height); 5 | -------------------------------------------------------------------------------- /mlx/mlx_png.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/mlx_png.o -------------------------------------------------------------------------------- /mlx/mlx_rgb.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** This is a generated file with rgb2c.pl and rgb.txt from 3 | ** the XFree86 distribution. 4 | */ 5 | 6 | 7 | struct s_col_name mlx_col_name[] = 8 | { 9 | { "snow" , 0xfffafa }, 10 | { "ghost white" , 0xf8f8ff }, 11 | { "ghostwhite" , 0xf8f8ff }, 12 | { "white smoke" , 0xf5f5f5 }, 13 | { "whitesmoke" , 0xf5f5f5 }, 14 | { "gainsboro" , 0xdcdcdc }, 15 | { "floral white" , 0xfffaf0 }, 16 | { "floralwhite" , 0xfffaf0 }, 17 | { "old lace" , 0xfdf5e6 }, 18 | { "oldlace" , 0xfdf5e6 }, 19 | { "linen" , 0xfaf0e6 }, 20 | { "antique white" , 0xfaebd7 }, 21 | { "antiquewhite" , 0xfaebd7 }, 22 | { "papaya whip" , 0xffefd5 }, 23 | { "papayawhip" , 0xffefd5 }, 24 | { "blanched almond" , 0xffebcd }, 25 | { "blanchedalmond" , 0xffebcd }, 26 | { "bisque" , 0xffe4c4 }, 27 | { "peach puff" , 0xffdab9 }, 28 | { "peachpuff" , 0xffdab9 }, 29 | { "navajo white" , 0xffdead }, 30 | { "navajowhite" , 0xffdead }, 31 | { "moccasin" , 0xffe4b5 }, 32 | { "cornsilk" , 0xfff8dc }, 33 | { "ivory" , 0xfffff0 }, 34 | { "lemon chiffon" , 0xfffacd }, 35 | { "lemonchiffon" , 0xfffacd }, 36 | { "seashell" , 0xfff5ee }, 37 | { "honeydew" , 0xf0fff0 }, 38 | { "mint cream" , 0xf5fffa }, 39 | { "mintcream" , 0xf5fffa }, 40 | { "azure" , 0xf0ffff }, 41 | { "alice blue" , 0xf0f8ff }, 42 | { "aliceblue" , 0xf0f8ff }, 43 | { "lavender" , 0xe6e6fa }, 44 | { "lavender blush" , 0xfff0f5 }, 45 | { "lavenderblush" , 0xfff0f5 }, 46 | { "misty rose" , 0xffe4e1 }, 47 | { "mistyrose" , 0xffe4e1 }, 48 | { "white" , 0xffffff }, 49 | { "black" , 0x0 }, 50 | { "dark slate" , 0x2f4f4f }, 51 | { "darkslategray" , 0x2f4f4f }, 52 | { "dark slate" , 0x2f4f4f }, 53 | { "darkslategrey" , 0x2f4f4f }, 54 | { "dim gray" , 0x696969 }, 55 | { "dimgray" , 0x696969 }, 56 | { "dim grey" , 0x696969 }, 57 | { "dimgrey" , 0x696969 }, 58 | { "slate gray" , 0x708090 }, 59 | { "slategray" , 0x708090 }, 60 | { "slate grey" , 0x708090 }, 61 | { "slategrey" , 0x708090 }, 62 | { "light slate" , 0x778899 }, 63 | { "lightslategray" , 0x778899 }, 64 | { "light slate" , 0x778899 }, 65 | { "lightslategrey" , 0x778899 }, 66 | { "gray" , 0xbebebe }, 67 | { "grey" , 0xbebebe }, 68 | { "light grey" , 0xd3d3d3 }, 69 | { "lightgrey" , 0xd3d3d3 }, 70 | { "light gray" , 0xd3d3d3 }, 71 | { "lightgray" , 0xd3d3d3 }, 72 | { "midnight blue" , 0x191970 }, 73 | { "midnightblue" , 0x191970 }, 74 | { "navy" , 0x80 }, 75 | { "navy blue" , 0x80 }, 76 | { "navyblue" , 0x80 }, 77 | { "cornflower blue" , 0x6495ed }, 78 | { "cornflowerblue" , 0x6495ed }, 79 | { "dark slate" , 0x483d8b }, 80 | { "darkslateblue" , 0x483d8b }, 81 | { "slate blue" , 0x6a5acd }, 82 | { "slateblue" , 0x6a5acd }, 83 | { "medium slate" , 0x7b68ee }, 84 | { "mediumslateblue" , 0x7b68ee }, 85 | { "light slate" , 0x8470ff }, 86 | { "lightslateblue" , 0x8470ff }, 87 | { "medium blue" , 0xcd }, 88 | { "mediumblue" , 0xcd }, 89 | { "royal blue" , 0x4169e1 }, 90 | { "royalblue" , 0x4169e1 }, 91 | { "blue" , 0xff }, 92 | { "dodger blue" , 0x1e90ff }, 93 | { "dodgerblue" , 0x1e90ff }, 94 | { "deep sky" , 0xbfff }, 95 | { "deepskyblue" , 0xbfff }, 96 | { "sky blue" , 0x87ceeb }, 97 | { "skyblue" , 0x87ceeb }, 98 | { "light sky" , 0x87cefa }, 99 | { "lightskyblue" , 0x87cefa }, 100 | { "steel blue" , 0x4682b4 }, 101 | { "steelblue" , 0x4682b4 }, 102 | { "light steel" , 0xb0c4de }, 103 | { "lightsteelblue" , 0xb0c4de }, 104 | { "light blue" , 0xadd8e6 }, 105 | { "lightblue" , 0xadd8e6 }, 106 | { "powder blue" , 0xb0e0e6 }, 107 | { "powderblue" , 0xb0e0e6 }, 108 | { "pale turquoise" , 0xafeeee }, 109 | { "paleturquoise" , 0xafeeee }, 110 | { "dark turquoise" , 0xced1 }, 111 | { "darkturquoise" , 0xced1 }, 112 | { "medium turquoise" , 0x48d1cc }, 113 | { "mediumturquoise" , 0x48d1cc }, 114 | { "turquoise" , 0x40e0d0 }, 115 | { "cyan" , 0xffff }, 116 | { "light cyan" , 0xe0ffff }, 117 | { "lightcyan" , 0xe0ffff }, 118 | { "cadet blue" , 0x5f9ea0 }, 119 | { "cadetblue" , 0x5f9ea0 }, 120 | { "medium aquamarine" , 0x66cdaa }, 121 | { "mediumaquamarine" , 0x66cdaa }, 122 | { "aquamarine" , 0x7fffd4 }, 123 | { "dark green" , 0x6400 }, 124 | { "darkgreen" , 0x6400 }, 125 | { "dark olive" , 0x556b2f }, 126 | { "darkolivegreen" , 0x556b2f }, 127 | { "dark sea" , 0x8fbc8f }, 128 | { "darkseagreen" , 0x8fbc8f }, 129 | { "sea green" , 0x2e8b57 }, 130 | { "seagreen" , 0x2e8b57 }, 131 | { "medium sea" , 0x3cb371 }, 132 | { "mediumseagreen" , 0x3cb371 }, 133 | { "light sea" , 0x20b2aa }, 134 | { "lightseagreen" , 0x20b2aa }, 135 | { "pale green" , 0x98fb98 }, 136 | { "palegreen" , 0x98fb98 }, 137 | { "spring green" , 0xff7f }, 138 | { "springgreen" , 0xff7f }, 139 | { "lawn green" , 0x7cfc00 }, 140 | { "lawngreen" , 0x7cfc00 }, 141 | { "green" , 0xff00 }, 142 | { "chartreuse" , 0x7fff00 }, 143 | { "medium spring" , 0xfa9a }, 144 | { "mediumspringgreen" , 0xfa9a }, 145 | { "green yellow" , 0xadff2f }, 146 | { "greenyellow" , 0xadff2f }, 147 | { "lime green" , 0x32cd32 }, 148 | { "limegreen" , 0x32cd32 }, 149 | { "yellow green" , 0x9acd32 }, 150 | { "yellowgreen" , 0x9acd32 }, 151 | { "forest green" , 0x228b22 }, 152 | { "forestgreen" , 0x228b22 }, 153 | { "olive drab" , 0x6b8e23 }, 154 | { "olivedrab" , 0x6b8e23 }, 155 | { "dark khaki" , 0xbdb76b }, 156 | { "darkkhaki" , 0xbdb76b }, 157 | { "khaki" , 0xf0e68c }, 158 | { "pale goldenrod" , 0xeee8aa }, 159 | { "palegoldenrod" , 0xeee8aa }, 160 | { "light goldenrod" , 0xfafad2 }, 161 | { "lightgoldenrodyellow" , 0xfafad2 }, 162 | { "light yellow" , 0xffffe0 }, 163 | { "lightyellow" , 0xffffe0 }, 164 | { "yellow" , 0xffff00 }, 165 | { "gold" , 0xffd700 }, 166 | { "light goldenrod" , 0xeedd82 }, 167 | { "lightgoldenrod" , 0xeedd82 }, 168 | { "goldenrod" , 0xdaa520 }, 169 | { "dark goldenrod" , 0xb8860b }, 170 | { "darkgoldenrod" , 0xb8860b }, 171 | { "rosy brown" , 0xbc8f8f }, 172 | { "rosybrown" , 0xbc8f8f }, 173 | { "indian red" , 0xcd5c5c }, 174 | { "indianred" , 0xcd5c5c }, 175 | { "saddle brown" , 0x8b4513 }, 176 | { "saddlebrown" , 0x8b4513 }, 177 | { "sienna" , 0xa0522d }, 178 | { "peru" , 0xcd853f }, 179 | { "burlywood" , 0xdeb887 }, 180 | { "beige" , 0xf5f5dc }, 181 | { "wheat" , 0xf5deb3 }, 182 | { "sandy brown" , 0xf4a460 }, 183 | { "sandybrown" , 0xf4a460 }, 184 | { "tan" , 0xd2b48c }, 185 | { "chocolate" , 0xd2691e }, 186 | { "firebrick" , 0xb22222 }, 187 | { "brown" , 0xa52a2a }, 188 | { "dark salmon" , 0xe9967a }, 189 | { "darksalmon" , 0xe9967a }, 190 | { "salmon" , 0xfa8072 }, 191 | { "light salmon" , 0xffa07a }, 192 | { "lightsalmon" , 0xffa07a }, 193 | { "orange" , 0xffa500 }, 194 | { "dark orange" , 0xff8c00 }, 195 | { "darkorange" , 0xff8c00 }, 196 | { "coral" , 0xff7f50 }, 197 | { "light coral" , 0xf08080 }, 198 | { "lightcoral" , 0xf08080 }, 199 | { "tomato" , 0xff6347 }, 200 | { "orange red" , 0xff4500 }, 201 | { "orangered" , 0xff4500 }, 202 | { "red" , 0xff0000 }, 203 | { "hot pink" , 0xff69b4 }, 204 | { "hotpink" , 0xff69b4 }, 205 | { "deep pink" , 0xff1493 }, 206 | { "deeppink" , 0xff1493 }, 207 | { "pink" , 0xffc0cb }, 208 | { "light pink" , 0xffb6c1 }, 209 | { "lightpink" , 0xffb6c1 }, 210 | { "pale violet" , 0xdb7093 }, 211 | { "palevioletred" , 0xdb7093 }, 212 | { "maroon" , 0xb03060 }, 213 | { "medium violet" , 0xc71585 }, 214 | { "mediumvioletred" , 0xc71585 }, 215 | { "violet red" , 0xd02090 }, 216 | { "violetred" , 0xd02090 }, 217 | { "magenta" , 0xff00ff }, 218 | { "violet" , 0xee82ee }, 219 | { "plum" , 0xdda0dd }, 220 | { "orchid" , 0xda70d6 }, 221 | { "medium orchid" , 0xba55d3 }, 222 | { "mediumorchid" , 0xba55d3 }, 223 | { "dark orchid" , 0x9932cc }, 224 | { "darkorchid" , 0x9932cc }, 225 | { "dark violet" , 0x9400d3 }, 226 | { "darkviolet" , 0x9400d3 }, 227 | { "blue violet" , 0x8a2be2 }, 228 | { "blueviolet" , 0x8a2be2 }, 229 | { "purple" , 0xa020f0 }, 230 | { "medium purple" , 0x9370db }, 231 | { "mediumpurple" , 0x9370db }, 232 | { "thistle" , 0xd8bfd8 }, 233 | { "snow1" , 0xfffafa }, 234 | { "snow2" , 0xeee9e9 }, 235 | { "snow3" , 0xcdc9c9 }, 236 | { "snow4" , 0x8b8989 }, 237 | { "seashell1" , 0xfff5ee }, 238 | { "seashell2" , 0xeee5de }, 239 | { "seashell3" , 0xcdc5bf }, 240 | { "seashell4" , 0x8b8682 }, 241 | { "antiquewhite1" , 0xffefdb }, 242 | { "antiquewhite2" , 0xeedfcc }, 243 | { "antiquewhite3" , 0xcdc0b0 }, 244 | { "antiquewhite4" , 0x8b8378 }, 245 | { "bisque1" , 0xffe4c4 }, 246 | { "bisque2" , 0xeed5b7 }, 247 | { "bisque3" , 0xcdb79e }, 248 | { "bisque4" , 0x8b7d6b }, 249 | { "peachpuff1" , 0xffdab9 }, 250 | { "peachpuff2" , 0xeecbad }, 251 | { "peachpuff3" , 0xcdaf95 }, 252 | { "peachpuff4" , 0x8b7765 }, 253 | { "navajowhite1" , 0xffdead }, 254 | { "navajowhite2" , 0xeecfa1 }, 255 | { "navajowhite3" , 0xcdb38b }, 256 | { "navajowhite4" , 0x8b795e }, 257 | { "lemonchiffon1" , 0xfffacd }, 258 | { "lemonchiffon2" , 0xeee9bf }, 259 | { "lemonchiffon3" , 0xcdc9a5 }, 260 | { "lemonchiffon4" , 0x8b8970 }, 261 | { "cornsilk1" , 0xfff8dc }, 262 | { "cornsilk2" , 0xeee8cd }, 263 | { "cornsilk3" , 0xcdc8b1 }, 264 | { "cornsilk4" , 0x8b8878 }, 265 | { "ivory1" , 0xfffff0 }, 266 | { "ivory2" , 0xeeeee0 }, 267 | { "ivory3" , 0xcdcdc1 }, 268 | { "ivory4" , 0x8b8b83 }, 269 | { "honeydew1" , 0xf0fff0 }, 270 | { "honeydew2" , 0xe0eee0 }, 271 | { "honeydew3" , 0xc1cdc1 }, 272 | { "honeydew4" , 0x838b83 }, 273 | { "lavenderblush1" , 0xfff0f5 }, 274 | { "lavenderblush2" , 0xeee0e5 }, 275 | { "lavenderblush3" , 0xcdc1c5 }, 276 | { "lavenderblush4" , 0x8b8386 }, 277 | { "mistyrose1" , 0xffe4e1 }, 278 | { "mistyrose2" , 0xeed5d2 }, 279 | { "mistyrose3" , 0xcdb7b5 }, 280 | { "mistyrose4" , 0x8b7d7b }, 281 | { "azure1" , 0xf0ffff }, 282 | { "azure2" , 0xe0eeee }, 283 | { "azure3" , 0xc1cdcd }, 284 | { "azure4" , 0x838b8b }, 285 | { "slateblue1" , 0x836fff }, 286 | { "slateblue2" , 0x7a67ee }, 287 | { "slateblue3" , 0x6959cd }, 288 | { "slateblue4" , 0x473c8b }, 289 | { "royalblue1" , 0x4876ff }, 290 | { "royalblue2" , 0x436eee }, 291 | { "royalblue3" , 0x3a5fcd }, 292 | { "royalblue4" , 0x27408b }, 293 | { "blue1" , 0xff }, 294 | { "blue2" , 0xee }, 295 | { "blue3" , 0xcd }, 296 | { "blue4" , 0x8b }, 297 | { "dodgerblue1" , 0x1e90ff }, 298 | { "dodgerblue2" , 0x1c86ee }, 299 | { "dodgerblue3" , 0x1874cd }, 300 | { "dodgerblue4" , 0x104e8b }, 301 | { "steelblue1" , 0x63b8ff }, 302 | { "steelblue2" , 0x5cacee }, 303 | { "steelblue3" , 0x4f94cd }, 304 | { "steelblue4" , 0x36648b }, 305 | { "deepskyblue1" , 0xbfff }, 306 | { "deepskyblue2" , 0xb2ee }, 307 | { "deepskyblue3" , 0x9acd }, 308 | { "deepskyblue4" , 0x688b }, 309 | { "skyblue1" , 0x87ceff }, 310 | { "skyblue2" , 0x7ec0ee }, 311 | { "skyblue3" , 0x6ca6cd }, 312 | { "skyblue4" , 0x4a708b }, 313 | { "lightskyblue1" , 0xb0e2ff }, 314 | { "lightskyblue2" , 0xa4d3ee }, 315 | { "lightskyblue3" , 0x8db6cd }, 316 | { "lightskyblue4" , 0x607b8b }, 317 | { "slategray1" , 0xc6e2ff }, 318 | { "slategray2" , 0xb9d3ee }, 319 | { "slategray3" , 0x9fb6cd }, 320 | { "slategray4" , 0x6c7b8b }, 321 | { "lightsteelblue1" , 0xcae1ff }, 322 | { "lightsteelblue2" , 0xbcd2ee }, 323 | { "lightsteelblue3" , 0xa2b5cd }, 324 | { "lightsteelblue4" , 0x6e7b8b }, 325 | { "lightblue1" , 0xbfefff }, 326 | { "lightblue2" , 0xb2dfee }, 327 | { "lightblue3" , 0x9ac0cd }, 328 | { "lightblue4" , 0x68838b }, 329 | { "lightcyan1" , 0xe0ffff }, 330 | { "lightcyan2" , 0xd1eeee }, 331 | { "lightcyan3" , 0xb4cdcd }, 332 | { "lightcyan4" , 0x7a8b8b }, 333 | { "paleturquoise1" , 0xbbffff }, 334 | { "paleturquoise2" , 0xaeeeee }, 335 | { "paleturquoise3" , 0x96cdcd }, 336 | { "paleturquoise4" , 0x668b8b }, 337 | { "cadetblue1" , 0x98f5ff }, 338 | { "cadetblue2" , 0x8ee5ee }, 339 | { "cadetblue3" , 0x7ac5cd }, 340 | { "cadetblue4" , 0x53868b }, 341 | { "turquoise1" , 0xf5ff }, 342 | { "turquoise2" , 0xe5ee }, 343 | { "turquoise3" , 0xc5cd }, 344 | { "turquoise4" , 0x868b }, 345 | { "cyan1" , 0xffff }, 346 | { "cyan2" , 0xeeee }, 347 | { "cyan3" , 0xcdcd }, 348 | { "cyan4" , 0x8b8b }, 349 | { "darkslategray1" , 0x97ffff }, 350 | { "darkslategray2" , 0x8deeee }, 351 | { "darkslategray3" , 0x79cdcd }, 352 | { "darkslategray4" , 0x528b8b }, 353 | { "aquamarine1" , 0x7fffd4 }, 354 | { "aquamarine2" , 0x76eec6 }, 355 | { "aquamarine3" , 0x66cdaa }, 356 | { "aquamarine4" , 0x458b74 }, 357 | { "darkseagreen1" , 0xc1ffc1 }, 358 | { "darkseagreen2" , 0xb4eeb4 }, 359 | { "darkseagreen3" , 0x9bcd9b }, 360 | { "darkseagreen4" , 0x698b69 }, 361 | { "seagreen1" , 0x54ff9f }, 362 | { "seagreen2" , 0x4eee94 }, 363 | { "seagreen3" , 0x43cd80 }, 364 | { "seagreen4" , 0x2e8b57 }, 365 | { "palegreen1" , 0x9aff9a }, 366 | { "palegreen2" , 0x90ee90 }, 367 | { "palegreen3" , 0x7ccd7c }, 368 | { "palegreen4" , 0x548b54 }, 369 | { "springgreen1" , 0xff7f }, 370 | { "springgreen2" , 0xee76 }, 371 | { "springgreen3" , 0xcd66 }, 372 | { "springgreen4" , 0x8b45 }, 373 | { "green1" , 0xff00 }, 374 | { "green2" , 0xee00 }, 375 | { "green3" , 0xcd00 }, 376 | { "green4" , 0x8b00 }, 377 | { "chartreuse1" , 0x7fff00 }, 378 | { "chartreuse2" , 0x76ee00 }, 379 | { "chartreuse3" , 0x66cd00 }, 380 | { "chartreuse4" , 0x458b00 }, 381 | { "olivedrab1" , 0xc0ff3e }, 382 | { "olivedrab2" , 0xb3ee3a }, 383 | { "olivedrab3" , 0x9acd32 }, 384 | { "olivedrab4" , 0x698b22 }, 385 | { "darkolivegreen1" , 0xcaff70 }, 386 | { "darkolivegreen2" , 0xbcee68 }, 387 | { "darkolivegreen3" , 0xa2cd5a }, 388 | { "darkolivegreen4" , 0x6e8b3d }, 389 | { "khaki1" , 0xfff68f }, 390 | { "khaki2" , 0xeee685 }, 391 | { "khaki3" , 0xcdc673 }, 392 | { "khaki4" , 0x8b864e }, 393 | { "lightgoldenrod1" , 0xffec8b }, 394 | { "lightgoldenrod2" , 0xeedc82 }, 395 | { "lightgoldenrod3" , 0xcdbe70 }, 396 | { "lightgoldenrod4" , 0x8b814c }, 397 | { "lightyellow1" , 0xffffe0 }, 398 | { "lightyellow2" , 0xeeeed1 }, 399 | { "lightyellow3" , 0xcdcdb4 }, 400 | { "lightyellow4" , 0x8b8b7a }, 401 | { "yellow1" , 0xffff00 }, 402 | { "yellow2" , 0xeeee00 }, 403 | { "yellow3" , 0xcdcd00 }, 404 | { "yellow4" , 0x8b8b00 }, 405 | { "gold1" , 0xffd700 }, 406 | { "gold2" , 0xeec900 }, 407 | { "gold3" , 0xcdad00 }, 408 | { "gold4" , 0x8b7500 }, 409 | { "goldenrod1" , 0xffc125 }, 410 | { "goldenrod2" , 0xeeb422 }, 411 | { "goldenrod3" , 0xcd9b1d }, 412 | { "goldenrod4" , 0x8b6914 }, 413 | { "darkgoldenrod1" , 0xffb90f }, 414 | { "darkgoldenrod2" , 0xeead0e }, 415 | { "darkgoldenrod3" , 0xcd950c }, 416 | { "darkgoldenrod4" , 0x8b6508 }, 417 | { "rosybrown1" , 0xffc1c1 }, 418 | { "rosybrown2" , 0xeeb4b4 }, 419 | { "rosybrown3" , 0xcd9b9b }, 420 | { "rosybrown4" , 0x8b6969 }, 421 | { "indianred1" , 0xff6a6a }, 422 | { "indianred2" , 0xee6363 }, 423 | { "indianred3" , 0xcd5555 }, 424 | { "indianred4" , 0x8b3a3a }, 425 | { "sienna1" , 0xff8247 }, 426 | { "sienna2" , 0xee7942 }, 427 | { "sienna3" , 0xcd6839 }, 428 | { "sienna4" , 0x8b4726 }, 429 | { "burlywood1" , 0xffd39b }, 430 | { "burlywood2" , 0xeec591 }, 431 | { "burlywood3" , 0xcdaa7d }, 432 | { "burlywood4" , 0x8b7355 }, 433 | { "wheat1" , 0xffe7ba }, 434 | { "wheat2" , 0xeed8ae }, 435 | { "wheat3" , 0xcdba96 }, 436 | { "wheat4" , 0x8b7e66 }, 437 | { "tan1" , 0xffa54f }, 438 | { "tan2" , 0xee9a49 }, 439 | { "tan3" , 0xcd853f }, 440 | { "tan4" , 0x8b5a2b }, 441 | { "chocolate1" , 0xff7f24 }, 442 | { "chocolate2" , 0xee7621 }, 443 | { "chocolate3" , 0xcd661d }, 444 | { "chocolate4" , 0x8b4513 }, 445 | { "firebrick1" , 0xff3030 }, 446 | { "firebrick2" , 0xee2c2c }, 447 | { "firebrick3" , 0xcd2626 }, 448 | { "firebrick4" , 0x8b1a1a }, 449 | { "brown1" , 0xff4040 }, 450 | { "brown2" , 0xee3b3b }, 451 | { "brown3" , 0xcd3333 }, 452 | { "brown4" , 0x8b2323 }, 453 | { "salmon1" , 0xff8c69 }, 454 | { "salmon2" , 0xee8262 }, 455 | { "salmon3" , 0xcd7054 }, 456 | { "salmon4" , 0x8b4c39 }, 457 | { "lightsalmon1" , 0xffa07a }, 458 | { "lightsalmon2" , 0xee9572 }, 459 | { "lightsalmon3" , 0xcd8162 }, 460 | { "lightsalmon4" , 0x8b5742 }, 461 | { "orange1" , 0xffa500 }, 462 | { "orange2" , 0xee9a00 }, 463 | { "orange3" , 0xcd8500 }, 464 | { "orange4" , 0x8b5a00 }, 465 | { "darkorange1" , 0xff7f00 }, 466 | { "darkorange2" , 0xee7600 }, 467 | { "darkorange3" , 0xcd6600 }, 468 | { "darkorange4" , 0x8b4500 }, 469 | { "coral1" , 0xff7256 }, 470 | { "coral2" , 0xee6a50 }, 471 | { "coral3" , 0xcd5b45 }, 472 | { "coral4" , 0x8b3e2f }, 473 | { "tomato1" , 0xff6347 }, 474 | { "tomato2" , 0xee5c42 }, 475 | { "tomato3" , 0xcd4f39 }, 476 | { "tomato4" , 0x8b3626 }, 477 | { "orangered1" , 0xff4500 }, 478 | { "orangered2" , 0xee4000 }, 479 | { "orangered3" , 0xcd3700 }, 480 | { "orangered4" , 0x8b2500 }, 481 | { "red1" , 0xff0000 }, 482 | { "red2" , 0xee0000 }, 483 | { "red3" , 0xcd0000 }, 484 | { "red4" , 0x8b0000 }, 485 | { "deeppink1" , 0xff1493 }, 486 | { "deeppink2" , 0xee1289 }, 487 | { "deeppink3" , 0xcd1076 }, 488 | { "deeppink4" , 0x8b0a50 }, 489 | { "hotpink1" , 0xff6eb4 }, 490 | { "hotpink2" , 0xee6aa7 }, 491 | { "hotpink3" , 0xcd6090 }, 492 | { "hotpink4" , 0x8b3a62 }, 493 | { "pink1" , 0xffb5c5 }, 494 | { "pink2" , 0xeea9b8 }, 495 | { "pink3" , 0xcd919e }, 496 | { "pink4" , 0x8b636c }, 497 | { "lightpink1" , 0xffaeb9 }, 498 | { "lightpink2" , 0xeea2ad }, 499 | { "lightpink3" , 0xcd8c95 }, 500 | { "lightpink4" , 0x8b5f65 }, 501 | { "palevioletred1" , 0xff82ab }, 502 | { "palevioletred2" , 0xee799f }, 503 | { "palevioletred3" , 0xcd6889 }, 504 | { "palevioletred4" , 0x8b475d }, 505 | { "maroon1" , 0xff34b3 }, 506 | { "maroon2" , 0xee30a7 }, 507 | { "maroon3" , 0xcd2990 }, 508 | { "maroon4" , 0x8b1c62 }, 509 | { "violetred1" , 0xff3e96 }, 510 | { "violetred2" , 0xee3a8c }, 511 | { "violetred3" , 0xcd3278 }, 512 | { "violetred4" , 0x8b2252 }, 513 | { "magenta1" , 0xff00ff }, 514 | { "magenta2" , 0xee00ee }, 515 | { "magenta3" , 0xcd00cd }, 516 | { "magenta4" , 0x8b008b }, 517 | { "orchid1" , 0xff83fa }, 518 | { "orchid2" , 0xee7ae9 }, 519 | { "orchid3" , 0xcd69c9 }, 520 | { "orchid4" , 0x8b4789 }, 521 | { "plum1" , 0xffbbff }, 522 | { "plum2" , 0xeeaeee }, 523 | { "plum3" , 0xcd96cd }, 524 | { "plum4" , 0x8b668b }, 525 | { "mediumorchid1" , 0xe066ff }, 526 | { "mediumorchid2" , 0xd15fee }, 527 | { "mediumorchid3" , 0xb452cd }, 528 | { "mediumorchid4" , 0x7a378b }, 529 | { "darkorchid1" , 0xbf3eff }, 530 | { "darkorchid2" , 0xb23aee }, 531 | { "darkorchid3" , 0x9a32cd }, 532 | { "darkorchid4" , 0x68228b }, 533 | { "purple1" , 0x9b30ff }, 534 | { "purple2" , 0x912cee }, 535 | { "purple3" , 0x7d26cd }, 536 | { "purple4" , 0x551a8b }, 537 | { "mediumpurple1" , 0xab82ff }, 538 | { "mediumpurple2" , 0x9f79ee }, 539 | { "mediumpurple3" , 0x8968cd }, 540 | { "mediumpurple4" , 0x5d478b }, 541 | { "thistle1" , 0xffe1ff }, 542 | { "thistle2" , 0xeed2ee }, 543 | { "thistle3" , 0xcdb5cd }, 544 | { "thistle4" , 0x8b7b8b }, 545 | { "gray0" , 0x0 }, 546 | { "grey0" , 0x0 }, 547 | { "gray1" , 0x30303 }, 548 | { "grey1" , 0x30303 }, 549 | { "gray2" , 0x50505 }, 550 | { "grey2" , 0x50505 }, 551 | { "gray3" , 0x80808 }, 552 | { "grey3" , 0x80808 }, 553 | { "gray4" , 0xa0a0a }, 554 | { "grey4" , 0xa0a0a }, 555 | { "gray5" , 0xd0d0d }, 556 | { "grey5" , 0xd0d0d }, 557 | { "gray6" , 0xf0f0f }, 558 | { "grey6" , 0xf0f0f }, 559 | { "gray7" , 0x121212 }, 560 | { "grey7" , 0x121212 }, 561 | { "gray8" , 0x141414 }, 562 | { "grey8" , 0x141414 }, 563 | { "gray9" , 0x171717 }, 564 | { "grey9" , 0x171717 }, 565 | { "gray10" , 0x1a1a1a }, 566 | { "grey10" , 0x1a1a1a }, 567 | { "gray11" , 0x1c1c1c }, 568 | { "grey11" , 0x1c1c1c }, 569 | { "gray12" , 0x1f1f1f }, 570 | { "grey12" , 0x1f1f1f }, 571 | { "gray13" , 0x212121 }, 572 | { "grey13" , 0x212121 }, 573 | { "gray14" , 0x242424 }, 574 | { "grey14" , 0x242424 }, 575 | { "gray15" , 0x262626 }, 576 | { "grey15" , 0x262626 }, 577 | { "gray16" , 0x292929 }, 578 | { "grey16" , 0x292929 }, 579 | { "gray17" , 0x2b2b2b }, 580 | { "grey17" , 0x2b2b2b }, 581 | { "gray18" , 0x2e2e2e }, 582 | { "grey18" , 0x2e2e2e }, 583 | { "gray19" , 0x303030 }, 584 | { "grey19" , 0x303030 }, 585 | { "gray20" , 0x333333 }, 586 | { "grey20" , 0x333333 }, 587 | { "gray21" , 0x363636 }, 588 | { "grey21" , 0x363636 }, 589 | { "gray22" , 0x383838 }, 590 | { "grey22" , 0x383838 }, 591 | { "gray23" , 0x3b3b3b }, 592 | { "grey23" , 0x3b3b3b }, 593 | { "gray24" , 0x3d3d3d }, 594 | { "grey24" , 0x3d3d3d }, 595 | { "gray25" , 0x404040 }, 596 | { "grey25" , 0x404040 }, 597 | { "gray26" , 0x424242 }, 598 | { "grey26" , 0x424242 }, 599 | { "gray27" , 0x454545 }, 600 | { "grey27" , 0x454545 }, 601 | { "gray28" , 0x474747 }, 602 | { "grey28" , 0x474747 }, 603 | { "gray29" , 0x4a4a4a }, 604 | { "grey29" , 0x4a4a4a }, 605 | { "gray30" , 0x4d4d4d }, 606 | { "grey30" , 0x4d4d4d }, 607 | { "gray31" , 0x4f4f4f }, 608 | { "grey31" , 0x4f4f4f }, 609 | { "gray32" , 0x525252 }, 610 | { "grey32" , 0x525252 }, 611 | { "gray33" , 0x545454 }, 612 | { "grey33" , 0x545454 }, 613 | { "gray34" , 0x575757 }, 614 | { "grey34" , 0x575757 }, 615 | { "gray35" , 0x595959 }, 616 | { "grey35" , 0x595959 }, 617 | { "gray36" , 0x5c5c5c }, 618 | { "grey36" , 0x5c5c5c }, 619 | { "gray37" , 0x5e5e5e }, 620 | { "grey37" , 0x5e5e5e }, 621 | { "gray38" , 0x616161 }, 622 | { "grey38" , 0x616161 }, 623 | { "gray39" , 0x636363 }, 624 | { "grey39" , 0x636363 }, 625 | { "gray40" , 0x666666 }, 626 | { "grey40" , 0x666666 }, 627 | { "gray41" , 0x696969 }, 628 | { "grey41" , 0x696969 }, 629 | { "gray42" , 0x6b6b6b }, 630 | { "grey42" , 0x6b6b6b }, 631 | { "gray43" , 0x6e6e6e }, 632 | { "grey43" , 0x6e6e6e }, 633 | { "gray44" , 0x707070 }, 634 | { "grey44" , 0x707070 }, 635 | { "gray45" , 0x737373 }, 636 | { "grey45" , 0x737373 }, 637 | { "gray46" , 0x757575 }, 638 | { "grey46" , 0x757575 }, 639 | { "gray47" , 0x787878 }, 640 | { "grey47" , 0x787878 }, 641 | { "gray48" , 0x7a7a7a }, 642 | { "grey48" , 0x7a7a7a }, 643 | { "gray49" , 0x7d7d7d }, 644 | { "grey49" , 0x7d7d7d }, 645 | { "gray50" , 0x7f7f7f }, 646 | { "grey50" , 0x7f7f7f }, 647 | { "gray51" , 0x828282 }, 648 | { "grey51" , 0x828282 }, 649 | { "gray52" , 0x858585 }, 650 | { "grey52" , 0x858585 }, 651 | { "gray53" , 0x878787 }, 652 | { "grey53" , 0x878787 }, 653 | { "gray54" , 0x8a8a8a }, 654 | { "grey54" , 0x8a8a8a }, 655 | { "gray55" , 0x8c8c8c }, 656 | { "grey55" , 0x8c8c8c }, 657 | { "gray56" , 0x8f8f8f }, 658 | { "grey56" , 0x8f8f8f }, 659 | { "gray57" , 0x919191 }, 660 | { "grey57" , 0x919191 }, 661 | { "gray58" , 0x949494 }, 662 | { "grey58" , 0x949494 }, 663 | { "gray59" , 0x969696 }, 664 | { "grey59" , 0x969696 }, 665 | { "gray60" , 0x999999 }, 666 | { "grey60" , 0x999999 }, 667 | { "gray61" , 0x9c9c9c }, 668 | { "grey61" , 0x9c9c9c }, 669 | { "gray62" , 0x9e9e9e }, 670 | { "grey62" , 0x9e9e9e }, 671 | { "gray63" , 0xa1a1a1 }, 672 | { "grey63" , 0xa1a1a1 }, 673 | { "gray64" , 0xa3a3a3 }, 674 | { "grey64" , 0xa3a3a3 }, 675 | { "gray65" , 0xa6a6a6 }, 676 | { "grey65" , 0xa6a6a6 }, 677 | { "gray66" , 0xa8a8a8 }, 678 | { "grey66" , 0xa8a8a8 }, 679 | { "gray67" , 0xababab }, 680 | { "grey67" , 0xababab }, 681 | { "gray68" , 0xadadad }, 682 | { "grey68" , 0xadadad }, 683 | { "gray69" , 0xb0b0b0 }, 684 | { "grey69" , 0xb0b0b0 }, 685 | { "gray70" , 0xb3b3b3 }, 686 | { "grey70" , 0xb3b3b3 }, 687 | { "gray71" , 0xb5b5b5 }, 688 | { "grey71" , 0xb5b5b5 }, 689 | { "gray72" , 0xb8b8b8 }, 690 | { "grey72" , 0xb8b8b8 }, 691 | { "gray73" , 0xbababa }, 692 | { "grey73" , 0xbababa }, 693 | { "gray74" , 0xbdbdbd }, 694 | { "grey74" , 0xbdbdbd }, 695 | { "gray75" , 0xbfbfbf }, 696 | { "grey75" , 0xbfbfbf }, 697 | { "gray76" , 0xc2c2c2 }, 698 | { "grey76" , 0xc2c2c2 }, 699 | { "gray77" , 0xc4c4c4 }, 700 | { "grey77" , 0xc4c4c4 }, 701 | { "gray78" , 0xc7c7c7 }, 702 | { "grey78" , 0xc7c7c7 }, 703 | { "gray79" , 0xc9c9c9 }, 704 | { "grey79" , 0xc9c9c9 }, 705 | { "gray80" , 0xcccccc }, 706 | { "grey80" , 0xcccccc }, 707 | { "gray81" , 0xcfcfcf }, 708 | { "grey81" , 0xcfcfcf }, 709 | { "gray82" , 0xd1d1d1 }, 710 | { "grey82" , 0xd1d1d1 }, 711 | { "gray83" , 0xd4d4d4 }, 712 | { "grey83" , 0xd4d4d4 }, 713 | { "gray84" , 0xd6d6d6 }, 714 | { "grey84" , 0xd6d6d6 }, 715 | { "gray85" , 0xd9d9d9 }, 716 | { "grey85" , 0xd9d9d9 }, 717 | { "gray86" , 0xdbdbdb }, 718 | { "grey86" , 0xdbdbdb }, 719 | { "gray87" , 0xdedede }, 720 | { "grey87" , 0xdedede }, 721 | { "gray88" , 0xe0e0e0 }, 722 | { "grey88" , 0xe0e0e0 }, 723 | { "gray89" , 0xe3e3e3 }, 724 | { "grey89" , 0xe3e3e3 }, 725 | { "gray90" , 0xe5e5e5 }, 726 | { "grey90" , 0xe5e5e5 }, 727 | { "gray91" , 0xe8e8e8 }, 728 | { "grey91" , 0xe8e8e8 }, 729 | { "gray92" , 0xebebeb }, 730 | { "grey92" , 0xebebeb }, 731 | { "gray93" , 0xededed }, 732 | { "grey93" , 0xededed }, 733 | { "gray94" , 0xf0f0f0 }, 734 | { "grey94" , 0xf0f0f0 }, 735 | { "gray95" , 0xf2f2f2 }, 736 | { "grey95" , 0xf2f2f2 }, 737 | { "gray96" , 0xf5f5f5 }, 738 | { "grey96" , 0xf5f5f5 }, 739 | { "gray97" , 0xf7f7f7 }, 740 | { "grey97" , 0xf7f7f7 }, 741 | { "gray98" , 0xfafafa }, 742 | { "grey98" , 0xfafafa }, 743 | { "gray99" , 0xfcfcfc }, 744 | { "grey99" , 0xfcfcfc }, 745 | { "gray100" , 0xffffff }, 746 | { "grey100" , 0xffffff }, 747 | { "dark grey" , 0xa9a9a9 }, 748 | { "darkgrey" , 0xa9a9a9 }, 749 | { "dark gray" , 0xa9a9a9 }, 750 | { "darkgray" , 0xa9a9a9 }, 751 | { "dark blue" , 0x8b }, 752 | { "darkblue" , 0x8b }, 753 | { "dark cyan" , 0x8b8b }, 754 | { "darkcyan" , 0x8b8b }, 755 | { "dark magenta" , 0x8b008b }, 756 | { "darkmagenta" , 0x8b008b }, 757 | { "dark red" , 0x8b0000 }, 758 | { "darkred" , 0x8b0000 }, 759 | { "light green" , 0x90ee90 }, 760 | { "lightgreen" , 0x90ee90 }, 761 | { "none", -1 }, 762 | { 0, 0 } 763 | }; 764 | -------------------------------------------------------------------------------- /mlx/mlx_shaders.c: -------------------------------------------------------------------------------- 1 | // mlx_shaders.c 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "mlx_int.h" 8 | 9 | 10 | void display_log(GLuint object, void (*param_func)(), void (*getlog_func)()) 11 | { 12 | GLint log_length; 13 | char *log; 14 | 15 | param_func(object, GL_INFO_LOG_LENGTH, &log_length); 16 | log = malloc(log_length); 17 | getlog_func(object, log_length, NULL, log); 18 | fprintf(stderr, "%s", log); 19 | free(log); 20 | } 21 | 22 | 23 | int mlx_shaders_pixel(glsl_info_t *glsl) 24 | { 25 | char *source; 26 | int length; 27 | GLint action_ok; 28 | 29 | glsl->pixel_vshader = glCreateShader(GL_VERTEX_SHADER); 30 | source = strdup("#version 110 \n" 31 | "attribute vec2 position;" 32 | "varying vec2 texcoord;" 33 | "void main()" 34 | "{" 35 | " gl_Position = vec4( position, 0.0, 1.0);" 36 | " texcoord = vec2(position[0]+1.0, 1.0 - position[1]) / 2.0;" 37 | "}"); 38 | length = strlen(source); 39 | glShaderSource(glsl->pixel_vshader, 1, (const GLchar**)&source, &length); 40 | glCompileShader(glsl->pixel_vshader); 41 | free(source); 42 | 43 | glGetShaderiv(glsl->pixel_vshader, GL_COMPILE_STATUS, &action_ok); 44 | if (!action_ok) { 45 | fprintf(stderr, "Failed to compile pixel vshader :\n"); 46 | display_log(glsl->pixel_vshader, glGetShaderiv, glGetShaderInfoLog); 47 | return (1); 48 | } 49 | 50 | glsl->pixel_fshader = glCreateShader(GL_FRAGMENT_SHADER); 51 | source = strdup("#version 110 \n" 52 | "uniform sampler2D texture;" 53 | "varying vec2 texcoord;" 54 | "void main()" 55 | "{" 56 | " gl_FragColor = texture2D(texture, texcoord);" 57 | "}"); 58 | length = strlen(source); 59 | glShaderSource(glsl->pixel_fshader, 1, (const GLchar**)&source, &length); 60 | glCompileShader(glsl->pixel_fshader); 61 | free(source); 62 | 63 | glGetShaderiv(glsl->pixel_fshader, GL_COMPILE_STATUS, &action_ok); 64 | if (!action_ok) { 65 | fprintf(stderr, "Failed to compile pixel fshader :\n"); 66 | display_log(glsl->pixel_fshader, glGetShaderiv, glGetShaderInfoLog); 67 | return (1); 68 | } 69 | 70 | glsl->pixel_program = glCreateProgram(); 71 | glAttachShader(glsl->pixel_program, glsl->pixel_vshader); 72 | glAttachShader(glsl->pixel_program, glsl->pixel_fshader); 73 | glLinkProgram(glsl->pixel_program); 74 | 75 | glGetProgramiv(glsl->pixel_program, GL_LINK_STATUS, &action_ok); 76 | if (!action_ok) { 77 | fprintf(stderr, "Failed to link pixel shader program:\n"); 78 | display_log(glsl->pixel_program, glGetProgramiv, glGetProgramInfoLog); 79 | return (1); 80 | } 81 | 82 | glFlush(); 83 | 84 | return (0); 85 | } 86 | 87 | 88 | int mlx_shaders_image(glsl_info_t *glsl) 89 | { 90 | char *source; 91 | int length; 92 | GLint action_ok; 93 | 94 | glsl->image_vshader = glCreateShader(GL_VERTEX_SHADER); 95 | source = strdup("#version 110 \n" 96 | "attribute vec2 position;" 97 | "uniform vec2 winhalfsize;" 98 | "uniform vec2 imagepos;" 99 | "uniform vec2 imagesize;" 100 | "varying vec2 texcoord;" 101 | "void main()" 102 | "{" 103 | " texcoord = position / imagesize;" 104 | " vec2 pos = position - winhalfsize + imagepos;" 105 | " pos = pos / winhalfsize;" 106 | " gl_Position = vec4( pos, 0.0, 1.0);" 107 | "}"); 108 | length = strlen(source); 109 | glShaderSource(glsl->image_vshader, 1, (const GLchar**)&source, &length); 110 | glCompileShader(glsl->image_vshader); 111 | free(source); 112 | 113 | glGetShaderiv(glsl->image_vshader, GL_COMPILE_STATUS, &action_ok); 114 | if (!action_ok) { 115 | fprintf(stderr, "Failed to compile image vshader :\n"); 116 | display_log(glsl->image_vshader, glGetShaderiv, glGetShaderInfoLog); 117 | return (1); 118 | } 119 | 120 | glsl->image_fshader = glCreateShader(GL_FRAGMENT_SHADER); 121 | source = strdup("#version 110 \n" 122 | "uniform sampler2D texture;" 123 | "varying vec2 texcoord;" 124 | "void main()" 125 | "{" 126 | " gl_FragColor = texture2D(texture, texcoord);" 127 | "}"); 128 | length = strlen(source); 129 | glShaderSource(glsl->image_fshader, 1, (const GLchar**)&source, &length); 130 | glCompileShader(glsl->image_fshader); 131 | free(source); 132 | 133 | glGetShaderiv(glsl->image_fshader, GL_COMPILE_STATUS, &action_ok); 134 | if (!action_ok) { 135 | fprintf(stderr, "Failed to compile image fshader :\n"); 136 | display_log(glsl->image_fshader, glGetShaderiv, glGetShaderInfoLog); 137 | return (1); 138 | } 139 | 140 | glsl->image_program = glCreateProgram(); 141 | glAttachShader(glsl->image_program, glsl->image_vshader); 142 | glAttachShader(glsl->image_program, glsl->image_fshader); 143 | glLinkProgram(glsl->image_program); 144 | 145 | glGetProgramiv(glsl->image_program, GL_LINK_STATUS, &action_ok); 146 | if (!action_ok) { 147 | fprintf(stderr, "Failed to link image shader program:\n"); 148 | display_log(glsl->image_program, glGetProgramiv, glGetProgramInfoLog); 149 | return (1); 150 | } 151 | 152 | glFlush(); 153 | 154 | return (0); 155 | } 156 | 157 | 158 | 159 | 160 | int mlx_shaders_font(glsl_info_t *glsl) 161 | { 162 | char *source; 163 | int length; 164 | GLint action_ok; 165 | 166 | glsl->font_vshader = glCreateShader(GL_VERTEX_SHADER); 167 | source = strdup("#version 110 \n" 168 | "attribute vec2 position;" 169 | "uniform vec2 winhalfsize;" 170 | "uniform vec2 fontposinwin;" 171 | "uniform vec2 fontposinatlas;" 172 | "uniform vec2 fontatlassize;" 173 | "varying vec2 texcoord;" 174 | "void main()" 175 | "{" 176 | #ifdef STRINGPUTX11 177 | " texcoord = (position * vec2(1.4, -1.4) + fontposinatlas ) / fontatlassize;" 178 | #else 179 | " texcoord = (position * vec2(1.0, -1.0) + fontposinatlas ) / fontatlassize;" 180 | #endif 181 | " vec2 pos = position - winhalfsize + fontposinwin;" 182 | " pos = pos / winhalfsize;" 183 | " gl_Position = vec4( pos, 0.0, 1.0);" 184 | "}"); 185 | length = strlen(source); 186 | glShaderSource(glsl->font_vshader, 1, (const GLchar**)&source, &length); 187 | glCompileShader(glsl->font_vshader); 188 | free(source); 189 | 190 | glGetShaderiv(glsl->font_vshader, GL_COMPILE_STATUS, &action_ok); 191 | if (!action_ok) { 192 | fprintf(stderr, "Failed to compile font vshader :\n"); 193 | display_log(glsl->font_vshader, glGetShaderiv, glGetShaderInfoLog); 194 | return (1); 195 | } 196 | 197 | glsl->font_fshader = glCreateShader(GL_FRAGMENT_SHADER); 198 | source = strdup("#version 110 \n" 199 | "uniform sampler2D texture;" 200 | "uniform vec4 color;" 201 | "varying vec2 texcoord;" 202 | "void main()" 203 | "{" 204 | " gl_FragColor = color * texture2D(texture, texcoord);" 205 | "}"); 206 | length = strlen(source); 207 | glShaderSource(glsl->font_fshader, 1, (const GLchar**)&source, &length); 208 | glCompileShader(glsl->font_fshader); 209 | free(source); 210 | 211 | glGetShaderiv(glsl->font_fshader, GL_COMPILE_STATUS, &action_ok); 212 | if (!action_ok) { 213 | fprintf(stderr, "Failed to compile font fshader :\n"); 214 | display_log(glsl->font_fshader, glGetShaderiv, glGetShaderInfoLog); 215 | return (1); 216 | } 217 | 218 | glsl->font_program = glCreateProgram(); 219 | glAttachShader(glsl->font_program, glsl->font_vshader); 220 | glAttachShader(glsl->font_program, glsl->font_fshader); 221 | glLinkProgram(glsl->font_program); 222 | 223 | glGetProgramiv(glsl->font_program, GL_LINK_STATUS, &action_ok); 224 | if (!action_ok) { 225 | fprintf(stderr, "Failed to link font shader program:\n"); 226 | display_log(glsl->font_program, glGetProgramiv, glGetProgramInfoLog); 227 | return (1); 228 | } 229 | 230 | glFlush(); 231 | 232 | return (0); 233 | } 234 | 235 | 236 | 237 | int mlx_shaders(glsl_info_t *glsl) 238 | { 239 | return (mlx_shaders_pixel(glsl) + mlx_shaders_image(glsl) + mlx_shaders_font(glsl)); 240 | } 241 | -------------------------------------------------------------------------------- /mlx/mlx_shaders.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/mlx_shaders.o -------------------------------------------------------------------------------- /mlx/mlx_xpm.c: -------------------------------------------------------------------------------- 1 | // mlx xpm 2 | // by ol 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "mlx_int.h" 12 | 13 | typedef struct s_xpm_col 14 | { 15 | int name; 16 | int col; 17 | } t_xpm_col; 18 | 19 | 20 | struct s_col_name 21 | { 22 | char *name; 23 | int color; 24 | }; 25 | 26 | //extern struct s_col_name mlx_col_name[]; 27 | #include "mlx_rgb.c" 28 | 29 | 30 | #define RETURN { if (colors) free(colors); if (tab) free(tab); \ 31 | if (colors_direct) free(colors_direct); \ 32 | if (img) mlx_destroy_image(xvar, img); \ 33 | return ((void *)0); } 34 | 35 | 36 | 37 | 38 | char *mlx_int_get_line(char *ptr,int *pos,int size) 39 | { 40 | int pos2; 41 | int pos3; 42 | int pos4; 43 | 44 | if ((pos2 = mlx_int_str_str(ptr+*pos,"\"",size-*pos))==-1) 45 | return ((char *)0); 46 | if ((pos3 = mlx_int_str_str(ptr+*pos+pos2+1,"\"",size-*pos-pos2-1))==-1) 47 | return ((char *)0); 48 | *(ptr+*pos+pos2) = 0; 49 | *(ptr+*pos+pos2+1+pos3) = 0; 50 | pos4 = *pos+pos2+1; 51 | *pos += pos2+pos3+2; 52 | return (ptr+pos4); 53 | } 54 | 55 | 56 | 57 | char *mlx_int_static_line(char **xpm_data,int *pos,int size) 58 | { 59 | static char *copy = 0; 60 | static int len = 0; 61 | int len2; 62 | char *str; 63 | 64 | str = xpm_data[(*pos)++]; 65 | if ((len2 = strlen(str))>len) 66 | { 67 | if (copy) 68 | free(copy); 69 | if (!(copy = malloc(len2+1))) 70 | return ((char *)0); 71 | len = len2; 72 | } 73 | /* strcpy(copy,str); */ 74 | strlcpy(copy, str, len2+1); 75 | return (copy); 76 | } 77 | 78 | 79 | int mlx_int_get_col_name(char *str,int size) 80 | { 81 | int result; 82 | 83 | result = 0; 84 | while (size--) 85 | result = (result<<8)+*(str++); 86 | return (result); 87 | } 88 | 89 | int mlx_int_get_text_rgb(char *name, char *end) 90 | { 91 | int i; 92 | char buff[64]; 93 | 94 | if (*name == '#') 95 | return (strtol(name+1,0,16)); 96 | if (end) 97 | { 98 | snprintf(buff, 64, "%s %s", name, end); 99 | name = buff; 100 | } 101 | i = 0; 102 | while (mlx_col_name[i].name) 103 | { 104 | if (!strcasecmp(mlx_col_name[i].name, name)) 105 | return (mlx_col_name[i].color); 106 | i ++; 107 | } 108 | return (0); 109 | } 110 | 111 | 112 | void mlx_int_xpm_set_pixel(mlx_img_list_t *img, char *data, int opp, int col, int x) 113 | { 114 | /* 115 | int dec; 116 | 117 | dec = opp; 118 | while (dec--) 119 | { 120 | if (img->image->byte_order) 121 | *(data+x*opp+dec) = col&0xFF; 122 | else 123 | *(data+x*opp+opp-dec-1) = col&0xFF; 124 | col >>= 8; 125 | } 126 | */ 127 | // opp is 4, do it the simple way 128 | *((unsigned int *)(data+4*x)) = col; 129 | } 130 | 131 | 132 | void *mlx_int_parse_xpm(mlx_ptr_t *xvar,void *info,int info_size,char *(*f)()) 133 | { 134 | int pos; 135 | char *line; 136 | char **tab; 137 | char *data; 138 | char *clip_data; 139 | int nc; 140 | int opp; 141 | int cpp; 142 | int col; 143 | int rgb_col; 144 | int col_name; 145 | int method; 146 | int x; 147 | int i; 148 | int j; 149 | mlx_img_list_t *img; 150 | t_xpm_col *colors; 151 | int *colors_direct; 152 | int width; 153 | int height; 154 | 155 | colors = 0; 156 | colors_direct = 0; 157 | img = 0; 158 | tab = 0; 159 | pos = 0; 160 | if (!(line = f(info,&pos,info_size)) || 161 | !(tab = mlx_int_str_to_wordtab(line)) || !(width = atoi(tab[0])) || 162 | !(height = atoi(tab[1])) || !(nc = atoi(tab[2])) || 163 | !(cpp = atoi(tab[3])) ) 164 | RETURN; 165 | free(tab); 166 | tab = 0; 167 | 168 | method = 0; 169 | if (cpp<=2) 170 | { 171 | method = 1; 172 | if (!(colors_direct = malloc((cpp==2?65536:256)*sizeof(int)))) 173 | RETURN; 174 | } 175 | else 176 | if (!(colors = malloc(nc*sizeof(*colors)))) 177 | RETURN; 178 | 179 | clip_data = 0; 180 | 181 | i = nc; 182 | while (i--) 183 | { 184 | if (!(line = f(info,&pos,info_size)) || 185 | !(tab = mlx_int_str_to_wordtab(line+cpp)) ) 186 | RETURN; 187 | j = 0; 188 | while (tab[j] && strcmp(tab[j++],"c")); 189 | 190 | if (!tab[j]) 191 | RETURN; 192 | 193 | rgb_col = mlx_int_get_text_rgb(tab[j], tab[j+1]); 194 | /* 195 | if ((rgb_col = mlx_int_get_text_rgb(tab[j], tab[j+1]))==-1) 196 | { 197 | if (!(clip_data = malloc(4*width*height)) || // ok, nice size .. 198 | !(clip_img = XCreateImage(xvar->display, xvar->visual, 199 | 1, XYPixmap, 0, clip_data, 200 | width, height, 8, (width+7)/8)) ) 201 | RETURN; 202 | memset(clip_data, 0xFF, 4*width*height); 203 | } 204 | */ 205 | if (method) 206 | colors_direct[mlx_int_get_col_name(line,cpp)] = rgb_col; 207 | // rgb_col>=0?mlx_get_color_value(xvar, rgb_col):rgb_col; 208 | else 209 | { 210 | colors[i].name = mlx_int_get_col_name(line,cpp); 211 | colors[i].col = rgb_col; // rgb_col>=0?mlx_get_color_value(xvar,rgb_col):rgb_col; 212 | } 213 | free(tab); 214 | tab = 0; 215 | } 216 | 217 | if (!(img = mlx_new_image(xvar,width,height))) 218 | RETURN; 219 | //opp = img->bpp/8; 220 | opp = 4; 221 | 222 | 223 | i = height; 224 | data = img->buffer; 225 | while (i--) 226 | { 227 | if (!(line = f(info,&pos,info_size))) 228 | RETURN; 229 | x = 0; 230 | while (xsize_line; 255 | data += img->width*4; 256 | } 257 | /* 258 | if (clip_data) 259 | { 260 | if (!(clip_pix = XCreatePixmap(xvar->display, xvar->root, 261 | width, height, 1)) ) 262 | RETURN; 263 | img->gc = XCreateGC(xvar->display, clip_pix, 0, &xgcv); 264 | XPutImage(xvar->display, clip_pix, img->gc, clip_img, 265 | 0, 0, 0, 0, width, height); 266 | XFreeGC(xvar->display, img->gc); 267 | xgcv.clip_mask = clip_pix; 268 | xgcv.function = GXcopy; 269 | xgcv.plane_mask = AllPlanes; 270 | img->gc = XCreateGC(xvar->display, xvar->root, GCClipMask|GCFunction| 271 | GCPlaneMask, &xgcv); 272 | XSync(xvar->display, False); 273 | XDestroyImage(clip_img); 274 | } 275 | */ 276 | if (colors) 277 | free(colors); 278 | if (colors_direct) 279 | free(colors_direct); 280 | return (img); 281 | } 282 | 283 | 284 | void mlx_int_file_get_rid_comment(char *ptr, int size) 285 | { 286 | int com_begin; 287 | int com_end; 288 | 289 | while ((com_begin = mlx_int_str_str_cote(ptr,"/*",size))!=-1) 290 | { 291 | com_end = mlx_int_str_str(ptr+com_begin+2,"*/",size-com_begin-2); 292 | memset(ptr+com_begin,' ',com_end+4); 293 | } 294 | while ((com_begin = mlx_int_str_str_cote(ptr,"//",size))!=-1) 295 | { 296 | com_end = mlx_int_str_str(ptr+com_begin+2,"\n",size-com_begin-2); 297 | memset(ptr+com_begin,' ',com_end+3); 298 | } 299 | } 300 | 301 | 302 | void *mlx_xpm_file_to_image(mlx_ptr_t *xvar,char *file,int *width,int *height) 303 | { 304 | int fd; 305 | int size; 306 | char *ptr; 307 | mlx_img_list_t *img; 308 | 309 | if ((fd = open(file,O_RDONLY))==-1 || (size = lseek(fd,0,SEEK_END))==-1 || 310 | (ptr = mmap(0,size,PROT_WRITE|PROT_READ,MAP_PRIVATE,fd,0))== 311 | (void *)MAP_FAILED) 312 | { 313 | if (fd>=0) 314 | close(fd); 315 | return ((void *)0); 316 | } 317 | mlx_int_file_get_rid_comment(ptr, size); 318 | if ((img = mlx_int_parse_xpm(xvar,ptr,size,mlx_int_get_line))) 319 | { 320 | *width = img->width; 321 | *height = img->height; 322 | } 323 | munmap(ptr,size); 324 | close(fd); 325 | return (img); 326 | } 327 | 328 | void *mlx_xpm_to_image(mlx_ptr_t *xvar,char **xpm_data,int *width,int *height) 329 | { 330 | mlx_img_list_t *img; 331 | 332 | if ((img = mlx_int_parse_xpm(xvar,xpm_data,0,mlx_int_static_line))) 333 | { 334 | *width = img->width; 335 | *height = img->height; 336 | } 337 | return (img); 338 | } 339 | -------------------------------------------------------------------------------- /mlx/mlx_xpm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pseudo-programmer42/cub3d_lect/716329a7b5dc394f4cbb4427193fe1433fe9957b/mlx/mlx_xpm.o -------------------------------------------------------------------------------- /srcs/cub3d.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cub3d.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/08/16 16:04:40 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/27 18:19:40 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | #include "hyochoi_macros.h" 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | t_all a; 19 | int err; 20 | 21 | errno = 0; 22 | /* 23 | ** Check arguments, init structures and init mlx pointer 24 | */ 25 | if ((argc == 3 && ft_strcmp(argv[2], "--save")) || argc < 2 || argc > 3) 26 | return (error_msg(INVALID_ARG)); 27 | 28 | init_all(&a); 29 | 30 | if (!(a.mlx.mlx = mlx_init())) 31 | return (error_msg(MLX_ERROR)); 32 | /* 33 | ** Parse map file 34 | */ 35 | if ((err = parse_map(argv[1], &a))) 36 | return (error_msg(err)); 37 | 38 | printf(PRINT_COLOR_GREEN"parsing done!"PRINT_COLOR_RESET"\n"); 39 | 40 | /* 41 | ** minckim's code start------------------------------------------------------- 42 | */ 43 | t_runtime r; 44 | convert_data(&r, &a); 45 | 46 | if (argc == 3) 47 | return ((err = save_bmp(&r)) == 0 ? 0 : error_msg(err)); 48 | 49 | mlx_hook(r.screen.win, 2, 1, key_press_manager, &r.key); 50 | mlx_hook(r.screen.win, 3, 2, key_release_manager, &r.key); 51 | mlx_hook(r.screen.win, 17, 1L << 5, cub_close, 0); 52 | mlx_loop_hook(r.screen.mlx, play, &r); 53 | mlx_loop(r.screen.mlx); 54 | /* 55 | ** minckim's code end--------------------------------------------------------- 56 | */ 57 | return (0); 58 | } 59 | -------------------------------------------------------------------------------- /srcs/draw_frame.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | #include "minckim_macros.h" 3 | 4 | static void refresh_screen(t_pixel **pixel, int w, int h) 5 | { 6 | int y; 7 | int x; 8 | t_pixel *tmp; 9 | 10 | x = -1; 11 | while (++x < w) 12 | { 13 | y = -1; 14 | while (++y < h) 15 | { 16 | tmp = pixel[x] + y; 17 | tmp->distance = INFINITY; 18 | *(tmp->color) = COLOR_DEFAULT; 19 | } 20 | } 21 | } 22 | 23 | static void refresh_ray(t_ray *ray, t_vec *dir, t_vec *plane, int w) 24 | { 25 | int x; 26 | t_vec plane_tmp; 27 | 28 | plane_tmp = vec_mul(*plane, -(double)w / 2); 29 | ray[0].distance = INFINITY; 30 | ray[0].dir = vec_add(*dir, plane_tmp); 31 | x = 0; 32 | while (++x < w) 33 | { 34 | ray[x].dir = vec_add(ray[x - 1].dir, *plane); 35 | ray[x].distance = INFINITY; 36 | } 37 | } 38 | 39 | t_screen *draw_frame(t_runtime *r) 40 | { 41 | t_screen *s; 42 | 43 | s = &r->screen; 44 | refresh_screen(s->pixel, s->w, s->h); 45 | s->origin = r->player_origin; 46 | s->dir = r->player_dir; 47 | s->plane = r->player_plane; 48 | refresh_ray(s->ray, &s->dir, &s->plane, s->w); 49 | draw_floor(s, r->color_floor, r->color_ceiling); 50 | draw_wall(&r->screen, r->wall); 51 | draw_sprite(&r->screen, r->sprite); 52 | return (s); 53 | } -------------------------------------------------------------------------------- /srcs/init_all.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init_all.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/08/16 20:13:39 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/18 12:47:57 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | #include "hyochoi_macros.h" 15 | 16 | static void init_img(t_all *a) 17 | { 18 | int i; 19 | 20 | i = 0; 21 | a->mlx.mlx = 0; 22 | a->mlx.win = 0; 23 | while (i < TEXNUM_MAX) 24 | { 25 | a->tex[i].h = 0; 26 | a->tex[i].w = 0; 27 | a->tex[i].bits_per_pixel = 0; 28 | a->tex[i].size_line = 0; 29 | a->tex[i].endian = 0; 30 | a->tex[i].ptr = 0; 31 | a->tex[i].addr = 0; 32 | i++; 33 | } 34 | a->img.ptr = 0; 35 | a->img.addr = 0; 36 | a->img.w = 0; 37 | a->img.h = 0; 38 | } 39 | 40 | static void init_player(t_all *a) 41 | { 42 | a->pl.pos_x = 0; 43 | a->pl.pos_y = 0; 44 | } 45 | 46 | static void init_map(t_all *a) 47 | { 48 | a->map.pl_num = 0; 49 | a->map.map = 0; 50 | a->map.list = 0; 51 | a->map.h = 0; 52 | a->map.info_num = 0; 53 | a->map.color[CEIL] = 0; 54 | a->map.color[FLOOR] = 0; 55 | a->map.color_flag[CEIL] = 0; 56 | a->map.color_flag[FLOOR] = 0; 57 | a->map.dir_init = 0; 58 | } 59 | 60 | static void init_key(t_all *a) 61 | { 62 | a->key.w = 0; 63 | a->key.s = 0; 64 | a->key.a = 0; 65 | a->key.d = 0; 66 | a->key.arr_l = 0; 67 | a->key.arr_r = 0; 68 | } 69 | 70 | void init_all(t_all *a) 71 | { 72 | init_img(a); 73 | init_player(a); 74 | init_map(a); 75 | init_key(a); 76 | } 77 | -------------------------------------------------------------------------------- /srcs/parse_all.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parse_all.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/08/18 15:35:56 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/25 01:27:11 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | #include "hyochoi_macros.h" 15 | 16 | static int check_map(t_all *a) 17 | { 18 | int j; 19 | int err; 20 | t_list *cur; 21 | t_list *next; 22 | 23 | j = 0; 24 | /* 25 | ** Free fore blank lines in list 26 | */ 27 | cur = a->map.list; 28 | while (is_blank_line(cur->content)) 29 | { 30 | free(cur->content); 31 | next = cur->next; 32 | free(cur); 33 | cur = next; 34 | } 35 | 36 | a->map.h = ft_lstsize(cur); 37 | /* 38 | ** Make a pointer array(char *) 39 | ** and move maps from list to array 40 | */ 41 | if (!(a->map.map = (char **)malloc(sizeof(char *) * (a->map.h + 1)))) 42 | return (MALLOC_FAILED); 43 | 44 | while (j < a->map.h && (a->map.map[j++] = cur->content)) 45 | { 46 | next = cur->next; 47 | free(cur); 48 | cur = next; 49 | } 50 | a->map.map[a->map.h] = 0; 51 | /* 52 | ** Map valid check 53 | */ 54 | if ((err = map_valid_check(a)) != 0) 55 | return (err); 56 | return (0); 57 | } 58 | 59 | static int parse_info(char *line, t_all *a) 60 | { 61 | int i; 62 | 63 | i = 0; 64 | while (line[i] == ' ') 65 | i++; 66 | 67 | if (line[i] == 'S' && line[i + 1] == 'O' && line[i + 2] == ' ') 68 | return (parse_tex(&line[i + 3], SOUTH, a)); 69 | 70 | if (line[i] == 'N' && line[i + 1] == 'O' && line[i + 2] == ' ') 71 | return (parse_tex(&line[i + 3], NORTH, a)); 72 | 73 | if (line[i] == 'E' && line[i + 1] == 'A' && line[i + 2] == ' ') 74 | return (parse_tex(&line[i + 3], EAST, a)); 75 | 76 | if (line[i] == 'W' && line[i + 1] == 'E' && line[i + 2] == ' ') 77 | return (parse_tex(&line[i + 3], WEST, a)); 78 | 79 | if (line[i] == 'S' && line[i + 1] == ' ') 80 | return (parse_tex(&line[i + 2], SPRITE, a)); 81 | 82 | if (line[i] == 'F' && line[i + 1] == ' ') 83 | return (parse_color(&line[i + 2], FLOOR, a)); 84 | 85 | if (line[i] == 'C' && line[i + 1] == ' ') 86 | return (parse_color(&line[i + 2], CEIL, a)); 87 | 88 | if (line[i] == 'R' && line[i + 1] == ' ') 89 | return (parse_resol(&line[i + 2], a)); 90 | 91 | return (INVALID_MAP); 92 | } 93 | 94 | static int parse_line(char *line, t_all *a) 95 | { 96 | int err; 97 | t_list *new; 98 | 99 | /* 100 | ** If map informations(resolution, textures, colors, etc...) are not enough, 101 | ** give line to parse_info function. 102 | ** Else, save rest lines to linked list. 103 | */ 104 | if (a->map.info_num != INFO_PARSED) 105 | { 106 | if (is_blank_line(line)) 107 | { 108 | free(line); 109 | return (0); 110 | } 111 | if ((err = parse_info(line, a)) == 0) 112 | a->map.info_num += 1; 113 | free(line); 114 | return (err); 115 | } 116 | else 117 | { 118 | if ((new = ft_lstnew(line)) == 0) 119 | return (MALLOC_FAILED); 120 | ft_lstadd_back(&(a->map.list), new); 121 | } 122 | return (0); 123 | } 124 | 125 | int parse_map(char *filename, t_all *a) 126 | { 127 | int fd; 128 | int gnl_output; 129 | int parse_output; 130 | int err; 131 | char *temp; 132 | 133 | if ((fd = open(filename, O_RDONLY)) < 0) 134 | return (OPEN_ERROR); 135 | while (1) 136 | { 137 | gnl_output = get_next_line(fd, &temp); 138 | 139 | if (gnl_output == -1) 140 | break ; 141 | /* 142 | ** Parse gnl output strings until gnl ends, or error occurs 143 | */ 144 | parse_output = parse_line(temp, a); 145 | 146 | if (gnl_output == 0 || parse_output != 0) 147 | break ; 148 | } 149 | close(fd); 150 | 151 | if (gnl_output == -1) 152 | return (GNL_ERROR); 153 | else if (parse_output != 0 || a->map.info_num != INFO_PARSED) 154 | return (parse_output != 0 ? parse_output : LACKING_INFO); 155 | /* 156 | ** If there is no error in parsing function, 157 | ** convert map from list to array and check is valid 158 | */ 159 | if ((err = check_map(a))) 160 | return (err); 161 | return (0); 162 | } 163 | -------------------------------------------------------------------------------- /srcs/parse_tool.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parse_tool.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/08/21 20:28:30 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/25 03:25:50 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | #include "hyochoi_macros.h" 15 | 16 | int parse_tex(char *line, int n, t_all *a) 17 | { 18 | int i; 19 | int fd; 20 | char *filename; 21 | 22 | i = 0; 23 | /* 24 | ** If there is a duplicate texture data, return error 25 | */ 26 | if (a->tex[n].addr != 0) 27 | return (MULTIPLE_INFO); 28 | 29 | line = skip_(SPACE, line); 30 | /* 31 | ** Parse filename and line validity check 32 | */ 33 | if (!(filename = (char *)malloc(sizeof(char) * (ft_strlen(line) + 1)))) 34 | return (MALLOC_FAILED); 35 | 36 | while (*line != ' ' && *line != '\0') 37 | filename[i++] = *(line++); 38 | filename[i] = '\0'; 39 | 40 | if ((line = skip_(SPACE, line)) && *line != '\0') 41 | return (INAPP_VALUE); 42 | 43 | if (((fd = open(filename, O_RDONLY)) < 0)) 44 | { 45 | free(filename); 46 | return (OPEN_ERROR); 47 | } 48 | /* 49 | ** If filename is valid, convert xpm file to image and make a texture array. 50 | */ 51 | if (!(a->tex[n].ptr = mlx_xpm_file_to_image(a->mlx.mlx, filename, &(a->tex[n].w), &(a->tex[n].h))) || 52 | !(a->tex[n].addr = (unsigned int *)mlx_get_data_addr(a->tex[n].ptr, 53 | &(a->tex[n].bits_per_pixel), &(a->tex[n].size_line), &(a->tex[n].endian)))) 54 | { 55 | if (a->tex[n].ptr != 0) 56 | mlx_destroy_image(a->mlx.mlx, a->tex[n].ptr); 57 | a->tex[n].ptr = 0; 58 | free(filename); 59 | close(fd); 60 | return (MLX_ERROR); 61 | } 62 | 63 | free(filename); 64 | close(fd); 65 | 66 | return (0); 67 | } 68 | 69 | int parse_color(char *line, int n, t_all *a) 70 | { 71 | int r; 72 | int g; 73 | int b; 74 | 75 | /* 76 | ** If cub3d have parsed color data, return error 77 | */ 78 | if (a->map.color_flag[n] != 0) 79 | return (MULTIPLE_INFO); 80 | a->map.color_flag[n] = 1; 81 | 82 | line = skip_(SPACE, line); 83 | 84 | /* 85 | ** Parse R, G, B value 86 | */ 87 | r = ft_atoi(line); 88 | 89 | line = skip_(NUM, line); 90 | if ((line = skip_(SPACE, line)) && *line != ',') 91 | return (INAPP_VALUE); 92 | line = skip_(SPACE, ++line); 93 | 94 | g = ft_atoi(line); 95 | 96 | line = skip_(NUM, line); 97 | if ((line = skip_(SPACE, line)) && *line != ',') 98 | return (INAPP_VALUE); 99 | line = skip_(SPACE, ++line); 100 | 101 | b = ft_atoi(line); 102 | 103 | line = skip_(NUM, line); 104 | if (((line = skip_(SPACE, line)) && *line != '\0') || 105 | (r < 0 || g < 0 || b < 0 || r > 255 || g > 255 || b > 255)) 106 | return (INAPP_VALUE); 107 | a->map.color[n] += r * 256 * 256 + g * 256 + b; 108 | 109 | return (0); 110 | } 111 | 112 | int parse_resol(char *line, t_all *a) 113 | { 114 | /* 115 | ** If cub3d have parsed resolution data, return error 116 | */ 117 | if (a->img.w || a->img.h) 118 | return (MULTIPLE_INFO); 119 | 120 | line = skip_(SPACE, line); 121 | 122 | /* 123 | ** Parse resolution value 124 | */ 125 | a->img.w = ft_atoi(line); 126 | 127 | line = skip_(NUM, line); 128 | line = skip_(SPACE, line); 129 | 130 | a->img.h = ft_atoi(line); 131 | 132 | line = skip_(NUM, line); 133 | line = skip_(SPACE, line); 134 | if (*line != '\0') 135 | return (INAPP_VALUE); 136 | 137 | /* 138 | ** Check resolution validity and change value 139 | */ 140 | if (a->img.w <= 0 || a->img.h <= 0) 141 | return (INAPP_VALUE); 142 | if (a->img.w < WIN_W_MIN || a->img.h < WIN_H_MIN) 143 | return (TOO_SMALL_RES); 144 | 145 | if (a->img.w > WIN_W_MAX) 146 | a->img.w = WIN_W_MAX; 147 | if (a->img.h > WIN_H_MAX) 148 | a->img.h = WIN_H_MAX; 149 | 150 | return (0); 151 | } 152 | 153 | static int map_closed_check(t_all *a, t_mapcheck *m, int i, int j) 154 | { 155 | int err; 156 | 157 | /* 158 | ** Check given j, i's surroundings with forward, back line length 159 | */ 160 | err = 0; 161 | if (j > 0 && m->len_for > i - 1) 162 | err += a->map.map[j - 1][i - 1] == ' ' ? 1 : 0; 163 | if (j > 0 && m->len_for > i) 164 | err += a->map.map[j - 1][i] == ' ' ? 1 : 0; 165 | if (j > 0 && m->len_for > i + 1) 166 | err += a->map.map[j - 1][i + 1] == ' ' ? 1 : 0; 167 | if (i > 0) 168 | err += a->map.map[j][i - 1] == ' ' ? 1 : 0; 169 | if (i < m->len_cur) 170 | err += a->map.map[j][i + 1] == ' ' ? 1 : 0; 171 | if (j < a->map.h - 1 && m->len_back > i - 1) 172 | err += a->map.map[j + 1][i - 1] == ' ' ? 1 : 0; 173 | if (j < a->map.h - 1 && m->len_back > i) 174 | err += a->map.map[j + 1][i] == ' ' ? 1 : 0; 175 | if (j < a->map.h - 1 && m->len_back > i + 1) 176 | err += a->map.map[j + 1][i + 1] == ' ' ? 1 : 0; 177 | return (err > 0 ? NOT_SURROUNDED : 0); 178 | } 179 | 180 | int map_valid_check(t_all *a) 181 | { 182 | t_mapcheck m; 183 | int i; 184 | int j; 185 | int err; 186 | char tmp; 187 | 188 | j = 0; 189 | m.err_cnt = 0; 190 | while (a->map.map[j] != 0) 191 | { 192 | /* 193 | ** In this check function, I suggested non-rectangle map input(with no padding). 194 | ** So we need fore, back line's length to prevent SEGFAULT. 195 | */ 196 | i = 0; 197 | if (j != 0) 198 | m.len_for = ft_strlen(a->map.map[j - 1]); 199 | if (j < a->map.h - 1) 200 | m.len_back = ft_strlen(a->map.map[j + 1]); 201 | m.len_cur = ft_strlen(a->map.map[j]); 202 | 203 | while (a->map.map[j][i] != 0) 204 | { 205 | tmp = a->map.map[j][i]; 206 | /* 207 | ** Check map components. 208 | ** If there is invalid component in a map or multiple user location, 209 | ** return error. 210 | */ 211 | if (tmp == 'W' || tmp == 'E' || tmp == 'S' || tmp == 'N') 212 | { 213 | a->pl.pos_x = (double)i + 0.5; 214 | a->pl.pos_y = (double)j + 0.5; 215 | a->map.dir_init = tmp; 216 | a->map.pl_num++; 217 | if (a->map.pl_num > 1) 218 | return (MULTI_PLAYER); 219 | } 220 | if (!(tmp == ' ' || tmp == '0' || tmp == '1' || tmp == '2' 221 | || tmp == 'W' || tmp == 'E' || tmp == 'S' || tmp == 'N')) 222 | return (INVALID_COMP); 223 | /* 224 | ** When given j, i's value is neither space nor wall, 225 | ** check if a map is closed. 226 | */ 227 | if (tmp != ' ' && tmp != '1') 228 | { 229 | if (j == 0 || j == a->map.h - 1 || i == 0 || i == m.len_cur - 1) 230 | return (NOT_SURROUNDED); 231 | if ((err = map_closed_check(a, &m, i, j)) != 0) 232 | return (NOT_SURROUNDED); 233 | } 234 | i++; 235 | } 236 | j++; 237 | } 238 | return (a->map.pl_num == 0 ? NO_PLAYER : 0); 239 | } 240 | -------------------------------------------------------------------------------- /srcs/print_msg.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_msg.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/08/16 17:45:12 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/25 01:20:32 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | #include "hyochoi_macros.h" 15 | 16 | static void msg_one(int num) 17 | { 18 | if (num == INVALID_ARG) 19 | write(1, "Invalid argument.\n", 18); 20 | else if (num == OPEN_ERROR) 21 | write(1, "Occured when opening a file.\n", 29); 22 | else if (num == GNL_ERROR) 23 | write(1, "Occured in GNL function.\n", 25); 24 | else if (num == INVALID_MAP) 25 | write(1, "Invalid map.\n", 13); 26 | else if (num == MALLOC_FAILED) 27 | write(1, "Malloc failed.\n", 15); 28 | else if (num == MULTIPLE_INFO) 29 | write(1, "Map has multiple information.\n", 30); 30 | else if (num == LACKING_INFO) 31 | write(1, "Not enough map information.\n", 28); 32 | else if (num == MLX_ERROR) 33 | write(1, "Occured in MLX function.\n", 25); 34 | else if (num == INAPP_VALUE) 35 | write(1, "Inappropriate value in map file.\n", 33); 36 | else if (num == TOO_SMALL_RES) 37 | write(1, "Too small resolution in map file.\n", 34); 38 | } 39 | 40 | static void msg_two(int num) 41 | { 42 | if (num == NOT_SURROUNDED) 43 | write(1, "Map is not surrounded by wall.\n", 31); 44 | else if (num == MULTI_PLAYER) 45 | write(1, "Map has multiple user location.\n", 32); 46 | else if (num == INVALID_COMP) 47 | write(1, "Map has an invalid component.\n", 30); 48 | else if (num == NO_PLAYER) 49 | write(1, "Map has no player.\n", 19); 50 | } 51 | 52 | int error_msg(int num) 53 | { 54 | write(1, PRINT_COLOR_RED, 6); 55 | write(1, "Error\n", 6); 56 | write(1, PRINT_COLOR_RESET, 5); 57 | if (num >= -10) 58 | msg_one(num); 59 | else 60 | msg_two(num); 61 | if (errno != 0 && errno != 60) 62 | perror(""); 63 | /* 64 | ** Exit with user-defined error number. 65 | ** When exit() function called, OS will free all allocated memories. 66 | ** We can check the number with "echo $?" in shell. 67 | */ 68 | exit(num); 69 | } 70 | -------------------------------------------------------------------------------- /srcs/save_bmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* save_bmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/01 19:12:34 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/27 18:19:24 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | #include "hyochoi_macros.h" 15 | 16 | static void bmp_type(t_screen *s, int fd) 17 | { 18 | int tmp; 19 | int i; 20 | unsigned char buf[14]; 21 | 22 | i = 0; 23 | while (i < 14) 24 | buf[i++] = 0; 25 | buf[0] = (unsigned char)('B'); 26 | buf[1] = (unsigned char)('M'); 27 | tmp = 4 * (s->w * s->h) + 14 + 40; 28 | buf[2] = (unsigned char)(tmp % 256); 29 | buf[3] = (unsigned char)(tmp / 256 % 256); 30 | buf[4] = (unsigned char)(tmp / 256 / 256 % 256); 31 | buf[5] = (unsigned char)(tmp / 256 / 256 / 256); 32 | buf[10] = (unsigned char)(54); 33 | write(fd, buf, 14); 34 | } 35 | 36 | static void bmp_info(t_screen *s, int fd) 37 | { 38 | int i; 39 | unsigned char buf[40]; 40 | 41 | i = 0; 42 | while (i < 40) 43 | buf[i++] = 0; 44 | buf[0] = (unsigned char)(40); 45 | buf[4] = (unsigned char)(s->w % 256); 46 | buf[5] = (unsigned char)(s->w / 256 % 256); 47 | buf[6] = (unsigned char)(s->w / 256 / 256 % 256); 48 | buf[7] = (unsigned char)(s->w / 256 / 256 / 256); 49 | buf[8] = (unsigned char)(s->h % 256); 50 | buf[9] = (unsigned char)(s->h / 256 % 256); 51 | buf[10] = (unsigned char)(s->h / 256 / 256 % 256); 52 | buf[11] = (unsigned char)(s->h / 256 / 256 / 256); 53 | buf[12] = (unsigned char)(1); 54 | buf[14] = (unsigned char)(32); 55 | write(fd, buf, 40); 56 | } 57 | 58 | static void bmp_data(t_screen *s, int fd) 59 | { 60 | int tmp; 61 | int i; 62 | int j; 63 | unsigned char buf[4]; 64 | 65 | j = s->h - 1; 66 | while (j >= 0) 67 | { 68 | i = 0; 69 | while (i < s->w) 70 | { 71 | tmp = s->img.addr[i + j * s->w]; 72 | buf[0] = (unsigned char)(tmp % 256); 73 | buf[1] = (unsigned char)(tmp / 256 % 256); 74 | buf[2] = (unsigned char)(tmp / 256 / 256); 75 | buf[3] = (unsigned char)(0); 76 | write(fd, buf, 4); 77 | i++; 78 | } 79 | j--; 80 | } 81 | } 82 | 83 | int save_bmp(t_runtime *r) 84 | { 85 | int i; 86 | int fd; 87 | t_screen *s; 88 | 89 | i = 0; 90 | s = draw_frame(r); 91 | if (!(fd = open("img.bmp", O_CREAT | O_WRONLY | O_TRUNC, 0700))) 92 | return (OPEN_ERROR); 93 | bmp_type(s, fd); 94 | bmp_info(s, fd); 95 | bmp_data(s, fd); 96 | close(fd); 97 | cub_close(); 98 | return (0); 99 | } 100 | -------------------------------------------------------------------------------- /srcs_minckim/convert.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | #include "minckim_macros.h" 3 | 4 | t_vec set_player_dir(t_map *map) 5 | { 6 | if (map->dir_init == 'E') 7 | return vec_new(1, 0); 8 | if (map->dir_init == 'N') 9 | return vec_new(0, 1); 10 | if (map->dir_init == 'W') 11 | return vec_new(-1, 0); 12 | return vec_new(0, -1); 13 | } 14 | 15 | t_vec set_player_plane(t_map *map, int w) 16 | { 17 | double min; 18 | 19 | min = tan(FOV / 2) / w * 2; 20 | if (map->dir_init == 'E') 21 | return vec_new(0, -min); 22 | if (map->dir_init == 'N') 23 | return vec_new(min, 0); 24 | if (map->dir_init == 'W') 25 | return vec_new(0, min); 26 | return vec_new(min, 0); 27 | } 28 | 29 | void trans_map(t_map *raw) 30 | { 31 | int y; 32 | int h; 33 | char *tmp; 34 | char **map; 35 | 36 | h = raw->h; 37 | y = 0; 38 | map = raw->map; 39 | while (y < h / 2) 40 | { 41 | tmp = map[y]; 42 | map[y] = map[h - y - 1]; 43 | map[h - y - 1] = tmp; 44 | y++; 45 | } 46 | } 47 | 48 | void convert_data(t_runtime *r, t_all *a) 49 | { 50 | int i; 51 | t_screen *s; 52 | 53 | s = &r->screen; 54 | s->mlx = a->mlx.mlx; 55 | s->img = a->img; 56 | s->w = a->img.w; 57 | s->h = a->img.h; 58 | s->win = mlx_new_window(s->mlx, s->w, s->h, "cub3D LESSON"); 59 | s->img.ptr = mlx_new_image(s->mlx, s->w, s->h); 60 | s->img.addr = (unsigned int*)mlx_get_data_addr(s->img.ptr, \ 61 | &(s->img.bits_per_pixel), &(s->img.size_line), &(s->img.endian)); 62 | s->distance = 1 / tan(FOV / 2) * s->w / 2; 63 | s->pixel = init_pixel(s->w, s->h, &s->img); 64 | s->ray = malloc(sizeof(t_ray) * s->w); 65 | i = -1; 66 | while (++i < TEXNUM_MAX) 67 | r->texture[i] = a->tex[i]; 68 | trans_map(&a->map); 69 | r->player_dir = set_player_dir(&a->map); 70 | r->player_plane = set_player_plane(&a->map, s->w); 71 | r->wall = make_wall(a->map.map, r->texture); 72 | r->sprite = make_sprite(a->map.map, r->texture); 73 | r->key = a->key; 74 | r->player_origin = vec_new(a->pl.pos_x * WALL_W, (a->map.h - a->pl.pos_y - 1) * WALL_W); 75 | r->color_floor = a->map.color[FLOOR]; 76 | r->color_ceiling = a->map.color[CEILING]; 77 | } -------------------------------------------------------------------------------- /srcs_minckim/equation.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | 3 | t_vec equation_solver(t_vec *coeff0, t_vec *coeff1, t_vec *constant) 4 | { 5 | t_vec result; 6 | double det; 7 | 8 | det = coeff0->x * coeff1->y - coeff1->x * coeff0->y; 9 | result.x = (coeff1->y * constant->x - coeff1->x * constant->y) / det; 10 | result.y = (coeff0->x * constant->y - coeff0->y * constant->x) / det; 11 | return (result); 12 | } -------------------------------------------------------------------------------- /srcs_minckim/init_pixel.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | 3 | void img_put_color(t_img *img, int x, int y, unsigned int color) 4 | { 5 | unsigned int *point; 6 | 7 | point = (unsigned int*)((char*)img->addr + img->size_line * y \ 8 | + img->bits_per_pixel / 8 * x); 9 | *point = color; 10 | } 11 | 12 | unsigned int img_pick_color(t_img *img, int x, int y) 13 | { 14 | unsigned int *point; 15 | 16 | point = (unsigned int*)((char*)img->addr + img->size_line * y \ 17 | + img->bits_per_pixel / 8 * x); 18 | return *point; 19 | } 20 | 21 | t_pixel **init_pixel(int w, int h, t_img *img) 22 | { 23 | t_pixel **pixel; 24 | int x; 25 | int y; 26 | 27 | pixel = malloc(sizeof(t_pixel*) * w); 28 | x = -1; 29 | while (++x < w) 30 | { 31 | y = -1; 32 | pixel[x] = malloc(sizeof(t_pixel) * h); 33 | while (++y < h) 34 | { 35 | pixel[x][y].distance = INFINITY; 36 | pixel[x][y].color = (unsigned int*)((char*)img->addr\ 37 | + img->size_line * y + img->bits_per_pixel / 8 * x); 38 | } 39 | } 40 | return (pixel); 41 | } 42 | -------------------------------------------------------------------------------- /srcs_minckim/make_entity.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | #include "minckim_macros.h" 3 | 4 | int get_entity_num(char **map, char c) 5 | { 6 | int size; 7 | char *tmp; 8 | 9 | size = 0; 10 | while (*map) 11 | { 12 | tmp = *map++; 13 | while (*tmp) 14 | if (*tmp++ == c) 15 | size++; 16 | } 17 | return (size); 18 | } 19 | 20 | t_entity entity_new(t_vec a, t_vec b, t_img *texture) 21 | { 22 | t_entity result; 23 | 24 | result.a = a; 25 | result.b = b; 26 | result.texture = texture; 27 | return (result); 28 | } 29 | 30 | t_entity *make_wall_from_a_point(int x, int y, t_entity *start, t_img *tex) 31 | { 32 | t_vec btm_l; 33 | t_vec btm_r; 34 | t_vec top_l; 35 | t_vec top_r; 36 | 37 | btm_l = vec_new(x * WALL_W , y * WALL_W); 38 | btm_r = vec_new(x * WALL_W + WALL_W , y * WALL_W); 39 | top_l = vec_new(x * WALL_W , y * WALL_W + WALL_W); 40 | top_r = vec_new(x * WALL_W + WALL_W , y * WALL_W + WALL_W); 41 | start[0] = entity_new(btm_r, top_r, tex + EAST); 42 | start[1] = entity_new(top_r, top_l, tex + NORTH); 43 | start[2] = entity_new(top_l, btm_l, tex + WEST); 44 | start[3] = entity_new(btm_l, btm_r, tex + SOUTH); 45 | return start + 4; 46 | } 47 | 48 | t_entity *make_wall(char **map, t_img *texture) 49 | { 50 | t_entity *entity; 51 | t_entity *entity_tmp; 52 | int x; 53 | int y; 54 | 55 | entity = malloc(sizeof(t_entity) * (get_entity_num(map, '1') * 4 + 1)); 56 | entity_tmp = entity; 57 | y = -1; 58 | while (map[++y]) 59 | { 60 | x = -1; 61 | while (map[y][++x]) 62 | if (map[y][x] == '1') 63 | { 64 | entity = make_wall_from_a_point(x, y, entity, texture); 65 | } 66 | } 67 | entity->texture = 0; 68 | return entity_tmp; 69 | } 70 | 71 | t_entity *make_sprite(char **map, t_img *texture) 72 | { 73 | t_entity *entity; 74 | t_entity *entity_tmp; 75 | int size; 76 | int x; 77 | int y; 78 | 79 | size = get_entity_num(map, '2'); 80 | entity = malloc(sizeof(t_entity) * (size + 1)); 81 | entity_tmp = entity; 82 | y = -1; 83 | while (map[++y]) 84 | { 85 | x = -1; 86 | while (map[y][++x]) 87 | if (map[y][x] == '2') 88 | { 89 | *entity = entity_new(\ 90 | vec_new(x * WALL_W + WALL_W / 2, y * WALL_W + WALL_W / 2), \ 91 | vec_new(0, 0), \ 92 | texture + SPRITE); 93 | entity++; 94 | } 95 | } 96 | entity->texture = 0; 97 | return (entity_tmp); 98 | } -------------------------------------------------------------------------------- /srcs_minckim/play.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | #include "minckim_macros.h" 3 | 4 | void refresh_screen(t_pixel **pixel, int w, int h) 5 | { 6 | int y; 7 | int x; 8 | t_pixel *tmp; 9 | 10 | x = -1; 11 | while (++x < w) 12 | { 13 | y = -1; 14 | while (++y < h) 15 | { 16 | tmp = pixel[x] + y; 17 | tmp->distance = INFINITY; 18 | *(tmp->color) = COLOR_DEFAULT; 19 | } 20 | } 21 | } 22 | 23 | void refresh_ray(t_ray *ray, t_vec *dir, t_vec *plane, int w) 24 | { 25 | int x; 26 | t_vec plane_tmp; 27 | 28 | plane_tmp = vec_mul(*plane, -(double)w / 2); 29 | ray[0].distance = INFINITY; 30 | ray[0].dir = vec_add(*dir, plane_tmp); 31 | x = 0; 32 | while (++x < w) 33 | { 34 | ray[x].dir = vec_add(ray[x - 1].dir, *plane); 35 | ray[x].distance = INFINITY; 36 | } 37 | } 38 | 39 | int play(t_runtime *r) 40 | { 41 | t_screen *s; 42 | 43 | s = &r->screen; 44 | refresh_screen(s->pixel, s->w, s->h); 45 | player_move(r); 46 | s->origin = r->player_origin; 47 | s->dir = r->player_dir; 48 | s->plane = r->player_plane; 49 | refresh_ray(s->ray, &s->dir, &s->plane, s->w); 50 | draw_floor(s, r->color_floor, r->color_ceiling); 51 | draw_wall(&r->screen, r->wall); 52 | draw_sprite(&r->screen, r->sprite); 53 | mlx_put_image_to_window(r->screen.mlx, r->screen.win, r->screen.img.ptr, 0, 0); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /srcs_minckim/player_move.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | #include "minckim_macros.h" 3 | #include "hyochoi_macros.h" 4 | 5 | // copied from hyochoi 6 | int key_press_manager(int key, t_key *key_storage) 7 | { 8 | if (key == KEY_A) 9 | key_storage->a = 1; 10 | if (key == KEY_S) 11 | key_storage->s = 1; 12 | if (key == KEY_D) 13 | key_storage->d = 1; 14 | if (key == KEY_W) 15 | key_storage->w = 1; 16 | if (key == KEY_ARR_L) 17 | key_storage->arr_l = 1; 18 | if (key == KEY_ARR_R) 19 | key_storage->arr_r = 1; 20 | if (key == KEY_ESC) 21 | cub_close(); 22 | return (0); 23 | } 24 | 25 | int key_release_manager(int key, t_key *key_storage) 26 | { 27 | if (key == KEY_A) 28 | key_storage->a = 0; 29 | if (key == KEY_S) 30 | key_storage->s = 0; 31 | if (key == KEY_D) 32 | key_storage->d = 0; 33 | if (key == KEY_W) 34 | key_storage->w = 0; 35 | if (key == KEY_ARR_L) 36 | key_storage->arr_l = 0; 37 | if (key == KEY_ARR_R) 38 | key_storage->arr_r = 0; 39 | return (0); 40 | } 41 | 42 | int player_move(t_runtime *r) 43 | { 44 | t_vec dir; 45 | t_vec perp; 46 | 47 | dir = vec_mul(r->player_dir, MOVE_MIN); 48 | perp.x = -dir.y; 49 | perp.y = dir.x; 50 | if (r->key.w) 51 | r->player_origin = vec_add(r->player_origin, dir); 52 | if (r->key.a) 53 | r->player_origin = vec_add(r->player_origin, perp); 54 | if (r->key.s) 55 | r->player_origin = vec_sub(r->player_origin, dir); 56 | if (r->key.d) 57 | r->player_origin = vec_sub(r->player_origin, perp); 58 | if (r->key.arr_l) 59 | { 60 | r->player_dir = vec_rot_min_ccw(r->player_dir); 61 | r->player_plane = vec_rot_min_ccw(r->player_plane); 62 | } 63 | if (r->key.arr_r) 64 | { 65 | r->player_dir = vec_rot_min_cw(r->player_dir); 66 | r->player_plane = vec_rot_min_cw(r->player_plane); 67 | } 68 | return 0; 69 | } 70 | 71 | int cub_close(void) 72 | { 73 | printf("bye\n"); 74 | exit(0); 75 | } 76 | -------------------------------------------------------------------------------- /srcs_minckim/raycast.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | #include "minckim_macros.h" 3 | 4 | t_vec get_width_range(t_screen *screen, t_entity *wall) 5 | { 6 | // t_vec result; 7 | // t_vec result_a; 8 | // t_vec result_b; 9 | // t_vec constant; 10 | 11 | // constant = vec_sub(&wall->a, &screen->origin); 12 | // result_a = equation_solver(&screen->dir, &screen->plane, &constant); 13 | // constant = vec_sub(&wall->b, &screen->origin); 14 | // result_b = equation_solver(&screen->dir, &screen->plane, &constant); 15 | // result.x = 0; 16 | // result.y = 0; 17 | // if (result_a.x < 0 && result_b.x < 0) 18 | // return (result); 19 | // else if (result_a.x < 0 || result_a.x < 0) 20 | // result.y = screen->w; 21 | // else 22 | // { 23 | // result.x = result_a.y / result_a.x + screen->w / 2; 24 | // result.y = result_b.y / result_b.x + screen->w / 2; 25 | // if (result.x > result.y) 26 | // { 27 | // double tmp = result.y; 28 | // result.y = result.x; 29 | // result.x = tmp; 30 | // } 31 | // result.y += 1; 32 | // result.x = result.x < 0 ? 0 : result.x; 33 | // result.y = result.y > screen->w - 1 ? screen->w - 1 : result.y; 34 | // // result.y = screen->w; 35 | // } 36 | // return (result); 37 | t_vec result; 38 | (void)wall; 39 | result.x = 0; 40 | result.y = screen->w - 1; 41 | return result; 42 | } 43 | 44 | t_vec ray_x_face(t_ray *ray, t_entity *wall, t_vec *origin) 45 | { 46 | t_vec coeff; 47 | t_vec constant; 48 | 49 | coeff = vec_sub(wall->a, wall->b); 50 | constant = vec_sub(wall->a, *origin); 51 | return (equation_solver(&ray->dir, &coeff, &constant)); 52 | } 53 | 54 | void draw_vertical(t_screen *screen, t_entity *wall, t_vec *hit_info, int x) 55 | { 56 | int screen_y0; 57 | int screen_y1; 58 | int screen_y_h; 59 | int texture_x; 60 | int texture_y; 61 | unsigned int color; 62 | 63 | texture_x = hit_info->y * wall->texture->w; 64 | screen_y0 = -(WALL_H - EYE_LEVEL) / hit_info->x * screen->distance + screen->h / 2; 65 | screen_y1 = (EYE_LEVEL) / hit_info->x * screen->distance + screen->h / 2; 66 | screen_y_h = screen_y1 - screen_y0; 67 | screen_y0 = screen_y0 < 0 ? 0 : screen_y0; 68 | while (screen_y0 < screen_y1 && screen_y0 < screen->h) 69 | { 70 | texture_y = wall->texture->h - (screen_y1 - screen_y0) * wall->texture->h / (float)screen_y_h; 71 | if ((color = img_pick_color(wall->texture, texture_x, texture_y)) \ 72 | != 0xff000000) 73 | { 74 | *(screen->pixel[x][screen_y0].color) = color; 75 | screen->pixel[x][screen_y0].distance = hit_info->x; 76 | } 77 | screen_y0++; 78 | } 79 | } 80 | 81 | void draw_wall(t_screen *screen, t_entity *wall) 82 | { 83 | t_vec range; 84 | int x; 85 | int x_max; 86 | t_vec hit_info; 87 | t_ray *ray; 88 | 89 | while (wall->texture) 90 | { 91 | range = get_width_range(screen, wall); 92 | x = (int)range.x; 93 | x_max = (int)range.y; 94 | while (x < x_max) 95 | { 96 | ray = screen->ray + x; 97 | hit_info = ray_x_face(ray, wall, &screen->origin); 98 | if (0 < hit_info.x && 0 <= hit_info.y && hit_info.y <= 1) 99 | { 100 | if (hit_info.x < ray->distance) 101 | { 102 | ray->distance = hit_info.x; 103 | draw_vertical(screen, wall, &hit_info, x); 104 | } 105 | } 106 | x++; 107 | } 108 | wall++; 109 | } 110 | } 111 | 112 | void draw_sprite(t_screen *screen, t_entity *sprite) 113 | { 114 | t_entity sprite_tmp; 115 | t_vec dir; 116 | t_vec range; 117 | int min; 118 | int max; 119 | t_vec hit_info; 120 | t_ray *ray; 121 | 122 | sprite_tmp.texture = sprite->texture; 123 | dir.x = -screen->dir.y * WALL_W / 2; 124 | dir.y = screen->dir.x * WALL_W / 2; 125 | while (sprite->texture) 126 | { 127 | sprite_tmp.a = vec_add(sprite->a, dir); 128 | sprite_tmp.b = vec_sub(sprite->a, dir); 129 | range = get_width_range(screen, &sprite_tmp); 130 | min = (int)range.x; 131 | max = (int)range.y; 132 | while (min < max) 133 | { 134 | ray = screen->ray + min; 135 | hit_info = ray_x_face(ray, &sprite_tmp, &screen->origin); 136 | if (0 < hit_info.x && 0 <= hit_info.y && hit_info.y <= 1) 137 | { 138 | if (hit_info.x < ray->distance) 139 | { 140 | ray->distance = hit_info.x; 141 | draw_vertical(screen, &sprite_tmp, &hit_info, min); 142 | } 143 | } 144 | min++; 145 | } 146 | sprite++; 147 | } 148 | } 149 | 150 | void draw_floor(t_screen *screen, int color_floor, int color_ceiling) 151 | { 152 | int x; 153 | int y; 154 | t_pixel **pixel; 155 | 156 | pixel = screen->pixel; 157 | y = -1; 158 | while (++y < screen->h / 2) 159 | { 160 | x = -1; 161 | while (++x < screen->w) 162 | *(pixel[x][y].color) = color_ceiling; 163 | } 164 | while (++y < screen->h) 165 | { 166 | x = -1; 167 | while (++x < screen->w) 168 | *(pixel[x][y].color) = color_floor; 169 | } 170 | } -------------------------------------------------------------------------------- /srcs_minckim/vector.c: -------------------------------------------------------------------------------- 1 | #include "minckim_cub3d.h" 2 | #include "minckim_macros.h" 3 | 4 | t_vec vec_new(double x, double y) 5 | { 6 | t_vec result; 7 | 8 | result.x = x; 9 | result.y = y; 10 | return (result); 11 | } 12 | 13 | t_vec vec_sub(t_vec a, t_vec b) 14 | { 15 | a.x -= b.x; 16 | a.y -= b.y; 17 | return (a); 18 | } 19 | 20 | t_vec vec_rot_min_ccw(t_vec a) 21 | { 22 | static double sin_unit; 23 | static double cos_unit; 24 | t_vec result; 25 | 26 | sin_unit = sin_unit ? sin_unit : sin(M_PI * ANGLE_MIN / 180); 27 | cos_unit = cos_unit ? cos_unit : cos(M_PI * ANGLE_MIN / 180); 28 | result.x = cos_unit * a.x - sin_unit * a.y; 29 | result.y = cos_unit * a.y + sin_unit * a.x; 30 | return (result); 31 | } 32 | 33 | t_vec vec_rot_min_cw(t_vec a) 34 | { 35 | static double sin_unit; 36 | static double cos_unit; 37 | t_vec result; 38 | 39 | sin_unit = sin_unit ? sin_unit : -sin(M_PI * ANGLE_MIN / 180); 40 | cos_unit = cos_unit ? cos_unit : cos(M_PI * ANGLE_MIN / 180); 41 | result.x = cos_unit * a.x - sin_unit * a.y; 42 | result.y = cos_unit * a.y + sin_unit * a.x; 43 | return (result); 44 | } 45 | 46 | t_vec vec_add(t_vec a, t_vec b) 47 | { 48 | a.x += b.x; 49 | a.y += b.y; 50 | return (a); 51 | } 52 | 53 | t_vec vec_mul(t_vec a, double b) 54 | { 55 | a.x *= b; 56 | a.y *= b; 57 | return (a); 58 | } 59 | -------------------------------------------------------------------------------- /textures/blue.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *_32b313d1c6147e1aa928b0b911758ef[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "64 64 20 1 ", 5 | " c black", 6 | ". c #202020", 7 | "X c #2C2C2C", 8 | "o c gray22", 9 | "O c #000040", 10 | "+ c #00004C", 11 | "@ c #000058", 12 | "# c #000064", 13 | "$ c #000070", 14 | "% c #00007C", 15 | "& c gray33", 16 | "* c #000088", 17 | "= c #000098", 18 | "- c #0000A4", 19 | "; c #0000B0", 20 | ": c #0000BC", 21 | "> c #0000D4", 22 | ", c #0000FC", 23 | "< c #2024FC", 24 | "1 c #4040FC", 25 | /* pixels */ 26 | "o$$$$$$$$$$$$$$$$$$$$$$$$$$$oo$$$$$$$$$$$$$oo$$$$$$$$$$$$o$$$$$o", 27 | ".=====================***%$$ .========*%%%% .========%%#@ ----* ", 28 | ".===%=$==*$**$***$$=%$$$$#@+ .==**%==*%%%%+ .=;-;-;;;;;*$ --%*% ", 29 | ".=*$%***$$=*=@**@%$$**$$#@@+ .=*==%**$%$=#+ .=-;;=;:-;=*$ ==-*+ ", 30 | ".=%=#*#*$$$*%%$@%#*@$#@@+++@ .=*%%**$%$#+@+ .=;-=:;-=*%%# %#$$+ ", 31 | ".=%$===@$$$=***%%$#@$$$##@+@ .==%==$###%@++ .=;=:<:=;=*#$ *%#@+ ", 32 | ".=**--===*===**#%+$@$$$$#+++ .===%%$#%%%#@+ .=-;=;;==-$$# *$+++ ", 33 | ".==*=--=*$@==*%%$###@$$#@+@@ .==%$%%%$%$@++ .=-;-=---=$#$ *@+@+ ", 34 | ".=*$*==%*$*##%%%@@$%+###@@++ .=*%%#%==$#$+@ .=-:--%%==*#@ ", 35 | ".=%%%=$$$==#+@+#+%#$#@##@+++ .=%%#$$%%#@+@+ .=-;=-%%%*%@+ *---= ", 36 | ".*$%=%%#*==##+++#@$#@#$$#+@+ .*%$##%%#$@+@+ .=-;;=%%%%$@@ =-*-% ", 37 | ".*%*$$=%@$+$++++@@%OO+$$$+++ .*%$###%+#+#++ .=-;;-=%%*$@@ *=%*@ ", 38 | ".*$*$@%%$@$@$@+#+#+OOO+##+@+ .*$%##@@#@+@@+ .=-;;=**$%#++ *=*#+ ", 39 | ".**$$$#@++@$@#@@++++OO++++++ .%%##@++++@+@+ .=-=;%*%*$$+@ %%$@# ", 40 | ".*$*@@#@$@#++@+@@@@+++++++++ .%@%@@++++++++ .*;-;;%=%#@++ $#@++ ", 41 | ". . .*;-=**%%@+@+ ", 42 | "=;-=-=====-====..............................*-;*;$*$$@++ #:::::", 43 | ";-=*=;-=%==$=*$ ..@$*$**==$$++OO...+#**=%$##.*;-;**:*$#@+ %::>;:", 44 | ";-**%==%$**$$** .@$****=-==%@##@+..#*=$%$%$@ **=-;**$$#+@ =:>,>-", 45 | "-=*--**$$$*$$$$ .***-=--$%*@#@+@#..$=$#@$@+$ %--;-*%$@+@@ =::>-:", 46 | "=%*-;-$$**$*+$$ O-*-%-=$+#$#++@#$ .%%=$##+#@ %;--##%$#@@+ -::-=-", 47 | "==%*--$*$$@@#$$ O==--=-*#$#+@@@+# .#=%%@+$++ $===###$#+++ ;::-:=", 48 | "-%$%*=*$$*@#$++ +=--=:*%$#@@@+##+ .##@++@+@@ $*==%###@+++ -:-;-=", 49 | "-=%*$*$**$#$@$+ #*==%$%%##$#@#++# .. $**;$$###+@+ -:-=*;", 50 | "--*%-****$$#+++ #*=%$*%#+##*#$#$# ==;;==**% .**=**$##+@++ =::-::", 51 | "-=%$%--@$*%%#+@ #*%$-=*$#$%:-%#@# =;;=*%#%$ .*;-=*$$#@@@+ =:-:;-", 52 | "=*$%=--=*$%%#+@ +%%$*-**%%*-%$$#$ =*==%#=#@ .=;-;=*%#@+@@ =:::-=", 53 | "-*-**=-=@$###++ O$$%-1-%=*$*%#%@$ ==*$%=%## .=-=;=*$#@@#+ %:::--", 54 | "=%%==*==@#+@+++ O$$%*-==*==%*$+$+ ===*=##%# .==*=%*%$+++@ %:-::=", 55 | "=*%===*$#@+@OO+ $%*=%=%=-#-#+@#+ =:-=%#@@@ .=*=%$$$$++++ %::;--", 56 | "*%*%==$@@+++OO@ @$$*$*=*=-#+@$++ =*-=%###@ .=*;=*%##+#++ %::-*=", 57 | "*%%$%%$@@#$$@+@ .+@$$$$*=#$*#$+++ =**=$@@#+ .==-;=%#@$$++ %:-:-*", 58 | "*%=*#$$#@#$$@++ ..O+++++++++++++O =*=@#$#@+ .==--==*$@$@+ %:-:%%", 59 | "*=*%%$@@+@##@++ ... ==%$@@@++ .=;;=****%@++ %::%*%", 60 | "$**%$$$$+@O++@+ ..@@@@@@###@@@@+..=%%#++@++ .=;;=*$**%$#+ #:-:**", 61 | "$%%$#$$$+OOO+++ ..@#$**$*%%$$@@+ .==%$@+@++ .=*;*;$#%$#@+ #:-**%", 62 | "$%@#$@$$+OOOO@+ ..@$**%*$#%##%#+ .==$*#@+++ .===-**##@@@+ #--*%%", 63 | "$*$@$@#+@O+OO++ ..@*%#**#%#+#*## .==*=$#@++ .=;=**#++#+++ #-*%*%", 64 | "$%*%#$#+++OOO++ ..@*$*$$*#%@+##+ .=*=*%$#@@ .=;*=*#@++#++ #-***%", 65 | "$*%$@#$#++@++@@ ..@%**$#@+@++@++ .==%$$#@@+ .*-**$$#@@+++ #***$*", 66 | " ..@*%$@++@++###+ .**$$@#+@+ .*=*$+$++@+++ . ", 67 | "......++##%%%$#@++. .*=$@##++@ .%$+@+##@++++ .####.", 68 | "..*=-;;;;;;;;;;;;---==**$.........**$#@++++ .%$@@++++@+#+ .#+$+.", 69 | "..=;;;;;-=-;===;==--$%%%@ .++@@#..*$%#+++++ .. .+++#.", 70 | "..=;--=--;;;*=;;;-=%-@$+@ .+*%-$ .%$%@#@+@@ ....................", 71 | "..;;;--;;**=;;*-%=**$%@+@ .+=-*+ .$$@#@++++ ..;;;;;;;;;;;;;---=.", 72 | "..;=-=---***;=%%=$***$#++ .@--=% .. ..;;;;--==---==-;=% ", 73 | "..;;-*%--=**=;;===$**$++@ .@=%#+ .%*=-===*%%..;--;-=::=-=*;;**% ", 74 | "..;;*=;-%=---$==,=*@#@+@+ .#*#++ .*=-=*-*-## .;;--=;;:$$*%$*#%$ ", 75 | "..;;;;=%;%-$$=%==*$**$@@@ .#%@+@ .=--=%$%#+@ .;-;-=--=$$$*=$$$# ", 76 | "..;=;*==;-*$$$==#$*$$@+$+ .. .*=%%$#+@++ .;;->>=*=*$$=%%*@+ ", 77 | "..;-**;>;=%=$$==%%$*##@++ ........ .;-=>>:;-=*-$#**@+ ", 78 | "..;=;=>1>**=*%$%%%%$OO+@+ .***=======***%%##..;-=*::=*$*$%**$@+ ", 79 | "..;-;-;>;=$==@@@$%%#OOO++ .*=*%=%*==%*=$$##+ .;==-*%*%=%$%=%#@+ ", 80 | "..;=;---%%%$#@@@@###+OO++ .**%%%===-*%%%@#++ .;;=**%%$%***%#@++ ", 81 | "..=;--=$$*=**$@@@@#@+$+++ .=%==-*=*===*%$#+@ .;-=*$$$#$%*$@+@#+ ", 82 | "..;=;-%%-$==*#$+#$$+@+++@ .==%=*%$%*==*%$+@@ .;-;-=%#++$#++OO@+ ", 83 | "..;=;=*%$@%==*#@+$$$+@+@+ .*%=*%=$%***%$#@++ .;;==$$*++@#++OOO+ ", 84 | "..-=%%%=@%@%**##@$$$++#++ .*=%$$$%#$$%$#@+++ .-*=*;*$$@#+#++OO+ ", 85 | "..-=#@%@=$$*+@++@+$$++@@+ .%*=$#$@$#$###+@@+ .-;=$*##$+@+++@@@+ ", 86 | "..==%%@%+@+@#@#@#++++++++ .%*%$#@##@+#++@@++ .=$*$%*$#@+@+@++++ ", 87 | "..=*$%*######@@@@@@@@@@@@ .%$######@@@@@@#@#..=*$%$##@@@@@@@@@@.", 88 | "oo$$$$$$$$$$$$$$$$$$$$$$$oo$$$$$$$$$$$$$$$$$oo$$$$$$$$$$$$$$$$$X", 89 | "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" 90 | }; 91 | -------------------------------------------------------------------------------- /textures/brick.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *ade294b008e64c12cbc0ee1c64d3988e[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "64 64 26 1 ", 5 | " c #202020", 6 | ". c #2C2C2C", 7 | "X c gray19", 8 | "o c #343434", 9 | "O c gray22", 10 | "+ c #400000", 11 | "@ c #4C0000", 12 | "# c #580000", 13 | "$ c #640000", 14 | "% c #700000", 15 | "& c #7C0000", 16 | "* c gray25", 17 | "= c #484848", 18 | "- c #4C4C4C", 19 | "; c gray33", 20 | ": c gray36", 21 | "> c #646464", 22 | ", c #880000", 23 | "< c #980000", 24 | "1 c #A40000", 25 | "2 c #B00000", 26 | "3 c #BC0000", 27 | "4 c #C80000", 28 | "5 c #D40000", 29 | "6 c #E00000", 30 | "7 c #EC0000", 31 | /* pixels */ 32 | "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", 33 | ";O;=====;======;=O=======;;;;;;=========;;;;;;;====OOO==;;=====O", 34 | "O775555555551==;775555555555555555551=O;77776666665555544443331=", 35 | "=74225,15,41$.=;73,3,3,3&3<3<3&3&341%.=O51424224%4&424,21&4<41&.", 36 | "=51&1,1<%1,%$.=;53,,1,%11%<11%<1%1<%#.=;511&$1,%,1&,11,1&,1#1%$.", 37 | "=5&11%<11%1#+.=;5,31,1%1,1%1%,&1%<&%#.==5%1$,1$1%1&1,1$1<&1&1%@.", 38 | "=41&,<&1%,1%+.=;33,1,,<1%,%1,%<&,%1%+.O=3%1&1,$1%%11%1,11&1#1,@.", 39 | "=3&11%1<%<%#+.==34,1%1%1%1%<,%&1%<&%#.O=3<1$,1&,%,&1,1$1&,1&1%+.", 40 | "=31%,#%&#%,%+.O;<<%%@,%%,%@%,%,%@,%%+.O=3%%#%%@,@%#,@%@,$@%,%,+.", 41 | "=&$$$++++++++.O=&$$#########+++++++++.O=,$$$$$$$$$$#########+++.", 42 | "==........ .O;=..... .. .....=;O........... .......", 43 | ";OOOO====OOOOO=O=======OOO;===OO======;O=;;;;:>>:;;==*O*;O;===;O", 44 | "7555333333333331=;=7755555544444443333333333333333331=*;77555555", 45 | "2,,2<&2&<,2$2,1+O=;7%24&3$,3,,4<1,&,1,1,1&4$4,<34<41+O=O74&2,2,2", 46 | ",1,1&,,$1,$,11%+.==52%1&11,1$1%$,1&1&1$11,$1,1%,1%1%+O=;42&1,&1<", 47 | "%11,<$1&,,<$1,%+.O=5%1,<,,$,,%1,$1,&$&,,1#,&1%1%11%%+.*=4&111&1&", 48 | "1,,1,&&,&1<$1$%+.O=5%11&1$,1$%1$,1&,&1$1,#1$1%,%1%1%+.O=42&1&1<&", 49 | "%1$1%,&<$1$,11%+.=;52#1&1,$,#,%$1,&#&1$,#,1$,1%,1%1%+ O=2&11#1&,", 50 | "%2$%@&%<%<%#<%%+.=;21%,#%<%,$%,#%<%<$,%,,#<%%,@%<%,%+ *;21&%#%<%", 51 | "+@@@@@@@@@@@@+++O=;1$$$$$$$$$$$$$##################++ =O1$+$++$+", 52 | "... ... ... .O=O=Oo......ooOOo... ... .....O=;=.......", 53 | ";;=OO=;O;O==OOOO==;===*O*O*O*===;O;=*OOO**==;*OO*===;=;O;*O*;===", 54 | ";775555555333333333333333331=;;77555555555533333333333333333331=", 55 | ";7,23&3&3&2,2,24,2,2,32$24%+O=;73%512,12,2<2&$1,%,1&,1<,1&,1#1+.", 56 | "O72,1<1&%,11,<,1,$1%1,$,1%2+.=:53<1$1$,%<,$1$,1$1%1&1,1$1<&1&%+.", 57 | "=3$11%,#1$1%1,<1$,1%,1,$&1%+.;>53&1&1$1%<1&1&1,$1%%1<%<,11&1#%+.", 58 | "=32,1@1#%,1,%&1&,$&1$1,$1%#+.;:5%1%<,$,<%1$,$,1&,%,&1,1$1&,1&%+.", 59 | ";3%$1@,&1$1#1$1,$,1,$1$,&&2+.=;54%1%1,1,%1$1#%%@1@1#1@11,$@%,%+.", 60 | "=41$%%%#%@%%,$,%%,%%$%<%<%%+.O;41%<%,#%%%%$,%,@1#%1@%,@1#1@1%%+.", 61 | "=1<$$$$$$$$$$$$$$@@@@@@@@@++.O;1$$$$$$$$$$$$$$############+#&#+.", 62 | "==O.................OOO......===........ .OOO. ...........", 63 | ";;;;;==O=OOO=;>>;;;===**OO*OO*;===;O;=;=*O*O*=;;==*O*===;O;===;O", 64 | "33333333331=O;55555555333333333333333331=;;475555555553333333333", 65 | "1212<42241+O=O51$1%11$1,11%1,$11$11%1&1+O=;51&1%11$1&,&1%1,1&1%1", 66 | "1&,1<1&1,%+.=;5$1&,1&1<%1,,%,,1,$1%,1$1+O;;51#1%1,$1,1&,,<%<&<,%", 67 | ",&1<&1&<1%+.=O5$11&<1%1%1,1#1@,,1,%<1&1+O;>51&1%1,1,11&1%<,1<&1%", 68 | "1,&1<1<&,%+O=;5<$1&1&#<,,,%,1$1,$,<%,1&+O;>5&11%1$11&&1&1,<<,&%,", 69 | "1,&1&1&<1%+O==5$1&#<1%,,1,1%,@$<$1<%1&1+O=;41&11%1,,&1&1%<,1&,1%", 70 | "%%,<%%<%%%+O==<$11#1&%1%1$1#,$1,$<%<1$&+.=;41#1%11,1,1&1%<%<&11%", 71 | "++@@@@@@@++.==1$$$$$$$$$##############++.==1$$$$$$$$$$$$$$$####+", 72 | "............=;=. ..OOO. .........O===....OO=O.. .......", 73 | ";=;O=OO==OOOO==OOO=====;;==OOO;===;O=O=O==;=;O;=;:>;-==O;=;=;O;O", 74 | "O7555555533333333333333333333331O;O7755555555333333333333333331=", 75 | ";51,,1$11&<1&1%,1$1&1,1$1$11&%1+O=;711$1$1#1%1#1,#<,%1,&1&<%,1+.", 76 | "=51$1,$1,<&1$,#1<$1#,<,$1&1&1%<+.==5,1$,$&#,#<&1#1$1%,%1,&<1$,+.", 77 | "=5,$1$,%$1$1,&,1$<,,#&1@<#,$,,%+.O=5,1$$,1&#<1#1%%1%1&1%,$1+.", 78 | "O51,1,$1,&<1&1#,1$1#,1#<1&111%&+.O=51$1,$1#1%<&&#,$1&,<,<&%1,1+.", 79 | ";51$,1$1$1$1$1%1$$1#,&1&,$<$,%1+ O;51$$1$#1#<#1$,%1%&1&<,$%+.", 80 | "O41$1$,%,<&1,,,1<$,&#$1@1%<&1%1+ O;4%1$,$1#&1&#<%,$1%1%1&<%1$1+.", 81 | ";1$$$$$$$$$@@@@@@@@@@@@@@@@@@@@+.=;1$$$$$$$$$#####@@@@@@@@@@@++.", 82 | "O=..........OOO. ... .. ..=O=...... .oOo. .... ......", 83 | ";O;O;=;====;>>;=;O;=;=*OOO*=;==O==;O;========-;;;-====OX XO==;=", 84 | "555553333333333331=;=77555555553333333333333331=O;75555533333333", 85 | "&1#11$<$1&4&1$1&1+.==51111$1@1&1$1$1<1$1$1#1%$+O=;51$,1#1%1%1$1%", 86 | "1&&1&1$<&$1#&$,$,+.==5&1%1$%@&<&1,$,<$&,%,#1$1+o=;5$1#1%1%,1%1,@", 87 | "1&#&1$<$&1,$<&+.=*5&&%1$&&1#1@11$1$1$,#,,$%+.=;5$1%%11,%,%$%,", 88 | "&<#<$$1$1&1#&%$1$+.=*51@%1$1@1&<$1$,&$1$1,#%<$+.=;51$#1,%%1%1$%1", 89 | "$1&1$1$,1$1&#,1&,+.=*5@11$1&%,#&@1$1$&&$,%#,%$+.==5$1,1%1,%%#1%1", 90 | "$1&&$<<$<$$1$<+.==5@1%1$&&,@<$,@1&$1,$1#1#,+.O=5$1#%#1%1%#$1@", 91 | "&<#1$$,$&&1#1$%,&+.=;41@%1$1@,$1$1%$1$&1$,%,$,+.O=4$$#1#1%1%1$1%", 92 | "@@@@@@@@@@@@@@@@++.==1+++++@@@@@@@@@@@@@@@@@@++ =;1++@@@@@@@@@@@", 93 | "...................=;=Ooo.............. . O==...... ..", 94 | "O;O;OOO=====;;O====;;;;;===;===;==O===*OO*OO*=*O*;==O;====*O*===", 95 | ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" 96 | }; 97 | -------------------------------------------------------------------------------- /textures/grey.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *_6d8d0d5cfac43ebab2580ae93268b99[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "64 64 16 1 ", 5 | " c #202020", 6 | ". c #2C2C2C", 7 | "X c gray22", 8 | "o c #484848", 9 | "O c gray33", 10 | "+ c #646464", 11 | "@ c gray44", 12 | "# c #7C7C7C", 13 | "$ c gray55", 14 | "% c #989898", 15 | "& c gray66", 16 | "* c #B4B4B4", 17 | "= c #C0C0C0", 18 | "- c #D0D0D0", 19 | "; c gainsboro", 20 | ": c #ECECEC", 21 | /* pixels */ 22 | "................................................................", 23 | "OOOOOOOOOOo;;;;;;;;;;;;;;;; X$&=------==&$O ooooooooooOOOOOOOOOO", 24 | "OOOOOOOOOOo;---=------=---$ X$=*$&$$$$$$&OX X@$$$$$$#@+ooooooooo", 25 | "$$$$$$$$ Oo;-=*-*-=*-==*-$+ X&;&$&$&$&$&$OX X$;;;;;;=*&$$$$$$$$$", 26 | "-;--*&$$ Oo;-&&&&$&&&&%&&#O X$=$$$&&&&$$@OX X$;;===-;;;;;-------", 27 | "&&&&&&#@ Oo:=%&&&&&&%&$&&@O Xo&$&&$$&$&@OOX X$;*&$&$&&&$$&$&$&$%", 28 | "*%&%&$#@ oo:-&$&%&$%%%$%&$O Xo#$$&$&&$&@XXX X$;*$&$%$$$&$$%%&%$&", 29 | "**#*$%+O Xo:-$%$&&&$#$%&$+O Xoo$&&&$$$&@XXX.X$;*&$%#&%#$&$&&$#&%", 30 | "%&&%%@oo .o==&%&$&$%$%$$&@O oOo#XXXXXXXXX .X#;;$&$#%#%$&$%#&&#$", 31 | "$$$$@Ooo .o&:&&&$$%$$$$&$O#. XoXXXXXXXXX ooo=-$$$%%%%$$&$&&$&$", 32 | "@@@@OoXX .o$;$&$%#$$&$&$&@O. XoX.. ....oOo=*$&$&$$$@@+++++++", 33 | "OOooo .o@;&$%###&%$$#$@+X Xoooo@#$&&&&%@XOo*$@@+++OOOOooooooo", 34 | " ....XOo;-$$$%%$$&#$&$OX Xo@&&;;:;--%@O Oo$@++OoooXXXXX.....", 35 | ".........XOo&-$&$#$&$$%$$@OX .X&=;-*$&$&$@X XXXXXXXXXXX.. ", 36 | "XXXXXXXXXXOo$-&$&%$#$#$&$OOX .o&;=&$$$$$$OX X..........XXXXXXXXX", 37 | "OoXXXXXXXXOo@=$&%$&%&$&$&O@o .o&:&$$&$&$&OX XoOOOO ooooooooooooo", 38 | "%%%%%%%%%. Oo==%$$%%#$$$$@OO o&:$$&&$&$$OX X@&;;OoO#%%%%%%%%%%%", 39 | "------**@X Oo$=$&$#$$$&$@$@o o&-&&$$&$&$OX X&;=& o#=;;;;;;;;;;;", 40 | "&%$&$&&@@X Oo@%&$&&$&@$@O@Oo o&-&$@@@@OOOX X;=&$ o%;-**********", 41 | "&$&$&$$@@X oOo%%@@@@OO@O@Oo. .&$@$@XXXXXXX +;*$$ o%;*&%&%&$%$%%", 42 | "&%$&$&&@OX oOo+OOOOoooX. .$@XXX +;&&$ o%;&$%$%$%##$$", 43 | "&$&$&$$@OX oOo.... .XOX ......$;&&$ o$=$&%&%&$%$%%", 44 | "$&##$$&@OX XOOoX ooooooo$$%&&%$$#O.X+=;&$# o$=&$%$%$%##$$", 45 | "$$##$&$+oX X$;;;;=*&$+Xo+#&&==-;;;;;;;-%+ X#;=&&@ o#=&%$&$&$##$%", 46 | "$$&&$$$+oX X;=&%%%&%%XOo#-::;=*========@O X$;*%$@ o@=$&%$%$$&$&$", 47 | "%&$$&$@OXX X;&$%&&%%& Oo%:=$&$&%$&%&%$$+o X$:&$&O oo$@+OOO++@#$$", 48 | "+++++OoXXX X;&&&$&$&+ oo%:&&&$#&&$$$&$&Oo X$:&&%O O XXoooooooOOO", 49 | "OooXXX .X;$&$&*$#+ oo%:$&$%&$$#$&%$$Oo X$:$&$O OX XXo", 50 | " ...ooo;&%$&$&#+ Xo%:%$&$$#$#$$$&$Oo X$:&&$O OXXXXXXX... ", 51 | "o++@$%$$$$o ;&#%$&$#+X o%:&$$$#$#$&#$$&Oo X$:%&&O OoooOOOOOOOXXX", 52 | "+$*;;;---&@ ;&&$#$&$+X o$;=%$&$&##$#$&$OX X$;$$&O o&;;*$+OoooooO", 53 | "+*--=*&&%&o :%%&#$&%@o o#-=&$$#$##&$&$#OX.X$;&%$O o;$$*;;;;;;%XO", 54 | "+;*&&$&%&$@ :%&%&&$&@o XO=:&&%$&$&##$&@OX.X$;$&$O o;$$&&$$&$$#XO", 55 | "+;*%$&$&$&@ :%$%&$&$+O .O&:&&&$$#$##&$@o..o$-&&$O o;&&$$&&$&$+Xo", 56 | "+;&%&$&&#&O ;&&&%$%&+O .O$:=&$&$$%$&$&+o .o$-&$&O o-&$&&$$&$&+Xo", 57 | "+;*&$$&%&$O ;=#%#&&%+O o@-=&$#%$$%$%$+X .o$-$&OO o=&&&$&$$&&OXo", 58 | "+;*$&&$&$&O =;$%&$%$+O oO*;&$&$&$&$$+OX Xo#&#OXXXo&&$&&$&$$&OXX", 59 | "+;*&&$&%&$O *;#&$$$&@O XO$=&$#++OooXXXX XoOXXXXoo @OoOOOOOOOOXX", 60 | "+;*$&&$&#&O %;%#&&$$@O XOo$$+OoX XoOooOOOOO X", 61 | "+;*$$%&$&$O #;&%&$$&+O .ooX... ........XoOOoooooooooXXXXXXXXXX", 62 | "+;*&&$%&%&O O:$&$$&$+O .oooXXXXXooooooooooOO#################XO", 63 | "+;*$$&$&$@O o:&$$#$&@O. .X+#############$$%%&**======*=======#XO", 64 | "+-*&&%&$&O .o:&$&$$$+O. X#===============-;;:::::::::::;;--%+.o", 65 | "+-*%&$&&$O .o:$&#%$&+OX #=-::::::;::;::::::;;-=*===&==*==*#O.o", 66 | "+=*&$&$$$O Xo;&&$&$$+OX #=-*=====&==&===**&%%$$&$&$$$&$&$&@O.X", 67 | "o=$$&$@@@O Xo-$$&$&$@OX +=;*$$$$$$&$&&%$%&&&%#%$&&%$&$&&&$@O.X", 68 | "o*&&&$OOO .Xo*&$$&$$@OX o=;=$%$&$&$%&$$%&&$%$%$$#$%%$&$&$&@O.X", 69 | "o###OOOOO .Xo%%&$$$@+OX .*;-&&$$$$#$$&$%$&%#$$&$$&$##$#%$%@O X", 70 | "oX OOOO Xoo#++++++OOX .&;-$$&$&$$&$##%%$&$&%$&$$#$$$&$##@O X", 71 | "OOOoX . .XoXXXXXXXXXXX .$-**$$&$&#$$%#%$%#&$$#$$$$&$#$%##@O X", 72 | "ooooooo...XoXX .$%@@@@@@@&$&&$$#$$$#$$&$&$$&$$$$%@O X", 73 | "o#&&&&%$+ooOXX............XOoXX.....O@@#$###################+O X", 74 | "o&=;;;;;=&&$@+OoXXXXXXXXXXoOooXXXXXX.oOOOOOOOOOOOOOOOOOOOOOOO .X", 75 | "o&;&&$=-;;;=&&&&&&&&$$$##@++OOoooooXX............. .Xo", 76 | "o&;&&&**$&=;;;;;;;;;;;;--==&&&&&$oOoXXXXXXXXX...............XXoo", 77 | "o&;&$&##&%$#&$&$&$&&$&$&&*=;;;-=#XoOoooXXXXXXXXXXXXXXXXXXXXXoooO", 78 | "o&-&&&%&$%$%&$$&##$&#$$$&$&$&&&#+ XOo&;;&#oooOOOooooooooooooOOOO", 79 | "o&=&$&##%&$&&%$#&#$#%$&$&$&$&&&+O Xoo;&&;;;;*OOo*;;;;;;;-=*%#ooO", 80 | "o&$++++++++++++++++++++++++++++OOX Xo;*******+O#;&&&$&$&%&$&;% O", 81 | "o$+XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX .o*&**%+%$+o*&$&$&&&$&$&$&O O", 82 | "oX .oXoXO+++++o;&%%&&%%&%&&$$O O", 83 | "OoXXX...............................XOOXX XOOOOOOOOOOOOOOO O", 84 | "OOOOooooooooXXXXXXXXXXXXXXXXXXXXXXXXXOOOOXXXXXXOO O", 85 | "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 86 | }; 87 | -------------------------------------------------------------------------------- /textures/tree.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *_39f988af1f74a6cbebd2b7a87074693S1wN5gCaTaKpuLfA[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "48 48 7 1 ", 5 | " c None", 6 | ". c #352E1D", 7 | "X c #6D491D", 8 | "o c #275D4E", 9 | "O c #40BF40", 10 | "+ c #AC8D25", 11 | "@ c #91E26A", 12 | /* pixels */ 13 | " @@ ", 14 | " oo ooo OO@OO @@ ", 15 | " oo o@@OOOO@O@ @ @@@ ", 16 | " o ooOO OOoo@O@@OOO@@@@ @ @ ", 17 | " ooooOOOOOoOOO@@@OO@@@@@OO@ ", 18 | " ooooO@@OOOOOOOOOO@OO@@@O @@ ", 19 | " ooOOoO@OOOOOOOOOOO@OO@@@OOO@@ ", 20 | " ooOOOOOOOO@@OOOOO@@@O@OO@@@@@ ", 21 | " oooooOOO@@@OO@@OoOOOOOOO@@@@@ ", 22 | " o ooO@OOOO@@@@@@OOoO@OOOO@@@@@@@ ", 23 | " o ooO@OO@O@@@@@@OOoOOOOOOO@O@@@@ @ ", 24 | " ooooo@OOO@@@@@@@@OOOO@@@O@@OOOO@@@ ", 25 | " ooOooOOOOOO@@O@OO@@OOOOO@@@@@@@@@@@ ", 26 | " oOOOoooOOOOOOO@@O@OOOOOOOOO@@@@@@@@@@@ ", 27 | " ooooo@@OOOOOOOOO@@@OOOOO@OOO@@@@@O@ @ ", 28 | " ooOoO@@oOOOO@OOOOO@OOOOOO@@@@@@@@OO@@ ", 29 | " oooOOOoooOoOO@@@OOOOOoooOOOOO@@O@@@@@@ ", 30 | " ooOOOooOOooOOOOOOOOooooooOOO@@OO@@@@@ ", 31 | " ooOoooooooooOOOOOOOoooooOoOOO@O@@@@@@@@ ", 32 | " oooooooooOOoOOOOOOOOooooooO@OOOO@@OO@@@ ", 33 | " ooooOoooooooOOooOoooooooo@ooOOO@O@@O@@ ", 34 | " ooooOoOooooooooooooooOOoooooOoOOOO@O@ ", 35 | " oooooOOOooooooooooooOOOOOoooOOOOOOOOO ", 36 | " ooooooooooooooOoooooooOoOooooOOOooO OO ", 37 | " ooooooooooooooooooooooOooooooooooo O ", 38 | " o oooooooooOOOOooooooooooooooooOooo ", 39 | " ooooooooooooOOOoooooooooooooOoOOo ", 40 | " oooooooooooOOoOoooooooooooooOOOooo ", 41 | " ooooooooooooooooooooooooooooooo ", 42 | " oooooooooooooooooooooooooooooooooo ", 43 | " ooooooooooooo oooooooooo ", 44 | " oo ooooooooo . .oooooo ", 45 | " ...+ o .... o ", 46 | " ...X .... ", 47 | " ..X+ .... ", 48 | " ...XX .... ", 49 | " .....X.... ", 50 | " ......... ", 51 | " ........X ", 52 | " ....XX.X+ ", 53 | " ....X.XX ", 54 | " ....X.X+ ", 55 | " ....X..+ ", 56 | " ......X+X ", 57 | " .......X++X ", 58 | " ........XX ", 59 | " ........XX.+ ", 60 | " ... .X+ " 61 | }; 62 | -------------------------------------------------------------------------------- /textures/wood.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *_80d32f324d6458ef55585791f109459[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "64 64 9 1 ", 5 | " c #28200C", 6 | ". c #382C14", 7 | "X c #403018", 8 | "o c #483818", 9 | "O c #543C1C", 10 | "+ c #5C4020", 11 | "@ c #6C4824", 12 | "# c #744C28", 13 | "$ c #905C34", 14 | /* pixels */ 15 | "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 16 | "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", 17 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 18 | "OOOOOOO.O .OOOOOOOOOOOOOO.O .OOOO.O .OOOOOO.O .OOOOOOOOOOO.O .OO", 19 | "oo@####O$ O$+oooo##o@o@ooO$ O$#o#O$ O$+ooo+O$ O$o#oo#o#o##O$ O$@", 20 | "oo@###+O$ O$@oooO##o@o@ooO$ O$#o#O$ O$#+o+#O$ O$o#oo#o#o##O$ O$@", 21 | "oo+###OO$ O$#OooO##o@o@ooO$ O$@o#O$ O$##+##O$ O$o#oo#o#o##O$ O$@", 22 | "OoO@##OO$ O$#Ooo+#@o@o@ooO$ O$@o#O$ O$#####O$ O$o#oo#o#o##O$ O$@", 23 | "@oo+#+oO$ O$#+oO@#@o@o@ooO$ O$@o#O$ O$#####O$ O$o#oo#o#o@#O$ O$O", 24 | "@oOo+OoO$ O$#@o+##+o@o@ooO$ O$+o#O$ O$#####O$ O$o#oo#o#o@#O$ O$O", 25 | "#OooOooO$ O$##+@##+o@o@ooO$ O$+o#O$ O$#####O$ O$o#oo#o#o+#O$ O$O", 26 | "#OooooOO$ O$##@###Oo@o@ooO$ O$oo#O$ O$#####O$ O$o#oo#o#o+#O$ O$o", 27 | "@#oooO#O$ O$##@###Oo@o@ooO$ O$o+#O$ O$@####O$ O$o#oo#o#Oo#O$ O$o", 28 | "@#OooO#O$ O$#####@oo@o@ooO$ O$o+#O$ O$@###@O$ O$o#oo#o#Oo#O$ O$o", 29 | "+##oO##O$ O$#####@oo@o@ooO$ O$o##O$ O$+###@O$ O$o#oo#o#Oo#O$ O$o", 30 | "O###O#+O$ O$@####@oo@o@ooO$ O$+##O$ O$o@#@+O$ O$o#oo#o#Oo#O$ O$O", 31 | "o@##+#OO$ O$@####+oo@o@ooO$ O$+#@O$ O$o+@+oO$ O$o#oo#o#+o#O$ O$O", 32 | "o@####OO$ O$+####+oo@o@ooO$ O$##@O$ O$oo+ooO$ O$o#oo#o#+o@O$ O$O", 33 | "o+@###oO$ O$+####Ooo@o@ooO$ O$##+O$ O$oooooO$ O$o#oo#o#@o@O$ O$@", 34 | "oO@##+oO$ O$O@###OoO@o@ooO$ O$#@+O$ O$oooooO$ O$o#oo#o#@o@O$ O$@", 35 | "oo+##OoO$ O$O@##@ooO@o@ooO$ O$#@oO$ O$oooooO$ O$o#oo#o#@o+O$ O$@", 36 | "ooO##OoO$ O$o+##@ooO@o@ooO$ O$#+oO$ O$+ooo+O$ O$o#oo#o##o+O$ O$@", 37 | "OoO+OooO$ O$o+##+oo+@o@ooO$ O$@ooO$ O$@+o+@O$ O$o#oo#o@#ooO$ O$@", 38 | "OooOooOO$ O$oO@@+oo+@o@ooO$ O$+ooO$ O$#@+@#O$ O$o#oo#o@#OoO$ O$@", 39 | "OoooooOO$ O$oO++Ooo++o@ooO$ O$oooO$ O$##@##O$ O$o#oo#o+#OoO$ O$+", 40 | "@ooooo@O$ O$ooOOooo@Oo@ooO$ O$oooO$ O$#####O$ O$o#oo#o+#+oO$ O$O", 41 | "@ooooo@O$ O$ooooooo@Oo@ooO$ O$ooOO$ O$#####O$ O$o#oo#oO#+oO$ O$O", 42 | "@OoooO+O$ O$ooooooO#oo@ooO$ O$ooOO$ O$#####O$ O$o#oo#oO#@OO$ O$o", 43 | "+OoooOOO$ O$oooooo+#oo@ooO$ O$oo+O$ O$@####O$ O$o#oo#oO#@OO$ O$o", 44 | "O@ooo@OO$ O$oooooo+#oo@ooO$ O$oo@O$ O$@####O$ O$o#oo#oo##+o$ O$o", 45 | "O@Ooo@oO$ O$oooooo@#oo@ooO$ O$oO@O$ O$+###@O$ O$o#oo#oo##+O$ O$o", 46 | "o+Oo@+oO$ O$Oooooo@#oo@ooO$ O$o+#O$ O$+@##@O$ O$o#oo#oo@#@O$ O$o", 47 | "oO@O@OoO$ O$+ooooO#@oO@ooO$ O$O@#O$ O$o@##+O$ O$o#oo#oo@#@O$ O$o", 48 | "oO@O@OoO$ O$@Oooo+#@oO@ooO$ O$+##O$ O$o+#@oO$ O$o#oo#Oo+##O$ O$o", 49 | "oo+@+ooO$ O$#+ooO+#+o+@ooO$ O$@##O$ O$oo@+oO$ O$o#oo#Oo+##O$ O$O", 50 | "ooO+OooO$ O$#@OoO##Oo+@ooO$ O$###O$ O$oo+ooO$ O$o#oo#OoO##O$ O$O", 51 | "oooOoooO$ O$##+o+##Oo+@ooO$ O$##@O$ O$oooooO$ O$o#oo#OoO##O$ O$+", 52 | "oooooooO$ O$###+###oo@@ooO$ O$##@O$ O$oooooO$ O$o#oo#+oO##O$ O$@", 53 | "ooooooOO$ O$######@oo@@ooO$ O$##+O$ O$oooooO$ O$o#oo#+oo@#O$ O$@", 54 | "OoooooOO$ O$@#####@oo#@ooO$ O$#@+O$ O$+ooooO$ O$o#oo#@oo@#O$ O$@", 55 | "Oooooo+O$ O$@#####+oo#@ooO$ O$#@oO$ O$+ooooO$ O$o#oo@@oo+#O$ O$O", 56 | "@OoooO+O$ O$+#####Ooo#+ooO$ O$#+oO$ O$@+oo+O$ O$o#oo@#oo+#O$ O$O", 57 | "@OoooO@O$ O$O#####ooo#+ooO$ O$@+oO$ O$#+o++O$ O$o#oo+#OoO@O$ O$o", 58 | "@+OoO+@O$ O$o@###@ooO#oooO$ O$@ooO$ O$#@++@O$ O$o#oo+#OoO@O$ O$o", 59 | "@@OOO@+O$ O$o+###+oo+#oooO$ O$+ooO$ O$##+@#O$ O$o#ooO#OoO+O$ O$o", 60 | "+@+O+@OO$ O$oO@#@Ooo@#oooO$ O$oooO$ O$##@##O$ O$o#ooO#+oo+O$ O$o", 61 | "O@@+@@OO$ O$ooO@+ooo@#oooO$ O$ooOO$ O$#####O$ O$o#ooO#+OoOO$ O$o", 62 | "O@@+@@oO$ O$oooOOooo#@oooO$ O$ooOO$ O$#####O$ O$o#ooO#@OoOO$ O$o", 63 | "o@###@oO$ O$OooooooO#@oooO$ O$oo+O$ O$####@O$ O$o#ooo#@OoOO$ O$o", 64 | "o+###@oO$ O$Ooooooo+#+oooO$ O$oO@O$ O$@###@O$ O$o#ooo@#+ooO$ O$o", 65 | "oO@##@oO$ O$+oooooo@#+oooO$ O$o+#O$ O$@###+O$ O$o#ooo@#+OoO$ O$o", 66 | "oO@##+oO$ O$@Oooooo@#ooooO$ O$O@#O$ O$+###+O$ O$o#ooo+#@OoO$ O$O", 67 | "oo+#@+oO$ O$#OooooO#@ooooO$ O$@##O$ O$+##@OO$ O$o#ooo+#@+oO$ O$O", 68 | "ooO@@OoO$ O$#+oooo+#@oo+oO$ O$###O$ O$O@#@OO$ O$o#OooO@#+OO$ O$+", 69 | "ooO@+OoO$ O$@@Oooo@#+oo+oO$ O$###O$ O$O@#+oO$ O$o#OooO+#@OO$ O$+", 70 | "ooo++ooO$ O$@#OooO@#ooo@oO$ O$###O$ O$o+@OoO$ O$o#Oooo+#@OO$ O$@", 71 | "oooOOooO$ O$+#@OoO##ooo@oO$ O$###O$ O$oO+ooO$ O$o#OoooO@#OO$ O$@", 72 | "OoooooOO$ O$o@#@O##@ooo@oO$ O$###O$ O$ooOooO$ O$o@+oooO+#@O$ O$#", 73 | "OoooooOO$ O$o+#####+ooo#oO$ O$##@O$ O$oooooO$ O$o@@oooo+#@O$ O$#", 74 | "+ooooo@O$ O$oo####@ooo+#oO$ O$##@O$ O$oooooO$ O$o+@OoooO@@O$ O$#", 75 | "+OoooO@O$ O$oo+@#@+ooo@#oO$ O$#@+O$ O$oooooO$ O$o+#Ooooo++O$ O$#", 76 | "@OoooO@O$ O$ooo+@+oooo@#oO$ O$#@+O$ O$oooooO$ O$oo#OoooooOO$ O$#", 77 | "@+oooO@O$ O$oooo+oooo@##+O$ O$@+oO$ O$oooooO$ O$+o#+ooooooO$ O$#", 78 | "+++++++O$ O$+ooooooo+++++O$ O$+ooO$ O$+ooo+O$ O$+o++ooooooO$ O$+" 79 | }; 80 | -------------------------------------------------------------------------------- /utils/etc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* etc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/07/03 18:04:01 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/18 00:09:09 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | #include "hyochoi_macros.h" 15 | 16 | int ft_isblank(const char c) 17 | { 18 | if ((c >= 9 && c <= 13) || c == 32) 19 | return (c); 20 | else 21 | return (0); 22 | } 23 | 24 | int is_blank_line(const char *s) 25 | { 26 | int i; 27 | 28 | i = 0; 29 | while (s[i]) 30 | { 31 | if (ft_isblank(s[i++])) 32 | continue; 33 | else 34 | return (0); 35 | } 36 | return (1); 37 | } 38 | 39 | char *skip_(int flag, char *str) 40 | { 41 | if (flag == SPACE) 42 | { 43 | while (*str == ' ') 44 | str++; 45 | } 46 | else if (flag == NUM) 47 | { 48 | while (*str >= '0' && *str <= '9') 49 | str++; 50 | } 51 | return (str); 52 | } 53 | 54 | int ft_atoi(const char *nptr) 55 | { 56 | unsigned long long result; 57 | size_t i; 58 | int minus; 59 | 60 | result = 0; 61 | minus = 1; 62 | i = 0; 63 | while (ft_isblank(*nptr)) 64 | { 65 | nptr++; 66 | } 67 | if (*nptr == '-' || *nptr == '+') 68 | { 69 | if (*nptr == '-') 70 | minus *= -1; 71 | i++; 72 | } 73 | while (nptr[i] >= '0' && nptr[i] <= '9') 74 | { 75 | result = result * 10 + (nptr[i] - '0'); 76 | if (result > 4294967295) 77 | return (minus == 1 ? -1 : 0); 78 | i++; 79 | } 80 | return (result * minus); 81 | } 82 | -------------------------------------------------------------------------------- /utils/ft_strdup.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char *ft_strdup(const char *s1) 4 | { 5 | size_t len; 6 | char *res; 7 | char *c; 8 | 9 | len = 0; 10 | while (*s1++) 11 | len++; 12 | len++; 13 | s1 -= len; 14 | if (!(res = (char*)malloc(sizeof(char) * len))) 15 | return (0); 16 | c = res; 17 | while (*s1) 18 | *c++ = *s1++; 19 | *c = 0; 20 | return (res); 21 | } 22 | -------------------------------------------------------------------------------- /utils/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/07/05 20:17:42 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/10 18:16:14 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | #define EOF_REACHED 0 16 | #define NL_EXIST 1 17 | #define ERROR -1 18 | #define BUFFER_SIZE 2048 19 | 20 | static int check_newline(char **saved, char *ptr, char **line) 21 | { 22 | char *temp; 23 | 24 | if (ptr != 0) 25 | { 26 | *line = ft_strndup(*saved, ptr - *saved); 27 | temp = ft_strndup(ptr + 1, ft_strlen(ptr + 1)); 28 | free(*saved); 29 | *saved = temp; 30 | return (NL_EXIST); 31 | } 32 | if (*saved != 0) 33 | { 34 | *line = *saved; 35 | *saved = 0; 36 | } 37 | else 38 | *line = ft_strndup("", 0); 39 | return (EOF_REACHED); 40 | } 41 | 42 | int get_next_line(int fd, char **line) 43 | { 44 | static char *saved; 45 | char buf[BUFFER_SIZE + 1]; 46 | char *temp; 47 | char *nl_ptr; 48 | ssize_t len; 49 | 50 | if (fd < 0 || line == 0) 51 | return (ERROR); 52 | if (saved == 0 && !(saved = ft_strndup("", 0))) 53 | return (ERROR); 54 | while ((nl_ptr = ft_strchr(saved, '\n')) == 0 && 55 | (len = read(fd, buf, BUFFER_SIZE)) > 0) 56 | { 57 | buf[len] = 0; 58 | if (!(temp = ft_strjoin(saved, buf))) 59 | return (ERROR); 60 | if (saved != 0) 61 | free(saved); 62 | saved = temp; 63 | } 64 | if (len < 0) 65 | return (ERROR); 66 | return (check_newline(&saved, nl_ptr, line)); 67 | } 68 | -------------------------------------------------------------------------------- /utils/list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* list.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/07/02 21:07:54 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/10 18:16:20 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | t_list *ft_lstnew(char *content) 16 | { 17 | t_list *newlst; 18 | 19 | newlst = (t_list *)malloc(sizeof(t_list)); 20 | if (!newlst) 21 | return (0); 22 | newlst->content = content; 23 | newlst->next = 0; 24 | return (newlst); 25 | } 26 | 27 | void ft_lstadd_back(t_list **lst, t_list *new) 28 | { 29 | t_list *cur; 30 | 31 | if (!lst || !new) 32 | return ; 33 | if (!*lst) 34 | { 35 | *lst = new; 36 | return ; 37 | } 38 | cur = *lst; 39 | while (cur->next) 40 | { 41 | cur = cur->next; 42 | } 43 | cur->next = new; 44 | } 45 | 46 | int ft_lstsize(t_list *lst) 47 | { 48 | int n; 49 | 50 | n = 1; 51 | if (!lst) 52 | return (0); 53 | while (lst->next) 54 | { 55 | lst = lst->next; 56 | n++; 57 | } 58 | return (n); 59 | } 60 | -------------------------------------------------------------------------------- /utils/str.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* str.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hyochoi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/07/08 13:55:45 by hyochoi #+# #+# */ 9 | /* Updated: 2020/09/10 18:16:26 by hyochoi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | while (s[len]) 21 | { 22 | len++; 23 | } 24 | return (len); 25 | } 26 | 27 | char *ft_strchr(const char *s, int c) 28 | { 29 | size_t i; 30 | size_t len; 31 | 32 | if (s == 0) 33 | return (0); 34 | i = 0; 35 | len = ft_strlen(s); 36 | while (i <= len) 37 | { 38 | if (s[i] == c) 39 | return ((char *)s + i); 40 | i++; 41 | } 42 | return (0); 43 | } 44 | 45 | char *ft_strndup(char *str, size_t len) 46 | { 47 | char *newstr; 48 | size_t i; 49 | 50 | i = 0; 51 | if (!(newstr = (char *)malloc(sizeof(char) * (len + 1)))) 52 | return (0); 53 | while (i < len) 54 | { 55 | newstr[i] = str[i]; 56 | i++; 57 | } 58 | newstr[i] = 0; 59 | return (newstr); 60 | } 61 | 62 | char *ft_strjoin(char const *s1, char const *s2) 63 | { 64 | char *newstr; 65 | char *tmp; 66 | size_t len; 67 | 68 | if (!s1 || !s2) 69 | return (0); 70 | len = ft_strlen(s1) + ft_strlen(s2); 71 | if (!(newstr = (char *)malloc(sizeof(char) * (len + 1)))) 72 | return (0); 73 | tmp = newstr; 74 | while (*s1) 75 | *tmp++ = *s1++; 76 | while (*s2) 77 | *tmp++ = *s2++; 78 | *tmp = 0; 79 | return (newstr); 80 | } 81 | 82 | int ft_strcmp(const char *s1, const char *s2) 83 | { 84 | size_t i; 85 | 86 | i = 0; 87 | while (s1[i] || s2[i]) 88 | { 89 | if (s1[i] != s2[i]) 90 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 91 | i++; 92 | } 93 | return (0); 94 | } 95 | --------------------------------------------------------------------------------