├── Cub3D ├── Cub3D_bonus ├── DOCUMENTATION.md ├── Makefile ├── README.md ├── ennemy ├── blood.png ├── blood.xpm ├── ennemy-1.png ├── ennemy-1.xpm ├── ennemy-2.png ├── ennemy-2.xpm ├── ennemy-3.png ├── ennemy-3.xpm ├── ennemy-4.png ├── ennemy-4.xpm ├── ennemy-5.png ├── ennemy-5.xpm ├── ennemy-6.png ├── ennemy-6.xpm ├── ennemy-7.png ├── ennemy-7.xpm ├── ennemy-8.png ├── ennemy-8.xpm ├── ennemy-9.png └── ennemy-9.xpm ├── fr.subject.pdf ├── items ├── ammo.png ├── ammo.xpm ├── ammo1.png ├── ammo1.xpm ├── game_over.png ├── game_over.xpm ├── screen.png ├── treasure.png ├── treasure.xpm ├── treasure1.png ├── treasure1.xpm ├── treasure2.png └── treasure2.xpm ├── libft ├── Makefile ├── ft_atoi.c ├── ft_atoi_conv.c ├── ft_bzero.c ├── ft_calloc.c ├── ft_ctoa.c ├── ft_isalnum.c ├── ft_isalpha.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_isprint.c ├── ft_itoa.c ├── ft_itoa_long.c ├── ft_lstadd_back_bonus.c ├── ft_lstadd_front_bonus.c ├── ft_lstclear_bonus.c ├── ft_lstdelone_bonus.c ├── ft_lstiter_bonus.c ├── ft_lstlast_bonus.c ├── ft_lstmap_bonus.c ├── ft_lstnew_bonus.c ├── ft_lstsize_bonus.c ├── ft_memccpy.c ├── ft_memchr.c ├── ft_memcmp.c ├── ft_memcpy.c ├── ft_memmove.c ├── ft_memset.c ├── ft_putchar.c ├── ft_putchar_fd.c ├── ft_putendl_fd.c ├── ft_putnbr.c ├── ft_putnbr_fd.c ├── ft_putstr.c ├── ft_putstr_fd.c ├── ft_split.c ├── ft_strchr.c ├── ft_strdup.c ├── ft_strdup_char.c ├── ft_strdup_free.c ├── ft_strjoin.c ├── ft_strjoin_free.c ├── ft_strlcat.c ├── ft_strlcpy.c ├── ft_strlen.c ├── ft_strmapi.c ├── ft_strncmp.c ├── ft_strnstr.c ├── ft_strrchr.c ├── ft_strreplace.c ├── ft_strsub.c ├── ft_strtrim.c ├── ft_substr.c ├── ft_tolower.c ├── ft_toupper.c ├── libft.a └── libft.h ├── maps ├── master_map.cub ├── test.cub ├── test4.cub ├── test5.cub ├── test6.cub └── test7.cub ├── sound ├── collision.mp3 ├── gun_change.mp3 ├── gun_shot.mp3 └── music.mp3 ├── srcs ├── bonus │ ├── cub3d_bonus.h │ ├── cub3d_utils2_bonus.c │ ├── cub3d_utils3_bonus.c │ ├── cub3d_utils_bonus.c │ ├── display_bonus.c │ ├── display_items_bonus.c │ ├── error2_bonus.c │ ├── error_bonus.c │ ├── events_bonus.c │ ├── exit_bonus.c │ ├── free_bonus.c │ ├── init2_bonus.c │ ├── init_bonus.c │ ├── items2_bonus.c │ ├── items_bonus.c │ ├── main_bonus.c │ ├── map_bonus.c │ ├── minimap_bonus.c │ ├── move_bonus.c │ ├── parser_bonus.c │ ├── parser_tex_bonus.c │ ├── ray2_bonus.c │ ├── ray_bonus.c │ ├── save2_bonus.c │ ├── save_bonus.c │ ├── sprite2_bonus.c │ ├── sprite3_bonus.c │ ├── sprite_bonus.c │ ├── textures_bonus.c │ ├── weapon2_bonus.c │ └── weapon_bonus.c ├── gnl │ ├── get_next_line.c │ ├── get_next_line.h │ └── get_next_line_utils.c ├── mandatory │ ├── cub3d.h │ ├── cub3d_utils.c │ ├── cub3d_utils2.c │ ├── cub3d_utils3.c │ ├── display.c │ ├── display_items.c │ ├── error.c │ ├── error2.c │ ├── events.c │ ├── exit.c │ ├── free.c │ ├── init.c │ ├── init2.c │ ├── items.c │ ├── items2.c │ ├── main.c │ ├── map.c │ ├── minimap.c │ ├── move.c │ ├── parser.c │ ├── parser_tex.c │ ├── ray.c │ ├── ray2.c │ ├── save.c │ ├── save2.c │ ├── sprite.c │ ├── sprite2.c │ ├── sprite3.c │ ├── textures.c │ ├── weapon.c │ └── weapon2.c └── minilibx │ ├── Makefile │ ├── font.c │ ├── font.xcf │ ├── mlx.h │ ├── mlx_init_loop.m │ ├── mlx_int.h │ ├── mlx_int_str_to_wordtab.c │ ├── mlx_mouse.m │ ├── mlx_new_image.m │ ├── mlx_new_window.h │ ├── mlx_new_window.m │ ├── mlx_opengl.h │ ├── mlx_opengl.m │ ├── mlx_png.c │ ├── mlx_png.h │ ├── mlx_rgb.c │ ├── mlx_shaders.c │ └── mlx_xpm.c ├── textures ├── 10.xpm ├── EA.bmp ├── EA.xpm ├── NO.bmp ├── NO.xpm ├── SO.bmp ├── SO.xpm ├── WE.bmp ├── WE.xpm ├── aim-original.png ├── aim.png ├── aim.xpm ├── barrel.png ├── barrel.xpm ├── bonus.png ├── bonus.xpm ├── bricks.bmp ├── bricks.png ├── bricks.xpm ├── door.bmp ├── door.xpm ├── life.png ├── life.xpm ├── sound.png └── sound.xpm └── weapon ├── bullets.png ├── bullets.xpm ├── firegun.png ├── firegun.xpm ├── gun.png ├── gun.xpm ├── shotgun.png ├── shotgun.xpm ├── sniper.png ├── sniper.xpm ├── weapons.png └── weapons.xpm /Cub3D: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/Cub3D -------------------------------------------------------------------------------- /Cub3D_bonus: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/Cub3D_bonus -------------------------------------------------------------------------------- /DOCUMENTATION.md: -------------------------------------------------------------------------------- 1 | cub3d project @42born2code 2 | 3 | # MLX 🎨 4 | 5 | ## MLX CONFIG for non-42 computer: 6 | 7 | [INSTALLATION TUTORIAL](https://achedeuzot.me/2014/12/20/installer-la-minilibx/) 8 | 9 | ## COMPILATION WITH MLX: 10 | 11 | ### using X11 12 | 13 | ```shell 14 | gcc -I /usr/X11/include -g -L /usr/X11/lib -lX11 -lmlx -lXext .c 15 | ``` 16 | 17 | ### using terminal 18 | 19 | ```shell 20 | gcc .c libft/.c get_next_line/*.c -L minilibx_opengl -l mlx -framework OpenGL -framework AppKit 21 | ``` 22 | 23 | 🌟 Have a look at the `Makefile` to see how mlx was used 24 | 25 | ### DOCUMENTATION 26 | Example of basic MLX function [here](https://github.com/keuhdall/images_example/blob/master/example.c) 27 | 28 | # RAYCASTING 💫 29 | 30 | ## tutorial 31 | ▶️BEST TUTORIAL FOR RAYCASTING: [lodev.org](https://lodev.org/cgtutor/raycasting.html) 32 | 33 | ▶️UNDERSTANGING BMP FORMAT: [engineering.purdue.edu](https://engineering.purdue.edu/ece264/17au/hw/HW15) 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CUB3D by phperrot 🕺 2 | ## PRESENTATION 3 | 4 | This project aims at reproducing a basic FPS game by using the raycasting method. 5 | 6 | ## WANNA PLAY? 💥 7 | ### Installation 8 | 9 | first, copy paste the following code on your terminal: 10 | 11 | ```shell 12 | 13 | cd ~ && cd Downloads && git clone https://github.com/phperrot/cub3d.git cub3d && cd cub3d && chmod 777 Cub3D_bonus && chmod 777 Cub3D && ./Cub3D_bonus maps/master_map.cub 14 | 15 | ``` 16 | this should open a new window: 17 | ![Image of Cub3D](https://github.com/phperrot/cub3d/blob/master/items/screen.png) 18 | 19 | ### Commands 20 | 21 | | KEY | Action | 22 | | ------------- |:-------------:| 23 | | `ESC` | exit game | 24 | | `SPACE BAR` | shoot | 25 | | `A` | move left | 26 | | `D` | move right | 27 | | `W` | move forward | 28 | | `S` | move backward | 29 | | `→` | turn right | 30 | | `←` | turn left | 31 | | `M` | hide/display map| 32 | | `O` | open/close door| 33 | | `P` | open/close secret door| 34 | | `option` | change weapon | 35 | | `F10` | sound on/off | 36 | 37 | 38 | ### Rules 39 | 40 | Collect treasures to unlock new levels ! 41 | 42 | ### Modifications 👨‍🎨 43 | You can now create your own map by following this norm: 44 | | Object | Code | 45 | | ------------- |:-------------:| 46 | | `1` | wall | 47 | | `0` | floor | 48 | | `2` | sprite #1 | 49 | | `3` | sprite #2 | 50 | | `N`/`E`/`W`/`S`| player initial position + orientation| 51 | | `A` | ammo | 52 | | `B` | bonus | 53 | | `O` | ennemy | 54 | | `T` | treasure | 55 | | `D` | door | 56 | | `P` | secret door | 57 | 58 | You can have a look at the [subject](https://github.com/phperrot/cub3d/blob/master/fr.subject.pdf) for further details on how `.cub` files work. 59 | 60 | ⚠️ Be careful ! All your paths must be valid and your map must be closed (but not necessarily a square 😉) 61 | 62 | ## WANNA CODE? 👨‍💻 63 | 64 | Some useful links can be found in the [documentation file](https://github.com/phperrot/cub3d/blob/master/DOCUMENTATION.md) 65 | 66 | -------------------------------------------------------------------------------- /ennemy/blood.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/blood.png -------------------------------------------------------------------------------- /ennemy/ennemy-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-1.png -------------------------------------------------------------------------------- /ennemy/ennemy-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-2.png -------------------------------------------------------------------------------- /ennemy/ennemy-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-3.png -------------------------------------------------------------------------------- /ennemy/ennemy-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-4.png -------------------------------------------------------------------------------- /ennemy/ennemy-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-5.png -------------------------------------------------------------------------------- /ennemy/ennemy-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-6.png -------------------------------------------------------------------------------- /ennemy/ennemy-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-7.png -------------------------------------------------------------------------------- /ennemy/ennemy-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-8.png -------------------------------------------------------------------------------- /ennemy/ennemy-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/ennemy/ennemy-9.png -------------------------------------------------------------------------------- /fr.subject.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/fr.subject.pdf -------------------------------------------------------------------------------- /items/ammo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/items/ammo.png -------------------------------------------------------------------------------- /items/ammo1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/items/ammo1.png -------------------------------------------------------------------------------- /items/game_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/items/game_over.png -------------------------------------------------------------------------------- /items/screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/items/screen.png -------------------------------------------------------------------------------- /items/treasure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/items/treasure.png -------------------------------------------------------------------------------- /items/treasure1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/items/treasure1.png -------------------------------------------------------------------------------- /items/treasure2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/items/treasure2.png -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: phperrot +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2019/11/06 11:03:14 by phperrot #+# #+# # 9 | # Updated: 2019/11/19 12:09:20 by phperrot ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | FLAG = -Wall -Wextra -Werror 14 | 15 | HEADER = libft.h 16 | 17 | NAME = libft.a 18 | 19 | SRC = ft_memset.c \ 20 | ft_bzero.c\ 21 | ft_memcpy.c\ 22 | ft_memccpy.c\ 23 | ft_memmove.c\ 24 | ft_memchr.c\ 25 | ft_memcmp.c\ 26 | ft_strlen.c\ 27 | ft_isalpha.c\ 28 | ft_isdigit.c\ 29 | ft_isalnum.c\ 30 | ft_isascii.c\ 31 | ft_isprint.c\ 32 | ft_toupper.c\ 33 | ft_tolower.c\ 34 | ft_strchr.c\ 35 | ft_strrchr.c\ 36 | ft_strncmp.c\ 37 | ft_strlcpy.c\ 38 | ft_strlcat.c\ 39 | ft_strnstr.c\ 40 | ft_atoi.c\ 41 | ft_atoi_conv.c\ 42 | ft_calloc.c\ 43 | ft_strdup.c\ 44 | ft_strdup_free.c\ 45 | ft_strdup_char.c\ 46 | ft_substr.c\ 47 | ft_strjoin.c\ 48 | ft_strjoin_free.c\ 49 | ft_strtrim.c\ 50 | ft_split.c\ 51 | ft_ctoa.c\ 52 | ft_itoa.c\ 53 | ft_itoa_long.c\ 54 | ft_strmapi.c\ 55 | ft_putchar_fd.c\ 56 | ft_putstr_fd.c\ 57 | ft_putendl_fd.c\ 58 | ft_putnbr_fd.c\ 59 | ft_putchar.c\ 60 | ft_putstr.c\ 61 | ft_putnbr.c\ 62 | ft_strreplace.c\ 63 | ft_strsub.c\ 64 | 65 | SRCSBONUS = ft_lstnew_bonus.c\ 66 | ft_lstadd_front_bonus.c\ 67 | ft_lstsize_bonus.c\ 68 | ft_lstlast_bonus.c\ 69 | ft_lstadd_back_bonus.c\ 70 | ft_lstdelone_bonus.c\ 71 | ft_lstclear_bonus.c\ 72 | ft_lstiter_bonus.c\ 73 | ft_lstmap_bonus.c 74 | 75 | OBJ = $(SRC:.c=.o) 76 | 77 | BOBJ = $(SRCSBONUS:.c=.o) 78 | 79 | all: $(NAME) 80 | 81 | $(NAME): $(OBJ) 82 | @ar rc $(NAME) $(OBJ) 83 | @echo "$(NAME) created" 84 | @ranlib $(NAME) 85 | @echo "$(NAME) indexed" 86 | 87 | bonus: $(OBJ) $(BOBJ) 88 | @ar rc $(NAME) $(OBJ) $(BOBJ) 89 | @echo "$(NAME) created" 90 | @ranlib $(NAME) 91 | @echo "$(NAME) indexed" 92 | @echo "bonus added" 93 | 94 | %.o: %.c 95 | @gcc $(FLAG) -I includes -c $< -o $@ 96 | 97 | clean: 98 | @rm -f $(OBJ) $(BOBJ) 99 | @echo "OBJ deleted" 100 | 101 | fclean: clean 102 | @rm -f $(NAME) 103 | @echo "$(NAME) deleted" 104 | 105 | re: fclean all 106 | 107 | .PHONY: all, clean, fclean, re, bonus 108 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 13:14:14 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 16:11:44 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int i; 18 | int sign; 19 | int nb; 20 | 21 | sign = 1; 22 | i = 0; 23 | nb = 0; 24 | while ((char)str[i] == '\n' | (char)str[i] == '\r' | (char)str[i] == '\t' 25 | | (char)str[i] == '\f' | (char)str[i] == '\v' | (char)str[i] == ' ') 26 | i++; 27 | if ((char)str[i] == '+' && (char)str[i + 1] != '-') 28 | i++; 29 | if ((char)str[i] == '-') 30 | { 31 | sign = -sign; 32 | i++; 33 | } 34 | while (((char)str[i] - '0') >= 0 && ('9' - (char)str[i]) >= 0) 35 | { 36 | nb = (nb * 10 + ((char)str[i] - '0')); 37 | i++; 38 | } 39 | return (nb * sign); 40 | } 41 | -------------------------------------------------------------------------------- /libft/ft_atoi_conv.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 13:14:14 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/21 14:18:52 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | long ft_atoi_conv(const char *str, char conversion_type) 16 | { 17 | int i; 18 | int sign; 19 | unsigned long nb; 20 | 21 | sign = 1; 22 | i = 0; 23 | nb = 0; 24 | while ((char)str[i] == '\n' | (char)str[i] == '\r' | (char)str[i] == '\t' 25 | | (char)str[i] == '\f' | (char)str[i] == '\v' | (char)str[i] == ' ') 26 | i++; 27 | if ((char)str[i] == '+' && (char)str[i + 1] != '-') 28 | i++; 29 | if ((char)str[i] == '-') 30 | sign = -sign; 31 | if ((char)str[i] == '-') 32 | i++; 33 | while (((char)str[i] - '0') >= 0 && ('9' - (char)str[i]) >= 0) 34 | { 35 | nb = (nb * 10 + ((char)str[i] - '0')); 36 | i++; 37 | } 38 | if (ft_strchr("puxX", conversion_type) > 0) 39 | return (nb * sign); 40 | else 41 | return ((int)(nb * sign)); 42 | } 43 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 11:36:09 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:33:57 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | int i; 18 | char *temp; 19 | 20 | i = 0; 21 | temp = s; 22 | while (n) 23 | { 24 | temp[i] = 0; 25 | i++; 26 | n--; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/07 14:44:46 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 16:12:23 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t count, size_t size) 16 | { 17 | void *output; 18 | 19 | if (!(output = malloc(size * count))) 20 | return (NULL); 21 | ft_bzero(output, size * count); 22 | return (output); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_ctoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_ctoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 17:03:40 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 17:03:43 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_ctoa(char c) 16 | { 17 | char *str; 18 | 19 | if (!(str = malloc(sizeof(char) * 2))) 20 | return (NULL); 21 | str[0] = c; 22 | str[1] = '\0'; 23 | return (str); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 14:57:01 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:50:08 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if (ft_isdigit(c) == 1 || ft_isalpha(c) == 1) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 14:16:43 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:49:08 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) 18 | return (0); 19 | else 20 | return (1); 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 15:06:08 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/13 16:29:06 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | if (c >= 0 && c <= 127) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 14:43:08 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:01:25 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | if (c >= 48 && c <= 57) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 15:10:07 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:51:13 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | if (c >= 32 && c <= 126) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 14:30:48 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/18 17:14:21 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *ft_fill_char(int n, char *s, int i) 16 | { 17 | int tmp; 18 | 19 | i--; 20 | if (n < 0) 21 | { 22 | tmp = -n; 23 | s[0] = '-'; 24 | } 25 | else 26 | tmp = n; 27 | if (tmp >= 10) 28 | { 29 | ft_fill_char((tmp / 10), s, i); 30 | s[i] = ((tmp % 10) + '0'); 31 | } 32 | else 33 | s[i] = (tmp + '0'); 34 | return (s); 35 | } 36 | 37 | static int ft_size(int n) 38 | { 39 | int size; 40 | int tmp; 41 | 42 | size = 0; 43 | if (n < 0) 44 | { 45 | tmp = -n; 46 | size++; 47 | } 48 | else 49 | tmp = n; 50 | while (tmp > 0) 51 | { 52 | tmp = tmp / 10; 53 | size++; 54 | } 55 | return (size); 56 | } 57 | 58 | char *ft_itoa(int n) 59 | { 60 | int size; 61 | char *number; 62 | 63 | if (n == -2147483648) 64 | { 65 | number = ft_strdup("-2147483648"); 66 | return (number); 67 | } 68 | if (!n) 69 | return (ft_strdup("0")); 70 | size = ft_size(n); 71 | if (n == 0) 72 | { 73 | if (!(number = (char *)malloc(sizeof(*number) * 1))) 74 | return (NULL); 75 | number[0] = '0'; 76 | } 77 | else 78 | { 79 | if (!(number = (char *)malloc(sizeof(*number) * size + 1))) 80 | return (NULL); 81 | number = ft_fill_char(n, number, size); 82 | } 83 | number[size] = '\0'; 84 | return (number); 85 | } 86 | -------------------------------------------------------------------------------- /libft/ft_itoa_long.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 14:30:48 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/21 14:24:32 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *ft_fill_char(unsigned long n, char *s, int i, int negative) 16 | { 17 | unsigned long tmp; 18 | 19 | i--; 20 | if (negative) 21 | s[0] = '-'; 22 | tmp = n; 23 | if (tmp >= 10) 24 | { 25 | ft_fill_char((tmp / 10), s, i, negative); 26 | s[i] = ((tmp % 10) + '0'); 27 | } 28 | else 29 | s[i] = (tmp + '0'); 30 | return (s); 31 | } 32 | 33 | static int ft_size(unsigned long n, int negative) 34 | { 35 | int size; 36 | unsigned long tmp; 37 | 38 | size = 0; 39 | if (negative) 40 | size++; 41 | tmp = n; 42 | while (tmp > 0) 43 | { 44 | tmp = tmp / 10; 45 | size++; 46 | } 47 | return (size); 48 | } 49 | 50 | char *ft_itoa_long(long input) 51 | { 52 | int size; 53 | char *number; 54 | int negative; 55 | unsigned long n; 56 | 57 | negative = (input < 0) ? 1 : 0; 58 | n = (input < 0) ? -input : input; 59 | if (!n) 60 | return (ft_strdup("0")); 61 | size = ft_size(n, negative); 62 | if (n == 0) 63 | { 64 | if (!(number = (char *)malloc(sizeof(*number) * 1))) 65 | return (NULL); 66 | number[0] = '0'; 67 | } 68 | else 69 | { 70 | if (!(number = (char *)malloc(sizeof(*number) * size + 1))) 71 | return (NULL); 72 | number = ft_fill_char(n, number, size, negative); 73 | } 74 | number[size] = '\0'; 75 | return (number); 76 | } 77 | -------------------------------------------------------------------------------- /libft/ft_lstadd_back_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 18:16:35 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 16:59:12 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **alst, t_list *new) 16 | { 17 | t_list *tmp; 18 | 19 | tmp = *alst; 20 | if (*alst == NULL) 21 | *alst = new; 22 | else 23 | (ft_lstlast(*alst))->next = new; 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_lstadd_front_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 18:43:50 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 16:54:05 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **alst, t_list *new) 16 | { 17 | if (new && alst) 18 | { 19 | new->next = *alst; 20 | *alst = new; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstclear_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/13 15:10:18 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 17:04:49 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void*)) 16 | { 17 | t_list *tmp; 18 | t_list *next; 19 | 20 | if (!lst || !del) 21 | return ; 22 | tmp = *lst; 23 | while (tmp) 24 | { 25 | next = tmp->next; 26 | del(tmp->content); 27 | free(tmp); 28 | tmp = next; 29 | } 30 | *lst = NULL; 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_lstdelone_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 18:31:05 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 18:19:08 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 16 | { 17 | if (lst && del) 18 | { 19 | del(lst->content); 20 | free(lst); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstiter_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 18:57:54 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/13 15:46:14 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | t_list *tmp; 18 | 19 | tmp = lst; 20 | if (!(!lst || !f)) 21 | { 22 | while (tmp) 23 | { 24 | (*f)(tmp->content); 25 | tmp = tmp->next; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_lstlast_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 19:24:05 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 16:56:10 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | t_list *tmp; 18 | 19 | if (!lst) 20 | return (NULL); 21 | tmp = lst; 22 | while (tmp->next) 23 | tmp = tmp->next; 24 | return (tmp); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_lstmap_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/13 15:23:03 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 18:14:38 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 16 | { 17 | t_list *tmp; 18 | t_list *new; 19 | 20 | if (!lst || !f || !del) 21 | return (NULL); 22 | if (!(tmp = ft_lstnew(f(lst->content)))) 23 | return (NULL); 24 | lst = lst->next; 25 | while (lst) 26 | { 27 | new = ft_lstnew(f(lst->content)); 28 | if (!new) 29 | { 30 | ft_lstclear(&tmp, del); 31 | return (NULL); 32 | } 33 | ft_lstadd_back(&tmp, new); 34 | lst = lst->next; 35 | } 36 | return (tmp); 37 | } 38 | -------------------------------------------------------------------------------- /libft/ft_lstnew_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 18:09:37 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/06 15:49:26 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void *content) 16 | { 17 | t_list *elem; 18 | 19 | if (!(elem = malloc(sizeof(t_list)))) 20 | return (NULL); 21 | elem->content = content; 22 | elem->next = NULL; 23 | return (elem); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_lstsize_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 19:10:13 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 16:58:17 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | int i; 18 | t_list *tmp; 19 | 20 | i = 0; 21 | tmp = lst; 22 | while (tmp) 23 | { 24 | tmp = tmp->next; 25 | i++; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 17:43:58 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:41:16 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *point_dst; 19 | unsigned char *point_src; 20 | 21 | if (!dst && !src) 22 | return (NULL); 23 | i = 0; 24 | point_dst = (unsigned char*)dst; 25 | point_src = (unsigned char*)src; 26 | while (i < n) 27 | { 28 | point_dst[i] = point_src[i]; 29 | if (point_src[i] == (unsigned char)c) 30 | return ((void *)(dst + i + 1)); 31 | i++; 32 | } 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 10:13:14 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:43:34 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *tmp; 19 | 20 | tmp = (unsigned char*)s; 21 | i = 0; 22 | while (i < n) 23 | { 24 | if (tmp[i] == (unsigned char)c) 25 | return ((void *)(s + i)); 26 | i++; 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 10:26:01 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:46:32 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | unsigned char *tmp1; 18 | unsigned char *tmp2; 19 | 20 | tmp1 = (unsigned char*)s1; 21 | tmp2 = (unsigned char*)s2; 22 | while (n) 23 | { 24 | if (*tmp1 != *tmp2) 25 | return (*tmp1 - *tmp2); 26 | tmp1++; 27 | tmp2++; 28 | n--; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 18:16:01 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:38:56 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, void *src, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *tmp1; 19 | unsigned char *tmp2; 20 | 21 | i = 0; 22 | if (!src && !dst) 23 | return (NULL); 24 | tmp1 = (unsigned char*)dst; 25 | tmp2 = (unsigned char*)src; 26 | while (i < n) 27 | { 28 | tmp1[i] = tmp2[i]; 29 | i++; 30 | } 31 | return (dst); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/07 13:54:49 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:42:49 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | unsigned char *tmp1; 18 | unsigned char *tmp2; 19 | size_t i; 20 | 21 | if (len == 0) 22 | return (dst); 23 | if ((src == NULL && dst == NULL)) 24 | return (NULL); 25 | i = 0; 26 | len = len - 1; 27 | tmp1 = (unsigned char *)src; 28 | tmp2 = (unsigned char *)dst; 29 | if (tmp1 < tmp2) 30 | while ((int)(len) >= 0) 31 | { 32 | *(tmp2 + len) = *(tmp1 + len); 33 | len--; 34 | } 35 | else 36 | while (i <= len) 37 | { 38 | *(tmp2 + i) = *(tmp1 + i); 39 | i++; 40 | } 41 | return (dst); 42 | } 43 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 12:29:01 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:09:51 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | size_t i; 18 | unsigned char *tmp; 19 | 20 | i = 0; 21 | tmp = (unsigned char*)b; 22 | while (i < len) 23 | { 24 | tmp[i] = (unsigned char)c; 25 | i++; 26 | } 27 | return (b); 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/27 16:06:29 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:06:46 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar(char c) 16 | { 17 | write(1, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 15:08:40 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/06 19:14:33 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 14:35:24 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/19 11:52:33 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | ft_putstr_fd(s, fd); 18 | ft_putchar_fd('\n', fd); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/27 16:06:02 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:06:18 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr(int n) 16 | { 17 | if (n == -2147483648) 18 | { 19 | ft_putstr("-2147483648"); 20 | } 21 | else 22 | { 23 | if (n < 0) 24 | { 25 | ft_putchar('-'); 26 | n = n * -1; 27 | } 28 | if (n >= 10) 29 | ft_putnbr(n / 10); 30 | ft_putchar(n % 10 + '0'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 14:49:23 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/18 17:09:59 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (n == -2147483648) 18 | { 19 | ft_putstr_fd("-2147483648", fd); 20 | } 21 | else 22 | { 23 | if (n < 0) 24 | { 25 | ft_putchar_fd('-', fd); 26 | n = n * -1; 27 | } 28 | if (n >= 10) 29 | ft_putnbr_fd(n / 10, fd); 30 | ft_putchar_fd(n % 10 + '0', fd); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/27 16:06:52 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/03 10:35:42 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr(char *s) 16 | { 17 | if (s) 18 | write(1, s, ft_strlen(s)); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 14:31:05 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/18 17:05:34 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char *s, int fd) 16 | { 17 | if (s) 18 | write(fd, s, ft_strlen(s)); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/07 09:52:18 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/18 17:13:43 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_nb_occur(const char *s, char c) 16 | { 17 | int i; 18 | int nb; 19 | int is_char; 20 | int is_string; 21 | 22 | i = 0; 23 | nb = 0; 24 | is_char = 1; 25 | is_string = 0; 26 | while (s[i]) 27 | { 28 | if (s[i] != c && !is_string) 29 | { 30 | is_string = 1; 31 | is_char = 0; 32 | nb++; 33 | } 34 | if (s[i] == c && !is_char) 35 | { 36 | is_string = 0; 37 | is_char = 1; 38 | } 39 | i++; 40 | } 41 | return (nb); 42 | } 43 | 44 | static int get_start(int i, const char *s, char c, int tab[]) 45 | { 46 | int is_string; 47 | int start; 48 | 49 | start = 0; 50 | is_string = 0; 51 | while (s[i] != '\0') 52 | { 53 | if (s[i] != '\0' && s[i] != c && !is_string) 54 | { 55 | is_string = 1; 56 | start = i; 57 | while (s[i] != c && s[i] != '\0') 58 | i++; 59 | tab[0] = i - start; 60 | break ; 61 | } 62 | i++; 63 | } 64 | return (start); 65 | } 66 | 67 | static char **ft_check(char const *s, char c) 68 | { 69 | int check; 70 | char **tab_str; 71 | 72 | check = 0; 73 | if (!(tab_str = (char**)malloc(sizeof(char *) * 1))) 74 | return (NULL); 75 | if (!s && !c) 76 | { 77 | tab_str[0] = ft_strdup(""); 78 | return (tab_str); 79 | } 80 | while (s[check++] == c) 81 | { 82 | if (s[check] == '\0') 83 | { 84 | tab_str[0] = NULL; 85 | return (tab_str); 86 | } 87 | } 88 | return (0); 89 | } 90 | 91 | static char **ft_loop(char const *s, char c, int *tab, char **tab_str) 92 | { 93 | int i; 94 | int j; 95 | int k; 96 | 97 | i = 0; 98 | j = 0; 99 | while (i < ft_nb_occur(s, c)) 100 | { 101 | k = 0; 102 | j = get_start(j, s, c, tab); 103 | if (!(tab_str[i] = (char*)malloc(sizeof(char) * (tab[0] + 1)))) 104 | return (NULL); 105 | while (k < tab[0]) 106 | { 107 | tab_str[i][k] = s[j]; 108 | k++; 109 | j++; 110 | } 111 | tab_str[i][k] = '\0'; 112 | i++; 113 | } 114 | tab_str[i] = 0; 115 | return (tab_str); 116 | } 117 | 118 | char **ft_split(char const *s, char c) 119 | { 120 | int tab[1]; 121 | char **tab_str; 122 | 123 | if (!s) 124 | return (NULL); 125 | if (ft_check(s, c) != 0) 126 | return (ft_check(s, c)); 127 | if (ft_strncmp(s, "", 1) == 0) 128 | { 129 | if (!(tab_str = (char**)malloc(sizeof(char *) * 1))) 130 | return (NULL); 131 | tab_str[0] = NULL; 132 | return (tab_str); 133 | } 134 | if (!(tab_str = (char**)malloc(sizeof(char *) * (ft_nb_occur(s, c) + 1)))) 135 | return (NULL); 136 | return (ft_loop(s, c, tab, tab_str)); 137 | } 138 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 13:29:22 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:01:45 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | { 22 | if (s[i] == (char)c) 23 | return ((char *)&s[i]); 24 | i++; 25 | } 26 | if (s[i] == (char)c) 27 | return ((char *)&s[i]); 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 10:49:29 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:48:52 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | char *output; 18 | int i; 19 | int size; 20 | 21 | if (!s1) 22 | { 23 | return (NULL); 24 | } 25 | size = ft_strlen(s1); 26 | if (!(output = malloc(sizeof(char) * (size + 1)))) 27 | return (0); 28 | if (output == NULL) 29 | return (NULL); 30 | i = 0; 31 | while ((char)s1[i]) 32 | { 33 | output[i] = s1[i]; 34 | i++; 35 | } 36 | output[i] = '\0'; 37 | return (output); 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_strdup_char.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup_char.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 17:03:57 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 17:04:00 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup_char(char c) 16 | { 17 | char *output; 18 | 19 | if (!(output = malloc(sizeof(char) * 2))) 20 | return (NULL); 21 | output[0] = c; 22 | output[1] = '\0'; 23 | return (output); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strdup_free.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup_free.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/12/21 12:30:46 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/21 14:19:11 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup_free(char *to_free, const char *s1) 16 | { 17 | char *output; 18 | int i; 19 | int size; 20 | 21 | if (!s1) 22 | { 23 | return (NULL); 24 | } 25 | size = ft_strlen(s1); 26 | if (!(output = malloc(sizeof(char) * (size + 1)))) 27 | return (0); 28 | if (output == NULL) 29 | return (NULL); 30 | i = 0; 31 | while ((char)s1[i]) 32 | { 33 | output[i] = s1[i]; 34 | i++; 35 | } 36 | output[i] = '\0'; 37 | free(to_free); 38 | return (output); 39 | } 40 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 15:37:06 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:02:06 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | size_t len; 18 | char *out; 19 | 20 | len = ft_strlen((char *)s1) + ft_strlen((char *)s2) + 1; 21 | if (!(out = malloc(sizeof(char) * len))) 22 | return (NULL); 23 | ft_strlcpy(out, s1, ft_strlen(s1) + 1); 24 | ft_strlcat(out, s2, len); 25 | return (out); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strjoin_free.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin_free.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 17:04:12 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 17:04:15 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin_free(char const *s1, char const *s2, int param) 16 | { 17 | size_t len; 18 | char *out; 19 | 20 | len = ft_strlen((char *)s1) + ft_strlen((char *)s2) + 1; 21 | if (!(out = malloc(sizeof(char) * len))) 22 | return (NULL); 23 | ft_strlcpy(out, s1, ft_strlen((char*)s1) + 1); 24 | ft_strlcat(out, s2, len); 25 | if (param == 1 || param == 3) 26 | { 27 | free((char*)s1); 28 | s1 = NULL; 29 | } 30 | if (param == 2 || param == 3) 31 | { 32 | free((char *)s2); 33 | s2 = NULL; 34 | } 35 | return (out); 36 | } 37 | -------------------------------------------------------------------------------- /libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 09:42:45 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:02:16 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t i; 18 | size_t len; 19 | 20 | i = 0; 21 | while (dst[i] && i < dstsize) 22 | i++; 23 | len = i; 24 | if (i == dstsize) 25 | return (len + ft_strlen(src)); 26 | while (src[i - len] && i < dstsize - 1) 27 | { 28 | dst[i] = src[i - len]; 29 | i++; 30 | } 31 | if (len < dstsize) 32 | dst[i] = '\0'; 33 | return (len + ft_strlen(src)); 34 | } 35 | -------------------------------------------------------------------------------- /libft/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 16:28:52 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:02:25 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) 16 | { 17 | unsigned int count; 18 | unsigned int i; 19 | 20 | if (!src) 21 | return (0); 22 | if (dstsize == 0) 23 | return (ft_strlen(src)); 24 | count = 0; 25 | while (src[count] != '\0') 26 | count++; 27 | i = 0; 28 | while (src[i] != '\0' && i < (dstsize - 1)) 29 | { 30 | dst[i] = src[i]; 31 | i++; 32 | } 33 | dst[i] = '\0'; 34 | return (count); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 14:31:28 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/27 16:08:53 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | int i; 18 | 19 | if (!s) 20 | return (0); 21 | i = 0; 22 | while (s[i] != '\0') 23 | i++; 24 | return (i); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/07 13:37:58 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/18 17:04:46 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | unsigned int i; 18 | char *output; 19 | 20 | if (!s || !f) 21 | return (NULL); 22 | if (!(output = (char *)malloc(sizeof(*output) * ft_strlen(s) + 1))) 23 | return (NULL); 24 | i = 0; 25 | while (i < ft_strlen(s)) 26 | { 27 | output[i] = f(i, s[i]); 28 | i++; 29 | } 30 | output[i] = '\0'; 31 | return (output); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/12/21 14:19:28 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/21 14:19:54 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | 19 | if (n == 0) 20 | return (0); 21 | i = 0; 22 | while (s1[i] == s2[i] && (s1[i] != '\0' && s2[i] != '\0') && i < n - 1) 23 | { 24 | i++; 25 | } 26 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 16:43:57 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 16:10:44 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | char *small; 18 | char *large; 19 | size_t i; 20 | 21 | small = (char*)needle; 22 | large = (char*)haystack; 23 | if (*small == '\0') 24 | return (large); 25 | i = ft_strlen(small); 26 | while (*large && len-- >= i) 27 | { 28 | if (ft_memcmp(large, small, i) == 0) 29 | return (large); 30 | large++; 31 | } 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/06 13:33:56 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:57:41 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | char *output; 19 | 20 | i = 0; 21 | output = NULL; 22 | while (s[i]) 23 | { 24 | if (s[i] == (char)c) 25 | output = (char *)&s[i]; 26 | i++; 27 | } 28 | if (s[i] == (char)c) 29 | return ((char *)&s[i]); 30 | return (output); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_strreplace.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strreplace.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 17:04:30 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 17:04:33 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strreplace(char *str, char old, char new) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (str[i] != '\0') 21 | { 22 | if (str[i] == old) 23 | str[i] = new; 24 | i++; 25 | } 26 | return (str); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_strsub.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsub.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 17:04:42 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 17:04:51 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strsub(char *str, int start, int len) 16 | { 17 | int i; 18 | char *output; 19 | 20 | i = 0; 21 | if (!str) 22 | return (NULL); 23 | if (!(output = malloc(sizeof(char) * (len + 1)))) 24 | return (NULL); 25 | while (i < len && (str[start + i] != '\n')) 26 | { 27 | output[i] = str[start + i]; 28 | i++; 29 | } 30 | output[i] = '\0'; 31 | return (output); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 15:26:11 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/21 14:25:53 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_head_size(char *s1, char *set) 16 | { 17 | int i; 18 | int j; 19 | int set_size; 20 | 21 | i = 0; 22 | j = 0; 23 | set_size = 0; 24 | while (set[set_size] != '\0') 25 | set_size++; 26 | while (s1[i] != '\0') 27 | { 28 | while (s1[i] != set[j] && set[j] != '\0') 29 | j++; 30 | if ((j == set_size) | (s1[i] == '\0')) 31 | return (i); 32 | else 33 | { 34 | i++; 35 | j = 0; 36 | } 37 | } 38 | return (0); 39 | } 40 | 41 | int ft_tail_size(char const *s1, char const *set) 42 | { 43 | int i; 44 | int j; 45 | 46 | i = (int)ft_strlen(s1) - 1; 47 | j = 0; 48 | while (i >= 0) 49 | { 50 | while (s1[i] != set[j] && set[j] != '\0') 51 | { 52 | j++; 53 | } 54 | if (j == (int)(ft_strlen(set)) | i == -1) 55 | { 56 | return (ft_strlen(s1) - (i + 1)); 57 | } 58 | else 59 | { 60 | i--; 61 | j = 0; 62 | } 63 | } 64 | return (ft_strlen(s1)); 65 | } 66 | 67 | char *ft_empty_input(char const *s1, char const *set) 68 | { 69 | if ((s1[0] == '\0') || (set[0] == '\0')) 70 | return ((char *)s1); 71 | else 72 | return (NULL); 73 | } 74 | 75 | char *ft_strtrim(char const *s1, char const *set) 76 | { 77 | int malloc_size; 78 | int i; 79 | char *output; 80 | 81 | if (!s1 || !set) 82 | return (NULL); 83 | if ((ft_empty_input(s1, set)) != NULL) 84 | return (ft_empty_input(s1, set)); 85 | malloc_size = ft_strlen((char*)s1) - \ 86 | ft_head_size((char*)s1, (char*)set) - ft_tail_size(s1, set); 87 | if (malloc_size > 0) 88 | { 89 | if (!(output = malloc(sizeof(char) * (malloc_size + 1)))) 90 | return (0); 91 | } 92 | else 93 | return (ft_strdup("")); 94 | i = 0; 95 | while (i < malloc_size) 96 | { 97 | output[i] = s1[ft_head_size((char*)s1, (char*)set) + i]; 98 | i++; 99 | } 100 | output[i] = '\0'; 101 | return (output); 102 | } 103 | -------------------------------------------------------------------------------- /libft/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/05 17:24:17 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/19 11:46:30 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_substr(char const *s, unsigned int start, size_t len) 16 | { 17 | int i; 18 | char *output; 19 | 20 | if (!s) 21 | return (NULL); 22 | i = 0; 23 | if ((size_t)start > ft_strlen(s)) 24 | return (ft_strdup("")); 25 | output = (char *)malloc(sizeof(*output) * len + 1); 26 | if (output == NULL) 27 | return (NULL); 28 | while (len) 29 | { 30 | output[i] = s[start]; 31 | start++; 32 | i++; 33 | len--; 34 | } 35 | output[i] = '\0'; 36 | return (output); 37 | } 38 | -------------------------------------------------------------------------------- /libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 15:30:55 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:52:33 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (c >= 'A' && c <= 'Z') 18 | return (c + 32); 19 | else 20 | return (c); 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/04 15:22:55 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/14 15:51:45 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (c >= 'a' && c <= 'z') 18 | return (c - 32); 19 | else 20 | return (c); 21 | } 22 | -------------------------------------------------------------------------------- /libft/libft.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/libft/libft.a -------------------------------------------------------------------------------- /maps/master_map.cub: -------------------------------------------------------------------------------- 1 | R 500 500 2 | NO ./textures/NO.xpm 3 | SO ./textures/SO.xpm 4 | WE ./textures/WE.xpm 5 | EA ./textures/EA.xpm 6 | S ./items/treasure.xpm 7 | S1 ./textures/barrel.xpm 8 | D ./textures/door.xpm 9 | B ./textures/bonus.xpm 10 | A ./items/ammo.xpm 11 | O ./ennemy/ennemy-1.xpm 12 | T ./items/treasure.xpm 13 | F 182,182,182 14 | C 182,182,182 15 | 16 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 17 | 1 A 1 B 1 0 1 0 1 T 1 T 1 T 1 T 1 0 1 0 1 0 1 0 1 B 1 A 1 18 | 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 19 | 1 O 1 O 1 0 1 0 D 0 1 0 1 0 1 0 D 0 1 0 1 0 1 0 1 O 1 O 1 20 | 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 21 | 1 0 1 0 1 0 1 0 P 0 0 O O O 0 0 P 0 1 0 1 0 1 0 1 0 1 0 1 22 | 1 0 1 0 1 0 1 0 1 0 0 0 3 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 23 | 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 24 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 25 | 1 0 0 0 0 0 0 0 1 0 0 0 N 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 26 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 27 | 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 28 | 1 0 1 0 1 0 1 0 1 0 0 0 3 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 29 | 1 0 1 0 1 0 1 0 P 0 0 O O O 0 0 P 0 1 0 1 0 1 0 1 0 1 0 1 30 | 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 31 | 1 O 1 O 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 O 1 O 1 32 | 1 0 0 0 1 0 0 0 D 0 0 0 1 0 0 0 D 0 0 0 1 0 0 0 1 0 0 0 1 33 | 1 A 1 B 1 0 1 0 1 T 1 T 1 T 1 T 1 0 1 0 1 0 1 0 1 B 1 A 1 34 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 35 | -------------------------------------------------------------------------------- /maps/test.cub: -------------------------------------------------------------------------------- 1 | R 8000 7500 2 | NO textures/NO.xpm 3 | SO ./textures/SO.xpm 4 | WE ./textures/WE.xpm 5 | EA ./textures/EA.xpm 6 | S ./items/treasure.xpm 7 | F 182,182,182 8 | C 182,182,182 9 | 10 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 | 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 12 | 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 13 | 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 14 | 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 15 | 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 16 | 1 0 1 0 1 0 1 0 1 0 0 0 2 2 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 17 | 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 18 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 19 | 1 0 0 0 0 0 0 0 1 0 0 0 N 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 20 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 21 | 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 22 | 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 23 | 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 24 | 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 25 | 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 26 | 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 27 | 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 28 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 29 | -------------------------------------------------------------------------------- /maps/test4.cub: -------------------------------------------------------------------------------- 1 | R 530 600 2 | NO ./textures/NO.xpm 3 | SO ./textures/SO.xpm 4 | WE ./textures/WE.xpm 5 | EA ./textures/bricks.xpm 6 | S ./textures/bricks.xpm 7 | F 220,100,2 8 | C 225,30,0 9 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 11 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 12 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 13 | 1 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 1 14 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 15 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 16 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 2 0 0 0 0 0 0 0 0 0 1 17 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 18 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 19 | 1 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 1 20 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 21 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 22 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 23 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 24 | 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 25 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 26 | -------------------------------------------------------------------------------- /maps/test5.cub: -------------------------------------------------------------------------------- 1 | R 8000 4000 2 | NO ./textures/NO.xpm 3 | SO ./textures/SO.xpm 4 | WE ./textures/WE.xpm 5 | EA ./textures/EA.xpm 6 | S ./items/treasure.xpm 7 | F 182, 192, 182 8 | C 192, 182, 182 9 | 10 | 1 11 | 1 1 12 | 1 0 1 13 | 1 0 0 1 14 | 1 0 0 0 1 15 | 1 0 0 0 1 16 | 1 N 0 0 1 17 | 1 0 0 0 0 1 18 | 1 0 0 0 0 1 1 1 1 1 19 | 1 0 0 0 0 0 0 0 0 1 20 | 1 0 0 0 2 0 0 1 1 21 | 1 1 1 1 1 1 1 1 22 | -------------------------------------------------------------------------------- /maps/test6.cub: -------------------------------------------------------------------------------- 1 | R 1000 750 2 | NO ./textures/NO.xpm 3 | SO ./textures/SO.xpm 4 | WE ./textures/WE.xpm 5 | EA ./textures/EA.xpm 6 | S ./items/treasure.xpm 7 | S1 ./textures/barrel.xpm 8 | D ./textures/door.xpm 9 | B ./textures/bonus.xpm 10 | A ./items/ammo1.xpm 11 | O ./ennemy/ennemy-1.xpm 12 | T ./items/treasure.xpm 13 | F 182,182,182 14 | C 182,182,182 15 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 16 | 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 17 | 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 18 | 1 1 1 1 0 1 1 1 1 D 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 19 | 1 1 1 1 0 1 1 1 1 D 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1 20 | 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 21 | 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 22 | 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 23 | 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 24 | 1 1 0 0 0 0 0 0 0 T E A B 0 0 0 0 0 0 0 0 O O O O O O O 1 25 | 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 26 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 27 | -------------------------------------------------------------------------------- /maps/test7.cub: -------------------------------------------------------------------------------- 1 | R 1000 750 2 | NO ./textures/NO.xpm 3 | SO ./textures/SO.xpm 4 | WE ./textures/WE.xpm 5 | EA ./textures/EA.xpm 6 | S ./items/treasure.xpm 7 | S1 ./textures/barrel.xpm 8 | D ./textures/door.xpm 9 | B ./textures/bonus.xpm 10 | A ./items/ammo.xpm 11 | O ./ennemy/ennemy-1.xpm 12 | T ./items/treasure.xpm 13 | F 182,182,182 14 | C 182,182,182 15 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 16 | 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 17 | 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 18 | 1 1 1 1 0 1 1 1 1 D 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 19 | 1 1 1 1 0 1 1 1 1 D 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1 20 | 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 21 | 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 22 | 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 23 | 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 24 | 1 1 0 0 0 0 0 0 0 T E A B 0 0 0 0 0 0 0 0 O O O O O O O 1 25 | 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 26 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 27 | -------------------------------------------------------------------------------- /sound/collision.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/sound/collision.mp3 -------------------------------------------------------------------------------- /sound/gun_change.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/sound/gun_change.mp3 -------------------------------------------------------------------------------- /sound/gun_shot.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/sound/gun_shot.mp3 -------------------------------------------------------------------------------- /sound/music.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/sound/music.mp3 -------------------------------------------------------------------------------- /srcs/bonus/cub3d_utils2_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cub3d_utils2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:26:33 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 14:29:55 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | t_map *ft_lstnew_map(void *content) 16 | { 17 | t_map *elem; 18 | 19 | if (!(elem = malloc(sizeof(t_list)))) 20 | return (NULL); 21 | elem->line = content; 22 | elem->next = NULL; 23 | return (elem); 24 | } 25 | 26 | void ft_lstadd_back_map(t_map **alst, t_map *new) 27 | { 28 | t_map *tmp; 29 | 30 | tmp = *alst; 31 | if (*alst == NULL) 32 | *alst = new; 33 | else 34 | (ft_lstlast_map(*alst))->next = new; 35 | } 36 | 37 | void ft_lstclear_map(t_map **lst) 38 | { 39 | t_map *tmp; 40 | t_map *next; 41 | 42 | if (!lst) 43 | return ; 44 | tmp = *lst; 45 | while (tmp) 46 | { 47 | next = tmp->next; 48 | free(tmp); 49 | tmp = next; 50 | } 51 | *lst = NULL; 52 | } 53 | 54 | int ft_lstsize_map(t_map *lst) 55 | { 56 | t_map *tmp; 57 | int i; 58 | 59 | i = 0; 60 | if (!lst) 61 | return (i); 62 | tmp = lst; 63 | while (tmp) 64 | { 65 | tmp = tmp->next; 66 | i++; 67 | } 68 | return (i); 69 | } 70 | 71 | char **from_lst_to_tab(t_map *lst) 72 | { 73 | t_map *tmp; 74 | int i; 75 | int j; 76 | char **tab; 77 | 78 | tmp = lst; 79 | if (!(tab = malloc(sizeof(char*) * (ft_lstsize_map(lst) + 1)))) 80 | return (NULL); 81 | i = 0; 82 | while (i < ft_lstsize_map(lst)) 83 | { 84 | if (!(tab[i] = malloc(sizeof(char) * (ft_strlen(tmp->line) + 1)))) 85 | return (NULL); 86 | j = 0; 87 | while (tmp->line[j] != '\0') 88 | { 89 | tab[i][j] = tmp->line[j]; 90 | j++; 91 | } 92 | tab[i][j] = '\0'; 93 | tmp = tmp->next; 94 | i++; 95 | } 96 | return (tab); 97 | } 98 | -------------------------------------------------------------------------------- /srcs/bonus/cub3d_utils_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cub3d_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:05:58 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 14:28:50 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | double ft_abs(double x) 16 | { 17 | if (x < 0) 18 | x = -x; 19 | return (x); 20 | } 21 | 22 | int count_char(char *str, char c) 23 | { 24 | int i; 25 | int count; 26 | 27 | i = 0; 28 | count = 0; 29 | while (str[i] != '\0') 30 | { 31 | if (str[i] == c) 32 | count++; 33 | i++; 34 | } 35 | return (count); 36 | } 37 | 38 | char *withdraw_char(char *str, char c) 39 | { 40 | int i; 41 | int j; 42 | int new_len; 43 | int old_len; 44 | char *new_str; 45 | 46 | old_len = ft_strlen(str); 47 | new_len = old_len - count_char(str, c); 48 | if (!(new_str = malloc(sizeof(char) * (new_len + 1)))) 49 | return (NULL); 50 | i = 0; 51 | j = 0; 52 | while (i < old_len) 53 | { 54 | if (str[i] != c) 55 | { 56 | new_str[j] = str[i]; 57 | j++; 58 | } 59 | i++; 60 | } 61 | new_str[j] = '\0'; 62 | return (new_str); 63 | } 64 | 65 | t_map *ft_lstlast_map(t_map *lst) 66 | { 67 | t_map *tmp; 68 | 69 | if (!lst) 70 | return (NULL); 71 | tmp = lst; 72 | while (tmp->next) 73 | tmp = tmp->next; 74 | return (tmp); 75 | } 76 | -------------------------------------------------------------------------------- /srcs/bonus/display_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* display.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:34:45 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:46:48 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int ft_put_pixel(t_img *img, unsigned int color, int p_x, int p_y) 16 | { 17 | if (p_y >= img->height || p_x >= img->width || p_x < 0\ 18 | || p_y < 0 || ((p_y * img->width + p_x) >= (img->width * img->height - 1))) 19 | return (PIXEL_ERROR); 20 | img->img_data[p_y * img->width + p_x] = color; 21 | return (SUCCESS); 22 | } 23 | 24 | char *ft_disp_col(t_env *env, int x, char tex, int y) 25 | { 26 | char *tex_path; 27 | int height; 28 | 29 | if (tex == 'E') 30 | height = env->tex_e->height; 31 | if (tex == 'N') 32 | height = env->tex_n->height; 33 | if (tex == 'W') 34 | height = env->tex_w->height; 35 | if (tex == 'S') 36 | height = env->tex_s->height; 37 | while (y < env->ray.wstart) 38 | ft_put_pixel(env->img, env->ceil, x, y++); 39 | while (y >= env->ray.wstart && y <= env->ray.wend) 40 | { 41 | env->ray.tex_y = (int)env->ray.tex_pos & (height - 1); 42 | env->ray.tex_pos += env->ray.step_tex; 43 | tex_path = pix_color(env, 0); 44 | ft_put_pixel(env->img, env->color, x, y); 45 | y++; 46 | } 47 | while (y < env->height) 48 | ft_put_pixel(env->img, env->floor, x, y++); 49 | return (tex_path); 50 | } 51 | 52 | int ft_disp_screen(t_env *env, int x) 53 | { 54 | char tex; 55 | 56 | while (x < env->width) 57 | { 58 | ft_init_ray(env, x); 59 | ft_direction_ray(env); 60 | tex = ft_hit_ray(env, 0); 61 | ft_size_ray(env); 62 | ft_wall_tex(env, tex); 63 | ft_disp_col(env, x++, tex, 0); 64 | env->ray.zbuffer[x] = env->ray.dist; 65 | } 66 | init_sprite(env); 67 | init_minimap(*env); 68 | ft_disp_items(env); 69 | env->weapon_shot = 0; 70 | if (env->save_flag == 1) 71 | { 72 | if (launch_save(env) != SUCCESS) 73 | ft_putstr("Error\nError while writing save file"); 74 | ft_exit(env); 75 | } 76 | return (SUCCESS); 77 | } 78 | -------------------------------------------------------------------------------- /srcs/bonus/error2_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* error2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:58:00 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 14:58:10 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int ft_error_tex_inputs(int error, char *orientation) 16 | { 17 | error = ft_strlen(orientation); 18 | free(orientation); 19 | ft_putstr("Error\nWrong texture path"); 20 | return (ERROR); 21 | } 22 | 23 | void ft_error_arg2(int error) 24 | { 25 | if (error == MAP_ERROR_NOT_CLOSED) 26 | ft_putstr("Error\nMap not closed"); 27 | if (error == MAP_ERROR_SAME_SIZE) 28 | ft_putstr("Error\nAll lines of the map are not the same size"); 29 | } 30 | 31 | int ft_error_arg(int error) 32 | { 33 | if (error == NB_ARG_ERROR_TOO_FEW) 34 | ft_putstr("Error\nNot enough arguments"); 35 | if (error == NB_ARG_ERROR_TOO_MANY) 36 | ft_putstr("Error\nToo many arguments"); 37 | if (error == FILE_EXT_ERROR) 38 | ft_putstr("Error\nFirst argument is not a .cub file"); 39 | if (error == SAVE_FLAG_ERROR) 40 | ft_putstr("Error\nNot a valid flag"); 41 | if (error == FILE_READ_ERROR) 42 | ft_putstr("Error\nCould not complete file reading"); 43 | if (error == SCREEN_ERROR) 44 | ft_putstr("Error\nWrong screen dimensions"); 45 | if (error == PATH_ERROR) 46 | ft_putstr("Error\nWrong texture path"); 47 | if (error == RGB_ERROR) 48 | ft_putstr("Error\nProblems with floor or ceil"); 49 | if (error == MAP_ERROR_WRONG_CHAR) 50 | ft_putstr("Error\nInvalid character in the map"); 51 | if (error == MAP_ERROR_MANY_POS) 52 | ft_putstr("Error\nSeveral positions entered in the map"); 53 | if (error == MAP_ERROR_NO_POS) 54 | ft_putstr("Error\nNo position was entered in the map"); 55 | ft_error_arg2(error); 56 | return (ERROR); 57 | } 58 | -------------------------------------------------------------------------------- /srcs/bonus/exit_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* exit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:01:59 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:43:02 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | void ft_free_ennemy(t_env *env) 16 | { 17 | ft_free_tex(env, env->tex_ennemy_1); 18 | ft_free_tex(env, env->tex_ennemy_2); 19 | ft_free_tex(env, env->tex_ennemy_3); 20 | ft_free_tex(env, env->tex_ennemy_4); 21 | ft_free_tex(env, env->tex_ennemy_5); 22 | ft_free_tex(env, env->tex_ennemy_6); 23 | ft_free_tex(env, env->tex_ennemy_7); 24 | ft_free_tex(env, env->tex_ennemy_8); 25 | ft_free_tex(env, env->tex_ennemy_9); 26 | ft_free_tex(env, env->tex_ennemy); 27 | } 28 | 29 | int ft_exit(t_env *env) 30 | { 31 | if (!env->error) 32 | { 33 | ft_free_img(env, env->img); 34 | ft_free_tex(env, env->tex_s); 35 | ft_free_tex(env, env->tex_n); 36 | ft_free_tex(env, env->tex_w); 37 | ft_free_tex(env, env->tex_e); 38 | ft_free_tex(env, env->tex_sprite); 39 | ft_free_tex(env, env->tex_sprite1); 40 | ft_free_tex(env, env->tex_door); 41 | ft_free_tex(env, env->tex_bonus); 42 | ft_free_ennemy(env); 43 | ft_free_tex(env, env->tex_ammo); 44 | ft_free_tex(env, env->tex_treasure); 45 | ft_free_img(env, env->sound_icon); 46 | ft_free_img(env, env->bullet); 47 | ft_free_img(env, env->lives_icon); 48 | ft_free_img(env, env->target); 49 | ft_free_img(env, env->weapon_img); 50 | mlx_destroy_window(env->mlx_ptr, env->win_ptr); 51 | } 52 | exit(0); 53 | return (SUCCESS); 54 | } 55 | -------------------------------------------------------------------------------- /srcs/bonus/free_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* free.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:05:18 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:05:37 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | void ft_free_tex(t_env *env, t_tex *tex) 16 | { 17 | if (tex) 18 | { 19 | mlx_destroy_image(env->mlx_ptr, tex->tex_ptr); 20 | free(tex); 21 | tex = NULL; 22 | } 23 | } 24 | 25 | void ft_free_img(t_env *env, t_img *img) 26 | { 27 | if (img) 28 | { 29 | mlx_destroy_image(env->mlx_ptr, img->img_ptr); 30 | free(img); 31 | img = NULL; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /srcs/bonus/init2_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:09:40 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:58:26 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int from_rgb_to_hex(t_rgb color) 16 | { 17 | int output; 18 | 19 | output = 256 * 256 * color.r + 256 * color.g + color.b; 20 | return (output); 21 | } 22 | 23 | void get_arg_for_env(t_env *env, t_arg arg, int y, int x) 24 | { 25 | t_map *tmp; 26 | 27 | tmp = arg.map; 28 | while (tmp) 29 | { 30 | x = 0; 31 | while (tmp->line[x] != '\0') 32 | { 33 | if (ft_strchr("NSWE", tmp->line[x])) 34 | { 35 | env->orientation = tmp->line[x]; 36 | env->player_x = x++; 37 | env->player_y = y++; 38 | } 39 | if (ft_strchr("23DBAOT", tmp->line[x])) 40 | env->sprite_count++; 41 | if (tmp->line[x] == 'O') 42 | env->ennemy_count++; 43 | x++; 44 | } 45 | env->map_width = x; 46 | tmp = tmp->next; 47 | y++; 48 | } 49 | env->map_height = y - 1; 50 | } 51 | 52 | t_img *ft_new_img(t_env *env, char *file) 53 | { 54 | t_img *img; 55 | 56 | if (!(img = malloc(sizeof(t_img)))) 57 | { 58 | return (NULL); 59 | } 60 | if (!(img->img_ptr = mlx_xpm_file_to_image(env->mlx_ptr, file, 61 | &img->width, &img->height))) 62 | { 63 | free(img); 64 | return (NULL); 65 | } 66 | if (!(img->img_data = (int *)mlx_get_data_addr(img->img_ptr, &img->bpp, 67 | &img->size_line, &img->endian))) 68 | { 69 | free(img->img_ptr); 70 | free(img); 71 | return (NULL); 72 | } 73 | return (img); 74 | } 75 | -------------------------------------------------------------------------------- /srcs/bonus/init_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:06:16 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:12:34 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int init_items(t_env *env) 16 | { 17 | env->error = 0; 18 | if ((env->sound_icon = ft_new_img(env, "./textures/sound.xpm")) == NULL) 19 | env->error = IMG_ERROR_ITEM; 20 | if ((env->bullet = ft_new_img(env, "./weapon/bullets.xpm")) == NULL) 21 | env->error = IMG_ERROR_ITEM; 22 | if ((env->lives_icon = ft_new_img(env, "./textures/life.xpm")) == NULL) 23 | env->error = IMG_ERROR_ITEM; 24 | if ((env->target = ft_new_img(env, "./textures/aim.xpm")) == NULL) 25 | env->error = IMG_ERROR_ITEM; 26 | if (env->error) 27 | { 28 | ft_error(env->error, env); 29 | return (IMG_ERROR_ITEM); 30 | } 31 | return (SUCCESS); 32 | } 33 | 34 | int init_raybuffer(t_env *env) 35 | { 36 | if (!(env->ray.zbuffer = malloc(sizeof(double) * (env->width + 1)))) 37 | return (ZBUFFER_ERROR); 38 | return (SUCCESS); 39 | } 40 | 41 | void init_env_orientation(t_env *env) 42 | { 43 | if (env->orientation == 'N') 44 | { 45 | env->ray.diry = -0.99; 46 | env->ray.planex = -0.80; 47 | } 48 | if (env->orientation == 'S') 49 | { 50 | env->ray.diry = 0.99; 51 | env->ray.planex = 0.80; 52 | } 53 | if (env->orientation == 'E') 54 | { 55 | env->ray.dirx = 0.99; 56 | env->ray.planey = -0.80; 57 | } 58 | if (env->orientation == 'W') 59 | { 60 | env->ray.dirx = -0.99; 61 | env->ray.planey = 0.80; 62 | } 63 | } 64 | 65 | void init_env_2(t_env *env, t_arg arg) 66 | { 67 | env->width = arg.screen_w; 68 | env->height = arg.screen_h; 69 | env->floor = from_rgb_to_hex(arg.floor); 70 | env->ceil = from_rgb_to_hex(arg.ceil); 71 | env->arg = arg; 72 | init_env_orientation(env); 73 | env->mlx_ptr = mlx_init(); 74 | env->player_x += 0.5; 75 | env->player_y += 0.5; 76 | env->lives = 3; 77 | env->minimap = 1; 78 | env->target_divider = 1; 79 | } 80 | 81 | t_env init_env(t_arg arg) 82 | { 83 | t_env env; 84 | 85 | ft_bzero(&env, sizeof(t_env)); 86 | ft_bzero(&env.ray, sizeof(t_ray)); 87 | get_arg_for_env(&env, arg, 0, 0); 88 | env.ray.speed = 0.1; 89 | if (!(env.map = from_lst_to_tab(arg.map))) 90 | { 91 | env.error = FROM_LST_ERROR; 92 | ft_error(env.error, &env); 93 | } 94 | init_env_2(&env, arg); 95 | init_env_orientation(&env); 96 | init_weapon(&env); 97 | init_tex(&env); 98 | init_items(&env); 99 | env.map_height = ft_lstsize_map(env.arg.map); 100 | if ((env.error = init_raybuffer(&env)) != SUCCESS) 101 | { 102 | ft_error(env.error, &env); 103 | } 104 | init_sprite(&env); 105 | return (env); 106 | } 107 | -------------------------------------------------------------------------------- /srcs/bonus/items2_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* items2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:15:49 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:16:13 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | void ft_bonus(t_env *env) 16 | { 17 | env->lives = 3; 18 | env->sprite_count--; 19 | } 20 | -------------------------------------------------------------------------------- /srcs/bonus/items_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* items.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:13:02 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:17:07 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | void ft_reload_ammo(t_env *env) 16 | { 17 | env->weapon_list.weapon1.ammo += 10; 18 | env->weapon_list.weapon2.ammo += 10; 19 | env->weapon_list.weapon3.ammo += 10; 20 | env->weapon_list.weapon4.ammo += 10; 21 | env->sprite_count--; 22 | } 23 | 24 | void ft_game_over(t_env *env) 25 | { 26 | ft_putstr("\n###############GAME OVER##############\n"); 27 | ft_exit(env); 28 | } 29 | 30 | void ft_ennemy(t_env *env) 31 | { 32 | env->lives -= 0.1; 33 | if (env->lives <= 0) 34 | ft_game_over(env); 35 | } 36 | 37 | void ft_new_level(t_env *env, int i, int x, int y) 38 | { 39 | while (i <= (env->level * 2)) 40 | { 41 | y = rand() % env->map_height; 42 | x = rand() % ft_strlen(env->map[y]); 43 | if (env->map[y][x] == '0') 44 | { 45 | env->map[y][x] = 'O'; 46 | i++; 47 | } 48 | } 49 | env->ennemy_count += env->level * 2 + 1; 50 | env->sprite_count += env->level * 2 + 2; 51 | i = 0; 52 | while (i < 1) 53 | { 54 | y = rand() % env->map_height; 55 | x = rand() % env->map_width; 56 | if (env->map[y][x] == '0') 57 | env->map[y][x] = 'T'; 58 | if (env->map[y][x] == 'T') 59 | i++; 60 | } 61 | env->treasure_caught = 0; 62 | env->target_divider += 0.5; 63 | init_sprite(env); 64 | } 65 | 66 | void ft_next_level(t_env *env) 67 | { 68 | if (env->treasure_caught) 69 | { 70 | ft_putstr("######LEVEL "); 71 | ft_putnbr(env->level); 72 | ft_putstr(" COMPLETED######\n"); 73 | env->level++; 74 | ft_putstr("\n##################################\nEntering new level\n"); 75 | ft_putstr("\nlevel #"); 76 | ft_putnbr(env->level); 77 | ft_putstr("\n\n##################################\n"); 78 | ft_new_level(env, 0, 0, 0); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /srcs/bonus/main_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/01/21 09:15:57 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:26:18 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int main_arg_check(int ac, char **av) 16 | { 17 | int error; 18 | 19 | error = 0; 20 | if (ac > 3) 21 | error = NB_ARG_ERROR_TOO_MANY; 22 | if (ac < 2) 23 | error = NB_ARG_ERROR_TOO_FEW; 24 | if (ac == 2) 25 | if (ft_strncmp(av[1] + ft_strlen(av[1]) - 4, ".cub", 4) != 0) 26 | error = FILE_EXT_ERROR; 27 | if (ac == 3) 28 | if (ft_strncmp(av[2], "--save", 6) != 0) 29 | error = SAVE_FLAG_ERROR; 30 | if (error) 31 | { 32 | ft_error_arg(error); 33 | return (ARGUMENT_ERROR); 34 | } 35 | return (SUCCESS); 36 | } 37 | 38 | int launch_env(t_arg arg, int ac) 39 | { 40 | t_env env; 41 | 42 | if (check_map(arg.map, 0, 0) != SUCCESS) 43 | return (MAP_ERROR); 44 | env = init_env(arg); 45 | if (ac == 3) 46 | env.save_flag = 1; 47 | events(env); 48 | return (SUCCESS); 49 | } 50 | 51 | int main(int ac, char **av) 52 | { 53 | int fd1; 54 | int ret; 55 | char *buff; 56 | t_arg arg; 57 | 58 | ft_bzero(&arg, sizeof(arg)); 59 | ret = 0; 60 | buff = NULL; 61 | if (main_arg_check(ac, av) != SUCCESS) 62 | return (0); 63 | fd1 = open(av[1], O_RDWR); 64 | while ((ret = get_next_line(fd1, &buff)) > 0) 65 | { 66 | if (fetch_arguments(&arg, buff) != SUCCESS) 67 | return (ARGUMENT_ERROR); 68 | free(buff); 69 | } 70 | if (buff) 71 | free(buff); 72 | if (ret == -1) 73 | return (ft_error_arg(FILE_READ_ERROR)); 74 | if (launch_env(arg, ac) != SUCCESS) 75 | return (MAP_ERROR); 76 | return (0); 77 | } 78 | -------------------------------------------------------------------------------- /srcs/bonus/map_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* map.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:22:46 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:56:54 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int check_map_same_size(t_map *map) 16 | { 17 | t_map *tmp; 18 | int size; 19 | 20 | tmp = map; 21 | size = (int)ft_strlen(tmp->line); 22 | while (tmp) 23 | { 24 | if ((int)ft_strlen(tmp->line) != size) 25 | return (ERROR); 26 | tmp = tmp->next; 27 | } 28 | return (SUCCESS); 29 | } 30 | 31 | int check_non_square_map(t_map *tmp, t_map *previous, int i, int j) 32 | { 33 | t_map *next; 34 | 35 | tmp = tmp->next; 36 | next = tmp->next; 37 | while (tmp->next) 38 | { 39 | i = 0; 40 | while (tmp->line[i]) 41 | { 42 | if (tmp->line[i] != '1' && tmp->line[i] != ' ' && i >=\ 43 | (int)ft_strlen(previous->line)) 44 | return (MAP_ERROR_NOT_CLOSED); 45 | if (next) 46 | if (tmp->line[i] != '1' && tmp->line[i] != ' ' && i >=\ 47 | (int)ft_strlen(next->line)) 48 | return (MAP_ERROR_NOT_CLOSED); 49 | i++; 50 | } 51 | previous = tmp; 52 | tmp = tmp->next; 53 | next = tmp->next; 54 | j++; 55 | } 56 | return (SUCCESS); 57 | } 58 | 59 | int check_map_closed(t_map *map) 60 | { 61 | t_map *tmp; 62 | int i; 63 | 64 | tmp = map; 65 | if (check_non_square_map(tmp, tmp, 0, 1) != SUCCESS) 66 | return (MAP_ERROR_NOT_CLOSED); 67 | i = 0; 68 | while (tmp->line[i]) 69 | { 70 | if (tmp->line[i++] != '1') 71 | return (MAP_ERROR_NOT_CLOSED); 72 | } 73 | while (tmp->next) 74 | { 75 | if (tmp->line[0] != '1') 76 | return (MAP_ERROR_NOT_CLOSED); 77 | if (tmp->line[ft_strlen(tmp->line) - 1] != '1') 78 | return (MAP_ERROR_NOT_CLOSED); 79 | tmp = tmp->next; 80 | } 81 | i = 0; 82 | while (tmp->line[i]) 83 | if (tmp->line[i++] != '1') 84 | return (MAP_ERROR_NOT_CLOSED); 85 | return (SUCCESS); 86 | } 87 | 88 | int check_map(t_map *map, int count_pos, int i) 89 | { 90 | t_map *tmp; 91 | 92 | tmp = map; 93 | while (tmp) 94 | { 95 | i = 0; 96 | while (tmp->line[i]) 97 | { 98 | if (!ft_strchr("0123NSWEDBAOTP", tmp->line[i])) 99 | return (ft_error_arg(MAP_ERROR_WRONG_CHAR)); 100 | if (ft_strchr("NSWE", tmp->line[i])) 101 | count_pos++; 102 | if (count_pos > 1) 103 | return (ft_error_arg(MAP_ERROR_MANY_POS)); 104 | i++; 105 | } 106 | tmp = tmp->next; 107 | } 108 | if (!count_pos) 109 | return (ft_error_arg(MAP_ERROR_NO_POS)); 110 | if (check_map_closed(map) != SUCCESS) 111 | return (ft_error_arg(MAP_ERROR_NOT_CLOSED)); 112 | return (SUCCESS); 113 | } 114 | -------------------------------------------------------------------------------- /srcs/bonus/minimap_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minimap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:27:15 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:37:05 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | void draw_square(t_env env, int x, int y, int color) 16 | { 17 | int i; 18 | int j; 19 | int width; 20 | int height; 21 | 22 | i = x; 23 | j = y; 24 | height = MINIMAP_SIZE; 25 | width = height; 26 | while (i < width + x) 27 | { 28 | j = y; 29 | while (j < height + y) 30 | { 31 | ft_put_pixel(env.img, color, j, i); 32 | j++; 33 | } 34 | i++; 35 | } 36 | } 37 | 38 | int get_color_minimap(t_env env, int i, int j) 39 | { 40 | int color; 41 | 42 | if (env.map[i][j] == '1') 43 | color = COLOR_MAP_WALL; 44 | if (ft_strchr("23", env.map[i][j])) 45 | color = COLOR_MAP_SPRITE; 46 | if (ft_strchr("NSEW0FQ", env.map[i][j])) 47 | color = COLOR_MAP_FLOOR; 48 | if (ft_strchr("D", env.map[i][j])) 49 | color = COLOR_MAP_DOOR; 50 | if (ft_strchr("P", env.map[i][j])) 51 | color = COLOR_MAP_SECRET_DOOR; 52 | if (ft_strchr("B", env.map[i][j])) 53 | color = COLOR_MAP_BONUS; 54 | if (ft_strchr("A", env.map[i][j])) 55 | color = COLOR_MAP_AMMO; 56 | if (ft_strchr("O", env.map[i][j])) 57 | color = COLOR_MAP_ENNEMY; 58 | if (ft_strchr("T", env.map[i][j])) 59 | color = COLOR_MAP_TREASURE; 60 | return (color); 61 | } 62 | 63 | void init_minimap(t_env env) 64 | { 65 | int i; 66 | int j; 67 | int color; 68 | int offset_x; 69 | 70 | if (!env.minimap) 71 | return ; 72 | offset_x = env.width / 2 - (ft_strlen(env.map[0]) * MINIMAP_SIZE) / 2; 73 | i = 0; 74 | while (i < env.map_height) 75 | { 76 | j = 0; 77 | while (env.map[i][j] != '\0') 78 | { 79 | color = get_color_minimap(env, i, j); 80 | draw_square(env, i * MINIMAP_SIZE, offset_x +\ 81 | j * MINIMAP_SIZE, color); 82 | j++; 83 | } 84 | i++; 85 | } 86 | draw_square(env, (env.player_y - 0.5) * \ 87 | MINIMAP_SIZE, offset_x + (env.player_x) * MINIMAP_SIZE, COLOR_MAP_PLAYER); 88 | } 89 | -------------------------------------------------------------------------------- /srcs/bonus/ray2_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ray2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:53:11 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:43:48 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int ft_get_tex_height(t_env *env, char tex) 16 | { 17 | int height; 18 | 19 | height = 64; 20 | if (tex == 'E') 21 | height = env->tex_e->height; 22 | if (tex == 'N') 23 | height = env->tex_n->height; 24 | if (tex == 'W') 25 | height = env->tex_w->height; 26 | if (tex == 'S') 27 | height = env->tex_s->height; 28 | return (height); 29 | } 30 | 31 | int ft_get_tex_width(t_env *env, char tex) 32 | { 33 | int width; 34 | 35 | width = 64; 36 | if (tex == 'E') 37 | width = env->tex_e->width; 38 | if (tex == 'N') 39 | width = env->tex_n->width; 40 | if (tex == 'W') 41 | width = env->tex_w->width; 42 | if (tex == 'S') 43 | width = env->tex_s->width; 44 | return (width); 45 | } 46 | 47 | void ft_wall_tex(t_env *env, char tex) 48 | { 49 | double wallx; 50 | int height; 51 | int width; 52 | 53 | width = ft_get_tex_width(env, tex); 54 | height = ft_get_tex_height(env, tex); 55 | if (env->ray.door) 56 | { 57 | width = env->tex_door->width; 58 | height = env->tex_door->height; 59 | } 60 | if (env->ray.wall == 0) 61 | wallx = env->ray.rposy - env->ray.dist * env->ray.rdiry; 62 | else 63 | wallx = env->ray.rposx - env->ray.dist * env->ray.rdirx; 64 | wallx -= (int)wallx; 65 | env->ray.tex_x = (int)(wallx * width); 66 | if (env->ray.wall == 0) 67 | env->ray.tex_x = width + env->ray.tex_x; 68 | else if (env->ray.wall == 1) 69 | env->ray.tex_x = width + env->ray.tex_x - 1; 70 | env->ray.step_tex = 1 * width / env->ray.rh; 71 | env->ray.tex_pos = (env->ray.wstart - env->height / 2\ 72 | + env->ray.rh / 2) * env->ray.step_tex - 1; 73 | } 74 | -------------------------------------------------------------------------------- /srcs/bonus/save2_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* save2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:56:57 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:47:19 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int ft_save(t_env *env) 16 | { 17 | t_save save; 18 | 19 | save.height = env->height - 1; 20 | save.width = env->width; 21 | save.pad = (4 - ((int)save.width * 3) % 4) % 4; 22 | save.file_header = create_file_header(env, save.pad); 23 | save.img_header = create_img_header(save.height, save.width); 24 | if ((save.fd = open(SCREEN_PATH, O_WRONLY | O_CREAT, RIGHTS)) < 0) 25 | return (OPEN_ERROR); 26 | if (write_headers(&save) != SUCCESS) 27 | { 28 | close(save.fd); 29 | return (COLOR_ERROR); 30 | } 31 | if (write_colors(env, save.fd, save.height, save.width) != SUCCESS) 32 | { 33 | close(save.fd); 34 | return (COLOR_ERROR); 35 | } 36 | close(save.fd); 37 | return (SUCCESS); 38 | } 39 | 40 | int launch_save(t_env *env) 41 | { 42 | ft_save(env); 43 | env->save_flag = 0; 44 | return (SUCCESS); 45 | } 46 | -------------------------------------------------------------------------------- /srcs/bonus/save_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* save.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/01/27 09:53:37 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:47:05 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | static void set_in_char(unsigned char *start, int value) 16 | { 17 | start[0] = (unsigned char)(value); 18 | start[1] = (unsigned char)(value >> 8); 19 | start[2] = (unsigned char)(value >> 16); 20 | start[3] = (unsigned char)(value >> 24); 21 | } 22 | 23 | unsigned char *create_file_header(t_env *env, int pad) 24 | { 25 | int file_size; 26 | int bpp; 27 | static unsigned char file_header[14] = { 28 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 29 | 30 | bpp = BYTES_PER_PIX; 31 | file_size = 54 + (bpp * ((int)env->width + pad) * 32 | (int)env->height); 33 | file_header[0] = (unsigned char)('B'); 34 | file_header[1] = (unsigned char)('M'); 35 | set_in_char(file_header + 2, file_size); 36 | set_in_char(file_header + 10, FILE_HEADER_SIZE + IMG_HEADER_SIZE); 37 | return (file_header); 38 | } 39 | 40 | unsigned char *create_img_header(int height, int width) 41 | { 42 | static unsigned char img_header[40] = { 43 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 45 | 46 | set_in_char(img_header, 40); 47 | set_in_char(img_header + 4, width); 48 | set_in_char(img_header + 8, height); 49 | img_header[12] = (unsigned char)(1); 50 | set_in_char(img_header + 14, COMPRESSION); 51 | return (img_header); 52 | } 53 | 54 | int write_headers(t_save *save) 55 | { 56 | if ((write(save->fd, save->file_header, FILE_HEADER_SIZE)) < 0) 57 | { 58 | close(save->fd); 59 | return (WRITE_ERROR); 60 | } 61 | if ((write(save->fd, save->img_header, IMG_HEADER_SIZE)) < 0) 62 | { 63 | close(save->fd); 64 | return (WRITE_ERROR); 65 | } 66 | return (SUCCESS); 67 | } 68 | 69 | int write_colors(t_env *env, int fd, int height, int width) 70 | { 71 | static unsigned char rgb[3] = { 72 | 0, 0, 0 }; 73 | int i; 74 | int j; 75 | 76 | i = 0; 77 | while (i < (int)env->height) 78 | { 79 | j = 0; 80 | while (j < (int)env->width) 81 | { 82 | rgb[0] = ((env->img->img_data[(height - i) * width + j]) >> 16); 83 | rgb[1] = ((env->img->img_data[(height - i) * width + j]) >> 8); 84 | rgb[2] = (env->img->img_data[(height - i) * width + j]); 85 | if ((write(fd, rgb + 2, 1)) < 0) 86 | return (WRITE_ERROR); 87 | if ((write(fd, rgb + 1, 1)) < 0) 88 | return (WRITE_ERROR); 89 | if ((write(fd, rgb, 1)) < 0) 90 | return (WRITE_ERROR); 91 | j++; 92 | } 93 | i++; 94 | } 95 | return (SUCCESS); 96 | } 97 | -------------------------------------------------------------------------------- /srcs/bonus/sprite_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sprite.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 16:00:47 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:58:58 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | void get_sprite_tex(t_env *env, int x, int y, int i) 16 | { 17 | env->sprite_tab[i].pos_x = x + 0.5; 18 | env->sprite_tab[i].pos_y = y + 0.5; 19 | if (env->map[y][x] == '2') 20 | env->sprite_tab[i].tex = env->tex_sprite; 21 | if (env->map[y][x] == '3') 22 | env->sprite_tab[i].tex = env->tex_sprite1; 23 | if (env->map[y][x] == 'D') 24 | env->sprite_tab[i].tex = env->tex_door; 25 | if (env->map[y][x] == 'B') 26 | env->sprite_tab[i].tex = env->tex_bonus; 27 | if (env->map[y][x] == 'A') 28 | env->sprite_tab[i].tex = env->tex_ammo; 29 | if (env->map[y][x] == 'O') 30 | get_ennemy_sprite(env, i); 31 | if (env->map[y][x] == 'T') 32 | env->sprite_tab[i].tex = env->tex_treasure; 33 | env->sprite_tab[i].tex->sprite_x = x; 34 | env->sprite_tab[i].tex->sprite_y = y; 35 | } 36 | 37 | int ft_place_sprite(t_env *env) 38 | { 39 | int x; 40 | int y; 41 | int i; 42 | 43 | i = 0; 44 | y = 0; 45 | while (y < env->map_height) 46 | { 47 | x = 0; 48 | while (env->map[y][x] && i < env->sprite_count) 49 | { 50 | if (ft_strchr("23DBAOT", env->map[y][x])) 51 | { 52 | get_sprite_tex(env, x, y, i); 53 | i++; 54 | } 55 | x++; 56 | } 57 | y++; 58 | } 59 | return (SUCCESS); 60 | } 61 | 62 | int init_sprite(t_env *env) 63 | { 64 | int i; 65 | 66 | i = 0; 67 | if (!(env->sprite_tab = malloc(sizeof(t_sprite) * (env->sprite_count + 1)))) 68 | return (MALLOC_ERROR); 69 | if (!(env->ray.sp_order = malloc(sizeof(int) * env->sprite_count))) 70 | return (MALLOC_ERROR); 71 | if (!(env->ray.sp_distance = malloc(sizeof(double) * env->sprite_count))) 72 | return (MALLOC_ERROR); 73 | ft_place_sprite(env); 74 | ft_order_sprite(env); 75 | if (env->sprite_tab) 76 | { 77 | free(env->sprite_tab); 78 | } 79 | if (env->ray.sp_order) 80 | { 81 | free(env->ray.sp_order); 82 | } 83 | if (env->ray.sp_distance) 84 | { 85 | free(env->ray.sp_distance); 86 | } 87 | return (SUCCESS); 88 | } 89 | -------------------------------------------------------------------------------- /srcs/bonus/weapon2_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* weapon2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 16:24:26 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 17:00:03 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d_bonus.h" 14 | 15 | int init_weapon_img(t_env *env) 16 | { 17 | env->error = 0; 18 | if ((env->weapon_list.weapon1.img = ft_new_img(env,\ 19 | env->weapon_list.weapon1.path)) == NULL && !env->error) 20 | env->error = IMG_ERROR_WEAPON_1; 21 | if ((env->weapon_list.weapon2.img = ft_new_img(env,\ 22 | env->weapon_list.weapon2.path)) == NULL && !env->error) 23 | env->error = IMG_ERROR_WEAPON_2; 24 | if ((env->weapon_list.weapon3.img = ft_new_img(env,\ 25 | env->weapon_list.weapon3.path)) == NULL && !env->error) 26 | env->error = IMG_ERROR_WEAPON_3; 27 | if ((env->weapon_list.weapon4.img = ft_new_img(env,\ 28 | env->weapon_list.weapon4.path)) == NULL && !env->error) 29 | env->error = IMG_ERROR_WEAPON_4; 30 | if ((env->weapon_img = ft_new_img(env, env->current_weapon\ 31 | ->path)) == NULL) 32 | env->error = IMG_ERROR_WEAPON_5; 33 | if (env->error) 34 | { 35 | ft_error(env->error, env); 36 | return (IMG_ERROR); 37 | } 38 | return (SUCCESS); 39 | } 40 | 41 | void init_weapon2(t_env *env) 42 | { 43 | env->weapon_list.weapon4.name = "shotgun"; 44 | env->weapon_list.weapon4.width = 407; 45 | env->weapon_list.weapon4.height = 193; 46 | env->weapon_list.weapon4.ammo = 10; 47 | env->weapon_list.weapon4.path = "./weapon/shotgun.xpm"; 48 | env->weapon_list.weapon4.color = 0x0000ff; 49 | } 50 | 51 | int init_weapon(t_env *env) 52 | { 53 | env->weapon_id = 1; 54 | env->weapon_list.weapon1.name = "handgun"; 55 | env->weapon_list.weapon1.width = 407; 56 | env->weapon_list.weapon1.height = 193; 57 | env->weapon_list.weapon1.ammo = 10; 58 | env->weapon_list.weapon1.path = "./weapon/gun.xpm"; 59 | env->weapon_list.weapon1.color = 0x0000ff; 60 | env->weapon_list.weapon2.name = "firegun"; 61 | env->weapon_list.weapon2.width = 407; 62 | env->weapon_list.weapon2.height = 193; 63 | env->weapon_list.weapon2.ammo = 10; 64 | env->weapon_list.weapon2.path = "./weapon/firegun.xpm"; 65 | env->weapon_list.weapon2.color = 0x0000ff; 66 | env->weapon_list.weapon3.name = "sniper"; 67 | env->weapon_list.weapon3.width = 407; 68 | env->weapon_list.weapon3.height = 193; 69 | env->weapon_list.weapon3.ammo = 10; 70 | env->weapon_list.weapon3.path = "./weapon/sniper.xpm"; 71 | env->weapon_list.weapon3.color = 0x0000ff; 72 | init_weapon2(env); 73 | env->current_weapon = &env->weapon_list.weapon1; 74 | init_weapon_img(env); 75 | return (SUCCESS); 76 | } 77 | -------------------------------------------------------------------------------- /srcs/gnl/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/11 11:29:52 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/04 13:14:12 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | static char *ft_find_nl(char **temp) 16 | { 17 | char *stock_long; 18 | char *memory; 19 | 20 | memory = NULL; 21 | stock_long = ft_strdup_gnl((char*)temp); 22 | if (ft_strchr_gnl(*temp, '\n')) 23 | { 24 | free(stock_long); 25 | stock_long = NULL; 26 | stock_long = ft_strsub_gnl(*temp, 0, (ft_strlen_gnl(*temp)\ 27 | - ft_strlen_gnl((char*)ft_strchr_gnl(*temp, '\n')))); 28 | memory = ft_strdup_gnl(ft_strchr_gnl(*temp, '\n') + 1); 29 | free(*temp); 30 | *temp = NULL; 31 | *temp = (char *)ft_strdup_gnl(stock_long); 32 | } 33 | free(stock_long); 34 | return (memory); 35 | } 36 | 37 | char *ft_join_free_gnl(char *s1, char *s2) 38 | { 39 | char *tmp; 40 | 41 | tmp = ft_strdup_gnl(s1); 42 | free(s1); 43 | s1 = ft_strjoin_gnl(s2, tmp); 44 | free(tmp); 45 | free(s2); 46 | s2 = NULL; 47 | return (s1); 48 | } 49 | 50 | static int ft_read(int fd, char **temp) 51 | { 52 | char buffer[BUFFER_SIZE + 1]; 53 | int check; 54 | int ret; 55 | char *tmp; 56 | 57 | ret = 1; 58 | check = 1; 59 | while ((ret > 0) && check == 1) 60 | { 61 | ret = read(fd, buffer, BUFFER_SIZE); 62 | if (ret < 0) 63 | return (-1); 64 | buffer[ret] = '\0'; 65 | if (*temp) 66 | { 67 | tmp = ft_strdup_gnl(*temp); 68 | free(*temp); 69 | *temp = ft_strjoin_gnl(tmp, buffer); 70 | free(tmp); 71 | } 72 | else 73 | *temp = ft_strdup_gnl(buffer); 74 | check = ((ft_strchr_gnl(buffer, '\n')) ? 0 : 1); 75 | } 76 | return (ret); 77 | } 78 | 79 | int str_free(char *str, int i) 80 | { 81 | free(str); 82 | return (i); 83 | } 84 | 85 | int get_next_line(const int fd, char **line) 86 | { 87 | char *temp; 88 | static char *memor; 89 | int ret; 90 | 91 | temp = ft_strdup_gnl("\0"); 92 | ret = ft_read(fd, &temp); 93 | if (ret < 0 || fd < 0 || !(line) || BUFFER_SIZE <= 0) 94 | return (str_free(temp, -1)); 95 | temp = (memor != NULL) ? ft_join_free_gnl(temp, memor) : temp; 96 | memor = NULL; 97 | if ((ret == 0 && !ft_strchr_gnl(temp, '\n') && !ft_strchr_gnl(memor, '\n'))) 98 | { 99 | *line = ft_strdup_gnl(temp); 100 | return (str_free(temp, 0)); 101 | } 102 | if (ret >= 0) 103 | { 104 | memor = ft_find_nl(&temp); 105 | *line = ft_strdup_gnl(temp); 106 | } 107 | return (str_free(temp, 1)); 108 | } 109 | -------------------------------------------------------------------------------- /srcs/gnl/get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/11 11:29:47 by phperrot #+# #+# */ 9 | /* Updated: 2019/11/21 20:03:35 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | # ifndef __GET_NEXT_LINE_H 14 | # define __GET_NEXT_LINE_H 15 | # include 16 | # include 17 | # include 18 | # ifndef BUFFER_SIZE 19 | # define BUFFER_SIZE 42 20 | # endif 21 | 22 | int get_next_line(const int fd, char **line); 23 | size_t ft_strlen_gnl(const char *s); 24 | char *ft_strchr_gnl(const char *s, int c); 25 | char *ft_strdup_gnl(const char *s1); 26 | char *ft_strjoin_gnl(char const *s1, char const *s2); 27 | char *ft_strsub_gnl(char *str, int start, int len); 28 | 29 | # endif 30 | -------------------------------------------------------------------------------- /srcs/gnl/get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/13 10:30:07 by phperrot #+# #+# */ 9 | /* Updated: 2019/12/04 11:06:09 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | char *ft_strsub_gnl(char *str, int start, int len) 16 | { 17 | int i; 18 | char *output; 19 | 20 | i = 0; 21 | if (!str) 22 | return (NULL); 23 | if (!(output = malloc(sizeof(char) * (len + 1)))) 24 | return (NULL); 25 | while (i < len && (str[start + i] != '\n')) 26 | { 27 | output[i] = str[start + i]; 28 | i++; 29 | } 30 | output[i] = '\0'; 31 | return (output); 32 | } 33 | 34 | size_t ft_strlen_gnl(const char *s) 35 | { 36 | int i; 37 | 38 | if (!s) 39 | return (0); 40 | i = 0; 41 | while (s[i] != '\0') 42 | i++; 43 | return (i); 44 | } 45 | 46 | char *ft_strchr_gnl(const char *s, int c) 47 | { 48 | int i; 49 | char *tmp; 50 | 51 | i = 0; 52 | tmp = (char *)s; 53 | if (!s) 54 | return (NULL); 55 | if (c == '\0') 56 | { 57 | while (*tmp) 58 | tmp++; 59 | return ((char*)tmp); 60 | } 61 | while (tmp[i] != '\0') 62 | { 63 | if (tmp[i] == (char)c) 64 | return ((char *)&tmp[i]); 65 | i++; 66 | } 67 | if (*tmp == '\0' && c == '\0') 68 | { 69 | return (tmp); 70 | } 71 | return (NULL); 72 | } 73 | 74 | char *ft_strdup_gnl(const char *s1) 75 | { 76 | char *output; 77 | int i; 78 | int size; 79 | 80 | if (!s1) 81 | return (NULL); 82 | size = ft_strlen_gnl(s1); 83 | if (!(output = malloc(sizeof(char) * (size + 1)))) 84 | return (NULL); 85 | i = 0; 86 | while ((char)s1[i]) 87 | { 88 | output[i] = s1[i]; 89 | i++; 90 | } 91 | output[i] = '\0'; 92 | return (output); 93 | } 94 | 95 | char *ft_strjoin_gnl(char const *s1, char const *s2) 96 | { 97 | unsigned long i; 98 | char *output; 99 | unsigned long size1; 100 | unsigned long size2; 101 | 102 | if (!(s1 && s2)) 103 | return (NULL); 104 | size1 = ft_strlen_gnl(s1); 105 | size2 = ft_strlen_gnl(s2); 106 | if (!(output = malloc(sizeof(char) * (size1 + size2 + 1)))) 107 | return (NULL); 108 | i = 0; 109 | while (i < size1) 110 | { 111 | output[i] = s1[i]; 112 | i++; 113 | } 114 | while (i < size1 + size2) 115 | { 116 | output[i] = s2[i - size1]; 117 | i++; 118 | } 119 | output[i] = '\0'; 120 | return (output); 121 | } 122 | -------------------------------------------------------------------------------- /srcs/mandatory/cub3d_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cub3d_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:05:58 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 14:28:50 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | double ft_abs(double x) 16 | { 17 | if (x < 0) 18 | x = -x; 19 | return (x); 20 | } 21 | 22 | int count_char(char *str, char c) 23 | { 24 | int i; 25 | int count; 26 | 27 | i = 0; 28 | count = 0; 29 | while (str[i] != '\0') 30 | { 31 | if (str[i] == c) 32 | count++; 33 | i++; 34 | } 35 | return (count); 36 | } 37 | 38 | char *withdraw_char(char *str, char c) 39 | { 40 | int i; 41 | int j; 42 | int new_len; 43 | int old_len; 44 | char *new_str; 45 | 46 | old_len = ft_strlen(str); 47 | new_len = old_len - count_char(str, c); 48 | if (!(new_str = malloc(sizeof(char) * (new_len + 1)))) 49 | return (NULL); 50 | i = 0; 51 | j = 0; 52 | while (i < old_len) 53 | { 54 | if (str[i] != c) 55 | { 56 | new_str[j] = str[i]; 57 | j++; 58 | } 59 | i++; 60 | } 61 | new_str[j] = '\0'; 62 | return (new_str); 63 | } 64 | 65 | t_map *ft_lstlast_map(t_map *lst) 66 | { 67 | t_map *tmp; 68 | 69 | if (!lst) 70 | return (NULL); 71 | tmp = lst; 72 | while (tmp->next) 73 | tmp = tmp->next; 74 | return (tmp); 75 | } 76 | -------------------------------------------------------------------------------- /srcs/mandatory/cub3d_utils2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cub3d_utils2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:26:33 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 14:29:55 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | t_map *ft_lstnew_map(void *content) 16 | { 17 | t_map *elem; 18 | 19 | if (!(elem = malloc(sizeof(t_list)))) 20 | return (NULL); 21 | elem->line = content; 22 | elem->next = NULL; 23 | return (elem); 24 | } 25 | 26 | void ft_lstadd_back_map(t_map **alst, t_map *new) 27 | { 28 | t_map *tmp; 29 | 30 | tmp = *alst; 31 | if (*alst == NULL) 32 | *alst = new; 33 | else 34 | (ft_lstlast_map(*alst))->next = new; 35 | } 36 | 37 | void ft_lstclear_map(t_map **lst) 38 | { 39 | t_map *tmp; 40 | t_map *next; 41 | 42 | if (!lst) 43 | return ; 44 | tmp = *lst; 45 | while (tmp) 46 | { 47 | next = tmp->next; 48 | free(tmp); 49 | tmp = next; 50 | } 51 | *lst = NULL; 52 | } 53 | 54 | int ft_lstsize_map(t_map *lst) 55 | { 56 | t_map *tmp; 57 | int i; 58 | 59 | i = 0; 60 | if (!lst) 61 | return (i); 62 | tmp = lst; 63 | while (tmp) 64 | { 65 | tmp = tmp->next; 66 | i++; 67 | } 68 | return (i); 69 | } 70 | 71 | char **from_lst_to_tab(t_map *lst) 72 | { 73 | t_map *tmp; 74 | int i; 75 | int j; 76 | char **tab; 77 | 78 | tmp = lst; 79 | if (!(tab = malloc(sizeof(char*) * (ft_lstsize_map(lst) + 1)))) 80 | return (NULL); 81 | i = 0; 82 | while (i < ft_lstsize_map(lst)) 83 | { 84 | if (!(tab[i] = malloc(sizeof(char) * (ft_strlen(tmp->line) + 1)))) 85 | return (NULL); 86 | j = 0; 87 | while (tmp->line[j] != '\0') 88 | { 89 | tab[i][j] = tmp->line[j]; 90 | j++; 91 | } 92 | tab[i][j] = '\0'; 93 | tmp = tmp->next; 94 | i++; 95 | } 96 | return (tab); 97 | } 98 | -------------------------------------------------------------------------------- /srcs/mandatory/cub3d_utils3.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cub3d_utils3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:27:28 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:42:49 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | t_img *ft_new_image(t_env *env, int width, int height) 16 | { 17 | t_img *img; 18 | 19 | if (!(img = malloc(sizeof(t_img)))) 20 | return (NULL); 21 | if (!(img->img_ptr = mlx_new_image(env->mlx_ptr, width, height))) 22 | return (NULL); 23 | if (!(img->img_data = (int *)mlx_get_data_addr(img->img_ptr, &img->bpp, 24 | &img->size_line, &img->endian))) 25 | return (NULL); 26 | img->width = width; 27 | img->height = height; 28 | return (img); 29 | } 30 | 31 | void pixel_tex(t_tex *tex, t_env *env) 32 | { 33 | if (tex->width * env->ray.tex_y + env->ray.tex_x < (tex->width *\ 34 | tex->height)) 35 | env->color = tex->tex_data[tex->width * env->ray.tex_y +\ 36 | env->ray.tex_x]; 37 | } 38 | 39 | char *get_output_wall(t_env *env) 40 | { 41 | if (env->ray.stepx < 0 && env->ray.rmapx < (int)env->ray.\ 42 | rposx && env->ray.wall == 0) 43 | { 44 | pixel_tex(env->tex_e, env); 45 | return (env->arg.ea); 46 | } 47 | if (env->ray.stepx > 0 && env->ray.rmapx > (int)env->ray.rposx\ 48 | && env->ray.wall == 0) 49 | { 50 | pixel_tex(env->tex_w, env); 51 | return (env->arg.we); 52 | } 53 | if (env->ray.stepy < 0 && env->ray.rmapy < (int)env->ray.rposy &&\ 54 | env->ray.wall - 0) 55 | { 56 | pixel_tex(env->tex_n, env); 57 | return (env->arg.no); 58 | } 59 | if (env->ray.stepy > 0 && env->ray.rmapy > (int)\ 60 | env->ray.rposy && env->ray.wall == 1) 61 | pixel_tex(env->tex_s, env); 62 | return (env->arg.so); 63 | } 64 | 65 | char *pix_color(t_env *env, int sprite) 66 | { 67 | char *output; 68 | 69 | output = NULL; 70 | if (!sprite && !env->ray.door) 71 | { 72 | output = get_output_wall(env); 73 | } 74 | if (env->ray.door) 75 | { 76 | pixel_tex(env->tex_door, env); 77 | output = env->arg.door; 78 | } 79 | if (sprite && env->ray.sprite) 80 | { 81 | pixel_tex(env->tex_sprite, env); 82 | output = env->arg.sprite; 83 | } 84 | return (output); 85 | } 86 | 87 | void open_door(t_env *env, char c, int x, int y) 88 | { 89 | while (y < env->map_height) 90 | { 91 | x = 0; 92 | while (env->map[y][x]) 93 | { 94 | if (env->map[y][x] == c && env->open_door && ft_abs((double)(y -\ 95 | env->player_y)) <= 2 && (ft_abs((double)x - env->player_x) <= 2)) 96 | { 97 | if (c == 'D') 98 | env->sprite_count--; 99 | env->map[y][x] = (c == 'D') ? 'F' : 'Q'; 100 | } 101 | if (env->map[y][x] == c && !env->open_door && ft_abs((double)(y -\ 102 | env->player_y)) <= 2 && (ft_abs((double)x - env->player_x) <= 2)) 103 | { 104 | env->map[y][x] = (c == 'F') ? 'D' : 'P'; 105 | if (c == 'F') 106 | env->sprite_count++; 107 | } 108 | x++; 109 | } 110 | y++; 111 | } 112 | ft_disp_screen(env, 0); 113 | } 114 | -------------------------------------------------------------------------------- /srcs/mandatory/display.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* display.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:34:45 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:43:14 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int ft_put_pixel(t_img *img, unsigned int color, int p_x, int p_y) 16 | { 17 | if (p_y >= img->height || p_x >= img->width || p_x < 0\ 18 | || p_y < 0 || ((p_y * img->width + p_x) >= (img->width * img->height - 1))) 19 | return (PIXEL_ERROR); 20 | img->img_data[p_y * img->width + p_x] = color; 21 | return (SUCCESS); 22 | } 23 | 24 | char *ft_disp_col(t_env *env, int x, char tex, int y) 25 | { 26 | char *tex_path; 27 | int height; 28 | 29 | if (tex == 'E') 30 | height = env->tex_e->height; 31 | if (tex == 'N') 32 | height = env->tex_n->height; 33 | if (tex == 'W') 34 | height = env->tex_w->height; 35 | if (tex == 'S') 36 | height = env->tex_s->height; 37 | while (y < env->ray.wstart) 38 | ft_put_pixel(env->img, env->ceil, x, y++); 39 | while (y >= env->ray.wstart && y <= env->ray.wend) 40 | { 41 | env->ray.tex_y = (int)env->ray.tex_pos & (height - 1); 42 | env->ray.tex_pos += env->ray.step_tex; 43 | tex_path = pix_color(env, 0); 44 | ft_put_pixel(env->img, env->color, x, y); 45 | y++; 46 | } 47 | while (y < env->height) 48 | ft_put_pixel(env->img, env->floor, x, y++); 49 | return (tex_path); 50 | } 51 | 52 | int ft_disp_screen(t_env *env, int x) 53 | { 54 | char tex; 55 | 56 | while (x < env->width) 57 | { 58 | ft_init_ray(env, x); 59 | ft_direction_ray(env); 60 | tex = ft_hit_ray(env, 0); 61 | ft_size_ray(env); 62 | ft_wall_tex(env, tex); 63 | ft_disp_col(env, x++, tex, 0); 64 | env->ray.zbuffer[x] = env->ray.dist; 65 | } 66 | init_sprite(env); 67 | env->weapon_shot = 0; 68 | if (env->save_flag == 1) 69 | { 70 | if (launch_save(env) != SUCCESS) 71 | { 72 | ft_putstr("Error\nError while writing save file"); 73 | ft_exit(env); 74 | } 75 | ft_exit(env); 76 | } 77 | return (SUCCESS); 78 | } 79 | -------------------------------------------------------------------------------- /srcs/mandatory/error2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* error2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 14:58:00 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:26:35 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int ft_error_tex_inputs(int error, char *orientation) 16 | { 17 | error = ft_strlen(orientation); 18 | free(orientation); 19 | ft_putstr("Error\nWrong texture path"); 20 | return (ERROR); 21 | } 22 | 23 | void ft_error_arg2(int error) 24 | { 25 | if (error == MAP_ERROR_NOT_CLOSED) 26 | ft_putstr("Error\nMap not closed"); 27 | if (error == MAP_ERROR_SAME_SIZE) 28 | ft_putstr("Error\nAll lines of the map are not the same size"); 29 | if (error == UNKNOW_ARG) 30 | ft_putstr("Error\nUnknown arg"); 31 | } 32 | 33 | int ft_error_arg(int error) 34 | { 35 | if (error == NB_ARG_ERROR_TOO_FEW) 36 | ft_putstr("Error\nNot enough arguments"); 37 | if (error == NB_ARG_ERROR_TOO_MANY) 38 | ft_putstr("Error\nToo many arguments"); 39 | if (error == FILE_EXT_ERROR) 40 | ft_putstr("Error\nFirst argument is not a .cub file"); 41 | if (error == SAVE_FLAG_ERROR) 42 | ft_putstr("Error\nNot a valid flag"); 43 | if (error == FILE_READ_ERROR) 44 | ft_putstr("Error\nCould not complete file reading"); 45 | if (error == SCREEN_ERROR) 46 | ft_putstr("Error\nWrong screen dimensions"); 47 | if (error == PATH_ERROR) 48 | ft_putstr("Error\nWrong texture path"); 49 | if (error == RGB_ERROR) 50 | ft_putstr("Error\nProblems with floor or ceil"); 51 | if (error == MAP_ERROR_WRONG_CHAR) 52 | ft_putstr("Error\nInvalid character in the map"); 53 | if (error == MAP_ERROR_MANY_POS) 54 | ft_putstr("Error\nSeveral positions entered in the map"); 55 | if (error == MAP_ERROR_NO_POS) 56 | ft_putstr("Error\nNo position was entered in the map"); 57 | ft_error_arg2(error); 58 | return (ERROR); 59 | } 60 | -------------------------------------------------------------------------------- /srcs/mandatory/events.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* events.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:00:20 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:04:42 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int ft_key_hit(int key, t_env *env) 16 | { 17 | if (key == KEY_UP) 18 | env->move.up = 1; 19 | if (key == KEY_DOWN) 20 | env->move.down = 1; 21 | if (key == ROTATE_LEFT) 22 | env->move.left = 1; 23 | if (key == KEY_LEFT) 24 | env->move.strafl = 1; 25 | if (key == ROTATE_RIGHT) 26 | env->move.right = 1; 27 | if (key == KEY_RIGHT) 28 | env->move.strafr = 1; 29 | if (key == KEY_EXIT) 30 | ft_exit(env); 31 | return (SUCCESS); 32 | } 33 | 34 | int ft_key_release(int key, t_env *env) 35 | { 36 | if (key == KEY_UP) 37 | env->move.up = 0; 38 | if (key == KEY_DOWN) 39 | env->move.down = 0; 40 | if (key == ROTATE_LEFT) 41 | env->move.left = 0; 42 | if (key == KEY_LEFT) 43 | env->move.strafl = 0; 44 | if (key == ROTATE_RIGHT) 45 | env->move.right = 0; 46 | if (key == KEY_RIGHT) 47 | env->move.strafr = 0; 48 | return (SUCCESS); 49 | } 50 | 51 | int ft_run(t_env *env) 52 | { 53 | ft_free_img(env, env->img); 54 | if ((env->img = ft_new_image(env, env->width, env->height)) == NULL) 55 | { 56 | env->error = IMG_ERROR; 57 | ft_error(env->error, env); 58 | return (IMG_ERROR); 59 | } 60 | ft_move(env); 61 | ft_disp_screen(env, 0); 62 | mlx_put_image_to_window(env->mlx_ptr, env->win_ptr, env->img->img_ptr, 0, 63 | 0); 64 | return (SUCCESS); 65 | } 66 | 67 | int events(t_env env) 68 | { 69 | while (env.save_flag && env.width % 4) 70 | env.width++; 71 | if (env.save_flag) 72 | { 73 | if (!(env.win_ptr = mlx_new_window(env.mlx_ptr, 1, 1, "Cub3D"))) 74 | return (MLX_ERROR); 75 | } 76 | else 77 | { 78 | if (!(env.win_ptr = mlx_new_window(env.mlx_ptr, env.width, \ 79 | env.height, "Cub3D"))) 80 | return (MLX_ERROR); 81 | } 82 | mlx_hook(env.win_ptr, 17, STRUCTURENOTIFYMASK, ft_exit, &env); 83 | mlx_hook(env.win_ptr, KEYPRESS, KEYPRESSMASK, ft_key_hit, &env); 84 | mlx_hook(env.win_ptr, KEYRELEASE, KEYRELEASEMASK, ft_key_release, &env); 85 | mlx_loop_hook(env.mlx_ptr, ft_run, &env); 86 | mlx_loop(env.mlx_ptr); 87 | return (SUCCESS); 88 | } 89 | -------------------------------------------------------------------------------- /srcs/mandatory/exit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* exit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:01:59 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:43:02 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | void ft_free_ennemy(t_env *env) 16 | { 17 | ft_free_tex(env, env->tex_ennemy_1); 18 | ft_free_tex(env, env->tex_ennemy_2); 19 | ft_free_tex(env, env->tex_ennemy_3); 20 | ft_free_tex(env, env->tex_ennemy_4); 21 | ft_free_tex(env, env->tex_ennemy_5); 22 | ft_free_tex(env, env->tex_ennemy_6); 23 | ft_free_tex(env, env->tex_ennemy_7); 24 | ft_free_tex(env, env->tex_ennemy_8); 25 | ft_free_tex(env, env->tex_ennemy_9); 26 | ft_free_tex(env, env->tex_ennemy); 27 | } 28 | 29 | int ft_exit(t_env *env) 30 | { 31 | if (!env->error) 32 | { 33 | ft_free_img(env, env->img); 34 | ft_free_tex(env, env->tex_s); 35 | ft_free_tex(env, env->tex_n); 36 | ft_free_tex(env, env->tex_w); 37 | ft_free_tex(env, env->tex_e); 38 | ft_free_tex(env, env->tex_sprite); 39 | ft_free_tex(env, env->tex_sprite1); 40 | ft_free_tex(env, env->tex_door); 41 | ft_free_tex(env, env->tex_bonus); 42 | ft_free_ennemy(env); 43 | ft_free_tex(env, env->tex_ammo); 44 | ft_free_tex(env, env->tex_treasure); 45 | ft_free_img(env, env->sound_icon); 46 | ft_free_img(env, env->bullet); 47 | ft_free_img(env, env->lives_icon); 48 | ft_free_img(env, env->target); 49 | ft_free_img(env, env->weapon_img); 50 | mlx_destroy_window(env->mlx_ptr, env->win_ptr); 51 | } 52 | exit(0); 53 | return (SUCCESS); 54 | } 55 | -------------------------------------------------------------------------------- /srcs/mandatory/free.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* free.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:05:18 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:05:37 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | void ft_free_tex(t_env *env, t_tex *tex) 16 | { 17 | if (tex) 18 | { 19 | mlx_destroy_image(env->mlx_ptr, tex->tex_ptr); 20 | free(tex); 21 | tex = NULL; 22 | } 23 | } 24 | 25 | void ft_free_img(t_env *env, t_img *img) 26 | { 27 | if (img) 28 | { 29 | mlx_destroy_image(env->mlx_ptr, img->img_ptr); 30 | free(img); 31 | img = NULL; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /srcs/mandatory/init.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:06:16 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:12:34 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int init_items(t_env *env) 16 | { 17 | env->error = 0; 18 | if ((env->sound_icon = ft_new_img(env, "./textures/sound.xpm")) == NULL) 19 | env->error = IMG_ERROR_ITEM; 20 | if ((env->bullet = ft_new_img(env, "./weapon/bullets.xpm")) == NULL) 21 | env->error = IMG_ERROR_ITEM; 22 | if ((env->lives_icon = ft_new_img(env, "./textures/life.xpm")) == NULL) 23 | env->error = IMG_ERROR_ITEM; 24 | if ((env->target = ft_new_img(env, "./textures/aim.xpm")) == NULL) 25 | env->error = IMG_ERROR_ITEM; 26 | if (env->error) 27 | { 28 | ft_error(env->error, env); 29 | return (IMG_ERROR_ITEM); 30 | } 31 | return (SUCCESS); 32 | } 33 | 34 | int init_raybuffer(t_env *env) 35 | { 36 | if (!(env->ray.zbuffer = malloc(sizeof(double) * (env->width + 1)))) 37 | return (ZBUFFER_ERROR); 38 | return (SUCCESS); 39 | } 40 | 41 | void init_env_orientation(t_env *env) 42 | { 43 | if (env->orientation == 'N') 44 | { 45 | env->ray.diry = -0.99; 46 | env->ray.planex = -0.80; 47 | } 48 | if (env->orientation == 'S') 49 | { 50 | env->ray.diry = 0.99; 51 | env->ray.planex = 0.80; 52 | } 53 | if (env->orientation == 'E') 54 | { 55 | env->ray.dirx = 0.99; 56 | env->ray.planey = -0.80; 57 | } 58 | if (env->orientation == 'W') 59 | { 60 | env->ray.dirx = -0.99; 61 | env->ray.planey = 0.80; 62 | } 63 | } 64 | 65 | void init_env_2(t_env *env, t_arg arg) 66 | { 67 | env->width = arg.screen_w; 68 | env->height = arg.screen_h; 69 | env->floor = from_rgb_to_hex(arg.floor); 70 | env->ceil = from_rgb_to_hex(arg.ceil); 71 | env->arg = arg; 72 | init_env_orientation(env); 73 | env->mlx_ptr = mlx_init(); 74 | env->player_x += 0.5; 75 | env->player_y += 0.5; 76 | env->lives = 3; 77 | env->minimap = 1; 78 | env->target_divider = 1; 79 | } 80 | 81 | t_env init_env(t_arg arg) 82 | { 83 | t_env env; 84 | 85 | ft_bzero(&env, sizeof(t_env)); 86 | ft_bzero(&env.ray, sizeof(t_ray)); 87 | get_arg_for_env(&env, arg, 0, 0); 88 | env.ray.speed = 0.1; 89 | if (!(env.map = from_lst_to_tab(arg.map))) 90 | { 91 | env.error = FROM_LST_ERROR; 92 | ft_error(env.error, &env); 93 | } 94 | init_env_2(&env, arg); 95 | init_env_orientation(&env); 96 | init_weapon(&env); 97 | init_tex(&env); 98 | init_items(&env); 99 | env.map_height = ft_lstsize_map(env.arg.map); 100 | if ((env.error = init_raybuffer(&env)) != SUCCESS) 101 | { 102 | ft_error(env.error, &env); 103 | } 104 | init_sprite(&env); 105 | return (env); 106 | } 107 | -------------------------------------------------------------------------------- /srcs/mandatory/init2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:09:40 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:58:26 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int from_rgb_to_hex(t_rgb color) 16 | { 17 | int output; 18 | 19 | output = 256 * 256 * color.r + 256 * color.g + color.b; 20 | return (output); 21 | } 22 | 23 | void get_arg_for_env(t_env *env, t_arg arg, int y, int x) 24 | { 25 | t_map *tmp; 26 | 27 | tmp = arg.map; 28 | while (tmp) 29 | { 30 | x = 0; 31 | while (tmp->line[x] != '\0') 32 | { 33 | if (ft_strchr("NSWE", tmp->line[x])) 34 | { 35 | env->orientation = tmp->line[x]; 36 | env->player_x = x++; 37 | env->player_y = y++; 38 | } 39 | if (ft_strchr("23DBAOT", tmp->line[x])) 40 | env->sprite_count++; 41 | if (tmp->line[x] == 'O') 42 | env->ennemy_count++; 43 | x++; 44 | } 45 | env->map_width = x; 46 | tmp = tmp->next; 47 | y++; 48 | } 49 | env->map_height = y - 1; 50 | } 51 | 52 | t_img *ft_new_img(t_env *env, char *file) 53 | { 54 | t_img *img; 55 | 56 | if (!(img = malloc(sizeof(t_img)))) 57 | { 58 | return (NULL); 59 | } 60 | if (!(img->img_ptr = mlx_xpm_file_to_image(env->mlx_ptr, file, 61 | &img->width, &img->height))) 62 | { 63 | free(img); 64 | return (NULL); 65 | } 66 | if (!(img->img_data = (int *)mlx_get_data_addr(img->img_ptr, &img->bpp, 67 | &img->size_line, &img->endian))) 68 | { 69 | free(img->img_ptr); 70 | free(img); 71 | return (NULL); 72 | } 73 | return (img); 74 | } 75 | -------------------------------------------------------------------------------- /srcs/mandatory/items.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* items.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:13:02 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:17:07 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | void ft_reload_ammo(t_env *env) 16 | { 17 | env->weapon_list.weapon1.ammo += 10; 18 | env->weapon_list.weapon2.ammo += 10; 19 | env->weapon_list.weapon3.ammo += 10; 20 | env->weapon_list.weapon4.ammo += 10; 21 | env->sprite_count--; 22 | } 23 | 24 | void ft_game_over(t_env *env) 25 | { 26 | ft_putstr("\n###############GAME OVER##############\n"); 27 | ft_exit(env); 28 | } 29 | 30 | void ft_ennemy(t_env *env) 31 | { 32 | env->lives -= 0.1; 33 | if (env->lives <= 0) 34 | ft_game_over(env); 35 | } 36 | 37 | void ft_new_level(t_env *env, int i, int x, int y) 38 | { 39 | while (i <= (env->level * 2)) 40 | { 41 | y = rand() % env->map_height; 42 | x = rand() % env->map_width; 43 | if (env->map[y][x] == '0') 44 | { 45 | env->map[y][x] = 'O'; 46 | i++; 47 | } 48 | } 49 | env->ennemy_count += env->level * 2 + 1; 50 | env->sprite_count += env->level * 2 + 2; 51 | i = 0; 52 | while (i < 1) 53 | { 54 | y = rand() % env->map_height; 55 | x = rand() % env->map_width; 56 | if (env->map[y][x] == '0') 57 | env->map[y][x] = 'T'; 58 | if (env->map[y][x] == 'T') 59 | i++; 60 | } 61 | env->treasure_caught = 0; 62 | env->target_divider += 0.5; 63 | init_sprite(env); 64 | } 65 | 66 | void ft_next_level(t_env *env) 67 | { 68 | if (env->treasure_caught) 69 | { 70 | ft_putstr("######LEVEL "); 71 | ft_putnbr(env->level); 72 | ft_putstr(" COMPLETED######\n"); 73 | env->level++; 74 | ft_putstr("\n##################################\nEntering new level\n"); 75 | ft_putstr("\nlevel #"); 76 | ft_putnbr(env->level); 77 | ft_putstr("\n\n##################################\n"); 78 | ft_new_level(env, 0, 0, 0); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /srcs/mandatory/items2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* items2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:15:49 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:16:13 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | void ft_bonus(t_env *env) 16 | { 17 | env->lives = 3; 18 | env->sprite_count--; 19 | } 20 | -------------------------------------------------------------------------------- /srcs/mandatory/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/01/21 09:15:57 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:26:18 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int main_arg_check(int ac, char **av) 16 | { 17 | int error; 18 | 19 | error = 0; 20 | if (ac > 3) 21 | error = NB_ARG_ERROR_TOO_MANY; 22 | if (ac < 2) 23 | error = NB_ARG_ERROR_TOO_FEW; 24 | if (ac == 2) 25 | if (ft_strncmp(av[1] + ft_strlen(av[1]) - 4, ".cub", 4) != 0) 26 | error = FILE_EXT_ERROR; 27 | if (ac == 3) 28 | if (ft_strncmp(av[2], "--save", 6) != 0) 29 | error = SAVE_FLAG_ERROR; 30 | if (error) 31 | { 32 | ft_error_arg(error); 33 | return (ARGUMENT_ERROR); 34 | } 35 | return (SUCCESS); 36 | } 37 | 38 | int launch_env(t_arg arg, int ac) 39 | { 40 | t_env env; 41 | 42 | if (check_map(arg.map, 0, 0) != SUCCESS) 43 | return (MAP_ERROR); 44 | env = init_env(arg); 45 | if (ac == 3) 46 | env.save_flag = 1; 47 | events(env); 48 | return (SUCCESS); 49 | } 50 | 51 | int main(int ac, char **av) 52 | { 53 | int fd1; 54 | int ret; 55 | char *buff; 56 | t_arg arg; 57 | 58 | ft_bzero(&arg, sizeof(arg)); 59 | ret = 0; 60 | buff = NULL; 61 | if (main_arg_check(ac, av) != SUCCESS) 62 | return (0); 63 | fd1 = open(av[1], O_RDWR); 64 | while ((ret = get_next_line(fd1, &buff)) > 0) 65 | { 66 | if (fetch_arguments(&arg, buff) != SUCCESS) 67 | return (ARGUMENT_ERROR); 68 | free(buff); 69 | } 70 | if (buff) 71 | free(buff); 72 | if (ret == -1) 73 | return (ft_error_arg(FILE_READ_ERROR)); 74 | if (launch_env(arg, ac) != SUCCESS) 75 | return (MAP_ERROR); 76 | return (0); 77 | } 78 | -------------------------------------------------------------------------------- /srcs/mandatory/map.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* map.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:22:46 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:31:18 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int check_map_same_size(t_map *map) 16 | { 17 | t_map *tmp; 18 | int size; 19 | 20 | tmp = map; 21 | size = (int)ft_strlen(tmp->line); 22 | while (tmp) 23 | { 24 | if ((int)ft_strlen(tmp->line) != size) 25 | return (ERROR); 26 | tmp = tmp->next; 27 | } 28 | return (SUCCESS); 29 | } 30 | 31 | int check_non_square_map(t_map *tmp, t_map *previous, int i, int j) 32 | { 33 | t_map *next; 34 | 35 | tmp = tmp->next; 36 | next = tmp->next; 37 | while (tmp->next) 38 | { 39 | i = 0; 40 | while (tmp->line[i]) 41 | { 42 | if (tmp->line[i] != '1' && tmp->line[i] != ' ' && i >=\ 43 | (int)ft_strlen(previous->line)) 44 | return (MAP_ERROR_NOT_CLOSED); 45 | if (next) 46 | if (tmp->line[i] != '1' && tmp->line[i] != ' ' && i >=\ 47 | (int)ft_strlen(next->line)) 48 | return (MAP_ERROR_NOT_CLOSED); 49 | i++; 50 | } 51 | previous = tmp; 52 | tmp = tmp->next; 53 | next = tmp->next; 54 | j++; 55 | } 56 | return (SUCCESS); 57 | } 58 | 59 | int check_map_closed(t_map *map) 60 | { 61 | t_map *tmp; 62 | int i; 63 | 64 | tmp = map; 65 | if (check_non_square_map(tmp, tmp, 0, 1) != SUCCESS) 66 | return (MAP_ERROR_NOT_CLOSED); 67 | i = 0; 68 | while (tmp->line[i]) 69 | { 70 | if (tmp->line[i++] != '1') 71 | return (MAP_ERROR_NOT_CLOSED); 72 | } 73 | while (tmp->next) 74 | { 75 | if (tmp->line[0] != '1') 76 | return (MAP_ERROR_NOT_CLOSED); 77 | if (tmp->line[ft_strlen(tmp->line) - 1] != '1') 78 | return (MAP_ERROR_NOT_CLOSED); 79 | tmp = tmp->next; 80 | } 81 | i = 0; 82 | while (tmp->line[i]) 83 | if (tmp->line[i++] != '1') 84 | return (MAP_ERROR_NOT_CLOSED); 85 | return (SUCCESS); 86 | } 87 | 88 | int check_map(t_map *map, int count_pos, int i) 89 | { 90 | t_map *tmp; 91 | 92 | tmp = map; 93 | while (tmp) 94 | { 95 | i = 0; 96 | while (tmp->line[i]) 97 | { 98 | if (!ft_strchr("012NSWE", tmp->line[i])) 99 | return (ft_error_arg(MAP_ERROR_WRONG_CHAR)); 100 | if (ft_strchr("NSWE", tmp->line[i])) 101 | count_pos++; 102 | if (count_pos > 1) 103 | return (ft_error_arg(MAP_ERROR_MANY_POS)); 104 | i++; 105 | } 106 | tmp = tmp->next; 107 | } 108 | if (!count_pos) 109 | return (ft_error_arg(MAP_ERROR_NO_POS)); 110 | if (check_map_closed(map) != SUCCESS) 111 | return (ft_error_arg(MAP_ERROR_NOT_CLOSED)); 112 | return (SUCCESS); 113 | } 114 | -------------------------------------------------------------------------------- /srcs/mandatory/minimap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minimap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:27:15 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 15:30:46 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | void draw_square(t_env env, int x, int y, int color) 16 | { 17 | int i; 18 | int j; 19 | int width; 20 | int height; 21 | 22 | i = x; 23 | j = y; 24 | height = MINIMAP_SIZE; 25 | width = height; 26 | while (i < width + x) 27 | { 28 | j = y; 29 | while (j < height + y) 30 | { 31 | ft_put_pixel(env.img, color, j, i); 32 | j++; 33 | } 34 | i++; 35 | } 36 | } 37 | 38 | int get_color_minimap(t_env env, int i, int j) 39 | { 40 | int color; 41 | 42 | if (env.map[i][j] == '1') 43 | color = COLOR_MAP_WALL; 44 | if (ft_strchr("23", env.map[i][j])) 45 | color = COLOR_MAP_SPRITE; 46 | if (ft_strchr("NSEW0FQ", env.map[i][j])) 47 | color = COLOR_MAP_FLOOR; 48 | if (ft_strchr("D", env.map[i][j])) 49 | color = COLOR_MAP_DOOR; 50 | if (ft_strchr("P", env.map[i][j])) 51 | color = COLOR_MAP_SECRET_DOOR; 52 | if (ft_strchr("B", env.map[i][j])) 53 | color = COLOR_MAP_BONUS; 54 | if (ft_strchr("A", env.map[i][j])) 55 | color = COLOR_MAP_AMMO; 56 | if (ft_strchr("O", env.map[i][j])) 57 | color = COLOR_MAP_ENNEMY; 58 | if (ft_strchr("T", env.map[i][j])) 59 | color = COLOR_MAP_TREASURE; 60 | return (color); 61 | } 62 | 63 | void init_minimap(t_env env) 64 | { 65 | int i; 66 | int j; 67 | int color; 68 | int offset_x; 69 | 70 | offset_x = env.width / 2 - (ft_strlen(env.map[0]) * MINIMAP_SIZE) / 2; 71 | i = 0; 72 | while (i < env.map_height) 73 | { 74 | j = 0; 75 | while (env.map[i][j] != '\0') 76 | { 77 | color = get_color_minimap(env, i, j); 78 | draw_square(env, i * MINIMAP_SIZE, offset_x +\ 79 | j * MINIMAP_SIZE, color); 80 | j++; 81 | } 82 | i++; 83 | } 84 | draw_square(env, (env.player_y - 0.5) * \ 85 | MINIMAP_SIZE, offset_x + (env.player_x) * MINIMAP_SIZE, COLOR_MAP_PLAYER); 86 | } 87 | -------------------------------------------------------------------------------- /srcs/mandatory/parser_tex.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parser_tex.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/14 15:25:51 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:41:25 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int check_path(t_arg *arg, char *buff) 16 | { 17 | char *orientation; 18 | int i; 19 | 20 | if (ft_strlen(buff) < 1) 21 | return (PATH_ERROR); 22 | orientation = ft_strsub(buff, 0, 2); 23 | i = 2; 24 | while (ft_strchr(INVISI_CHAR, buff[i])) 25 | i++; 26 | if (!ft_strncmp("NO", orientation, 2)) 27 | { 28 | if (ft_strlen(arg->no)) 29 | return (ft_error_tex_inputs(PATH_ERROR, orientation)); 30 | arg->no = ft_strsub(buff, i, ft_strlen(buff) - i); 31 | } 32 | if (!ft_strncmp("SO", orientation, 2)) 33 | { 34 | if (ft_strlen(arg->so)) 35 | return (ft_error_tex_inputs(PATH_ERROR, orientation)); 36 | arg->so = ft_strsub(buff, i, ft_strlen(buff) - i); 37 | } 38 | return (check_path1(orientation, buff, i, arg)); 39 | } 40 | 41 | int check_path1(char *orientation, char *buff, int i, t_arg *arg) 42 | { 43 | if (!ft_strncmp("WE", orientation, 2)) 44 | { 45 | if (ft_strlen(arg->we)) 46 | return (ft_error_tex_inputs(PATH_ERROR, orientation)); 47 | arg->we = ft_strsub(buff, i, ft_strlen(buff) - i); 48 | } 49 | if (!ft_strncmp("EA", orientation, 2)) 50 | { 51 | if (ft_strlen(arg->ea)) 52 | return (ft_error_tex_inputs(PATH_ERROR, orientation)); 53 | arg->ea = ft_strsub(buff, i, ft_strlen(buff) - i); 54 | } 55 | if (!ft_strncmp("S ", orientation, 2)) 56 | { 57 | if (ft_strlen(arg->sprite)) 58 | return (ft_error_tex_inputs(PATH_ERROR, orientation)); 59 | arg->sprite = ft_strsub(buff, i, ft_strlen(buff) - i); 60 | } 61 | return (SUCCESS); 62 | } 63 | -------------------------------------------------------------------------------- /srcs/mandatory/ray2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ray2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:53:11 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:43:48 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int ft_get_tex_height(t_env *env, char tex) 16 | { 17 | int height; 18 | 19 | height = 64; 20 | if (tex == 'E') 21 | height = env->tex_e->height; 22 | if (tex == 'N') 23 | height = env->tex_n->height; 24 | if (tex == 'W') 25 | height = env->tex_w->height; 26 | if (tex == 'S') 27 | height = env->tex_s->height; 28 | return (height); 29 | } 30 | 31 | int ft_get_tex_width(t_env *env, char tex) 32 | { 33 | int width; 34 | 35 | width = 64; 36 | if (tex == 'E') 37 | width = env->tex_e->width; 38 | if (tex == 'N') 39 | width = env->tex_n->width; 40 | if (tex == 'W') 41 | width = env->tex_w->width; 42 | if (tex == 'S') 43 | width = env->tex_s->width; 44 | return (width); 45 | } 46 | 47 | void ft_wall_tex(t_env *env, char tex) 48 | { 49 | double wallx; 50 | int height; 51 | int width; 52 | 53 | width = ft_get_tex_width(env, tex); 54 | height = ft_get_tex_height(env, tex); 55 | if (env->ray.door) 56 | { 57 | width = env->tex_door->width; 58 | height = env->tex_door->height; 59 | } 60 | if (env->ray.wall == 0) 61 | wallx = env->ray.rposy - env->ray.dist * env->ray.rdiry; 62 | else 63 | wallx = env->ray.rposx - env->ray.dist * env->ray.rdirx; 64 | wallx -= (int)wallx; 65 | env->ray.tex_x = (int)(wallx * width); 66 | if (env->ray.wall == 0) 67 | env->ray.tex_x = width + env->ray.tex_x; 68 | else if (env->ray.wall == 1) 69 | env->ray.tex_x = width + env->ray.tex_x - 1; 70 | env->ray.step_tex = 1 * width / env->ray.rh; 71 | env->ray.tex_pos = (env->ray.wstart - env->height / 2\ 72 | + env->ray.rh / 2) * env->ray.step_tex - 1; 73 | } 74 | -------------------------------------------------------------------------------- /srcs/mandatory/save.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* save.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/01/27 09:53:37 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:49:35 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | static void set_in_char(unsigned char *start, int value) 16 | { 17 | start[0] = (unsigned char)(value); 18 | start[1] = (unsigned char)(value >> 8); 19 | start[2] = (unsigned char)(value >> 16); 20 | start[3] = (unsigned char)(value >> 24); 21 | } 22 | 23 | unsigned char *create_file_header(t_env *env, int pad) 24 | { 25 | int file_size; 26 | int bpp; 27 | static unsigned char file_header[14] = { 28 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 29 | 30 | bpp = BYTES_PER_PIX; 31 | file_size = 54 + (bpp * ((int)env->width + pad) * 32 | (int)env->height); 33 | file_header[0] = (unsigned char)('B'); 34 | file_header[1] = (unsigned char)('M'); 35 | set_in_char(file_header + 2, file_size); 36 | set_in_char(file_header + 10, FILE_HEADER_SIZE + IMG_HEADER_SIZE); 37 | return (file_header); 38 | } 39 | 40 | unsigned char *create_img_header(int height, int width) 41 | { 42 | static unsigned char img_header[40] = { 43 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 45 | 46 | set_in_char(img_header, 40); 47 | set_in_char(img_header + 4, width); 48 | set_in_char(img_header + 8, height); 49 | img_header[12] = (unsigned char)(1); 50 | set_in_char(img_header + 14, COMPRESSION); 51 | return (img_header); 52 | } 53 | 54 | int write_headers(t_save *save) 55 | { 56 | if ((write(save->fd, save->file_header, FILE_HEADER_SIZE)) < 0) 57 | { 58 | close(save->fd); 59 | return (WRITE_ERROR); 60 | } 61 | if ((write(save->fd, save->img_header, IMG_HEADER_SIZE)) < 0) 62 | { 63 | close(save->fd); 64 | return (WRITE_ERROR); 65 | } 66 | return (SUCCESS); 67 | } 68 | 69 | int write_colors(t_env *env, int fd, int height, int width) 70 | { 71 | static unsigned char rgb[3] = { 72 | 0, 0, 0 }; 73 | int i; 74 | int j; 75 | 76 | i = 0; 77 | while (i < (int)env->height) 78 | { 79 | j = 0; 80 | while (j < (int)env->width) 81 | { 82 | rgb[0] = ((env->img->img_data[(height - i) * width + j]) >> 16); 83 | rgb[1] = ((env->img->img_data[(height - i) * width + j]) >> 8); 84 | rgb[2] = (env->img->img_data[(height - i) * width + j]); 85 | if ((write(fd, rgb + 2, 1)) < 0) 86 | return (WRITE_ERROR); 87 | if ((write(fd, rgb + 1, 1)) < 0) 88 | return (WRITE_ERROR); 89 | if ((write(fd, rgb, 1)) < 0) 90 | return (WRITE_ERROR); 91 | j++; 92 | } 93 | i++; 94 | } 95 | return (SUCCESS); 96 | } 97 | -------------------------------------------------------------------------------- /srcs/mandatory/save2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* save2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 15:56:57 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/20 08:49:25 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int ft_save(t_env *env) 16 | { 17 | t_save save; 18 | 19 | save.height = env->height - 1; 20 | save.width = env->width; 21 | save.pad = (4 - ((int)save.width * 3) % 4) % 4; 22 | save.file_header = create_file_header(env, save.pad); 23 | save.img_header = create_img_header(save.height, save.width); 24 | if ((save.fd = open(SCREEN_PATH, O_WRONLY | O_CREAT, RIGHTS)) < 0) 25 | return (OPEN_ERROR); 26 | if (write_headers(&save) != SUCCESS) 27 | { 28 | close(save.fd); 29 | return (COLOR_ERROR); 30 | } 31 | if (write_colors(env, save.fd, save.height, save.width) != SUCCESS) 32 | { 33 | close(save.fd); 34 | return (COLOR_ERROR); 35 | } 36 | close(save.fd); 37 | return (SUCCESS); 38 | } 39 | 40 | int launch_save(t_env *env) 41 | { 42 | ft_save(env); 43 | env->save_flag = 0; 44 | return (SUCCESS); 45 | } 46 | -------------------------------------------------------------------------------- /srcs/mandatory/sprite.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sprite.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 16:00:47 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:58:58 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | void get_sprite_tex(t_env *env, int x, int y, int i) 16 | { 17 | env->sprite_tab[i].pos_x = x + 0.5; 18 | env->sprite_tab[i].pos_y = y + 0.5; 19 | if (env->map[y][x] == '2') 20 | env->sprite_tab[i].tex = env->tex_sprite; 21 | if (env->map[y][x] == '3') 22 | env->sprite_tab[i].tex = env->tex_sprite1; 23 | if (env->map[y][x] == 'D') 24 | env->sprite_tab[i].tex = env->tex_door; 25 | if (env->map[y][x] == 'B') 26 | env->sprite_tab[i].tex = env->tex_bonus; 27 | if (env->map[y][x] == 'A') 28 | env->sprite_tab[i].tex = env->tex_ammo; 29 | if (env->map[y][x] == 'O') 30 | get_ennemy_sprite(env, i); 31 | if (env->map[y][x] == 'T') 32 | env->sprite_tab[i].tex = env->tex_treasure; 33 | env->sprite_tab[i].tex->sprite_x = x; 34 | env->sprite_tab[i].tex->sprite_y = y; 35 | } 36 | 37 | int ft_place_sprite(t_env *env) 38 | { 39 | int x; 40 | int y; 41 | int i; 42 | 43 | i = 0; 44 | y = 0; 45 | while (y < env->map_height) 46 | { 47 | x = 0; 48 | while (env->map[y][x] && i < env->sprite_count) 49 | { 50 | if (ft_strchr("23DBAOT", env->map[y][x])) 51 | { 52 | get_sprite_tex(env, x, y, i); 53 | i++; 54 | } 55 | x++; 56 | } 57 | y++; 58 | } 59 | return (SUCCESS); 60 | } 61 | 62 | int init_sprite(t_env *env) 63 | { 64 | int i; 65 | 66 | i = 0; 67 | if (!(env->sprite_tab = malloc(sizeof(t_sprite) * (env->sprite_count + 1)))) 68 | return (MALLOC_ERROR); 69 | if (!(env->ray.sp_order = malloc(sizeof(int) * env->sprite_count))) 70 | return (MALLOC_ERROR); 71 | if (!(env->ray.sp_distance = malloc(sizeof(double) * env->sprite_count))) 72 | return (MALLOC_ERROR); 73 | ft_place_sprite(env); 74 | ft_order_sprite(env); 75 | if (env->sprite_tab) 76 | { 77 | free(env->sprite_tab); 78 | } 79 | if (env->ray.sp_order) 80 | { 81 | free(env->ray.sp_order); 82 | } 83 | if (env->ray.sp_distance) 84 | { 85 | free(env->ray.sp_distance); 86 | } 87 | return (SUCCESS); 88 | } 89 | -------------------------------------------------------------------------------- /srcs/mandatory/textures.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* textures.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/14 03:50:33 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 16:44:52 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | t_tex *ft_new_tex(t_env *env, char *file, char *type) 16 | { 17 | t_tex *tex; 18 | 19 | if (!(tex = malloc(sizeof(t_tex)))) 20 | return (NULL); 21 | if (!(tex->tex_ptr = mlx_xpm_file_to_image(env->mlx_ptr, file, 22 | &tex->width, &tex->height))) 23 | { 24 | free(tex); 25 | return (NULL); 26 | } 27 | if (!(tex->tex_data = (int *)mlx_get_data_addr(tex->tex_ptr, &tex->bpp, 28 | &tex->size_line, &tex->endian))) 29 | { 30 | free(tex->tex_ptr); 31 | free(tex); 32 | return (NULL); 33 | } 34 | tex->type = type; 35 | return (tex); 36 | } 37 | 38 | void init_tex_wall_sprite(t_env *env) 39 | { 40 | if ((env->tex_n = ft_new_tex(env, env->arg.no, "NO")) == NULL) 41 | env->error = TEX_ERROR_N; 42 | if ((env->tex_s = ft_new_tex(env, env->arg.so, "SO")) == NULL) 43 | env->error = TEX_ERROR_S; 44 | if ((env->tex_w = ft_new_tex(env, env->arg.we, "WE")) == NULL) 45 | env->error = TEX_ERROR_W; 46 | if ((env->tex_e = ft_new_tex(env, env->arg.ea, "EA")) == NULL) 47 | env->error = TEX_ERROR_E; 48 | if ((env->tex_sprite = ft_new_tex(env, env->arg.sprite, "sprite")) == NULL) 49 | env->error = TEX_ERROR_SP; 50 | } 51 | 52 | int init_tex(t_env *env) 53 | { 54 | env->error = 0; 55 | init_tex_wall_sprite(env); 56 | if (env->error) 57 | { 58 | env->error = TEX_ERROR; 59 | ft_error(env->error, env); 60 | } 61 | return (SUCCESS); 62 | } 63 | -------------------------------------------------------------------------------- /srcs/mandatory/weapon2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* weapon2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: phperrot +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/18 16:24:26 by phperrot #+# #+# */ 9 | /* Updated: 2020/02/18 17:00:03 by phperrot ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "cub3d.h" 14 | 15 | int init_weapon_img(t_env *env) 16 | { 17 | env->error = 0; 18 | if ((env->weapon_list.weapon1.img = ft_new_img(env,\ 19 | env->weapon_list.weapon1.path)) == NULL && !env->error) 20 | env->error = IMG_ERROR_WEAPON_1; 21 | if ((env->weapon_list.weapon2.img = ft_new_img(env,\ 22 | env->weapon_list.weapon2.path)) == NULL && !env->error) 23 | env->error = IMG_ERROR_WEAPON_2; 24 | if ((env->weapon_list.weapon3.img = ft_new_img(env,\ 25 | env->weapon_list.weapon3.path)) == NULL && !env->error) 26 | env->error = IMG_ERROR_WEAPON_3; 27 | if ((env->weapon_list.weapon4.img = ft_new_img(env,\ 28 | env->weapon_list.weapon4.path)) == NULL && !env->error) 29 | env->error = IMG_ERROR_WEAPON_4; 30 | if ((env->weapon_img = ft_new_img(env, env->current_weapon\ 31 | ->path)) == NULL) 32 | env->error = IMG_ERROR_WEAPON_5; 33 | if (env->error) 34 | { 35 | ft_error(env->error, env); 36 | return (IMG_ERROR); 37 | } 38 | return (SUCCESS); 39 | } 40 | 41 | void init_weapon2(t_env *env) 42 | { 43 | env->weapon_list.weapon4.name = "shotgun"; 44 | env->weapon_list.weapon4.width = 407; 45 | env->weapon_list.weapon4.height = 193; 46 | env->weapon_list.weapon4.ammo = 10; 47 | env->weapon_list.weapon4.path = "./weapon/shotgun.xpm"; 48 | env->weapon_list.weapon4.color = 0x0000ff; 49 | } 50 | 51 | int init_weapon(t_env *env) 52 | { 53 | env->weapon_id = 1; 54 | env->weapon_list.weapon1.name = "handgun"; 55 | env->weapon_list.weapon1.width = 407; 56 | env->weapon_list.weapon1.height = 193; 57 | env->weapon_list.weapon1.ammo = 10; 58 | env->weapon_list.weapon1.path = "./weapon/gun.xpm"; 59 | env->weapon_list.weapon1.color = 0x0000ff; 60 | env->weapon_list.weapon2.name = "firegun"; 61 | env->weapon_list.weapon2.width = 407; 62 | env->weapon_list.weapon2.height = 193; 63 | env->weapon_list.weapon2.ammo = 10; 64 | env->weapon_list.weapon2.path = "./weapon/firegun.xpm"; 65 | env->weapon_list.weapon2.color = 0x0000ff; 66 | env->weapon_list.weapon3.name = "sniper"; 67 | env->weapon_list.weapon3.width = 407; 68 | env->weapon_list.weapon3.height = 193; 69 | env->weapon_list.weapon3.ammo = 10; 70 | env->weapon_list.weapon3.path = "./weapon/sniper.xpm"; 71 | env->weapon_list.weapon3.color = 0x0000ff; 72 | init_weapon2(env); 73 | env->current_weapon = &env->weapon_list.weapon1; 74 | init_weapon_img(env); 75 | return (SUCCESS); 76 | } 77 | -------------------------------------------------------------------------------- /srcs/minilibx/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | 4 | NOM=libmlx.a 5 | 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 6 | SRC+= mlx_png.c mlx_mouse.m 7 | OBJ1=$(SRC:.c=.o) 8 | OBJ=$(OBJ1:.m=.o) 9 | CFLAGS+=-O2 10 | 11 | # add to match string put with X11 in size and position 12 | CFLAGS+= -DSTRINGPUTX11 13 | 14 | all: $(NOM) 15 | 16 | $(NOM): $(OBJ) 17 | ar -r $(NOM) $(OBJ) 18 | ranlib $(NOM) 19 | 20 | clean: 21 | rm -f $(NOM) $(OBJ) *~ 22 | rm -f mlx_app 23 | 24 | re: clean all 25 | -------------------------------------------------------------------------------- /srcs/minilibx/font.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/srcs/minilibx/font.xcf -------------------------------------------------------------------------------- /srcs/minilibx/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 | -------------------------------------------------------------------------------- /srcs/minilibx/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 | -------------------------------------------------------------------------------- /srcs/minilibx/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 | -------------------------------------------------------------------------------- /srcs/minilibx/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 | -------------------------------------------------------------------------------- /srcs/minilibx/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 | -------------------------------------------------------------------------------- /srcs/minilibx/mlx_png.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | void *mlx_png_file_to_image(void *xvar, char *file, int *width, int *height); 5 | -------------------------------------------------------------------------------- /textures/EA.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/EA.bmp -------------------------------------------------------------------------------- /textures/NO.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/NO.bmp -------------------------------------------------------------------------------- /textures/SO.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/SO.bmp -------------------------------------------------------------------------------- /textures/WE.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/WE.bmp -------------------------------------------------------------------------------- /textures/aim-original.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/aim-original.png -------------------------------------------------------------------------------- /textures/aim.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/aim.png -------------------------------------------------------------------------------- /textures/barrel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/barrel.png -------------------------------------------------------------------------------- /textures/bonus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/bonus.png -------------------------------------------------------------------------------- /textures/bricks.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/bricks.bmp -------------------------------------------------------------------------------- /textures/bricks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/bricks.png -------------------------------------------------------------------------------- /textures/door.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/door.bmp -------------------------------------------------------------------------------- /textures/life.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/life.png -------------------------------------------------------------------------------- /textures/sound.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/textures/sound.png -------------------------------------------------------------------------------- /weapon/bullets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/weapon/bullets.png -------------------------------------------------------------------------------- /weapon/firegun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/weapon/firegun.png -------------------------------------------------------------------------------- /weapon/gun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/weapon/gun.png -------------------------------------------------------------------------------- /weapon/shotgun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/weapon/shotgun.png -------------------------------------------------------------------------------- /weapon/sniper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/weapon/sniper.png -------------------------------------------------------------------------------- /weapon/weapons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phperrot/cub3d/201d420531f2e73a1a16857f7a63e9b6d95c2404/weapon/weapons.png --------------------------------------------------------------------------------