From 987ae156928f13606df895b6d49d0c014319d54e Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 22 Nov 2020 17:36:11 +0100 Subject: [PATCH] rtv1 --- Makefile | 86 +++++++++++++ README.md | 4 +- auteur | 1 + libft/Makefile | 122 +++++++++++++++++++ libft/ft_atof.c | 36 ++++++ libft/ft_atoi.c | 32 +++++ libft/ft_atoi_color.c | 33 +++++ libft/ft_atoi_len.c | 35 ++++++ libft/ft_atoi_split.c | 34 ++++++ libft/ft_bzero.c | 25 ++++ libft/ft_error.c | 19 +++ libft/ft_get_last_node.c | 23 ++++ libft/ft_isalnum.c | 18 +++ libft/ft_isalpha.c | 18 +++ libft/ft_isascii.c | 18 +++ libft/ft_isdigit.c | 18 +++ libft/ft_ishexdigit.c | 20 +++ libft/ft_isprint.c | 18 +++ libft/ft_isspace.c | 21 ++++ libft/ft_itoa.c | 37 ++++++ libft/ft_lstadd.c | 23 ++++ libft/ft_lstadd_preview.c | 19 +++ libft/ft_lstdel.c | 25 ++++ libft/ft_lstdelone.c | 19 +++ libft/ft_lstiter.c | 29 +++++ libft/ft_lstmap.c | 31 +++++ libft/ft_lstnew.c | 40 ++++++ libft/ft_memalloc.c | 27 +++++ libft/ft_memccpy.c | 37 ++++++ libft/ft_memchr.c | 31 +++++ libft/ft_memcmp.c | 33 +++++ libft/ft_memcpy.c | 30 +++++ libft/ft_memdel.c | 19 +++ libft/ft_memmove.c | 39 ++++++ libft/ft_memset.c | 28 +++++ libft/ft_power.c | 25 ++++ libft/ft_putchar.c | 18 +++ libft/ft_putchar_fd.c | 18 +++ libft/ft_putendl.c | 19 +++ libft/ft_putendl_fd.c | 19 +++ libft/ft_putnbr.c | 31 +++++ libft/ft_putnbr_fd.c | 31 +++++ libft/ft_putstr.c | 19 +++ libft/ft_putstr_fd.c | 19 +++ libft/ft_sort_int.c | 33 +++++ libft/ft_sqrt.c | 23 ++++ libft/ft_strcat.c | 32 +++++ libft/ft_strchr.c | 23 ++++ libft/ft_strclr.c | 19 +++ libft/ft_strcmp.c | 21 ++++ libft/ft_strcpy.c | 30 +++++ libft/ft_strdel.c | 18 +++ libft/ft_strdup.c | 29 +++++ libft/ft_strequ.c | 27 +++++ libft/ft_striter.c | 25 ++++ libft/ft_striteri.c | 25 ++++ libft/ft_strjoin.c | 34 ++++++ libft/ft_strlcat.c | 32 +++++ libft/ft_strlen.c | 23 ++++ libft/ft_strmap.c | 33 +++++ libft/ft_strmapi.c | 31 +++++ libft/ft_strncat.c | 30 +++++ libft/ft_strncmp.c | 21 ++++ libft/ft_strncpy.c | 31 +++++ libft/ft_strnequ.c | 27 +++++ libft/ft_strnew.c | 23 ++++ libft/ft_strnstr.c | 40 ++++++ libft/ft_strrchr.c | 28 +++++ libft/ft_strsplit.c | 87 ++++++++++++++ libft/ft_strstr.c | 38 ++++++ libft/ft_strsub.c | 28 +++++ libft/ft_strtab_isdigit.c | 33 +++++ libft/ft_strtrim.c | 40 ++++++ libft/ft_swap.c | 22 ++++ libft/ft_tolower.c | 20 +++ libft/ft_toupper.c | 20 +++ libft/get_next_line.c | 93 ++++++++++++++ libft/get_next_line.h | 22 ++++ libft/libft.h | 103 ++++++++++++++++ scenes/cone.scene | 7 ++ scenes/new.scene | 8 ++ scenes/plane.scene | 4 + scenes/rtv125.scene | 10 ++ scenes/sphere.scene | 4 + scenes/sphereup.scene | 4 + scenes/temple.scene | 23 ++++ scenes/test.scene | 9 ++ scenes/test2.scene | 8 ++ src/add_cone.c | 50 ++++++++ src/add_cylinder.c | 50 ++++++++ src/add_plane.c | 47 ++++++++ src/add_sphere.c | 42 +++++++ src/add_spot.c | 36 ++++++ src/color.c | 50 ++++++++ src/env_list.c | 55 +++++++++ src/main.c | 49 ++++++++ src/maths.c | 27 +++++ src/maths_cone.c | 55 +++++++++ src/maths_cylinder.c | 52 ++++++++ src/maths_plane.c | 36 ++++++ src/maths_sphere.c | 46 +++++++ src/mlx.c | 53 ++++++++ src/mlx_events.c | 36 ++++++ src/parse.c | 111 +++++++++++++++++ src/phong.c | 103 ++++++++++++++++ src/ray.c | 34 ++++++ src/raytrace.c | 63 ++++++++++ src/rtv1.h | 247 ++++++++++++++++++++++++++++++++++++++ src/scene_params.c | 59 +++++++++ src/set_camera.c | 69 +++++++++++ src/vec.c | 54 +++++++++ src/vec_arithmetics.c | 46 +++++++ 112 files changed, 3957 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 auteur create mode 100644 libft/Makefile create mode 100644 libft/ft_atof.c create mode 100644 libft/ft_atoi.c create mode 100644 libft/ft_atoi_color.c create mode 100644 libft/ft_atoi_len.c create mode 100644 libft/ft_atoi_split.c create mode 100644 libft/ft_bzero.c create mode 100644 libft/ft_error.c create mode 100644 libft/ft_get_last_node.c create mode 100644 libft/ft_isalnum.c create mode 100644 libft/ft_isalpha.c create mode 100644 libft/ft_isascii.c create mode 100644 libft/ft_isdigit.c create mode 100644 libft/ft_ishexdigit.c create mode 100644 libft/ft_isprint.c create mode 100644 libft/ft_isspace.c create mode 100644 libft/ft_itoa.c create mode 100644 libft/ft_lstadd.c create mode 100644 libft/ft_lstadd_preview.c create mode 100644 libft/ft_lstdel.c create mode 100644 libft/ft_lstdelone.c create mode 100644 libft/ft_lstiter.c create mode 100644 libft/ft_lstmap.c create mode 100644 libft/ft_lstnew.c create mode 100644 libft/ft_memalloc.c create mode 100644 libft/ft_memccpy.c create mode 100644 libft/ft_memchr.c create mode 100644 libft/ft_memcmp.c create mode 100644 libft/ft_memcpy.c create mode 100644 libft/ft_memdel.c create mode 100644 libft/ft_memmove.c create mode 100644 libft/ft_memset.c create mode 100644 libft/ft_power.c create mode 100644 libft/ft_putchar.c create mode 100644 libft/ft_putchar_fd.c create mode 100644 libft/ft_putendl.c create mode 100644 libft/ft_putendl_fd.c create mode 100644 libft/ft_putnbr.c create mode 100644 libft/ft_putnbr_fd.c create mode 100644 libft/ft_putstr.c create mode 100644 libft/ft_putstr_fd.c create mode 100644 libft/ft_sort_int.c create mode 100644 libft/ft_sqrt.c create mode 100644 libft/ft_strcat.c create mode 100644 libft/ft_strchr.c create mode 100644 libft/ft_strclr.c create mode 100644 libft/ft_strcmp.c create mode 100644 libft/ft_strcpy.c create mode 100644 libft/ft_strdel.c create mode 100644 libft/ft_strdup.c create mode 100644 libft/ft_strequ.c create mode 100644 libft/ft_striter.c create mode 100644 libft/ft_striteri.c create mode 100644 libft/ft_strjoin.c create mode 100644 libft/ft_strlcat.c create mode 100644 libft/ft_strlen.c create mode 100644 libft/ft_strmap.c create mode 100644 libft/ft_strmapi.c create mode 100644 libft/ft_strncat.c create mode 100644 libft/ft_strncmp.c create mode 100644 libft/ft_strncpy.c create mode 100644 libft/ft_strnequ.c create mode 100644 libft/ft_strnew.c create mode 100644 libft/ft_strnstr.c create mode 100644 libft/ft_strrchr.c create mode 100644 libft/ft_strsplit.c create mode 100644 libft/ft_strstr.c create mode 100644 libft/ft_strsub.c create mode 100644 libft/ft_strtab_isdigit.c create mode 100644 libft/ft_strtrim.c create mode 100644 libft/ft_swap.c create mode 100644 libft/ft_tolower.c create mode 100644 libft/ft_toupper.c create mode 100644 libft/get_next_line.c create mode 100644 libft/get_next_line.h create mode 100644 libft/libft.h create mode 100644 scenes/cone.scene create mode 100644 scenes/new.scene create mode 100644 scenes/plane.scene create mode 100644 scenes/rtv125.scene create mode 100644 scenes/sphere.scene create mode 100644 scenes/sphereup.scene create mode 100644 scenes/temple.scene create mode 100644 scenes/test.scene create mode 100644 scenes/test2.scene create mode 100644 src/add_cone.c create mode 100644 src/add_cylinder.c create mode 100644 src/add_plane.c create mode 100644 src/add_sphere.c create mode 100644 src/add_spot.c create mode 100644 src/color.c create mode 100644 src/env_list.c create mode 100644 src/main.c create mode 100644 src/maths.c create mode 100644 src/maths_cone.c create mode 100644 src/maths_cylinder.c create mode 100644 src/maths_plane.c create mode 100644 src/maths_sphere.c create mode 100644 src/mlx.c create mode 100644 src/mlx_events.c create mode 100644 src/parse.c create mode 100644 src/phong.c create mode 100644 src/ray.c create mode 100644 src/raytrace.c create mode 100644 src/rtv1.h create mode 100644 src/scene_params.c create mode 100644 src/set_camera.c create mode 100644 src/vec.c create mode 100644 src/vec_arithmetics.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..373f853 --- /dev/null +++ b/Makefile @@ -0,0 +1,86 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: gbrochar +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/02/06 12:13:51 by gbrochar #+# #+# # +# Updated: 2019/02/22 09:14:49 by gbrochar ### ########.fr # +# # +# **************************************************************************** # + +NAME = rtv1 + +SRC_FILE = main.c \ + parse.c \ + mlx.c \ + mlx_events.c \ + color.c \ + vec.c \ + vec_arithmetics.c \ + env_list.c \ + set_camera.c \ + scene_params.c \ + add_spot.c \ + add_plane.c \ + add_sphere.c \ + add_cylinder.c \ + add_cone.c \ + raytrace.c \ + ray.c \ + phong.c \ + maths.c \ + maths_plane.c \ + maths_sphere.c \ + maths_cylinder.c \ + maths_cone.c \ + +OBJ_FILE = $(SRC_FILE:.c=.o) + +SRC_DIR = src/ +OBJ_DIR = obj/ + +SRC = $(addprefix $(SRC_DIR), $(SRC_FILE)) +OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILE)) + +LIB = libft/libft.a -lm -lmlx -framework Appkit -framework OpenGL + +CC = gcc + +CFLAGS = -Wall -Werror -Wextra + +RED = \033[31m +GREEN = \033[32m +YELLOW = \033[33m +BLUE = \033[34m +CYAN = \033[36m +WHITE = \033[0m + +all: $(NAME) + +$(NAME): $(OBJ) $(SRC_DIR)rtv1.h + @make -C libft + @$(CC) $(CFLAGS) -c $(SRC) + @mv $(OBJ_FILE) $(OBJ_DIR) + @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) $(LIB) + @echo "$(GREEN)[OK]$(WHITE) RTv1" + +$(OBJ_DIR)%.o: $(SRC_DIR)%.c libft/ $(SRC_DIR)rtv1.h + @if [ ! -d ./obj ]; then \ + mkdir -p ./obj; \ + fi; + @$(CC) $(CFLAGS) -I libft/ -o $@ -c $< + @echo "$(CYAN)[CC]$(WHITE) $<" + +clean: + @make -C libft clean + @rm -rf $(OBJ_DIR) + +fclean: clean + @make -C libft fclean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/README.md b/README.md index 4dd78cb..db9c003 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # rtv1 -rtv1 : a basic raytracing engine made using minilibx. It features Phong shading, planes, spheres, cones and cylinders. \ No newline at end of file +rtv1 : a basic raytracing engine made using minilibx. It features Phong shading, planes, spheres, cones and cylinders. + +You need to have to minilibx installed to run it. It will not compile on linux systems. diff --git a/auteur b/auteur new file mode 100644 index 0000000..b363db4 --- /dev/null +++ b/auteur @@ -0,0 +1 @@ +gbrochar diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..2568650 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,122 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: gbrochar +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2015/11/27 19:26:17 by gbrochar #+# #+# # +# Updated: 2019/02/22 10:07:07 by gbrochar ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a + +SRC = ft_atoi.c \ + ft_atof.c \ + ft_bzero.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_isspace.c \ + ft_itoa.c \ + ft_lstadd.c \ + ft_lstdel.c \ + ft_lstdelone.c \ + ft_lstiter.c \ + ft_lstmap.c \ + ft_lstnew.c \ + ft_memalloc.c \ + ft_memccpy.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memdel.c \ + ft_memmove.c \ + ft_memset.c \ + ft_power.c \ + ft_putchar.c \ + ft_putchar_fd.c \ + ft_putendl.c \ + ft_putendl_fd.c \ + ft_putnbr.c \ + ft_putnbr_fd.c \ + ft_putstr.c \ + ft_putstr_fd.c \ + ft_sort_int.c \ + ft_sqrt.c \ + ft_strcat.c \ + ft_strchr.c \ + ft_strclr.c \ + ft_strcmp.c \ + ft_strcpy.c \ + ft_strdel.c \ + ft_strdup.c \ + ft_strequ.c \ + ft_striter.c \ + ft_striteri.c \ + ft_strjoin.c \ + ft_strlcat.c \ + ft_strlen.c \ + ft_strmap.c \ + ft_strmapi.c \ + ft_strncat.c \ + ft_strncmp.c \ + ft_strncpy.c \ + ft_strnequ.c \ + ft_strnew.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_strsplit.c \ + ft_strstr.c \ + ft_strsub.c \ + ft_strtrim.c \ + ft_swap.c \ + ft_tolower.c \ + ft_toupper.c \ + ft_lstadd_preview.c \ + ft_get_last_node.c \ + ft_ishexdigit.c \ + ft_atoi_len.c \ + ft_atoi_color.c \ + ft_error.c \ + get_next_line.c \ + ft_strtab_isdigit.c \ + ft_atoi_split.c \ + +OBJ = $(SRC:.c=.o) + +CC = gcc + +CFLAGS = # -Wall -Werror -Wextra + +RED = \033[31m +GREEN = \033[32m +YELLOW = \033[33m +BLUE = \033[34m +CYAN = \033[36m +WHITE= \033[0m + +all: $(NAME) + +$(NAME): $(OBJ) libft.h get_next_line.h + @$(CC) $(CFLAGS) -c $(SRC) + @ar rc $(NAME) $(OBJ) + @ranlib $(NAME) + @echo "$(GREEN)[OK]$(WHITE) Libft.a" + +%.o: %.c libft.h get_next_line.h + @$(CC) $(CFLAGS) -o $@ -c $< + @echo "$(CYAN)[OK]$(WHITE) $<" + +clean: + @rm -f $(OBJ) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/libft/ft_atof.c b/libft/ft_atof.c new file mode 100644 index 0000000..cbd3fe0 --- /dev/null +++ b/libft/ft_atof.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 14:47:22 by gbrochar #+# #+# */ +/* Updated: 2019/02/21 15:34:04 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +double ft_atof(const char *str) +{ + char *tmp; + double floating_part; + double integer_part; + + integer_part = ft_atoi(str); + floating_part = 0; + tmp = ft_strchr(str, '.'); + if (tmp) + { + floating_part = ft_atoi(++tmp); + while (*tmp >= '0' && *tmp <= '9') + { + floating_part /= (double)10; + tmp++; + } + } + if (integer_part < 0) + return (-1.0 * (fabs(integer_part) + floating_part)); + return (integer_part + floating_part); +} diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..fc7ac10 --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 19:18:43 by gbrochar #+# #+# */ +/* Updated: 2019/02/06 16:16:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi(const char *str) +{ + long result; + long sign; + + result = 0; + while (ft_isspace(*str)) + str++; + sign = (*str == '-') ? -1 : 1; + if (*str == '-' || *str == '+') + str++; + while (*str >= '0' && *str <= '9') + { + result = (result * 10) + (*str - '0'); + str++; + } + return ((int)(result * sign)); +} diff --git a/libft/ft_atoi_color.c b/libft/ft_atoi_color.c new file mode 100644 index 0000000..598c3cb --- /dev/null +++ b/libft/ft_atoi_color.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 19:18:43 by gbrochar #+# #+# */ +/* Updated: 2016/05/06 10:25:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi_color(char *hexa_color) +{ + int i; + int result; + + result = 0; + i = 5; + while (i >= 0) + { + if (hexa_color[i] >= '0' && hexa_color[i] <= '9') + result += (hexa_color[i] - '0') * ft_power(16, (6 - i)); + else if (hexa_color[i] >= 'A' && hexa_color[i] <= 'F') + result += (hexa_color[i] - '0') * ft_power(16, (6 - i)); + else + return (-1); + i--; + } + return (result); +} diff --git a/libft/ft_atoi_len.c b/libft/ft_atoi_len.c new file mode 100644 index 0000000..b52b7ce --- /dev/null +++ b/libft/ft_atoi_len.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 19:18:43 by gbrochar #+# #+# */ +/* Updated: 2016/05/06 11:45:55 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi_len(char *str, int *len) +{ + int result; + int sign; + + result = 0; + *len = 0; + sign = (*str == '-') ? -1 : 1; + if (*str == '-' || *str == '+') + { + str++; + len++; + } + while (*str >= '0' && *str <= '9') + { + result = (result * 10) + (*str - '0'); + str++; + len++; + } + return (result * sign); +} diff --git a/libft/ft_atoi_split.c b/libft/ft_atoi_split.c new file mode 100644 index 0000000..fab103c --- /dev/null +++ b/libft/ft_atoi_split.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/05/31 08:52:54 by gbrochar #+# #+# */ +/* Updated: 2016/05/31 09:15:51 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int *ft_atoi_split(char *str, char c, int *tab_len) +{ + int *tab; + char **str_tab; + int i; + + if (!str || ft_isdigit(c)) + return (NULL); + str_tab = ft_strsplit(str, c); + i = 0; + while (str_tab[i]) + ++i; + if (!(tab = (int *)malloc((i + 1) * sizeof(int)))) + return (NULL); + *tab_len = i; + i = -1; + while (str_tab[++i]) + tab[i] = atoi(str_tab[i]); + return (tab); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100644 index 0000000..e2131b2 --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 11:39:18 by gbrochar #+# #+# */ +/* Updated: 2015/11/24 20:46:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + char *byte; + + byte = s; + while (n-- > 0) + { + *byte = 0; + byte++; + } +} diff --git a/libft/ft_error.c b/libft/ft_error.c new file mode 100644 index 0000000..e96e1ef --- /dev/null +++ b/libft/ft_error.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/05/10 02:34:40 by gbrochar #+# #+# */ +/* Updated: 2016/05/11 10:55:43 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_error(char *str) +{ + ft_putstr_fd(str, 2); + exit(0); +} diff --git a/libft/ft_get_last_node.c b/libft/ft_get_last_node.c new file mode 100644 index 0000000..ef59d02 --- /dev/null +++ b/libft/ft_get_last_node.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_last_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/28 07:10:21 by gbrochar #+# #+# */ +/* Updated: 2016/02/28 07:12:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_get_last_node(t_list **alst) +{ + t_list *tmp; + + tmp = *alst; + while (tmp->next) + tmp = tmp->next; + return (tmp); +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100644 index 0000000..f21a2cc --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:51:03 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:22:14 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100644 index 0000000..e4f61d6 --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:59:08 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:42:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return (((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? 1 : 0)); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100644 index 0000000..c7daff9 --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:48:25 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:32:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return ((c >= 0 && c <= 127) ? 1 : 0); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100644 index 0000000..e850324 --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:53:19 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 14:41:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return ((c >= '0' && c <= '9') ? 1 : 0); +} diff --git a/libft/ft_ishexdigit.c b/libft/ft_ishexdigit.c new file mode 100644 index 0000000..e4b4691 --- /dev/null +++ b/libft/ft_ishexdigit.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ishexdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/05/05 23:22:51 by gbrochar #+# #+# */ +/* Updated: 2016/05/05 23:24:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_ishexdigit(int c) +{ + if (ft_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) + return (1); + return (0); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100644 index 0000000..72badb5 --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:44:48 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:33:24 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return ((c >= 32 && c <= 126) ? 1 : 0); +} diff --git a/libft/ft_isspace.c b/libft/ft_isspace.c new file mode 100644 index 0000000..89eb16a --- /dev/null +++ b/libft/ft_isspace.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 14:37:03 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 14:39:18 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(int c) +{ + if (c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r' + || c == ' ') + return (1); + return (0); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100644 index 0000000..26ee52e --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:56:59 by gbrochar #+# #+# */ +/* Updated: 2019/02/06 16:17:43 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_itoa(int n) +{ + char *str; + size_t str_size; + int n_mem; + + n_mem = n; + str_size = (n < 0) ? 3 : 2; + while ((n > 9 || n < -9) && str_size++) + n /= 10; + str = (char *)malloc((str_size--) * sizeof(char)); + if (!str) + return (NULL); + str[str_size--] = '\0'; + while (n_mem > 9 || n_mem < -9) + { + str[str_size--] = (n_mem < 0) ? -(n_mem % 10) + '0' : n_mem % 10 + '0'; + n_mem = n_mem / 10; + } + str[0] = (n_mem < 0) ? '-' : ((char)n_mem + '0'); + str[1] = (n_mem < 0) ? ((char)-n_mem + '0') : str[1]; + return (str); +} diff --git a/libft/ft_lstadd.c b/libft/ft_lstadd.c new file mode 100644 index 0000000..104dee9 --- /dev/null +++ b/libft/ft_lstadd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/01 14:24:17 by gbrochar #+# #+# */ +/* Updated: 2016/02/26 14:02:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + t_list *tmp; + + tmp = *alst; + while (tmp->next != NULL) + tmp = tmp->next; + tmp->next = new; +} diff --git a/libft/ft_lstadd_preview.c b/libft/ft_lstadd_preview.c new file mode 100644 index 0000000..6fd80c0 --- /dev/null +++ b/libft/ft_lstadd_preview.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_preview.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/01 14:24:17 by gbrochar #+# #+# */ +/* Updated: 2016/02/26 14:01:33 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_preview(t_list **alst, t_list *new) +{ + new->next = *alst; + *alst = new; +} diff --git a/libft/ft_lstdel.c b/libft/ft_lstdel.c new file mode 100644 index 0000000..9c02c08 --- /dev/null +++ b/libft/ft_lstdel.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/30 21:34:50 by gbrochar #+# #+# */ +/* Updated: 2015/12/05 15:10:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +{ + t_list *tmp; + + while (*alst) + { + tmp = *alst; + ft_lstdelone(alst, del); + *alst = tmp->next; + } +} diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c new file mode 100644 index 0000000..3448e9a --- /dev/null +++ b/libft/ft_lstdelone.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/30 21:27:05 by gbrochar #+# #+# */ +/* Updated: 2015/12/02 13:28:32 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + del((*alst)->content, (*alst)->content_size); + ft_memdel((void **)alst); +} diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c new file mode 100644 index 0000000..5cddff7 --- /dev/null +++ b/libft/ft_lstiter.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/02 13:38:16 by gbrochar #+# #+# */ +/* Updated: 2015/12/04 16:41:19 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + t_list *start; + t_list *tmp; + + start = lst; + while (lst->next) + { + tmp = lst; + f(lst); + lst = tmp->next; + } + f(lst); + lst = start; +} diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c new file mode 100644 index 0000000..8341e24 --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/02 14:33:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/02 16:17:18 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *lst_begin; + t_list *lst_new; + + lst_begin = ft_lstnew((f(lst))->content, (f(lst))->content_size); + lst_new = lst_begin; + lst = lst->next; + while (lst->next) + { + lst_new->next = ft_lstnew((f(lst))->content, (f(lst))->content_size); + lst_new = lst_new->next; + lst = lst->next; + } + lst_new->next = ft_lstnew((f(lst))->content, (f(lst))->content_size); + return (lst_begin); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100644 index 0000000..9413638 --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/30 21:21:09 by gbrochar #+# #+# */ +/* Updated: 2016/02/27 21:04:51 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *lst; + + lst = (t_list *)malloc(sizeof(t_list)); + if (!lst) + return (NULL); + if (!content) + { + lst->content = NULL; + lst->content_size = 0; + } + else + { + lst->content = ft_memalloc(content_size); + if (!lst->content) + { + free(lst); + return (NULL); + } + ft_memmove(lst->content, content, content_size); + lst->content_size = content_size; + } + lst->next = NULL; + return (lst); +} diff --git a/libft/ft_memalloc.c b/libft/ft_memalloc.c new file mode 100644 index 0000000..374edbf --- /dev/null +++ b/libft/ft_memalloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 18:11:36 by gbrochar #+# #+# */ +/* Updated: 2016/02/27 21:04:55 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + unsigned char *ptr; + size_t i; + + i = 0; + ptr = (unsigned char *)malloc(size); + if (!ptr) + return (NULL); + while (i < size) + ptr[i++] = 0; + return (ptr); +} diff --git a/libft/ft_memccpy.c b/libft/ft_memccpy.c new file mode 100644 index 0000000..af8cfb1 --- /dev/null +++ b/libft/ft_memccpy.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 14:40:00 by gbrochar #+# #+# */ +/* Updated: 2019/02/06 16:20:23 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memccpy(void *dst, const void *src, int c, size_t n) +{ + size_t i; + unsigned char to_find; + unsigned char *dst_byte; + const unsigned char *src_byte; + + to_find = (unsigned char)c; + i = 0; + dst_byte = (unsigned char *)dst; + src_byte = (const unsigned char *)src; + while (i != n && src_byte[i] != to_find) + { + dst_byte[i] = src_byte[i]; + i++; + } + if (src_byte[i] == to_find) + { + dst_byte[i] = to_find; + return ((void *)&dst_byte[i + 1]); + } + return (NULL); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100644 index 0000000..98b9a18 --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 13:03:54 by gbrochar #+# #+# */ +/* Updated: 2019/02/06 16:23:56 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + unsigned char to_find; + unsigned char *str; + size_t i; + + i = 0; + to_find = c; + str = (unsigned char *)s; + while (i < n) + { + if (str[i] == to_find) + return ((void *)&s[i]); + i++; + } + return (NULL); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100644 index 0000000..18aa48e --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:13:46 by gbrochar #+# #+# */ +/* Updated: 2015/11/30 14:46:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *s1_byte; + unsigned char *s2_byte; + size_t i; + + s1_byte = (unsigned char *)s1; + s2_byte = (unsigned char *)s2; + i = 0; + if (n <= 0) + return (0); + while (s1_byte[i] == s2_byte[i]) + { + if (i + 1 == n) + return (0); + i++; + } + return (s1_byte[i] - s2_byte[i]); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100644 index 0000000..8a42505 --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:42:55 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 14:43:02 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + unsigned char *dst_byte; + const unsigned char *src_byte; + + i = 0; + dst_byte = dst; + src_byte = src; + while (i < n) + { + dst_byte[i] = src_byte[i]; + i++; + } + return (dst); +} diff --git a/libft/ft_memdel.c b/libft/ft_memdel.c new file mode 100644 index 0000000..cd84e74 --- /dev/null +++ b/libft/ft_memdel.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 18:23:10 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 18:25:10 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memdel(void **ap) +{ + free(*ap); + *ap = NULL; +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100644 index 0000000..ce42e2a --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 12:36:04 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 13:03:31 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + unsigned char *dst_byte; + const unsigned char *src_byte; + size_t index; + + dst_byte = dst; + src_byte = src; + if (dst_byte > src_byte) + { + index = len; + while (index-- > 0) + dst_byte[index] = src_byte[index]; + } + else + { + index = 0; + while (index < len) + { + dst_byte[index] = src_byte[index]; + index++; + } + } + return (dst); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100644 index 0000000..8606775 --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/23 17:53:16 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 12:18:32 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + unsigned char *byte; + size_t i; + + byte = b; + i = 0; + while (i++ < len) + { + *byte = c; + byte++; + } + return (b); +} diff --git a/libft/ft_power.c b/libft/ft_power.c new file mode 100644 index 0000000..130d4a4 --- /dev/null +++ b/libft/ft_power.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_power.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/05 15:23:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:03:41 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_power(int nb, int pow) +{ + int ret; + + ret = 1; + if (pow < 0) + return (-1); + while (pow--) + ret *= nb; + return (ret); +} diff --git a/libft/ft_putchar.c b/libft/ft_putchar.c new file mode 100644 index 0000000..82125ba --- /dev/null +++ b/libft/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 12:01:00 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 16:47:24 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c new file mode 100644 index 0000000..4fc2ee7 --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:01:19 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:02:06 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libft/ft_putendl.c b/libft/ft_putendl.c new file mode 100644 index 0000000..52a3be5 --- /dev/null +++ b/libft/ft_putendl.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:04:13 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:04:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char const *s) +{ + ft_putstr(s); + ft_putchar('\n'); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100644 index 0000000..de5409b --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:04:58 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:05:51 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libft/ft_putnbr.c b/libft/ft_putnbr.c new file mode 100644 index 0000000..7e6ab30 --- /dev/null +++ b/libft/ft_putnbr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 08:58:30 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:49:28 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + if (n == -2147483648) + ft_putstr("-2147483648"); + else if (n < 0) + { + ft_putchar('-'); + ft_putnbr(-n); + } + else if (n > 9) + { + ft_putnbr(n / 10); + ft_putchar(n % 10 + '0'); + } + else + ft_putchar(n + '0'); +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..070cfd2 --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:10:52 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:49:38 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + ft_putstr_fd("-2147483648", fd); + else if (n < 0) + { + ft_putchar_fd('-', fd); + ft_putnbr_fd(-n, fd); + } + else if (n > 9) + { + ft_putnbr_fd(n / 10, fd); + ft_putchar_fd(n % 10 + '0', fd); + } + else + ft_putchar_fd(n + '0', fd); +} diff --git a/libft/ft_putstr.c b/libft/ft_putstr.c new file mode 100644 index 0000000..a21c751 --- /dev/null +++ b/libft/ft_putstr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:23:12 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:06:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + while (*s) + write(1, s++, 1); +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100644 index 0000000..a7d909a --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:02:48 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:04:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char const *s, int fd) +{ + while (*s) + write(fd, s++, 1); +} diff --git a/libft/ft_sort_int.c b/libft/ft_sort_int.c new file mode 100644 index 0000000..9ed36cd --- /dev/null +++ b/libft/ft_sort_int.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sort_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/05 18:36:08 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:04:19 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sort_int(int *tab, int size) +{ + int i; + int tmp; + + i = 0; + while ((i + 1) < size) + { + if (tab[i] > tab[i + 1]) + { + tmp = tab[i]; + tab[i] = tab[i + 1]; + tab[i + 1] = tmp; + i = 0; + } + else + i++; + } +} diff --git a/libft/ft_sqrt.c b/libft/ft_sqrt.c new file mode 100644 index 0000000..7d71faf --- /dev/null +++ b/libft/ft_sqrt.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sqrt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/07 08:28:11 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 08:50:43 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +long ft_sqrt(long n) +{ + long sqrt; + + sqrt = 0; + while ((sqrt * sqrt) != n && (sqrt * sqrt) < n) + sqrt++; + return (((sqrt * sqrt) == n) ? (sqrt) : (-1)); +} diff --git a/libft/ft_strcat.c b/libft/ft_strcat.c new file mode 100644 index 0000000..b0b3df0 --- /dev/null +++ b/libft/ft_strcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 16:26:53 by gbrochar #+# #+# */ +/* Updated: 2016/04/09 21:24:58 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *s1, const char *s2) +{ + char *p_s1; + const char *p_s2; + + p_s2 = s2; + p_s1 = s1; + while (*p_s1) + p_s1++; + while (*p_s2) + { + *p_s1 = *p_s2; + p_s1++; + p_s2++; + } + *p_s1 = '\0'; + return (s1); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100644 index 0000000..c257f96 --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 09:50:37 by gbrochar #+# #+# */ +/* Updated: 2019/02/08 11:50:45 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + if (s == NULL) + return (0); + while (*s != (char)c) + if (!*s++) + return (0); + return ((char *)s); +} diff --git a/libft/ft_strclr.c b/libft/ft_strclr.c new file mode 100644 index 0000000..6f9dc0a --- /dev/null +++ b/libft/ft_strclr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 20:22:52 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 21:09:55 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + while (*s) + *s++ = '\0'; +} diff --git a/libft/ft_strcmp.c b/libft/ft_strcmp.c new file mode 100644 index 0000000..1d70b10 --- /dev/null +++ b/libft/ft_strcmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 14:52:37 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:34:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + while (*s1 || *s2) + if (*s1++ != *s2++) + return ((unsigned char)*--s1 - (unsigned char)*--s2); + return (0); +} diff --git a/libft/ft_strcpy.c b/libft/ft_strcpy.c new file mode 100644 index 0000000..93f2ec1 --- /dev/null +++ b/libft/ft_strcpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 15:10:54 by gbrochar #+# #+# */ +/* Updated: 2016/04/09 16:49:30 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dst, const char *src) +{ + char *p_dest; + const char *p_src; + + p_src = src; + p_dest = dst; + while (*p_src) + { + *p_dest = *p_src; + p_dest++; + p_src++; + } + *p_dest = '\0'; + return (dst); +} diff --git a/libft/ft_strdel.c b/libft/ft_strdel.c new file mode 100644 index 0000000..75872b1 --- /dev/null +++ b/libft/ft_strdel.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 20:21:47 by gbrochar #+# #+# */ +/* Updated: 2015/12/08 15:21:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strdel(char **as) +{ + ft_memdel((void **)as); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100644 index 0000000..4b12a2d --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:47:23 by gbrochar #+# #+# */ +/* Updated: 2016/04/09 21:27:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *dest; + int i; + + i = 0; + dest = ft_strnew(ft_strlen(s1)); + while (s1[i]) + { + dest[i] = s1[i]; + i++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/libft/ft_strequ.c b/libft/ft_strequ.c new file mode 100644 index 0000000..0e229a6 --- /dev/null +++ b/libft/ft_strequ.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 08:35:37 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 15:47:45 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + int i; + + i = 0; + while (s1[i] == s2[i]) + { + if (s1[i] == '\0' && s2[i] == '\0') + return (1); + i++; + } + return (0); +} diff --git a/libft/ft_striter.c b/libft/ft_striter.c new file mode 100644 index 0000000..4748141 --- /dev/null +++ b/libft/ft_striter.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 20:37:55 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:51:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striter(char *s, void (*f)(char *)) +{ + int i; + + i = 0; + while (s[i]) + { + f(&s[i]); + i++; + } +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100644 index 0000000..bb633d0 --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 21:02:16 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:30:40 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *s)) +{ + unsigned int i; + + i = 0; + while (s[i]) + { + f(i, &s[i]); + i++; + } +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..6bd8cf1 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:48:11 by gbrochar #+# #+# */ +/* Updated: 2016/04/09 16:30:06 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, const char *s2) +{ + size_t i; + char *new; + + s1 = (s1 == NULL) ? "" : s1; + s2 = (s2 == NULL) ? "" : s2; + if (s1 && s2) + { + i = ft_strlen(s1) + ft_strlen(s2); + new = ft_strnew(i); + if (new) + { + new = ft_strcpy(new, s1); + new = ft_strcat(new, s2); + return (new); + } + } + return (NULL); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100644 index 0000000..9823fd5 --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/26 08:00:59 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 15:48:23 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t dst_len; + size_t src_len; + size_t i_src; + size_t i_dst; + + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + i_src = 0; + i_dst = dst_len; + if (dst_len >= size) + return (size + src_len); + while (src[i_src] && (size-- - (dst_len + 1)) > 0) + dst[i_dst++] = src[i_src++]; + dst[i_dst] = '\0'; + return (dst_len + src_len); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..52e4f06 --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:43:32 by gbrochar #+# #+# */ +/* Updated: 2016/04/09 16:57:01 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t i; + + i = 0; + while (s && s[i]) + i++; + return (i); +} diff --git a/libft/ft_strmap.c b/libft/ft_strmap.c new file mode 100644 index 0000000..fac7843 --- /dev/null +++ b/libft/ft_strmap.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 21:30:34 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 16:53:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + char *str; + int i; + int len; + + len = ft_strlen(s); + i = 0; + str = (char *)malloc((len + 1) * sizeof(char)); + if (!str) + return (NULL); + while (i < len) + { + str[i] = f(s[i]); + i++; + } + str[len] = '\0'; + return (str); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100644 index 0000000..a83e346 --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 21:39:30 by gbrochar #+# #+# */ +/* Updated: 2015/12/02 16:22:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *str; + unsigned int i; + + i = 0; + str = (char *)malloc((ft_strlen(s) + 1) * sizeof(char)); + if (!str) + return (NULL); + while (s[i]) + { + str[i] = f(i, s[i]); + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/ft_strncat.c b/libft/ft_strncat.c new file mode 100644 index 0000000..9a4b9eb --- /dev/null +++ b/libft/ft_strncat.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 19:29:04 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 12:33:35 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *s1, const char *s2, size_t n) +{ + size_t i_dst; + size_t i_src; + + i_dst = ft_strlen(s1); + i_src = 0; + while (i_src < n && s2[i_src] != '\0') + { + s1[i_dst] = s2[i_src]; + i_dst++; + i_src++; + } + s1[i_dst] = '\0'; + return (s1); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..db3be20 --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 14:57:36 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:34:45 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + while ((*s1 || *s2) && n--) + if (*s1++ != *s2++) + return ((unsigned char)*--s1 - (unsigned char)*--s2); + return (0); +} diff --git a/libft/ft_strncpy.c b/libft/ft_strncpy.c new file mode 100644 index 0000000..173165b --- /dev/null +++ b/libft/ft_strncpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 16:18:18 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 16:21:09 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dst, const char *src, size_t n) +{ + size_t i; + + i = 0; + while (i != n && src[i] != '\0') + { + dst[i] = src[i]; + i++; + } + while (i != n) + { + dst[i] = '\0'; + i++; + } + return (dst); +} diff --git a/libft/ft_strnequ.c b/libft/ft_strnequ.c new file mode 100644 index 0000000..ece7ced --- /dev/null +++ b/libft/ft_strnequ.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 08:37:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/05 16:24:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + size_t i; + + i = 0; + while (s1[i] == s2[i] || i == n) + { + if ((s1[i] == '\0' && s2[i] == '\0') || i == n) + return (1); + i++; + } + return (0); +} diff --git a/libft/ft_strnew.c b/libft/ft_strnew.c new file mode 100644 index 0000000..2c8ca40 --- /dev/null +++ b/libft/ft_strnew.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 18:26:56 by gbrochar #+# #+# */ +/* Updated: 2016/04/09 16:25:44 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnew(size_t size) +{ + char *str; + + str = (char *)malloc(sizeof(char) * (size + 1)); + if (!str) + return (NULL); + return (str); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..1982911 --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 12:49:32 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 16:39:13 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *s1, char *s2, size_t n) +{ + size_t i; + size_t i_mem; + size_t i_tofind; + + i_mem = 0; + if (s2[0] == '\0') + return ((char *)s1); + while (s1[i_mem] != '\0' && i_mem < n) + { + i_tofind = 0; + while (s1[i_mem] != s2[i_tofind] && s1[i_mem] != '\0') + i_mem++; + if (s1[i_mem] == '\0') + return (NULL); + i = i_mem; + while ((s1[i] == s2[i_tofind] || s2[i_tofind] == '\0') && i++ <= n) + if (s2[i_tofind++] == '\0') + return ((char *)&s1[i_mem]); + if (i > n) + return (NULL); + i_mem++; + } + return (NULL); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100644 index 0000000..e295607 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 10:05:56 by gbrochar #+# #+# */ +/* Updated: 2015/11/27 10:24:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + + i = ft_strlen(s); + if (c) + { + while (i--) + if (s[i] == (char)c) + return ((char *)&s[i]); + return (NULL); + } + return ((char *)&s[i]); +} diff --git a/libft/ft_strsplit.c b/libft/ft_strsplit.c new file mode 100644 index 0000000..0784026 --- /dev/null +++ b/libft/ft_strsplit.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:55:10 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:00:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_wordcount(char const *s, char c) +{ + int word_count; + int i; + + i = 0; + word_count = 0; + while (s[i] != '\0') + { + while (s[i] == c) + i++; + if (s[i] != '\0') + word_count++; + while (s[i] != c && s[i] != '\0') + i++; + } + return (word_count); +} + +static void *ft_taballoc(char const *s, char **str_tab, char c) +{ + int i; + int i2; + int wl; + + i = 0; + i2 = 0; + while (s[i2]) + { + wl = 0; + while (s[i2] == c && s[i2] != '\0') + i2++; + while (s[i2] != c && s[i2] != '\0') + { + wl++; + i2++; + } + if (wl != 0) + *str_tab++ = (char *)malloc((wl + 1) * sizeof(char)); + if (!(str_tab - 1)) + return (NULL); + } + return (*str_tab); +} + +char **ft_strsplit(char const *s, char c) +{ + char **str_tab; + int i; + int i2; + int i3; + int wc; + + i = 0; + i3 = 0; + wc = ft_wordcount(s, c); + str_tab = (char **)malloc((wc + 1) * sizeof(char *)); + if (!str_tab) + return (NULL); + ft_taballoc(s, str_tab, c); + while (i != wc) + { + i2 = 0; + while (s[i3] == c && s[i3] != '\0') + i3++; + while (s[i3] != c && s[i3] != '\0') + str_tab[i][i2++] = s[i3++]; + str_tab[i][i2] = '\0'; + i++; + } + str_tab[i] = NULL; + return (str_tab); +} diff --git a/libft/ft_strstr.c b/libft/ft_strstr.c new file mode 100644 index 0000000..5f985f0 --- /dev/null +++ b/libft/ft_strstr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 10:24:53 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 16:38:40 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *s1, char *s2) +{ + int i; + int i_mem; + int i_tofind; + + i_mem = 0; + if (s2[0] == '\0') + return ((char *)s1); + while (s1[i_mem] != '\0') + { + i_tofind = 0; + while (s1[i_mem] != s2[i_tofind] && s1[i_mem] != '\0') + i_mem++; + if (s1[i_mem] == '\0') + return (NULL); + i = i_mem; + while (s1[i++] == s2[i_tofind] || s2[i_tofind] == '\0') + if (s2[i_tofind++] == '\0') + return ((char *)&s1[i_mem]); + i_mem++; + } + return (NULL); +} diff --git a/libft/ft_strsub.c b/libft/ft_strsub.c new file mode 100644 index 0000000..4434689 --- /dev/null +++ b/libft/ft_strsub.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:14:36 by gbrochar #+# #+# */ +/* Updated: 2015/12/08 15:34:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *str; + size_t i; + + i = 0; + str = (char *)malloc((len + 1) * sizeof(char)); + if (!str) + return (NULL); + while (i < len) + str[i++] = s[start++]; + str[i] = '\0'; + return (str); +} diff --git a/libft/ft_strtab_isdigit.c b/libft/ft_strtab_isdigit.c new file mode 100644 index 0000000..4bca658 --- /dev/null +++ b/libft/ft_strtab_isdigit.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtab_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/05/31 08:48:47 by gbrochar #+# #+# */ +/* Updated: 2016/05/31 08:50:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strtab_isdigit(char **strtab) +{ + int i; + int j; + + i = 0; + while (strtab[i]) + { + j = 0; + while (strtab[i][j]) + { + if (strtab[i][j] != ' ' && ft_isdigit(strtab[i][j] == 0)) + return (0); + j++; + } + i++; + } + return (1); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..c3480c6 --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:54:12 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:00:33 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s) +{ + char *str; + size_t i; + size_t i2; + size_t ws; + + i = ft_strlen(s) - 1; + i2 = 0; + ws = 0; + while ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t') && i--) + ws++; + i = 0; + while ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t') && ws < ft_strlen(s)) + { + i++; + ws++; + } + str = (char *)malloc((ft_strlen(s) + 1 - ws) * sizeof(char)); + if (!str) + return (NULL); + while (i2 < (ft_strlen(s) - ws)) + str[i2++] = s[i++]; + str[i2] = '\0'; + return (str); +} diff --git a/libft/ft_swap.c b/libft/ft_swap.c new file mode 100644 index 0000000..5a99a20 --- /dev/null +++ b/libft/ft_swap.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/07 09:44:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:01:13 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_swap(int *a, int *b) +{ + int c; + + c = *a; + *a = *b; + *b = c; +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100644 index 0000000..22b7a20 --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:39:55 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 08:40:07 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if ((int)c >= 'A' && (int)c <= 'Z') + c += 32; + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100644 index 0000000..f0f24ec --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:43:24 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 16:01:01 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if ((int)c >= 'a' && (int)c <= 'z') + c -= 32; + return (c); +} diff --git a/libft/get_next_line.c b/libft/get_next_line.c new file mode 100644 index 0000000..4706e86 --- /dev/null +++ b/libft/get_next_line.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/03/22 00:00:51 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 10:11:53 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +char *ft_strjoin2(char *str, char *str2) +{ + char *tmp; + + tmp = ft_strjoin(str, str2); + free(str); + return (tmp); +} + +int gnl_manage_return(char **line, char **stock) +{ + char *tmp; + char *tmp2; + + if ((tmp = ft_strchr(*stock, '\n'))) + { + *tmp = '\0'; + tmp2 = ft_strdup(tmp + 1); + *line = ft_strdup(*stock); + ft_memdel((void **)stock); + *stock = ft_strdup(tmp2); + free(tmp2); + tmp2 = NULL; + tmp = NULL; + return (1); + } + if (ft_strlen(*stock) == 0) + { + ft_memdel((void **)stock); + return (0); + } + *line = ft_strdup(*stock); + ft_memdel((void **)stock); + return (1); +} + +int gnl_manage_read(char **line, char **stock) +{ + char *tmp; + char *tmp2; + + if ((tmp = ft_strchr(*stock, '\n'))) + { + *tmp = '\0'; + tmp2 = ft_strdup(tmp + 1); + *line = ft_strdup(*stock); + ft_memdel((void **)stock); + *stock = ft_strdup(tmp2); + free(tmp2); + tmp2 = NULL; + tmp = NULL; + return (1); + } + return (0); +} + +int get_next_line(int fd, char **line, int error) +{ + static char *stock = NULL; + char buffer[BUFF_SIZE + 1]; + int ret; + + if (stock && error) + ft_memdel((void **)&stock); + if (error) + return (-1); + buffer[0] = '\0'; + if (stock) + if (gnl_manage_read(line, &stock)) + return (1); + while ((ret = read(fd, buffer, BUFF_SIZE)) > 0) + { + buffer[ret] = '\0'; + stock = ft_strjoin2(stock, buffer); + if (gnl_manage_read(line, &stock)) + return (1); + } + return (ret < 0 ? ret : gnl_manage_return(line, &stock)); +} diff --git a/libft/get_next_line.h b/libft/get_next_line.h new file mode 100644 index 0000000..1da072b --- /dev/null +++ b/libft/get_next_line.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/28 22:19:58 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 10:03:10 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# include "libft.h" + +# define BUFF_SIZE 512 + +int get_next_line(int const fd, char **line, int error); + +#endif diff --git a/libft/libft.h b/libft/libft.h new file mode 100644 index 0000000..7518939 --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 12:52:28 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 14:24:19 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include +# include "get_next_line.h" +# include + +typedef struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +} t_list; + +t_list *ft_get_last_node(t_list **alst); +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstadd_preview(t_list **alst, t_list *new); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); +int ft_atoi(const char *str); +double ft_atof(const char *str); +int ft_atoi_color(char *hexa_color); +int ft_atoi_len(char *str, int *len); +int *ft_atoi_split(char *str, char c, int *tab_len); +void ft_bzero(void *s, size_t n); +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_strtab_isdigit(char **strtab); +int ft_isprint(int c); +int ft_isspace(int c); +char *ft_itoa(int n); +void *ft_memalloc(size_t size); +void *ft_memccpy(void *dst, const void *src, int c, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void ft_memdel(void **ap); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memset(void *b, int c, size_t len); +int ft_power(int nb, int pow); +void ft_putchar(char c); +void ft_putchar_fd(char c, int fd); +void ft_putendl(char const *s); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr(int n); +void ft_putnbr_fd(int n, int fd); +void ft_putstr(char const *s); +void ft_putstr_fd(char const *s, int fd); +void ft_sort_int(int *tab, int size); +long ft_sqrt(long n); +char *ft_strcat(char *s1, const char *s2); +char *ft_strchr(const char *s, int c); +void ft_strclr(char *s); +int ft_strcmp(const char *s1, const char *s2); +char *ft_strcpy(char *dst, const char *src); +void ft_strdel(char **as); +char *ft_strdup(const char *s1); +int ft_strequ(char const *s1, char const *s2); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *s)); +char *ft_strjoin(char const *s1, char const *s2); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strlen(const char *s); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +char *ft_strncat(char *s1, const char *s2, size_t n); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strncpy(char *dst, const char *src, size_t n); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strnew(size_t size); +char *ft_strnstr(const char *s1, char *s2, size_t n); +char *ft_strrchr(const char *s, int c); +char **ft_strsplit(char const *s, char c); +char *ft_strstr(const char *s1, char *s2); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strtrim(char const *s); +void ft_swap(int *a, int *b); +int ft_tolower(int c); +int ft_toupper(int c); +int ft_ishexdigit(int c); +void ft_error(char *str); + +#endif diff --git a/scenes/cone.scene b/scenes/cone.scene new file mode 100644 index 0000000..8dd6ddd --- /dev/null +++ b/scenes/cone.scene @@ -0,0 +1,7 @@ +CAMERA -5 0 -10 0 0 0 +SPOT -5 5 0 15 +SPOT 5 7 0 15 +CONE -3 0 0 0.2 0.5 0.3 0.15 192 255 80 +CONE 2 1 0 0.2 -0.5 0.3 0.5 80 255 80 +CONE 5 -2 -4 -0.2 0.5 0.3 0.1 255 255 80 +CONE -1 -5 4 0.2 0.5 -0.3 0.1 192 80 80 diff --git a/scenes/new.scene b/scenes/new.scene new file mode 100644 index 0000000..6207334 --- /dev/null +++ b/scenes/new.scene @@ -0,0 +1,8 @@ +CAMERA 0 5 -10 0 0 0 +AMBIENT 0 +SPECULAR 1 1.3 +PLANE 0 -1 0 0 1 0 64 64 64 +PLANE 0 0 1 0 0 -1 64 64 255 +SPHERE 0 0 0 1.5 255 0 0 +CYLIGROSSEPUTENDER -2 -1 0 1 1 0.2 1 64 220 64 +CONE 2 0 0 -1 1 -0.2 0.3 255 255 80 diff --git a/scenes/plane.scene b/scenes/plane.scene new file mode 100644 index 0000000..1c88c1f --- /dev/null +++ b/scenes/plane.scene @@ -0,0 +1,4 @@ +CAMERA 0 3 -10 0 0 0 +SPECULAR 0.1 0 +SPOT 0 5 5 6 +PLANE 0 0 0 0 1 0 255 255 255 diff --git a/scenes/rtv125.scene b/scenes/rtv125.scene new file mode 100644 index 0000000..2e22dcc --- /dev/null +++ b/scenes/rtv125.scene @@ -0,0 +1,10 @@ +CAMERA 3 5 0 0 0 0 +AMBIENT 0 +SPECULAR 3 0.5 +LIGHT 3 4 -5 20 +LIGHT 0 10 0 20 +PLANE 0 -1 0 0 1 0 64 64 64 +PLANE 0 0 1 0 0 -1 64 64 255 +SPHERE 0 0 0 1.5 255 0 0 +CYLINDER -2 -1 0 1 1 0.2 1 64 220 64 +CONE 2 0 0 -1 1 -0.2 0.3 255 255 80 diff --git a/scenes/sphere.scene b/scenes/sphere.scene new file mode 100644 index 0000000..5353fcf --- /dev/null +++ b/scenes/sphere.scene @@ -0,0 +1,4 @@ +CAMERA 10 10 10 0 0 0 +SPECULAR 3 0.2 +LIGHT 5 5 5 15 +SPHERE 0 0 0 3 64 192 92 diff --git a/scenes/sphereup.scene b/scenes/sphereup.scene new file mode 100644 index 0000000..3312084 --- /dev/null +++ b/scenes/sphereup.scene @@ -0,0 +1,4 @@ +CAMERA 0 0 0 0 10 0 +SPHERE 0 10 0 1 130 58 255 +SPECULAR 1.5 1 +SPOT 0 5 1 5 diff --git a/scenes/temple.scene b/scenes/temple.scene new file mode 100644 index 0000000..9e464b8 --- /dev/null +++ b/scenes/temple.scene @@ -0,0 +1,23 @@ +CAMERA 0 4 -23 0 3 0 +SPHERE 0 0 -10 0.8 255 0 0 +// +SPECULAR 0.7 1 +SPOT -14 5 -18 150 +SPOT 14 5 -18 150 +// +PLANE 0 -2 0 0 1 0 128 128 128 +PLANE 0 8 0 0 -1 0 64 64 64 +PLANE 0 0 8 0 0 -1 80 80 255 +// +SPHERE -6 8 -10 1.7 192 192 192 +SPHERE -6 -2 -10 1.7 192 192 192 +CYLINDER -6 0 -10 0 1 0 1 192 192 192 +SPHERE 6 8 -10 1.7 192 192 192 +SPHERE 6 -2 -10 1.7 192 192 192 +CYLINDER 6 0 -10 0 1 0 1 192 192 192 +SPHERE -6 8 2 1.7 192 192 192 +SPHERE -6 -2 2 1.7 192 192 192 +CYLINDER -6 0 2 0 1 0 1 192 192 192 +SPHERE 6 8 2 1.7 192 192 192 +SPHERE 6 -2 2 1.7 192 192 192 +CYLINDER 6 0 2 0 1 0 1 192 192 192 diff --git a/scenes/test.scene b/scenes/test.scene new file mode 100644 index 0000000..f85c742 --- /dev/null +++ b/scenes/test.scene @@ -0,0 +1,9 @@ +CAMERA 0 5 -10 0 0 0 +AMBIENT 0 +SPECULAR 1 0.9 +LIGHT 3 4 -5 30 +PLANE 0 -1 0 0 1 0 64 64 64 +PLANE 0 0 1 0 0 -1 64 64 255 +SPHERE 0 0 0 1.5 255 0 0 +CYLINDER -2 -1 0 1 1 0.2 1 64 220 64 +CONE 2 0 0 -1 1 -0.2 0.3 255 255 80 diff --git a/scenes/test2.scene b/scenes/test2.scene new file mode 100644 index 0000000..504de80 --- /dev/null +++ b/scenes/test2.scene @@ -0,0 +1,8 @@ +CAMERA 3 5 -5 0 0 0 +SPECULAR 0.5 0.2 +SPOT 3 5 -5 30 +PLANE 0 -1 0 0 1 0 64 64 64 +PLANE 0 0 1 0 0 -1 64 64 255 +SPHERE 0 0 0 1.5 255 0 0 +CYLINDER -2 -1 0 1 1 0.2 1 64 220 64 +CONE 2 0 0 -1 1 -0.2 0.3 255 255 80 diff --git a/src/add_cone.c b/src/add_cone.c new file mode 100644 index 0000000..ae824fe --- /dev/null +++ b/src/add_cone.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_cone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/07 21:42:11 by gbrochar #+# #+# */ +/* Updated: 2019/02/21 17:11:56 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void init_cone(t_cone *cone, t_color *c) +{ + cone->o = vec(0, 0, 0); + cone->d = vec(0, 1, 0); + cone->k = 0.15; + *c = color(255, 255, 255); +} + +int add_cone(char **data, t_env *e) +{ + int i; + t_cone *cone; + t_color c; + + i = 0; + ft_putendl("Adding cone"); + if (!(cone = (t_cone *)ft_memalloc(sizeof(t_cone)))) + return (FAILURE); + init_cone(cone, &c); + while (data[i] && i < 11) + i++; + if (i > 3) + cone->o = vec(ft_atof(data[1]), ft_atof(data[2]), ft_atof(data[3])); + if (i > 6) + { + cone->d = vec(ft_atof(data[4]), ft_atof(data[5]), ft_atof(data[6])); + if (FAILURE == check_direction(cone->d)) + return (FAILURE); + normalize(&cone->d); + } + if (i > 7) + cone->k = ft_atof(data[7]); + if (i > 10) + c = color(ft_atoi(data[8]), ft_atoi(data[9]), ft_atoi(data[10])); + return (add_obj_node(e, (void *)cone, CONE, c)); +} diff --git a/src/add_cylinder.c b/src/add_cylinder.c new file mode 100644 index 0000000..617a193 --- /dev/null +++ b/src/add_cylinder.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_cylinder.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/07 19:58:08 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 10:08:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void init_cylinder(t_cylinder *cylinder, t_color *c) +{ + cylinder->o = vec(0, 0, 0); + cylinder->d = vec(0, 1, 0); + cylinder->r = 1; + *c = color(255, 255, 255); +} + +int add_cylinder(char **data, t_env *e) +{ + int i; + t_cylinder *cylinder; + t_color c; + + i = 0; + ft_putendl("Adding cylinder"); + if (!(cylinder = (t_cylinder *)ft_memalloc(sizeof(t_cylinder)))) + return (FAILURE); + init_cylinder(cylinder, &c); + while (data[i] && i < 11) + ++i; + if (i > 3) + cylinder->o = vec(ft_atof(data[1]), ft_atof(data[2]), ft_atof(data[3])); + if (i > 6) + { + cylinder->d = vec(ft_atof(data[4]), ft_atof(data[5]), ft_atof(data[6])); + if (FAILURE == check_direction(cylinder->d)) + return (FAILURE); + normalize(&cylinder->d); + } + if (i > 7) + cylinder->r = ft_atof(data[7]); + if (i > 10) + c = color(ft_atoi(data[8]), ft_atoi(data[9]), ft_atoi(data[10])); + return (add_obj_node(e, (void *)cylinder, CYLINDER, c)); +} diff --git a/src/add_plane.c b/src/add_plane.c new file mode 100644 index 0000000..cfb4a9c --- /dev/null +++ b/src/add_plane.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_plane.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/07 21:42:30 by gbrochar #+# #+# */ +/* Updated: 2019/02/21 17:11:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void init_plane(t_plane *plane, t_color *c) +{ + plane->o = vec(0, 0, 0); + plane->d = vec(0, 1, 0); + *c = color(255, 255, 255); +} + +int add_plane(char **data, t_env *e) +{ + int i; + t_plane *plane; + t_color c; + + i = 0; + ft_putendl("Adding plane"); + if (!(plane = (t_plane *)ft_memalloc(sizeof(t_plane)))) + return (FAILURE); + init_plane(plane, &c); + while (data[i] && i < 10) + ++i; + if (i > 3) + plane->o = vec(ft_atof(data[1]), ft_atof(data[2]), ft_atof(data[3])); + if (i > 6) + { + plane->d = vec(ft_atof(data[4]), ft_atof(data[5]), ft_atof(data[6])); + if (FAILURE == check_direction(plane->d)) + return (FAILURE); + normalize(&plane->d); + } + if (i > 9) + c = color(ft_atoi(data[7]), ft_atoi(data[8]), ft_atoi(data[9])); + return (add_obj_node(e, (void *)plane, PLANE, c)); +} diff --git a/src/add_sphere.c b/src/add_sphere.c new file mode 100644 index 0000000..7604d05 --- /dev/null +++ b/src/add_sphere.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_sphere.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/07 21:41:54 by gbrochar #+# #+# */ +/* Updated: 2019/02/11 11:44:40 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void init_sphere(t_sphere *sphere, t_color *c) +{ + sphere->o = vec(0, 0, 0); + sphere->r = 1; + *c = color(255, 255, 255); +} + +int add_sphere(char **data, t_env *e) +{ + int i; + t_sphere *sphere; + t_color c; + + i = 0; + ft_putendl("Adding sphere"); + if (!(sphere = (t_sphere *)ft_memalloc(sizeof(t_sphere)))) + return (FAILURE); + init_sphere(sphere, &c); + while (data[i] && i < 8) + ++i; + if (i > 3) + sphere->o = vec(ft_atof(data[1]), ft_atof(data[2]), ft_atof(data[3])); + if (i > 4) + sphere->r = ft_atof(data[4]); + if (i > 7) + c = color(ft_atoi(data[5]), ft_atoi(data[6]), ft_atoi(data[7])); + return (add_obj_node(e, (void *)sphere, SPHERE, c)); +} diff --git a/src/add_spot.c b/src/add_spot.c new file mode 100644 index 0000000..21a61ee --- /dev/null +++ b/src/add_spot.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_spot.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 18:10:46 by gbrochar #+# #+# */ +/* Updated: 2019/02/13 19:02:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void init_spot(t_spot *spot) +{ + spot->o = vec(0, 0, 0); + spot->i = 1; +} + +int add_spot(char **data, t_env *e) +{ + int i; + t_spot spot; + + i = 0; + ft_putendl("Adding spot"); + init_spot(&spot); + while (data[i] && i < 5) + i++; + if (i > 3) + spot.o = vec(ft_atof(data[1]), ft_atof(data[2]), ft_atof(data[3])); + if (i > 4) + spot.i = ft_atof(data[4]); + return (add_spot_node(e, spot)); +} diff --git a/src/color.c b/src/color.c new file mode 100644 index 0000000..c0134d9 --- /dev/null +++ b/src/color.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 16:38:04 by gbrochar #+# #+# */ +/* Updated: 2019/02/08 20:47:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +t_color color(int r, int g, int b) +{ + t_color color; + + color.r = r; + color.g = g; + color.b = b; + return (color); +} + +t_color color_add(t_color c1, t_color c2) +{ + c1.r = c1.r + c2.r; + c1.g = c1.g + c2.g; + c1.b = c1.b + c2.b; + return (c1); +} + +t_color color_mul(t_color c, double scalar) +{ + c.r = (int)((double)c.r * scalar); + c.g = (int)((double)c.g * scalar); + c.b = (int)((double)c.b * scalar); + return (c); +} + +t_color color_cap(t_color c) +{ + c.r = (c.r > 255 ? 255 : c.r); + c.r = (c.r < 0 ? 0 : c.r); + c.g = (c.g > 255 ? 255 : c.g); + c.g = (c.g < 0 ? 0 : c.g); + c.b = (c.b > 255 ? 255 : c.b); + c.b = (c.b < 0 ? 0 : c.b); + return (c); +} diff --git a/src/env_list.c b/src/env_list.c new file mode 100644 index 0000000..c7528e0 --- /dev/null +++ b/src/env_list.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 17:08:36 by gbrochar #+# #+# */ +/* Updated: 2019/02/11 11:39:52 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +int add_obj_node(t_env *e, void *obj, int type, t_color c) +{ + t_obj_list *obj_node; + t_obj_list *list_browser; + + if (!(obj_node = (t_obj_list *)ft_memalloc(sizeof(t_obj_list)))) + return (FAILURE); + obj_node->obj = obj; + obj_node->type = type; + obj_node->c = c; + if (!e->objs) + e->objs = obj_node; + else + { + list_browser = e->objs; + while (list_browser->next) + list_browser = list_browser->next; + list_browser->next = obj_node; + } + return (SUCCESS); +} + +int add_spot_node(t_env *e, t_spot spot) +{ + t_spot_list *spot_node; + t_spot_list *list_browser; + + if (!(spot_node = (t_spot_list *)ft_memalloc(sizeof(t_spot_list)))) + return (FAILURE); + spot_node->spot = spot; + if (!e->spots) + e->spots = spot_node; + else + { + list_browser = e->spots; + while (list_browser->next) + list_browser = list_browser->next; + list_browser->next = spot_node; + } + return (SUCCESS); +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..e6cbe4f --- /dev/null +++ b/src/main.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 10:11:19 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 08:38:14 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void init_env(t_env *e) +{ + e->func.intersect_obj[PLANE] = &intersect_plane; + e->func.intersect_obj[SPHERE] = &intersect_sphere; + e->func.intersect_obj[CYLINDER] = &intersect_cylinder; + e->func.intersect_obj[CONE] = &intersect_cone; + e->func.normal_obj[PLANE] = &normal_plane; + e->func.normal_obj[SPHERE] = &normal_sphere; + e->func.normal_obj[CYLINDER] = &normal_cylinder; + e->func.normal_obj[CONE] = &normal_cone; +} + +int main(int ac, char *av[]) +{ + t_env e; + + if (ac == 2) + if (SUCCESS == parse(av[1], &e)) + if (SUCCESS == init_mlx(&e)) + { + init_env(&e); + raytrace(&e); + mlx_loop(e.mlx.ptr); + } + else + free_parsing(&e); + else + { + free_parsing(&e); + ft_putendl("file is not valid"); + } + else + ft_putendl("usage: ./rtv1 scene_file"); + return (0); +} diff --git a/src/maths.c b/src/maths.c new file mode 100644 index 0000000..77bc325 --- /dev/null +++ b/src/maths.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* maths.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 14:46:56 by gbrochar #+# #+# */ +/* Updated: 2019/02/08 20:18:48 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +int solve_quadratic(double params[3], double roots[2]) +{ + double delta; + + delta = params[1] * params[1] - (double)4 * params[0] * params[2]; + if (delta + E > 0) + { + roots[0] = (-params[1] - sqrt(delta)) / ((double)2 * params[0]); + roots[1] = (-params[1] + sqrt(delta)) / ((double)2 * params[0]); + return (SUCCESS); + } + return (FAILURE); +} diff --git a/src/maths_cone.c b/src/maths_cone.c new file mode 100644 index 0000000..346f0ba --- /dev/null +++ b/src/maths_cone.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* maths_cone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 20:02:35 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 17:26:00 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +double intersect_cone(void *obj, t_ray r) +{ + t_cone *co; + t_vec sub_ori; + double params[3]; + double roots[2]; + + co = (t_cone *)obj; + sub_ori = vec_sub(r.o, co->o); + params[0] = dot_product(r.d, r.d) - (pow(co->k, 2) + (double)1) + * pow(dot_product(r.d, co->d), 2); + params[1] = (double)2 * (dot_product(r.d, sub_ori) + - (pow(co->k, 2) + (double)1) * dot_product(r.d, co->d) + * dot_product(sub_ori, co->d)); + params[2] = dot_product(sub_ori, sub_ori) - (pow(co->k, 2) + (double)1) + * pow(dot_product(sub_ori, co->d), 2); + if (SUCCESS == solve_quadratic(params, roots)) + { + if (roots[0] > 0) + return (roots[0] - E); + else if (roots[1] > 0) + return (roots[1] - E); + } + return (FAILURE); +} + +t_vec normal_cone(void *obj, t_ray r, double t) +{ + t_cone *co; + t_vec p; + double m; + + co = (t_cone *)obj; + p = vec_add(r.o, vec_mul(r.d, t)); + m = dot_product(r.d, co->d) * t + + dot_product(vec_sub(r.o, co->o), co->d); + p = vec_sub(vec_sub(vec_sub(p, co->o), vec_mul(co->d, m)), + vec_mul(co->d, m * pow(co->k, 2))); + normalize(&p); + return (p); +} diff --git a/src/maths_cylinder.c b/src/maths_cylinder.c new file mode 100644 index 0000000..571c060 --- /dev/null +++ b/src/maths_cylinder.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* maths_cylinder.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 18:31:29 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 17:26:27 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +double intersect_cylinder(void *obj, t_ray r) +{ + t_cylinder *cy; + t_vec sub_ori; + double roots[2]; + double params[3]; + + cy = (t_cylinder *)obj; + sub_ori = vec_sub(r.o, cy->o); + params[0] = dot_product(r.d, r.d) - pow(dot_product(r.d, cy->d), 2); + params[1] = (double)2 * (dot_product(r.d, sub_ori) + - (dot_product(r.d, cy->d) * dot_product(sub_ori, cy->d))); + params[2] = dot_product(sub_ori, sub_ori) + - pow(dot_product(sub_ori, cy->d), 2) - pow(cy->r, 2); + if (SUCCESS == solve_quadratic(params, roots)) + { + if (roots[0] > 0) + return (roots[0] - E); + else if (roots[1] > 0) + return (roots[1] - E); + } + return (FAILURE); +} + +t_vec normal_cylinder(void *obj, t_ray r, double t) +{ + t_cylinder *cy; + t_vec p; + double m; + + cy = (t_cylinder *)obj; + p = vec_add(r.o, vec_mul(r.d, t)); + m = dot_product(r.d, cy->d) * t + + dot_product(vec_sub(r.o, cy->o), cy->d); + p = vec_sub(vec_sub(p, cy->o), vec_mul(cy->d, m)); + normalize(&p); + return (p); +} diff --git a/src/maths_plane.c b/src/maths_plane.c new file mode 100644 index 0000000..4c222cd --- /dev/null +++ b/src/maths_plane.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* maths_plane.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 19:57:48 by gbrochar #+# #+# */ +/* Updated: 2019/02/08 20:47:52 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +double intersect_plane(void *obj, t_ray r) +{ + t_plane *p; + + p = (t_plane *)obj; + if (dot_product(r.d, p->d) != 0) + { + return (-dot_product(vec_sub(r.o, p->o), p->d) + / dot_product(r.d, p->d) - E); + } + return (FAILURE); +} + +t_vec normal_plane(void *obj, t_ray r, double t) +{ + t_plane *p; + + (void)r; + (void)t; + p = (t_plane *)obj; + return (p->d); +} diff --git a/src/maths_sphere.c b/src/maths_sphere.c new file mode 100644 index 0000000..3221418 --- /dev/null +++ b/src/maths_sphere.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* maths_sphere.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 14:43:58 by gbrochar #+# #+# */ +/* Updated: 2019/02/11 14:15:57 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +double intersect_sphere(void *obj, t_ray r) +{ + t_sphere *s; + double roots[2]; + double params[3]; + + s = (t_sphere *)obj; + params[0] = 1; + params[1] = (double)2 * dot_product(r.d, vec_sub(r.o, s->o)); + params[2] = dot_product(vec_sub(r.o, s->o), vec_sub(r.o, s->o)) + - s->r * s->r; + if (SUCCESS == solve_quadratic(params, roots)) + { + if (roots[0] > 0) + return (roots[0] - E); + else if (roots[1] > 0) + return (roots[1] - E); + } + return (FAILURE); +} + +t_vec normal_sphere(void *obj, t_ray r, double t) +{ + t_sphere *s; + t_vec p; + + s = (t_sphere *)obj; + p = vec_add(r.o, vec_mul(r.d, t)); + p = vec_sub(p, s->o); + normalize(&p); + return (p); +} diff --git a/src/mlx.c b/src/mlx.c new file mode 100644 index 0000000..e2aba11 --- /dev/null +++ b/src/mlx.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mlx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 10:11:17 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 17:29:19 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void put_pixel(t_env *e, int x, int y, t_color c) +{ + e->mlx.data[y * (int)WINX + x] = c.b + (c.g << 8) + (c.r << 16); +} + +void mlx_free(char flags, t_mlx *mlx) +{ + if (flags & MLX_IMG) + mlx_destroy_image(mlx->ptr, mlx->img); + if (flags & MLX_WIN) + mlx_destroy_window(mlx->ptr, mlx->win); + if (flags & MLX_PTR) + free(mlx->ptr); + exit(0); +} + +int mlx_exit(char flags, t_mlx *mlx) +{ + mlx_free(flags, mlx); + ft_putendl("Error : mlx initialization failed"); + return (FAILURE); +} + +int init_mlx(t_env *e) +{ + if (!(e->mlx.ptr = mlx_init())) + return (mlx_exit(MLX_NOFLAG, &e->mlx)); + if (!(e->mlx.win = mlx_new_window( + e->mlx.ptr, (int)WINX, (int)WINY, "RTv1"))) + return (mlx_exit(MLX_PTR, &e->mlx)); + if (!(e->mlx.img = mlx_new_image(e->mlx.ptr, (int)WINX, (int)WINY))) + return (mlx_exit(MLX_PTR | MLX_WIN, &e->mlx)); + if (!(e->mlx.data = (int *)mlx_get_data_addr(e->mlx.img, &e->mlx.bpp, + &e->mlx.size_l, &e->mlx.endian))) + return (mlx_exit(MLX_PTR | MLX_WIN | MLX_IMG, &e->mlx)); + mlx_hook(e->mlx.win, 2, 1L << 1, key_hook, e); + mlx_hook(e->mlx.win, 17, 0, red_cross_hook, e); + return (SUCCESS); +} diff --git a/src/mlx_events.c b/src/mlx_events.c new file mode 100644 index 0000000..bbc8bc1 --- /dev/null +++ b/src/mlx_events.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mlx_events.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/21 14:57:38 by gbrochar #+# #+# */ +/* Updated: 2019/02/21 15:02:57 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +int key_hook(int keycode, void *e_void) +{ + t_env *e; + + e = (t_env *)e_void; + if (keycode == 53) + { + free_parsing(e); + mlx_free(MLX_PTR | MLX_IMG | MLX_WIN, &e->mlx); + } + return (SUCCESS); +} + +int red_cross_hook(void *e_void) +{ + t_env *e; + + e = (t_env *)e_void; + free_parsing(e); + mlx_free(MLX_PTR | MLX_IMG | MLX_WIN, &e->mlx); + return (SUCCESS); +} diff --git a/src/parse.c b/src/parse.c new file mode 100644 index 0000000..21774c9 --- /dev/null +++ b/src/parse.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 11:39:56 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 10:05:31 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void free_parsing(t_env *e) +{ + t_obj_list *obj_browser; + t_spot_list *spot_browser; + void *tmp; + + get_next_line(0, NULL, 1); + obj_browser = e->objs; + spot_browser = e->spots; + while (obj_browser) + { + tmp = (void *)obj_browser->next; + ft_memdel(&obj_browser->obj); + ft_memdel((void **)&obj_browser); + obj_browser = (t_obj_list *)tmp; + } + while (spot_browser) + { + tmp = (void *)spot_browser->next; + ft_memdel((void **)&spot_browser); + spot_browser = (t_spot_list *)tmp; + } +} + +int lex(char *str) +{ + if (ft_strstr(str, "//")) + return (COMMENTARY); + else if (ft_strstr(str, "CAM")) + return (CAMERA); + else if (ft_strstr(str, "SPOT") || ft_strstr(str, "LIGHT")) + return (SPOT); + else if (ft_strstr(str, "PLANE")) + return (PLANE); + else if (ft_strstr(str, "SPHERE")) + return (SPHERE); + else if (ft_strstr(str, "CYLINDER")) + return (CYLINDER); + else if (ft_strstr(str, "CONE")) + return (CONE); + else if (ft_strstr(str, "SPECULAR")) + return (SPECULAR); + else if (ft_strstr(str, "AMBIENT")) + return (AMBIENT); + return (FAILURE); +} + +int parse_line(char *line, t_env *e) +{ + char **split; + t_add_elem add_elem[8]; + int ret; + int i; + + add_elem[CAMERA] = &set_camera; + add_elem[SPOT] = &add_spot; + add_elem[PLANE] = &add_plane; + add_elem[SPHERE] = &add_sphere; + add_elem[CYLINDER] = &add_cylinder; + add_elem[CONE] = &add_cone; + add_elem[SPECULAR] = &set_specular; + add_elem[AMBIENT] = &set_ambient; + split = ft_strsplit(line, ' '); + ret = FAILURE; + i = -1; + if (split[0]) + if (FAILURE != (ret = lex(split[0])) && COMMENTARY != ret) + ret = add_elem[ret](split, e); + while (split[++i]) + ft_memdel((void **)&split[i]); + ft_memdel((void **)&split); + return (ret); +} + +int parse(char *path, t_env *e) +{ + int fd; + int ret; + char *line; + + line = NULL; + e->ambient = DEFAULT_AMBIENT; + e->spec_radius = DEFAULT_SPEC_RADIUS; + e->spec_ratio = DEFAULT_SPEC_RATIO; + e->objs = NULL; + e->spots = NULL; + init_camera(&e->camera); + fd = open(path, O_RDONLY); + while (0 < (ret = get_next_line(fd, &line, 0))) + { + ret = parse_line(line, e); + ft_memdel((void **)&line); + if (FAILURE == ret) + return (FAILURE); + } + return (ret); +} diff --git a/src/phong.c b/src/phong.c new file mode 100644 index 0000000..ae7fb46 --- /dev/null +++ b/src/phong.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* phong.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 15:38:13 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 18:12:11 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +t_ray get_light_ray(t_ray r, double t, t_spot spot) +{ + t_ray l_r; + + l_r.o = vec_add(r.o, vec_mul(r.d, t)); + l_r.d = vec_sub(spot.o, l_r.o); + normalize(&l_r.d); + return (l_r); +} + +double get_intensity(t_env *e, t_ray r, + t_spot spot, t_obj_list obj) +{ + t_ray l_r; + t_vec normal; + double length; + double i; + + l_r = get_light_ray(r, obj.t, spot); + length = vec_length(vec_sub(spot.o, l_r.o)); + normal = e->func.normal_obj[obj.type](obj.obj, r, obj.t); + i = dot_product(l_r.d, normal); + if (i > 0) + return ((spot.i * i) / (length * length)); + return (0); +} + +int intersect_phong(t_env *e, t_ray r, + t_spot spot, t_obj_list obj) +{ + t_ray l_r; + double length; + t_obj_list *ret; + + l_r = get_light_ray(r, obj.t, spot); + length = vec_length(vec_sub(spot.o, l_r.o)); + if (!(ret = intersect_ray(e, l_r)) || ret->t > length) + return (SUCCESS); + return (FAILURE); +} + +double get_specular(t_env *e, t_ray ray, + t_spot spot, t_obj_list obj) +{ + t_vec normal; + t_vec i; + t_vec r; + t_vec v; + double spec_coef; + + v = vec_sub(ray.o, vec_add(ray.o, vec_mul(ray.d, obj.t))); + i = vec_sub(vec_add(ray.o, vec_mul(ray.d, obj.t)), spot.o); + normal = e->func.normal_obj[obj.type](obj.obj, ray, obj.t); + normalize(&i); + r = get_r_vec(v, normal); + normalize(&r); + spec_coef = dot_product(r, i); + if (spec_coef > M_PI / 2 - e->spec_radius + && spec_coef < M_PI / 2 + e->spec_radius + && dot_product(normal, vec_mul(i, -1)) > 0) + return ((spec_coef + e->spec_radius - M_PI / 2) * e->spec_ratio); + return (0); +} + +t_color phong(t_env *e, t_ray r, t_obj_list *obj) +{ + t_spot_list *browser; + t_obj_list backup; + double i; + double specular; + + i = 0; + specular = 0; + backup = *obj; + browser = e->spots; + while (browser) + { + if (SUCCESS == intersect_phong(e, r, browser->spot, backup)) + i += get_intensity(e, r, browser->spot, backup); + specular += get_specular(e, r, browser->spot, backup); + browser = browser->next; + } + if (i > 1) + i = 1; + return (color_cap(color_add( + color_mul(obj->c, e->ambient + + ((double)1 - e->ambient) * i), + color_mul(color(255, 255, 255), specular)))); +} diff --git a/src/ray.c b/src/ray.c new file mode 100644 index 0000000..168a012 --- /dev/null +++ b/src/ray.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ray.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 13:32:52 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 17:29:07 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +t_vec get_r_vec(t_vec i_ray, t_vec normal) +{ + t_vec r_ray; + + r_ray = vec_sub(i_ray, vec_mul(normal, 2 * dot_product(i_ray, normal))); + return (r_ray); +} + +t_ray set_ray(t_env *e, int x, int y) +{ + t_ray r; + + r.o = e->camera.o; + r.d = vec_add(e->camera.ul, vec_sub( + vec_mul(e->camera.rv, ((double)x * e->camera.width / WINX)), + vec_mul(e->camera.uv, ((double)y * e->camera.height / WINY)))); + r.d = vec_sub(r.d, r.o); + normalize(&r.d); + return (r); +} diff --git a/src/raytrace.c b/src/raytrace.c new file mode 100644 index 0000000..26370a9 --- /dev/null +++ b/src/raytrace.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* raytrace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/07 15:35:45 by gbrochar #+# #+# */ +/* Updated: 2019/02/13 18:57:43 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +t_obj_list *intersect_ray(t_env *e, t_ray r) +{ + t_obj_list *browser; + t_obj_list *closest_obj; + + closest_obj = NULL; + browser = e->objs; + while (browser) + { + browser->t = e->func.intersect_obj[browser->type](browser->obj, r); + browser = browser->next; + } + browser = e->objs; + while (browser) + { + if (browser->t > 0 && (!closest_obj || browser->t < closest_obj->t)) + closest_obj = browser; + browser = browser->next; + } + return (closest_obj); +} + +void raytrace_pixel(t_env *e, int x, int y) +{ + t_ray r; + t_obj_list *closest_object; + + r = set_ray(e, x, y); + closest_object = intersect_ray(e, r); + if (closest_object) + put_pixel(e, x, y, phong(e, r, closest_object)); + else + put_pixel(e, x, y, color(0, 0, 0)); +} + +void raytrace(t_env *e) +{ + int x; + int y; + + y = -1; + while (++y < WINY) + { + x = -1; + while (++x < WINX) + raytrace_pixel(e, x, y); + } + mlx_put_image_to_window(e->mlx.ptr, e->mlx.win, e->mlx.img, 0, 0); +} diff --git a/src/rtv1.h b/src/rtv1.h new file mode 100644 index 0000000..b27e94d --- /dev/null +++ b/src/rtv1.h @@ -0,0 +1,247 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rtv1.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 10:11:47 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 16:24:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RTV1_H +# define RTV1_H + +# include "mlx.h" +# include +# include + +# include "../libft/libft.h" + +# define E (double)0.000001 +# define TO_RAD (double)57.2957795131 +# define SUCCESS 0 +# define FAILURE -1 +# define TRUE 1 +# define FALSE 0 + +# define PLANE 0 +# define SPHERE 1 +# define CYLINDER 2 +# define CONE 3 +# define SPOT 4 +# define CAMERA 5 +# define SPECULAR 6 +# define AMBIENT 7 +# define COMMENTARY 8 + +# define MLX_NOFLAG 0 +# define MLX_PTR 1 << 0 +# define MLX_WIN 1 << 1 +# define MLX_IMG 1 << 2 + +# define WINX (double)1280 +# define WINY (double)720 +# define ZOOM (double)1 +# define VP_DIST (double)1 + +# define DEFAULT_AMBIENT (double)0.2 +# define DEFAULT_SPEC_RADIUS (double)0.3 +# define DEFAULT_SPEC_RATIO (double)1 + +typedef struct s_vec t_vec; +typedef struct s_ray t_ray; + +struct s_vec +{ + double x; + double y; + double z; +}; + +struct s_ray +{ + t_vec o; + t_vec d; +}; + +typedef struct s_color t_color; + +struct s_color +{ + int r; + int g; + int b; +}; + +typedef struct s_plane t_plane; +typedef struct s_sphere t_sphere; +typedef struct s_cylinder t_cylinder; +typedef struct s_cone t_cone; + +struct s_plane +{ + t_vec o; + t_vec d; +}; + +struct s_sphere +{ + t_vec o; + double r; +}; + +struct s_cylinder +{ + t_vec o; + t_vec d; + double r; +}; + +struct s_cone +{ + t_vec o; + t_vec d; + double k; +}; + +typedef struct s_spot t_spot; + +struct s_spot +{ + t_vec o; + double i; +}; + +typedef struct s_camera t_camera; + +struct s_camera +{ + t_vec o; + t_vec d; + t_vec uv; + t_vec rv; + t_vec ul; + double width; + double height; + double dist; +}; + +typedef struct s_spot_list t_spot_list; +typedef struct s_obj_list t_obj_list; + +struct s_obj_list +{ + void *obj; + int type; + t_color c; + double t; + t_obj_list *next; +}; + +struct s_spot_list +{ + t_spot spot; + t_spot_list *next; +}; + +typedef struct s_mlx +{ + void *ptr; + void *win; + void *img; + int *data; + int size_l; + int endian; + int bpp; + int padding; +} t_mlx; + +typedef struct s_func +{ + double (*intersect_obj[4])(void *, t_ray); + t_vec (*normal_obj[4])(void *, t_ray, double); +} t_func; + +typedef struct s_env +{ + t_mlx mlx; + t_func func; + t_spot_list *spots; + t_obj_list *objs; + t_camera camera; + double spec_radius; + double spec_ratio; + double ambient; +} t_env; + +typedef int (*t_add_elem)(char **, t_env *); + +int parse(char *path, t_env *e); +int parse_line(char *line, t_env *e); +void free_parsing(t_env *e); +int lex(char *str); + +int set_specular(char **data, t_env *e); +int set_ambient(char **data, t_env *e); +void init_camera(t_camera *camera); +int set_camera(char **data, t_env *e); +void init_spot(t_spot *spot); +int add_spot(char **data, t_env *e); +void init_plane(t_plane *plane, t_color *c); +int add_plane(char **data, t_env *e); +void init_sphere(t_sphere *sphere, t_color *c); +int add_sphere(char **data, t_env *e); +void init_cylinder(t_cylinder *cylinder, t_color *c); +int add_cylinder(char **data, t_env *e); +void init_cone(t_cone *cone, t_color *c); +int add_cone(char **data, t_env *e); + +int add_obj_node(t_env *e, void *obj, int type, + t_color c); +int add_spot_node(t_env *e, t_spot spot); + +void mlx_free(char flags, t_mlx *mlx); +void put_pixel(t_env *e, int x, int y, t_color c); +int mlx_exit(char flags, t_mlx *mlx); +int init_mlx(t_env *e); +int key_hook(int keycode, void *e_void); +int red_cross_hook(void *e_void); + +t_color color(int r, int g, int b); +t_color color_add(t_color c1, t_color c2); +t_color color_mul(t_color c, double scalar); +t_color color_cap(t_color c); + +t_vec vec(double x, double y, double z); +t_vec vec_add(t_vec u, t_vec v); +t_vec vec_sub(t_vec u, t_vec v); +t_vec vec_mul(t_vec vec, double scalar); +double vec_length(t_vec vec); +t_vec cross_product(t_vec u, t_vec v); +double dot_product(t_vec u, t_vec v); +int check_direction(t_vec d); +t_vec normalize(t_vec *vec); + +t_ray set_ray(t_env *e, int x, int y); +t_vec get_r_vec(t_vec i_ray, t_vec normal); +t_vec rotate(t_vec vec, t_vec axis, double theta); + +int solve_quadratic(double params[3], double roots[2]); + +double intersect_plane(void *obj, t_ray r); +double intersect_sphere(void *obj, t_ray r); +double intersect_cylinder(void *obj, t_ray r); +double intersect_cone(void *obj, t_ray r); +t_vec normal_plane(void *obj, t_ray r, double t); +t_vec normal_sphere(void *obj, t_ray r, double t); +t_vec normal_cylinder(void *obj, t_ray r, double t); +t_vec normal_cone(void *obj, t_ray r, double t); + +void raytrace(t_env *e); +void raytrace_pixel(t_env *e, int x, int y); +t_obj_list *intersect_ray(t_env *e, t_ray r); +t_color phong(t_env *e, t_ray r, t_obj_list *obj); + +#endif diff --git a/src/scene_params.c b/src/scene_params.c new file mode 100644 index 0000000..f74cf97 --- /dev/null +++ b/src/scene_params.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* scene_params.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/13 16:40:53 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 16:53:36 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +int set_specular(char **data, t_env *e) +{ + int i; + double tmp; + + i = 0; + ft_putendl("Setting specular"); + while (data[i] && i < 3) + i++; + if (i > 1) + { + tmp = ft_atof(data[1]); + if (tmp > 0 && tmp < M_PI / 2) + e->spec_radius = tmp; + else + return (FAILURE); + } + if (i > 2) + e->spec_ratio = ft_atof(data[2]); + if (e->spec_ratio < 0) + e->spec_ratio = 0; + return (SUCCESS); +} + +int set_ambient(char **data, t_env *e) +{ + int i; + double tmp; + + i = 0; + ft_putendl("Setting ambient"); + while (data[i] && i < 2) + i++; + if (i > 1) + { + tmp = ft_atof(data[1]); + if (tmp < 0) + tmp = 0; + if (tmp < 1) + e->ambient = tmp; + else + return (FAILURE); + } + return (SUCCESS); +} diff --git a/src/set_camera.c b/src/set_camera.c new file mode 100644 index 0000000..e78f10d --- /dev/null +++ b/src/set_camera.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_camera.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/07 15:12:51 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 17:28:43 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +void set_orientation(t_camera *camera) +{ + camera->uv = vec(0, 1, 0); + camera->rv = cross_product(camera->uv, camera->d); + normalize(&camera->rv); + camera->uv = cross_product(camera->d, camera->rv); + camera->ul = vec_add(camera->o, + vec_add(vec_mul(camera->d, camera->dist), + vec_sub(vec_mul(camera->uv, (camera->height / 2)), + vec_mul(camera->rv, (camera->width / 2))))); +} + +void init_camera(t_camera *camera) +{ + if (WINX > WINY) + { + camera->height = 1 / ZOOM; + camera->width = WINX / WINY * (1 / ZOOM); + } + else + { + camera->width = 1 / ZOOM; + camera->height = WINX / WINY * (1 / ZOOM); + } + camera->dist = VP_DIST; + camera->o = vec(0, 0, 0); + camera->d = vec(0, 0, -1); + set_orientation(camera); +} + +int set_camera(char **data, t_env *e) +{ + int i; + + i = 0; + ft_putendl("Setting camera"); + while (data[i] && i < 7) + ++i; + if (i > 3) + e->camera.o = vec(ft_atof(data[1]), ft_atof(data[2]), ft_atof(data[3])); + if (i > 6) + { + e->camera.d = vec(ft_atof(data[4]), ft_atof(data[5]), ft_atof(data[6])); + e->camera.d = vec_sub(e->camera.d, e->camera.o); + if (FAILURE == check_direction(e->camera.d)) + return (FAILURE); + normalize(&e->camera.d); + if ((e->camera.d.y + E > 1 && e->camera.d.y - E < 1) + || (e->camera.d.y + E > -1 && e->camera.d.y - E < -1)) + e->camera.d.x = 0.01; + normalize(&e->camera.d); + set_orientation(&e->camera); + } + return (SUCCESS); +} diff --git a/src/vec.c b/src/vec.c new file mode 100644 index 0000000..3eb8878 --- /dev/null +++ b/src/vec.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/06 16:45:13 by gbrochar #+# #+# */ +/* Updated: 2019/02/22 12:48:34 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +t_vec vec(double x, double y, double z) +{ + t_vec vec; + + vec.x = x; + vec.y = y; + vec.z = z; + return (vec); +} + +t_vec cross_product(t_vec u, t_vec v) +{ + t_vec w; + + w.x = u.y * v.z - u.z * v.y; + w.y = u.z * v.x - u.x * v.z; + w.z = u.x * v.y - u.y * v.x; + return (w); +} + +double dot_product(t_vec u, t_vec v) +{ + return (u.x * v.x + u.y * v.y + u.z * v.z); +} + +double vec_length(t_vec vec) +{ + return (sqrt(dot_product(vec, vec))); +} + +t_vec normalize(t_vec *vec) +{ + double n; + + n = vec_length(*vec); + vec->x /= n; + vec->y /= n; + vec->z /= n; + return (*vec); +} diff --git a/src/vec_arithmetics.c b/src/vec_arithmetics.c new file mode 100644 index 0000000..6b01fd1 --- /dev/null +++ b/src/vec_arithmetics.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_arithmetics.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/08 15:57:41 by gbrochar #+# #+# */ +/* Updated: 2019/02/21 17:15:40 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rtv1.h" + +int check_direction(t_vec d) +{ + if (d.x + E > 0 && d.x - E < 0 + && d.y + E > 0 && d.y - E < 0 + && d.z + E > 0 && d.z - E < 0) + return (FAILURE); + return (SUCCESS); +} + +t_vec vec_mul(t_vec vec, double scalar) +{ + vec.x = vec.x * scalar; + vec.y = vec.y * scalar; + vec.z = vec.z * scalar; + return (vec); +} + +t_vec vec_add(t_vec u, t_vec v) +{ + u.x = u.x + v.x; + u.y = u.y + v.y; + u.z = u.z + v.z; + return (u); +} + +t_vec vec_sub(t_vec u, t_vec v) +{ + u.x = u.x - v.x; + u.y = u.y - v.y; + u.z = u.z - v.z; + return (u); +}