From 5fd959eeb88c9ea08eccdfe5404626b081bced8d Mon Sep 17 00:00:00 2001 From: ReverseSky Date: Wed, 9 Oct 2024 03:35:09 +0200 Subject: [PATCH] problemes affichage --- Libft/Makefile | 30 +++++++ Libft/ft_atoi.c | 34 ++++++++ Libft/ft_bzero.c | 26 ++++++ Libft/ft_calloc.c | 29 +++++++ Libft/ft_hexa.c | 64 +++++++++++++++ Libft/ft_isalnum.c | 19 +++++ Libft/ft_isalpha.c | 19 +++++ Libft/ft_isascii.c | 19 +++++ Libft/ft_isdigit.c | 19 +++++ Libft/ft_isprint.c | 19 +++++ Libft/ft_itoa.c | 62 +++++++++++++++ Libft/ft_lstadd_back.c | 30 +++++++ Libft/ft_lstadd_front.c | 18 +++++ Libft/ft_lstclear.c | 30 +++++++ Libft/ft_lstdelone.c | 22 ++++++ Libft/ft_lstiter.c | 27 +++++++ Libft/ft_lstlast.c | 21 +++++ Libft/ft_lstmap.c | 41 ++++++++++ Libft/ft_lstnew.c | 24 ++++++ Libft/ft_lstsize.c | 25 ++++++ Libft/ft_memchr.c | 26 ++++++ Libft/ft_memcmp.c | 26 ++++++ Libft/ft_memcpy.c | 28 +++++++ Libft/ft_memmove.c | 39 +++++++++ Libft/ft_memset.c | 26 ++++++ Libft/ft_printf.c | 63 +++++++++++++++ Libft/ft_put.c | 75 ++++++++++++++++++ Libft/ft_putchar_fd.c | 17 ++++ Libft/ft_putendl_fd.c | 27 +++++++ Libft/ft_putnbr_fd.c | 33 ++++++++ Libft/ft_putstr_fd.c | 26 ++++++ Libft/ft_split.c | 78 ++++++++++++++++++ Libft/ft_strchr.c | 28 +++++++ Libft/ft_strdup.c | 25 ++++++ Libft/ft_striteri.c | 26 ++++++ Libft/ft_strjoin.c | 41 ++++++++++ Libft/ft_strlcat.c | 37 +++++++++ Libft/ft_strlcpy.c | 28 +++++++ Libft/ft_strlen.c | 24 ++++++ Libft/ft_strmapi.c | 33 ++++++++ Libft/ft_strncmp.c | 26 ++++++ Libft/ft_strnstr.c | 38 +++++++++ Libft/ft_strrchr.c | 28 +++++++ Libft/ft_strtrim.c | 40 ++++++++++ Libft/ft_substr.c | 39 +++++++++ Libft/ft_tolower.c | 19 +++++ Libft/ft_toupper.c | 19 +++++ Libft/get_next_line.c | 110 ++++++++++++++++++++++++++ Libft/get_next_line_utils.c | 53 +++++++++++++ Libft/libft.h | 89 +++++++++++++++++++++ Makefile | 8 +- Minilibx | 1 + algo.c | 152 ++++++++++++++++++++++-------------- algo.h | 6 +- 54 files changed, 1849 insertions(+), 63 deletions(-) create mode 100644 Libft/Makefile create mode 100644 Libft/ft_atoi.c create mode 100644 Libft/ft_bzero.c create mode 100644 Libft/ft_calloc.c create mode 100644 Libft/ft_hexa.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_isprint.c create mode 100644 Libft/ft_itoa.c create mode 100644 Libft/ft_lstadd_back.c create mode 100644 Libft/ft_lstadd_front.c create mode 100644 Libft/ft_lstclear.c create mode 100644 Libft/ft_lstdelone.c create mode 100644 Libft/ft_lstiter.c create mode 100644 Libft/ft_lstlast.c create mode 100644 Libft/ft_lstmap.c create mode 100644 Libft/ft_lstnew.c create mode 100644 Libft/ft_lstsize.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_memmove.c create mode 100644 Libft/ft_memset.c create mode 100644 Libft/ft_printf.c create mode 100644 Libft/ft_put.c create mode 100644 Libft/ft_putchar_fd.c create mode 100644 Libft/ft_putendl_fd.c create mode 100644 Libft/ft_putnbr_fd.c create mode 100644 Libft/ft_putstr_fd.c create mode 100644 Libft/ft_split.c create mode 100644 Libft/ft_strchr.c create mode 100644 Libft/ft_strdup.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_strlcpy.c create mode 100644 Libft/ft_strlen.c create mode 100644 Libft/ft_strmapi.c create mode 100644 Libft/ft_strncmp.c create mode 100644 Libft/ft_strnstr.c create mode 100644 Libft/ft_strrchr.c create mode 100644 Libft/ft_strtrim.c create mode 100644 Libft/ft_substr.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_utils.c create mode 100644 Libft/libft.h create mode 160000 Minilibx diff --git a/Libft/Makefile b/Libft/Makefile new file mode 100644 index 0000000..fa0de42 --- /dev/null +++ b/Libft/Makefile @@ -0,0 +1,30 @@ +NAME = libft.a + +SRC = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c ft_isascii.c \ +ft_isdigit.c ft_isprint.c ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c \ +ft_memset.c ft_strchr.c ft_strdup.c ft_strlcat.c ft_strlcpy.c ft_strlen.c \ +ft_strncmp.c ft_strrchr.c ft_tolower.c ft_toupper.c ft_strnstr.c ft_substr.c\ +ft_strjoin.c ft_strtrim.c ft_itoa.c ft_strmapi.c ft_striteri.c ft_putchar_fd.c\ +ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c ft_split.c ft_lstnew.c\ +ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c\ +ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_hexa.c ft_printf.c\ +ft_put.c get_next_line.c get_next_line_utils.c + +OBJS = ${SRC:.c=.o} + +CC = gcc + +CFLAGS = -Wall -Werror -Wextra + +all: $(NAME) + +$(NAME): ${OBJS} + ar rc ${NAME} ${OBJS} + +clean : + rm -f ${OBJS} + +fclean : clean + rm -f $(NAME) + +re : fclean all diff --git a/Libft/ft_atoi.c b/Libft/ft_atoi.c new file mode 100644 index 0000000..8504ede --- /dev/null +++ b/Libft/ft_atoi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/04 14:30:22 by grobledo #+# #+# */ +/* Updated: 2023/02/04 14:30:24 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_atoi(const char *str) +{ + int i; + int is_neg; + int number; + + i = 0; + is_neg = 1; + number = 0; + while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32) + i++; + if (str[i] == '-' || str[i] == '+') + { + if (str[i] == '-') + is_neg *= -1; + i++; + } + while (str [i] >= '0' && str[i] <= '9') + number = (number * 10) + str[i++] - 48; + return (number * is_neg); +} diff --git a/Libft/ft_bzero.c b/Libft/ft_bzero.c new file mode 100644 index 0000000..b322d22 --- /dev/null +++ b/Libft/ft_bzero.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/08 14:32:10 by grobledo #+# #+# */ +/* Updated: 2023/02/08 14:32:22 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + unsigned char *str; + size_t i; + + i = 0; + str = s; + while (i < n) + { + str[i] = 0; + i++; + } +} diff --git a/Libft/ft_calloc.c b/Libft/ft_calloc.c new file mode 100644 index 0000000..ca1c5d6 --- /dev/null +++ b/Libft/ft_calloc.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/08 19:35:13 by grobledo #+# #+# */ +/* Updated: 2023/02/08 19:35:23 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_calloc(size_t count, size_t size) +{ + void *str; + size_t tmp; + + tmp = size * count; + if (size == 0 || count == 0) + return (malloc(0)); + if (tmp / count != size) + return (NULL); + str = malloc(size * count); + if (str == NULL) + return (NULL); + ft_bzero (str, size * count); + return (str); +} diff --git a/Libft/ft_hexa.c b/Libft/ft_hexa.c new file mode 100644 index 0000000..a439086 --- /dev/null +++ b/Libft/ft_hexa.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hexa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/04 09:38:27 by grobledo #+# #+# */ +/* Updated: 2023/04/04 09:38:28 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_hexa(unsigned int args, char format, int count) +{ + char *base; + char *shiftbase; + + base = "0123456789abcdef"; + shiftbase = "0123456789ABCDEF"; + if (format == 'x') + { + if (args >= 16) + count = ft_hexa(args / 16, format, count); + count = ft_putcharf(base[args % 16], count); + return (count); + } + if (format == 'X') + { + if (args >= 16) + count = ft_hexa(args / 16, format, count); + count = ft_putcharf(shiftbase[args % 16], count); + return (count); + } + return (0); +} + +int ft_starthexa(unsigned int args, char format, int count) +{ + if (!args && (format == 'x' || format == 'X')) + return (ft_putcharf('0', count)); + count = ft_hexa(args, format, count); + return (count); +} + +int ft_startpointer(unsigned long long int args, char format, int count) +{ + if (!args) + return (ft_putstrf("(nil)", count)); + count = ft_putstrf("0x", count); + count = ft_pointer(args, format, count); + return (count); +} + +int ft_pointer(unsigned long long int args, char format, int count) +{ + char *base; + + base = "0123456789abcdef"; + if (args >= 16) + count = ft_pointer(args / 16, format, count); + count = ft_putcharf(base[args % 16], count); + return (count); +} diff --git a/Libft/ft_isalnum.c b/Libft/ft_isalnum.c new file mode 100644 index 0000000..f5a8ec6 --- /dev/null +++ b/Libft/ft_isalnum.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 18:17:39 by grobledo #+# #+# */ +/* Updated: 2023/02/02 18:17:41 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) || ft_isdigit(c)) + return (1); + return (0); +} diff --git a/Libft/ft_isalpha.c b/Libft/ft_isalpha.c new file mode 100644 index 0000000..351bf89 --- /dev/null +++ b/Libft/ft_isalpha.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 17:36:30 by grobledo #+# #+# */ +/* Updated: 2023/02/02 17:36:34 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + return (1); + return (0); +} diff --git a/Libft/ft_isascii.c b/Libft/ft_isascii.c new file mode 100644 index 0000000..f95ded8 --- /dev/null +++ b/Libft/ft_isascii.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 18:20:38 by grobledo #+# #+# */ +/* Updated: 2023/02/02 18:20:40 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/Libft/ft_isdigit.c b/Libft/ft_isdigit.c new file mode 100644 index 0000000..beb94d6 --- /dev/null +++ b/Libft/ft_isdigit.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 17:48:20 by grobledo #+# #+# */ +/* Updated: 2023/02/02 17:48:22 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/Libft/ft_isprint.c b/Libft/ft_isprint.c new file mode 100644 index 0000000..acaa9b7 --- /dev/null +++ b/Libft/ft_isprint.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 18:27:24 by grobledo #+# #+# */ +/* Updated: 2023/02/02 18:27:25 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/Libft/ft_itoa.c b/Libft/ft_itoa.c new file mode 100644 index 0000000..3f1cdf6 --- /dev/null +++ b/Libft/ft_itoa.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/19 19:32:47 by grobledo #+# #+# */ +/* Updated: 2023/02/19 19:32:48 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int countsize(int nb) +{ + long int temp; + int size; + + size = 0; + temp = nb; + if (nb < 0) + { + temp = -nb; + size = size +1; + } + while (temp > 9) + { + temp = (temp / 10); + size++; + } + size++; + return (size); +} + +char *ft_itoa(int nb) +{ + long int temp; + char *res; + int size; + + if (nb == -2147483648) + return (ft_strdup("-2147483648")); + temp = nb; + size = countsize(nb); + res = ft_calloc(size + 1, sizeof (char)); + if (!res) + return (NULL); + if (nb < 0) + { + res[0] = '-'; + temp *= -1; + } + if (nb == 0) + res[0] = '0'; + while (temp > 0) + { + res[size - 1] = (temp % 10 + 48); + size--; + temp /= 10; + } + return (res); +} diff --git a/Libft/ft_lstadd_back.c b/Libft/ft_lstadd_back.c new file mode 100644 index 0000000..fd2389e --- /dev/null +++ b/Libft/ft_lstadd_back.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/23 11:38:44 by grobledo #+# #+# */ +/* Updated: 2023/02/23 11:38:46 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *temp; + + if (!new) + return ; + if (!*lst) + { + *lst = new; + return ; + } + else + { + temp = ft_lstlast(*lst); + temp->next = new; + } +} diff --git a/Libft/ft_lstadd_front.c b/Libft/ft_lstadd_front.c new file mode 100644 index 0000000..a24ae19 --- /dev/null +++ b/Libft/ft_lstadd_front.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/21 23:42:28 by grobledo #+# #+# */ +/* Updated: 2023/02/21 23:42:30 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + new->next = *lst; + *lst = new; +} diff --git a/Libft/ft_lstclear.c b/Libft/ft_lstclear.c new file mode 100644 index 0000000..3d8d38d --- /dev/null +++ b/Libft/ft_lstclear.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/23 22:05:11 by grobledo #+# #+# */ +/* Updated: 2023/02/23 22:05:12 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void*)) +{ + t_list *next; + t_list *current; + + if (lst == NULL || del == NULL || *lst == NULL) + return ; + current = *lst; + while (current != NULL) + { + next = current->next; + del(current->content); + free(current); + current = next; + } + *lst = NULL; +} diff --git a/Libft/ft_lstdelone.c b/Libft/ft_lstdelone.c new file mode 100644 index 0000000..da37e72 --- /dev/null +++ b/Libft/ft_lstdelone.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/23 20:15:05 by grobledo #+# #+# */ +/* Updated: 2023/02/23 20:15:06 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void*)) +{ + if (!lst) + return ; + if (!del) + return ; + del(lst->content); + free(lst); +} diff --git a/Libft/ft_lstiter.c b/Libft/ft_lstiter.c new file mode 100644 index 0000000..67041a2 --- /dev/null +++ b/Libft/ft_lstiter.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/23 22:42:44 by grobledo #+# #+# */ +/* Updated: 2023/02/23 22:42:45 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + t_list *node; + + node = lst; + if (lst == NULL || f == NULL) + return ; + node = lst; + while (node != NULL) + { + f(node->content); + node = node->next; + } +} diff --git a/Libft/ft_lstlast.c b/Libft/ft_lstlast.c new file mode 100644 index 0000000..9024c06 --- /dev/null +++ b/Libft/ft_lstlast.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/23 10:27:02 by grobledo #+# #+# */ +/* Updated: 2023/02/23 10:27:07 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (!lst) + return (NULL); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/Libft/ft_lstmap.c b/Libft/ft_lstmap.c new file mode 100644 index 0000000..8329e15 --- /dev/null +++ b/Libft/ft_lstmap.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/24 11:59:12 by grobledo #+# #+# */ +/* Updated: 2023/02/24 11:59:13 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(f)(void *), void (*del)(void *)) +{ + t_list *new; + t_list *current; + + if (!lst || !f ||!del) + return (NULL); + new = ft_lstnew(NULL); + if (!new) + return (NULL); + current = new; + while (lst) + { + current->content = f(lst->content); + if (lst->next) + { + current->next = ft_lstnew(NULL); + if (!current) + { + ft_lstclear(&new, del); + return (NULL); + } + current = current ->next; + } + lst = lst->next; + } + return (new); +} diff --git a/Libft/ft_lstnew.c b/Libft/ft_lstnew.c new file mode 100644 index 0000000..1cf613c --- /dev/null +++ b/Libft/ft_lstnew.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/21 19:40:32 by grobledo #+# #+# */ +/* Updated: 2023/02/21 22:58:42 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *new; + + new = malloc(sizeof(t_list)); + if (new == 0) + return (NULL); + new->content = content; + new->next = NULL; + return (new); +} diff --git a/Libft/ft_lstsize.c b/Libft/ft_lstsize.c new file mode 100644 index 0000000..c489c1a --- /dev/null +++ b/Libft/ft_lstsize.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/23 10:14:39 by grobledo #+# #+# */ +/* Updated: 2023/02/23 10:14:41 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + while (lst) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/Libft/ft_memchr.c b/Libft/ft_memchr.c new file mode 100644 index 0000000..6a35922 --- /dev/null +++ b/Libft/ft_memchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/08 20:08:44 by grobledo #+# #+# */ +/* Updated: 2023/02/08 20:08:45 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memchr(const void *str, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if ((((unsigned char *)str)[i]) == (unsigned char)c) + return ((void *)str + i); + i++; + } + return (NULL); +} diff --git a/Libft/ft_memcmp.c b/Libft/ft_memcmp.c new file mode 100644 index 0000000..112ccee --- /dev/null +++ b/Libft/ft_memcmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/08 20:15:03 by grobledo #+# #+# */ +/* Updated: 2023/02/08 20:15:05 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i]) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + return (0); +} diff --git a/Libft/ft_memcpy.c b/Libft/ft_memcpy.c new file mode 100644 index 0000000..407ca30 --- /dev/null +++ b/Libft/ft_memcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/08 15:04:22 by grobledo #+# #+# */ +/* Updated: 2023/02/08 15:04:24 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + int i; + + if (!dest && !src) + return (NULL); + i = 0; + while (n != 0) + { + ((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; + i++; + n--; + } + return ((void *)dest); +} diff --git a/Libft/ft_memmove.c b/Libft/ft_memmove.c new file mode 100644 index 0000000..c6e9153 --- /dev/null +++ b/Libft/ft_memmove.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/10 10:32:23 by grobledo #+# #+# */ +/* Updated: 2023/02/10 10:32:25 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + size_t i; + + if (!dest && !src) + return (NULL); + i = 0; + if (dest < src) + { + while (n--) + { + (((unsigned char *)dest)[i]) = (((unsigned char *)src)[i]); + i++; + } + } + else + { + i = i + n - 1; + while (n--) + { + (((unsigned char *)dest)[i]) = (((unsigned char *)src)[i]); + i--; + } + } + return ((void *)dest); +} diff --git a/Libft/ft_memset.c b/Libft/ft_memset.c new file mode 100644 index 0000000..ca28cfe --- /dev/null +++ b/Libft/ft_memset.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 18:40:30 by grobledo #+# #+# */ +/* Updated: 2023/02/02 18:40:33 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memset(void *str, int c, size_t n) +{ + int i; + + i = 0; + while (n != 0) + { + (((unsigned char *)str)[i]) = c; + i++; + n--; + } + return ((void *)str); +} diff --git a/Libft/ft_printf.c b/Libft/ft_printf.c new file mode 100644 index 0000000..f2c233c --- /dev/null +++ b/Libft/ft_printf.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/13 10:48:34 by grobledo #+# #+# */ +/* Updated: 2023/03/13 10:48:35 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_format(const char format, va_list args, int count) +{ + if (format == 'c') + count = ft_putcharf((char)va_arg(args, int), count); + else if (format == 's') + count = ft_putstrf(va_arg(args, char *), count); + else if (format == 'd') + count = ft_putnbrf(va_arg(args, int), count); + else if (format == 'i') + count = ft_putnbrf(va_arg(args, int), count); + else if (format == 'u') + count = ft_putnbrposf(va_arg(args, unsigned int), count); + else if (format == 'x') + count = ft_starthexa (va_arg(args, unsigned int), 'x', count); + else if (format == 'X') + count = ft_starthexa (va_arg(args, unsigned int), 'X', count); + else if (format == 'p') + count = ft_startpointer + (va_arg(args, unsigned long long int), 'p', count); + else if (format == '%') + count = ft_putcharf('%', count); + return (count); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int i; + int count; + + va_start(args, format); + i = 0; + count = 0; + while (format[i]) + { + if (format[i] != '%') + { + write(1, &format[i], 1); + count++; + } + else + { + i++; + count = ft_format(format[i], args, count); + } + i++; + } + va_end(args); + return (count); +} diff --git a/Libft/ft_put.c b/Libft/ft_put.c new file mode 100644 index 0000000..b29ff4d --- /dev/null +++ b/Libft/ft_put.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_put.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/13 10:26:43 by grobledo #+# #+# */ +/* Updated: 2023/04/13 10:26:45 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_putcharf(char c, int count) +{ + write(1, &c, 1); + count++; + return (count); +} + +int ft_putnbrf(int n, int count) +{ + if (n == -2147483648) + { + (write(1, "-2147483648", 11)); + count += 11; + return (count); + } + if (n < 0) + { + count = ft_putcharf('-', count); + n *= -1; + count = ft_putnbrf(n, count); + } + else if (n >= 10) + { + count = ft_putnbrf(n / 10, count); + count = ft_putnbrf(n % 10, count); + } + else + { + count = ft_putcharf(n + '0', count); + } + return (count); +} + +int ft_putnbrposf(unsigned int n, int count) +{ + if (n >= 10) + { + count = ft_putnbrposf(n / 10, count); + count = ft_putnbrposf(n % 10, count); + } + else + { + count = ft_putcharf(n + '0', count); + } + return (count); +} + +int ft_putstrf(char *s, int count) +{ + int i; + + if (!s) + return (ft_putstrf("(null)", count)); + i = 0; + while (s[i]) + { + write(1, &s[i], 1); + i++; + count++; + } + return (count); +} diff --git a/Libft/ft_putchar_fd.c b/Libft/ft_putchar_fd.c new file mode 100644 index 0000000..33fe262 --- /dev/null +++ b/Libft/ft_putchar_fd.c @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/20 23:21:04 by grobledo #+# #+# */ +/* Updated: 2023/02/20 23:21:05 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/Libft/ft_putendl_fd.c b/Libft/ft_putendl_fd.c new file mode 100644 index 0000000..bed0653 --- /dev/null +++ b/Libft/ft_putendl_fd.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/20 23:36:08 by grobledo #+# #+# */ +/* Updated: 2023/02/20 23:36:09 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + int i; + + if (!s) + return ; + i = 0; + while (s[i]) + { + write(fd, &s[i], 1); + i++; + } + ft_putchar_fd('\n', fd); +} diff --git a/Libft/ft_putnbr_fd.c b/Libft/ft_putnbr_fd.c new file mode 100644 index 0000000..89e7851 --- /dev/null +++ b/Libft/ft_putnbr_fd.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/20 23:43:01 by grobledo #+# #+# */ +/* Updated: 2023/02/20 23:43:02 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + { + write(fd, "-2147483648", 11); + } + else if (n < 0) + { + ft_putchar_fd('-', fd); + n *= -1; + ft_putnbr_fd(n, fd); + } + else if (n >= 10) + { + ft_putnbr_fd(n / 10, fd); + ft_putnbr_fd(n % 10, fd); + } + else + ft_putchar_fd(n + '0', fd); +} diff --git a/Libft/ft_putstr_fd.c b/Libft/ft_putstr_fd.c new file mode 100644 index 0000000..7a53068 --- /dev/null +++ b/Libft/ft_putstr_fd.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/20 23:29:21 by grobledo #+# #+# */ +/* Updated: 2023/02/20 23:29:26 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + int i; + + if (!s) + return ; + i = 0; + while (s[i]) + { + write(fd, &s[i], 1); + i++; + } +} diff --git a/Libft/ft_split.c b/Libft/ft_split.c new file mode 100644 index 0000000..f48337c --- /dev/null +++ b/Libft/ft_split.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/21 11:11:19 by grobledo #+# #+# */ +/* Updated: 2023/02/21 18:03:11 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_countword(const char *s, char c) +{ + int i; + int j; + + i = 0; + j = 0; + while (s[i]) + { + while (s[i] == c) + i++; + if (s[i] != '\0') + j++; + while (s[i] != c && s[i]) + i++; + } + return (j); +} + +char *ft_strdup_modif(const char *s, char c, size_t i) +{ + size_t k; + char *dest; + + k = 0; + while (s[i + k] && s[i + k] != c) + k++; + dest = ft_calloc(sizeof(char), (k + 1)); + k = 0; + if (dest == 0) + return (NULL); + while (s[i + k] && s[i + k] != c) + { + dest[k] = s[i + k]; + k++; + } + return (dest); +} + +char **ft_split(const char *s, char c) +{ + size_t i; + size_t j; + char **res; + + if (!s) + return (NULL); + res = ft_calloc(sizeof (char *), (ft_countword(s, c) + 1)); + if (!res) + return (NULL); + i = 0; + j = 0; + while (s[i]) + { + while (s[i] == c) + i++; + if (s[i] == '\0') + break ; + res[j] = ft_strdup_modif(s, c, i); + j++; + while (s[i] != c && s[i]) + i++; + } + return (res); +} diff --git a/Libft/ft_strchr.c b/Libft/ft_strchr.c new file mode 100644 index 0000000..8bb7eee --- /dev/null +++ b/Libft/ft_strchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/04 12:59:29 by grobledo #+# #+# */ +/* Updated: 2023/02/04 12:59:31 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strchr(const char *str, int tofind) +{ + int i; + + i = 0; + while (str[i]) + { + if ((unsigned char)str[i] == (unsigned char)tofind) + return ((char *)&str[i]); + i++; + } + if (tofind == '\0') + return ((char *)&str[i]); + return (0); +} diff --git a/Libft/ft_strdup.c b/Libft/ft_strdup.c new file mode 100644 index 0000000..b743a62 --- /dev/null +++ b/Libft/ft_strdup.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/08 20:55:55 by grobledo #+# #+# */ +/* Updated: 2023/02/13 21:30:44 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strdup(const char *src) +{ + size_t size; + char *dest; + + size = ft_strlen(src); + dest = (char *)malloc(size + 1); + if (dest == 0) + return (NULL); + ft_strlcpy(dest, src, size + 1); + return (dest); +} diff --git a/Libft/ft_striteri.c b/Libft/ft_striteri.c new file mode 100644 index 0000000..934c9d8 --- /dev/null +++ b/Libft/ft_striteri.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/20 22:29:06 by grobledo #+# #+# */ +/* Updated: 2023/02/20 22:29:07 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + size_t i; + + if (!s || !(*f)) + return ; + i = 0; + while (i < ft_strlen(s)) + { + (*f)(i, &s[i]); + i++; + } +} diff --git a/Libft/ft_strjoin.c b/Libft/ft_strjoin.c new file mode 100644 index 0000000..735da25 --- /dev/null +++ b/Libft/ft_strjoin.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/14 16:41:00 by grobledo #+# #+# */ +/* Updated: 2023/02/14 16:41:02 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t i; + size_t j; + char *dest; + + if (!s1 || !s2) + return (NULL); + dest = ft_calloc ((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof (char)); + if (dest == 0) + return (NULL); + i = 0; + j = 0; + while (s1[i] || s2[j]) + { + while (i < ft_strlen(s1)) + { + dest[i] = s1[i]; + i++; + } + while (j < ft_strlen(s2)) + { + dest[i + j] = s2[j]; + j++; + } + } + return (dest); +} diff --git a/Libft/ft_strlcat.c b/Libft/ft_strlcat.c new file mode 100644 index 0000000..89ae6b0 --- /dev/null +++ b/Libft/ft_strlcat.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 19:33:00 by grobledo #+# #+# */ +/* Updated: 2023/02/02 19:33:01 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_strlcat(char *dest, const char *src, size_t size) +{ + unsigned int i; + unsigned int j; + unsigned int dlen; + unsigned int slen; + + if ((dest == NULL || src == NULL) && size == 0) + return (0); + i = 0; + j = ft_strlen(dest); + dlen = j; + slen = ft_strlen(src); + if (size <= dlen) + return (slen + size); + while (src[i] != '\0' && i < size - dlen - 1) + { + dest[j] = src[i]; + i++; + j++; + } + dest[j] = '\0'; + return (dlen + slen); +} diff --git a/Libft/ft_strlcpy.c b/Libft/ft_strlcpy.c new file mode 100644 index 0000000..5201ec0 --- /dev/null +++ b/Libft/ft_strlcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 19:33:21 by grobledo #+# #+# */ +/* Updated: 2023/02/02 19:33:24 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_strlcpy(char *dest, const char *src, size_t size) +{ + unsigned int i; + + i = 0; + if (size == 0) + return (ft_strlen(src)); + while (src[i] && i < size - 1) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (ft_strlen(src)); +} diff --git a/Libft/ft_strlen.c b/Libft/ft_strlen.c new file mode 100644 index 0000000..45c7ee9 --- /dev/null +++ b/Libft/ft_strlen.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 18:31:38 by grobledo #+# #+# */ +/* Updated: 2023/02/02 18:31:39 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +size_t ft_strlen(const char *str) +{ + size_t i; + + i = 0; + while (str[i]) + { + i++; + } + return (i); +} diff --git a/Libft/ft_strmapi.c b/Libft/ft_strmapi.c new file mode 100644 index 0000000..6ce2ee1 --- /dev/null +++ b/Libft/ft_strmapi.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/20 21:51:01 by grobledo #+# #+# */ +/* Updated: 2023/02/20 21:51:02 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + size_t i; + size_t dlen; + char *dest; + + if (!s || !(*f)) + return (NULL); + i = 0; + dlen = ft_strlen(s); + dest = ft_calloc(dlen + 1, sizeof (char)); + if (!dest) + return (NULL); + while (i < ft_strlen(s)) + { + dest[i] = (*f)(i, s[i]); + i++; + } + return (dest); +} diff --git a/Libft/ft_strncmp.c b/Libft/ft_strncmp.c new file mode 100644 index 0000000..8a35309 --- /dev/null +++ b/Libft/ft_strncmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/04 14:33:04 by grobledo #+# #+# */ +/* Updated: 2023/02/04 14:33:06 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t size) +{ + size_t i; + + i = 0; + while ((s1[i] || s2[i]) && (i < size)) + { + if (s1[i] != s2[i]) + return (((unsigned char)(s1[i]) - ((unsigned char)s2[i]))); + i++; + } + return (0); +} diff --git a/Libft/ft_strnstr.c b/Libft/ft_strnstr.c new file mode 100644 index 0000000..1a8660a --- /dev/null +++ b/Libft/ft_strnstr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/13 22:07:12 by grobledo #+# #+# */ +/* Updated: 2023/02/13 22:07:14 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + + if ((big == NULL || little == NULL) && !len) + return (0); + i = 0; + j = 0; + if (little[i] == '\0') + return ((char *)big); + while (big[i] && i < len) + { + if (big[i] == little[j]) + { + while (big[i + j] == little[j] && (i + j) < len && little[j]) + j++; + if (little[j] == '\0') + return ((char *)&big[i]); + j = 0; + } + i++; + } + return (NULL); +} diff --git a/Libft/ft_strrchr.c b/Libft/ft_strrchr.c new file mode 100644 index 0000000..c1fb9e4 --- /dev/null +++ b/Libft/ft_strrchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/04 14:00:32 by grobledo #+# #+# */ +/* Updated: 2023/02/13 21:32:54 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strrchr(const char *str, int tofind) +{ + int i; + + i = 0; + while (str[i]) + i++; + while (i >= 0) + { + if ((unsigned char)str[i] == (unsigned char)tofind) + return ((char *)&str[i]); + i--; + } + return (0); +} diff --git a/Libft/ft_strtrim.c b/Libft/ft_strtrim.c new file mode 100644 index 0000000..02a14e6 --- /dev/null +++ b/Libft/ft_strtrim.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/18 18:34:05 by grobledo #+# #+# */ +/* Updated: 2023/02/18 18:34:08 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strtrim(const char *s1, const char *set) +{ + size_t i; + size_t j; + size_t k; + size_t dlen; + char *dest; + + if (!s1) + return (NULL); + i = 0; + j = ft_strlen(s1); + k = -1; + while (ft_strchr(set, s1[i]) != 0 && s1[i]) + i++; + if (!s1[i]) + return (ft_strdup("")); + while (ft_strchr(set, s1[j]) != 0 && j > 0) + j--; + dlen = (j - i); + dest = ft_calloc ((dlen + 2), sizeof (char)); + if (dest == 0) + return (NULL); + while (++k <= dlen) + dest[k] = s1[i + k]; + return (dest); +} diff --git a/Libft/ft_substr.c b/Libft/ft_substr.c new file mode 100644 index 0000000..22c55ea --- /dev/null +++ b/Libft/ft_substr.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/14 14:52:48 by grobledo #+# #+# */ +/* Updated: 2023/02/14 14:52:51 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_substr(const char *s, unsigned int start, size_t len) +{ + size_t i; + size_t j; + char *dest; + + if (!s) + return (NULL); + i = start; + j = 0; + if (start >= ft_strlen(s)) + return (ft_calloc(1, 1)); + if (ft_strlen(s + start) < len) + dest = (char *)ft_calloc((ft_strlen(s + start) + 1), sizeof(char)); + else + dest = (char *)ft_calloc((len + 1), sizeof(char)); + if (dest == 0) + return (NULL); + while (j < len && s[i]) + { + dest[j] = s[i]; + i++; + j++; + } + return (dest); +} diff --git a/Libft/ft_tolower.c b/Libft/ft_tolower.c new file mode 100644 index 0000000..7eae236 --- /dev/null +++ b/Libft/ft_tolower.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 20:49:29 by grobledo #+# #+# */ +/* Updated: 2023/02/02 20:49:30 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + c += 32; + return (c); +} diff --git a/Libft/ft_toupper.c b/Libft/ft_toupper.c new file mode 100644 index 0000000..2f7e5f2 --- /dev/null +++ b/Libft/ft_toupper.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 20:19:23 by grobledo #+# #+# */ +/* Updated: 2023/02/02 20:19:25 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + c -= 32; + return (c); +} diff --git a/Libft/get_next_line.c b/Libft/get_next_line.c new file mode 100644 index 0000000..da04a89 --- /dev/null +++ b/Libft/get_next_line.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 20:41:22 by grobledo #+# #+# */ +/* Updated: 2023/05/05 20:41:25 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_join( char *buffer, char *buff_read) +{ + char *temp; + + temp = ft_strjoing(buffer, buff_read); + free (buffer); + return (temp); +} + +char *ft_nextline(char *buffer) +{ + int i; + int j; + char *nextline; + + i = 0; + while (buffer[i] && buffer[i] != '\n') + i++; + if (buffer[i] == '\n') + i++; + if (!buffer[i]) + { + free (buffer); + return (NULL); + } + j = 0; + nextline = ft_calloc(ft_strleng(buffer) - i + 1, sizeof(char)); + while (buffer[i]) + { + nextline[j++] = buffer[i++]; + } + free (buffer); + return (nextline); +} + +char *ft_line(char *buffer) +{ + char *line; + int i; + + i = 0; + if (!buffer) + return (NULL); + while (buffer[i] && buffer[i] != '\n') + i++; + line = ft_calloc(i + 2, sizeof(char)); + i = 0; + while (buffer[i] && buffer[i] != '\n') + { + line[i] = buffer[i]; + i++; + } + if (buffer[i] && buffer[i] == '\n') + line[i] = '\n'; + return (line); +} + +char *ft_readfile(int fd, char *buffer) +{ + int byte_read; + char *buff_read; + + buff_read = ft_calloc(BUFFER_SIZE + 1, sizeof(char)); + byte_read = 1; + while (byte_read > 0) + { + byte_read = read(fd, buff_read, BUFFER_SIZE); + if (byte_read == 0) + break ; + if (byte_read == -1) + { + free(buff_read); + return (NULL); + } + buff_read[byte_read] = 0; + buffer = ft_join(buffer, buff_read); + if (ft_strrchr(buff_read, '\n')) + break ; + } + free (buff_read); + return (buffer); +} + +char *get_next_line(int fd) +{ + char *line; + static char *buffer = NULL; + + if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0) + return (NULL); + buffer = ft_readfile(fd, buffer); + if (!buffer) + return (NULL); + line = ft_line(buffer); + buffer = ft_nextline(buffer); + return (line); +} diff --git a/Libft/get_next_line_utils.c b/Libft/get_next_line_utils.c new file mode 100644 index 0000000..6878058 --- /dev/null +++ b/Libft/get_next_line_utils.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 20:41:39 by grobledo #+# #+# */ +/* Updated: 2023/05/05 20:41:41 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +size_t ft_strleng(const char *str) +{ + size_t i; + + i = 0; + while (str && str[i]) + { + i++; + } + return (i); +} + +char *ft_strjoing(char const *s1, char const *s2) +{ + size_t i; + size_t j; + char *dest; + + if (!s2) + return (NULL); + dest = ft_calloc ((ft_strleng(s1) + ft_strleng(s2) + 1), sizeof (char)); + if (dest == 0) + return (NULL); + i = 0; + j = 0; + while (s2[j]) + { + while (i < ft_strleng(s1)) + { + dest[i] = s1[i]; + i++; + } + while (j < ft_strleng(s2)) + { + dest[i + j] = s2[j]; + j++; + } + } + return (dest); +} diff --git a/Libft/libft.h b/Libft/libft.h new file mode 100644 index 0000000..c98ec10 --- /dev/null +++ b/Libft/libft.h @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/10 12:14:34 by grobledo #+# #+# */ +/* Updated: 2023/02/13 21:31:19 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ +#ifndef LIBFT_H +# define LIBFT_H +# include +# include +# include +# include +# include +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 10000 +# endif + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_atoi(const char *str); +void ft_bzero(void *s, size_t n); +void *ft_calloc(size_t count, size_t size); +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +void *ft_memchr(const void *str, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *str, int c, size_t n); +char *ft_strchr(const char *str, int tofind); +char *ft_strdup(const char *src); +int ft_strlcat(char *dest, const char *src, size_t size); +int ft_strlcpy(char *dest, const char *src, size_t size); +size_t ft_strlen(const char *str); +int ft_strncmp(const char *s1, const char *s2, size_t size); +char *ft_strrchr(const char *str, int tofind); +int ft_tolower(int c); +int ft_toupper(int c); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(const char *s1, const char *set); +char *ft_itoa(int nb); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +char **ft_split(const char *s, char c); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void*)); +void ft_lstclear(t_list **lst, void (*del)(void*)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(f)(void *), void (*del)(void *)); +int ft_hexa(unsigned int args, char format, int count); +int ft_format(const char format, va_list args, int count); +int ft_printf(const char *format, ...); +int ft_putstrf(char *s, int count); +int ft_putcharf(char c, int count); +int ft_putnbrposf(unsigned int n, int count); +int ft_putnbrf(int n, int count); +int ft_starthexa(unsigned int args, char format, int count); +int ft_pointer(unsigned long long int args, char format, int count); +int ft_startpointer(unsigned long long int args, char format, int count); +char *get_next_line(int fd); +size_t ft_strleng(const char *str); +char *ft_strjoing(char const *s1, char const *s2); +#endif diff --git a/Makefile b/Makefile index 8ba5229..2e563ef 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: greg +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/07/29 13:08:42 by greg #+# #+# # -# Updated: 2024/10/03 17:00:43 by greg ### ########.fr # +# Updated: 2024/10/09 03:07:50 by greg ### ########.fr # # # # **************************************************************************** # @@ -24,15 +24,15 @@ CC = gcc -g CFLAGS = -Wall -Werror -Wextra -obj : ${OBJS} - all: $(NAME) +obj : ${OBJS} + $(NAME): ${OBJS} make -C ${LIBFT} make -C ${MLX} - gcc ${OBJS} Libft/libft.a Minilibx/libmlx.a -lX11 -lXext -o cub3d + gcc ${OBJS} Libft/libft.a Minilibx/libmlx.a -lX11 -lXext -o cub3d -lm clean : rm -f ${OBJS} diff --git a/Minilibx b/Minilibx new file mode 160000 index 0000000..7dc53a4 --- /dev/null +++ b/Minilibx @@ -0,0 +1 @@ +Subproject commit 7dc53a411a7d4ae286c60c6229bd1e395b0efb82 diff --git a/algo.c b/algo.c index b67db6a..62414a9 100644 --- a/algo.c +++ b/algo.c @@ -6,11 +6,41 @@ /* By: greg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ -/* Updated: 2024/10/03 18:59:06 by greg ### ########.fr */ +/* Updated: 2024/10/09 03:30:08 by greg ### ########.fr */ /* */ /* ************************************************************************** */ #include "algo.h" +#include +#include + +int worldMap[mapWidth][mapHeight]= +{ + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1}, + {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1}, + {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} +}; static int initalgo(t_ray *ray) @@ -21,20 +51,60 @@ static int initalgo(t_ray *ray) ray->dirY = 0; ray->planeX = 0; ray->planeY = 0.66; // FOV de 66 degres - ray->currtime = 0; - ray->oldtime = 0; ray->movespeed = 0.1; ray->rotspeed = 0.1; return (0); } +static int keypress(int keycode, t_ray *ray, int **worldMap) +{ + //move forward if no wall in front of you + if (keycode == 119) + { + if(worldMap[(int)(ray->posX + ray->dirX * ray->movespeed)][(int)(ray->posY)] == 0) + ray->posX += ray->dirX * ray->movespeed; + if(worldMap[(int)(ray->posX)][((int)ray->posY + (int)ray->dirY * (int)ray->movespeed)] == 0) + ray->posY += ray->dirY * ray->movespeed; + } + //move backwards if no wall behind you + if (keycode == 115) + { + if(worldMap[(int)(ray->posX - ray->dirX * ray->movespeed)][(int)(ray->posY)] == 0) + ray->posX -= ray->dirX * ray->movespeed; + if(worldMap[(int)(ray->posX)][((int)ray->posY - (int)ray->dirY * (int)ray->movespeed)] == 0) + ray->posY -= ray->dirY * ray->movespeed; + } + //rotate to the right + if (keycode == 100) + { + //both camera direction and camera plane must be rotated + ray->oldDirX = ray->dirX; + ray->dirX = ray->dirX * cos(-ray->rotspeed) - ray->dirY * sin(-ray->rotspeed); + ray->dirY = ray->oldDirX * sin(-ray->rotspeed) + ray->dirY * cos(-ray->rotspeed); + ray->oldPlaneX = ray->planeX; + ray->planeX = ray->planeX * cos(-ray->rotspeed) - ray->planeY * sin(-ray->rotspeed); + ray->planeY = ray->oldPlaneX * sin(-ray->rotspeed) + ray->planeY * cos(-ray->rotspeed); + } + //rotate to the left + if (keycode == 97) + { + //both camera direction and camera plane must be rotated + ray->oldDirX = ray->dirX; + ray->dirX = ray->dirX * cos(ray->rotspeed) - ray->dirY * sin(ray->rotspeed); + ray->dirY = ray->oldDirX * sin(ray->rotspeed) + ray->dirY * cos(ray->rotspeed); + ray->oldPlaneX = ray->planeX; + ray->planeX = ray->planeX * cos(ray->rotspeed) - ray->planeY * sin(ray->rotspeed); + ray->planeY = ray->oldPlaneX * sin(ray->rotspeed) + ray->planeY * cos(ray->rotspeed); + } + return (0); +} -int algo() +int main(int /*argc*/, char */*argv*/[]) { t_ray ray; int x; double width; - int height; + double height; // quelle box sur la map on est int mapX; int mapY; @@ -43,12 +113,17 @@ int algo() int side; initalgo(&ray); - // init image; - x = 0; width = 640; height = 480; + + void *mlx_ptr; + void *win_ptr; + + mlx_ptr = mlx_init(); // Initialise la connexion à la MLX + win_ptr = mlx_new_window(mlx_ptr, width, height, "cub3d"); + while (x < width) { // position et direction du rayon @@ -118,67 +193,28 @@ int algo() if(drawStart < 0) drawStart = 0; int drawEnd; + drawEnd = lineHeight / 2 + height / 2; if(drawEnd >= height) drawEnd = height - 1; - - /*mlx fontction color - - give x and y sides different brightness - if (side == 1) {color = color / 2;} + int y = drawStart; + while (y <= drawEnd) + { + mlx_pixel_put(mlx_ptr, win_ptr, x, y, 0xFF0000); + y++; + } + x++; + } //draw the pixels of the stripe as a vertical line - verLine(x, drawStart, drawEnd, color); */ + //verLine(x, drawStart, drawEnd, color); */ - //timing for input to dissociate speed from speed processor (same speed on all pc) - ray.oldtime = ray.currtime; - ray.currtime = getTicks(); // put image to window // destroy old image + + mlx_hook(win_ptr, 2, 1L<<0, keypress, &ray); - // hook event - - readKeys(); - //move forward if no wall in front of you - if (keyDown(move_up)) - { - if(worldMap[(ray.posX + ray.dirX * ray.movespeed)][(int)(ray.posY)] == 0) - ray.posX += ray.dirX * ray.movespeed; - if(worldMap[(ray.posX)][((int)ray.posY + (int)ray.dirY * (int)ray.movespeed)] == 0) - ray.posY += ray.dirY * ray.movespeed; - } - //move backwards if no wall behind you - if (keyDown(moove_down)) - { - if(worldMap[(ray.posX - ray.dirX * ray.movespeed)][(int)(ray.posY)] == 0) - ray.posX -= ray.dirX * ray.movespeed; - if(worldMap[(ray.posX)][((int)ray.posY - (int)ray.dirY * (int)ray.movespeed)] == 0) - ray.posY -= ray.dirY * ray.movespeed; - } - //rotate to the right - if (keyDown(moove_right)) - { - //both camera direction and camera plane must be rotated - ray.oldDirX = ray.dirX; - ray.dirX = ray.dirX * cos(-ray.rotspeed) - ray.dirY * sin(-ray.rotspeed); - ray.dirY = ray.oldDirX * sin(-ray.rotspeed) + ray.dirY * cos(-ray.rotspeed); - ray.oldPlaneX = ray.planeX; - ray.planeX = ray.planeX * cos(-ray.rotspeed) - ray.planeY * sin(-ray.rotspeed); - ray.planeY = ray.oldPlaneX * sin(-ray.rotspeed) + ray.planeY * cos(-ray.rotspeed); - } - //rotate to the left - if (keyDown(moove_left)) - { - //both camera direction and camera plane must be rotated - ray.oldDirX = ray.dirX; - ray.dirX = ray.dirX * cos(ray.rotspeed) - ray.dirY * sin(ray.rotspeed); - ray.dirY = ray.oldDirX * sin(ray.rotspeed) + ray.dirY * cos(ray.rotspeed); - ray.oldPlaneX = ray.planeX; - ray.planeX = ray.planeX * cos(ray.rotspeed) - ray.planeY * sin(ray.rotspeed); - ray.planeY = ray.oldPlaneX * sin(ray.rotspeed) + ray.planeY * cos(ray.rotspeed); - } - } return (0); } diff --git a/algo.h b/algo.h index acb137f..47a1836 100644 --- a/algo.h +++ b/algo.h @@ -6,7 +6,7 @@ /* By: greg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ -/* Updated: 2024/10/03 18:59:43 by greg ### ########.fr */ +/* Updated: 2024/10/09 02:51:53 by greg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,10 @@ #include #include +#include "Minilibx/mlx.h" +#include "Minilibx/mlx_int.h" +#define mapWidth 24 +#define mapHeight 24 typedef struct s_ray {