Browse Source

README and getline start

master
narnaud 3 years ago
parent
commit
72f09acc43
  1. 52
      Makefile
  2. 58
      README.md
  3. 44
      includes/cub3d.h
  4. 62
      libft/Makefile
  5. 40
      libft/conv/ft_atoi.c
  6. 42
      libft/conv/ft_itoa.c
  7. 35
      libft/conv/ft_itox.c
  8. 19
      libft/conv/ft_tolower.c
  9. 19
      libft/conv/ft_toupper.c
  10. 33
      libft/conv/ft_utoa.c
  11. 27
      libft/dlist/ft_add.c
  12. 20
      libft/dlist/ft_n.c
  13. 41
      libft/dlist/ft_to_arr.c
  14. 32
      libft/i_slist/ft_first.c
  15. 25
      libft/i_slist/ft_free.c
  16. 27
      libft/i_slist/ft_is_in.c
  17. 21
      libft/is/ft_isalnum.c
  18. 21
      libft/is/ft_isalpha.c
  19. 21
      libft/is/ft_isascii.c
  20. 21
      libft/is/ft_isdigit.c
  21. 20
      libft/is/ft_islower.c
  22. 26
      libft/is/ft_isnumber.c
  23. 21
      libft/is/ft_isprint.c
  24. 22
      libft/is/ft_isspace.c
  25. 20
      libft/is/ft_isupper.c
  26. 108
      libft/libft.h
  27. 22
      libft/mem/ft_bzero.c
  28. 24
      libft/mem/ft_calloc.c
  29. 26
      libft/mem/ft_free_split.c
  30. 27
      libft/mem/ft_memchr.c
  31. 29
      libft/mem/ft_memcmp.c
  32. 28
      libft/mem/ft_memcpy.c
  33. 33
      libft/mem/ft_memmove.c
  34. 23
      libft/mem/ft_memset.c
  35. 18
      libft/nbr/ft_croissant.c
  36. 18
      libft/nbr/ft_decroissant.c
  37. 21
      libft/nbr/ft_max.c
  38. 21
      libft/nbr/ft_min.c
  39. 50
      libft/nbr/ft_nbrlen.c
  40. 18
      libft/put/ft_putchar_fd.c
  41. 21
      libft/put/ft_putendl_fd.c
  42. 61
      libft/put/ft_putnbr_fd.c
  43. 21
      libft/put/ft_putstr_fd.c
  44. 28
      libft/slist/ft_add_back.c
  45. 22
      libft/slist/ft_add_front.c
  46. 27
      libft/slist/ft_clear.c
  47. 22
      libft/slist/ft_delone.c
  48. 22
      libft/slist/ft_iter.c
  49. 22
      libft/slist/ft_last.c
  50. 29
      libft/slist/ft_map.c
  51. 25
      libft/slist/ft_new.c
  52. 23
      libft/slist/ft_size.c
  53. 27
      libft/str/ft_chr.c
  54. 31
      libft/str/ft_dup.c
  55. 27
      libft/str/ft_iteri.c
  56. 41
      libft/str/ft_join.c
  57. 27
      libft/str/ft_join_with.c
  58. 37
      libft/str/ft_lcat.c
  59. 30
      libft/str/ft_lcpy.c
  60. 33
      libft/str/ft_len.c
  61. 32
      libft/str/ft_mapi.c
  62. 34
      libft/str/ft_ncmp.c
  63. 42
      libft/str/ft_nstr.c
  64. 33
      libft/str/ft_rchr.c
  65. 32
      libft/str/ft_rev.c
  66. 42
      libft/str/ft_split.c
  67. 42
      libft/str/ft_sub.c
  68. 53
      libft/str/ft_trim.c
  69. 88
      mlx/.github/workflows/ci.yml
  70. 67
      mlx/.gitignore
  71. 25
      mlx/LICENSE
  72. 22
      mlx/Makefile
  73. 66
      mlx/Makefile.mk
  74. 55
      mlx/README.md
  75. 126
      mlx/configure
  76. 93
      mlx/man/man1/mlx.1
  77. 141
      mlx/man/man1/mlx_loop.1
  78. 192
      mlx/man/man1/mlx_new_image.1
  79. 79
      mlx/man/man1/mlx_new_window.1
  80. 84
      mlx/man/man1/mlx_pixel_put.1
  81. 93
      mlx/man/man3/mlx.3
  82. 141
      mlx/man/man3/mlx_loop.3
  83. 192
      mlx/man/man3/mlx_new_image.3
  84. 79
      mlx/man/man3/mlx_new_window.3
  85. 81
      mlx/man/man3/mlx_pixel_put.3
  86. 139
      mlx/mlx.h
  87. 21
      mlx/mlx_clear_window.c
  88. 18
      mlx/mlx_destroy_display.c
  89. 31
      mlx/mlx_destroy_image.c
  90. 38
      mlx/mlx_destroy_window.c
  91. 22
      mlx/mlx_expose_hook.c
  92. 104
      mlx/mlx_ext_randr.c
  93. 25
      mlx/mlx_flush_event.c
  94. 33
      mlx/mlx_get_color_value.c
  95. 23
      mlx/mlx_get_data_addr.c
  96. 40
      mlx/mlx_hook.c
  97. 99
      mlx/mlx_init.c
  98. 140
      mlx/mlx_int.h
  99. 28
      mlx/mlx_int_anti_resize_win.c
  100. 16
      mlx/mlx_int_do_nothing.c

52
Makefile

@ -0,0 +1,52 @@
NAME = cub3d
LIBFT = libft.a
SRCS = sources/cub3d.c
OBJS = $(SRCS:.c=.o)
CC = gcc
RM = rm -rf
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S), Linux)
LFLAGS = -L ./mlx -lmlx_Linux -lXext -lX11 -lm -lz -L. -lft
endif
ifeq ($(UNAME_S), Darwin)
LFLAGS = -lmlx -framework OpenGL -framework AppKit -L. -lft
endif
CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations
$(LIBFT):
#@git clone
@${MAKE} -C libft/
@cp libft/libft.a .
@cp libft/libft.h includes/
#@${RM} libft/
%.o:%.c
@${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
all: $(NAME)
$(NAME): $(LIBFT) $(OBJS)
@echo "Making Cub3D."
@${CC} -g ${OBJS} -o ${NAME} ${LFLAGS}
@echo "Done."
clean:
@echo "Cleaning objects."
@${RM} ${OBJS}
@echo "Done."
fclean: clean
@echo "Cleaning libft."
@${MAKE} -C libft fclean
@${RM} libft.a
@${RM} includes/libft.h
@echo "Cleaning binary."
@${RM} ${NAME}
@echo "Done."
re: fclean all
.PHONY: all clean fclean re

58
README.md

@ -1,2 +1,58 @@
# Cub3D
CUBE3D
===
[Subject](https://cdn.intra.42.fr/pdf/pdf/48490/fr.subject.pdf)
## Program life cycle :
- map parsing :
- extract textures,
- verify map format`
- import map `into array
- save player pos
- verify map validy
- initialize env
- generate window
- display view
- enable commands
- react to commands :
- calculate new pos,
- display new view,
- clean and exit
## thoughts :
## verify map validity :
The map must be closed with the player inside the walls.
- calloc a 2D array as X size is the size of the longer
line and y is the numbers of map lines.
- set the array cells to 1 for floor, 2 for walls and 3 for player.
- outside cells will be zeros.
- e.g :
0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 2 2 2 2 0 0 0 0 0
0 0 0 2 1 1 1 1 1 1 1 1 1 1 2 0 0 0 2 2 2 1 1 2 0 0 0 0 0
2 2 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 2 2 2 0 0 0
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 2
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 2 2 2 2 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 1 1 1 1 1 1 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- read line by line until you encounter first wall then turn around
the map following the walls and never going on an already visited
wall until you come back to starting pos.
## initialize env :
- required datas : initial player pov angle,
- walls height
- eyes positions from screen ( -> field of view)
- calculate fov rays unit vectors,
## display view :
- calculate distance between player and intersection between rays and walls
- calculate viewed height of walls for each verticle
- draw walls, floor and sky

44
includes/cub3d.h

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */
/* Updated: 2022/05/22 14:10:24 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CUB3D_H
# define CUB3D_H
# define UNIT 100
# define WALL_HEIGHT 200
# define PLAYER_HEIGHT 170
# define SCREEN_DISTANCE 50
# include <unistd.h>
# include <stdio.h>
# include <fcntl.h>
# include <errno.h>
# include <sting.h>
# include <math.h>
# include "../includes/libft.h"
# ifdef __linux__
# include "../includes/mlx.h"
# elif __APPLE__
# include <mlx.h>
# endif
# include "mlx_keycode.h"
typedef struct s_vector
{
int x;
int y;
int z;
} t_vec;
char *get_next_line(int fd);
#endif

62
libft/Makefile

@ -0,0 +1,62 @@
NAME = libft.a
SRCS = is/ft_isalpha.c is/ft_isdigit.c is/ft_isascii.c is/ft_isprint.c \
is/ft_isalnum.c is/ft_isspace.c is/ft_isnumber.c
SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \
str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \
str/ft_sub.c str/ft_join.c str/ft_trim.c str/ft_split.c \
str/ft_rev.c str/ft_mapi.c str/ft_iteri.c str/ft_join_with.c
SRCS += conv/ft_toupper.c conv/ft_tolower.c conv/ft_atoi.c conv/ft_itoa.c \
conv/ft_itox.c conv/ft_utoa.c
SRCS += mem/ft_bzero.c mem/ft_memset.c mem/ft_memchr.c mem/ft_memcpy.c \
mem/ft_memcmp.c mem/ft_memmove.c mem/ft_calloc.c mem/ft_free_split.c
SRCS += put/ft_putchar_fd.c put/ft_putnbr_fd.c put/ft_putendl_fd.c \
put/ft_putstr_fd.c
SRCS += nbr/ft_nbrlen.c nbr/ft_croissant.c nbr/ft_decroissant.c nbr/ft_max.c \
nbr/ft_min.c
SRCS += slist/ft_size.c slist/ft_new.c slist/ft_last.c \
slist/ft_add_back.c slist/ft_add_front.c slist/ft_delone.c \
slist/ft_clear.c slist/ft_iter.c slist/ft_map.c
SRCS += i_slist/ft_free.c i_slist/ft_first.c i_slist/ft_is_in.c
SRCS += dlist/ft_add.c dlist/ft_n.c dlist/ft_to_arr.c
OBJS = $(SRCS:.c=.o)
CC = gcc
AR = ar -rcs
RM = rm -rf
CFLAGS = -Wall -Wextra -Werror
.c.o:
@${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
all : $(NAME)
$(NAME): $(OBJS)
@echo "Making libft."
@${AR} ${NAME} ${OBJS}
@echo "Done."
clean:
@echo "Cleaning libft."
@${RM} ${OBJS}
@${RM} ${OBJS_BONUS}
@echo "Done."
fclean: clean
@${RM} ${NAME}
re: fclean all
rebonus: fclean bonus
.PHONY: all bonus re rebonus clean fclean

40
libft/conv/ft_atoi.c

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 17:06:25 by narnaud #+# #+# */
/* Updated: 2022/03/25 14:14:45 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_atoi(const char *str)
{
int sign;
double nb;
nb = 0;
sign = 1;
if (!ft_strncmp(str, "-2147483648", 11))
return (-2147483648);
while (*str == ' ' || (*str >= '\t' && *str <= '\r'))
str++;
if (*str == '-' && ++str)
sign = -1;
else if (*str == '+')
str++;
while (*str >= '0' && *str <= '9')
{
nb = nb * 10 + (*str - '0') % 10;
if (nb > 2147483647 && sign > 0)
return (-1);
else if (nb > 2147486648 && sign < 0)
return (0);
str++;
}
return ((int)(sign * nb));
}

42
libft/conv/ft_itoa.c

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 15:50:47 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_itoa(int n)
{
char *ret;
unsigned int i;
unsigned int neg;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
if (n == 0)
return (ft_strdup("0"));
i = 0;
neg = 0;
ret = ft_calloc(ft_ilen(n) + 1, sizeof(char));
if (!ret)
return (NULL);
if (n < 0)
{
n = -n;
neg = 1;
}
while (n)
{
ret[i++] = (n % 10) + '0';
n = n / 10;
}
ft_strrev(&ret, neg);
return (ret);
}

35
libft/conv/ft_itox.c

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itox.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/21 09:51:26 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_itox(unsigned long int n, const char *base)
{
char *ret;
size_t i;
unsigned int neg;
size_t size;
size = ft_longbaselen(n, 16);
if (n == 0)
return (ft_strdup("0"));
i = 0;
neg = 0;
ret = ft_calloc(size + 1, sizeof(char));
while (n)
{
ret[i++] = base[n % 16];
n = n / 16;
}
ft_strrev(&ret, neg);
return (ret);
}

19
libft/conv/ft_tolower.c

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 17:02:25 by narnaud #+# #+# */
/* Updated: 2021/10/19 12:35:58 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
int ft_tolower(int c)
{
if (c > 64 && c < 91)
return (c + 32);
else
return (c);
}

19
libft/conv/ft_toupper.c

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 17:02:28 by narnaud #+# #+# */
/* Updated: 2021/10/19 12:36:28 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
int ft_toupper(int c)
{
if (c > 96 && c < 123)
return (c - 32);
else
return (c);
}

33
libft/conv/ft_utoa.c

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/21 09:48:38 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_utoa(unsigned int n)
{
char *ret;
unsigned int i;
if (n == 4294967295)
return (ft_strdup("4294967295"));
if (n == 0)
return (ft_strdup("0"));
i = 0;
ret = ft_calloc(ft_ulen(n) + 1, sizeof(char));
while (n)
{
ret[i++] = (n % 10) + '0';
n = n / 10;
}
ft_strrev(&ret, 0);
return (ret);
}

27
libft/dlist/ft_add.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 10:46:25 by narnaud #+# #+# */
/* Updated: 2022/03/25 14:15:13 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
t_dlist *ft_dlst_add(t_dlist *prev, void *content)
{
t_dlist *new;
if (!content)
return (prev);
new = ft_calloc(1, sizeof(t_dlist));
if (prev)
prev->next = new;
new->previous = prev;
new->content = content;
return (new);
}

20
libft/dlist/ft_n.c

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_n.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 10:51:20 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:27:40 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
t_dlist *ft_dlst_n(t_dlist *lst, size_t n)
{
while (n-- && lst)
lst = lst->next;
return (lst);
}

41
libft/dlist/ft_to_arr.c

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_to_arr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 10:50:10 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:33:57 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char **ft_dlst_to_arr(t_dlist *ptr)
{
char **ret;
t_dlist *last;
t_dlist *tmp;
size_t count;
last = ptr;
count = 1;
if (!ptr)
return (ft_calloc(1, sizeof(char *)));
while (ptr->previous)
{
ptr = ptr->previous;
count++;
}
ret = ft_calloc(count + 1, sizeof(char *));
ret[count] = NULL;
while (count--)
{
ret[count] = last->content;
tmp = last->previous;
free(last);
last = tmp;
}
return (ret);
}

32
libft/i_slist/ft_first.c

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_first.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/25 09:06:44 by narnaud #+# #+# */
/* Updated: 2022/03/25 13:26:04 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
size_t ft_ilst_first(t_i_slist *lst, int (*fct)(int a, int b))
{
int previous;
size_t i;
i = 1;
if (!lst)
return (0);
while (lst)
{
previous = lst->nb;
lst = lst->next;
if (lst && fct(previous, lst->nb))
return (i);
i++;
}
return (-1);
}

25
libft/i_slist/ft_free.c

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/25 09:27:41 by narnaud #+# #+# */
/* Updated: 2022/03/25 14:18:37 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_ilst_free(t_i_slist *list)
{
t_i_slist *next;
while (list)
{
next = list->next;
free(list);
list = next;
}
}

27
libft/i_slist/ft_is_in.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_in.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/25 13:22:49 by narnaud #+# #+# */
/* Updated: 2022/03/25 14:19:02 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_ilst_is_in(int value, t_i_slist lst)
{
if (lst.nb == value && lst.next)
return (1);
while (lst.next)
{
if (lst.nb == value)
return (1);
else
lst = *(lst.next);
}
return (0);
}

21
libft/is/ft_isalnum.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:13:39 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isalnum(int ch)
{
if (ft_isalpha(ch) || ft_isdigit(ch))
return (1);
else
return (0);
}

21
libft/is/ft_isalpha.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 15:38:41 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isalpha(int ch)
{
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
return (1);
else
return (0);
}

21
libft/is/ft_isascii.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:14:38 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isascii(int ch)
{
if (ch >= 0 && ch < 128)
return (1);
else
return (0);
}

21
libft/is/ft_isdigit.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:13:16 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isdigit(int ch)
{
if (ch >= '0' && ch <= '9')
return (1);
else
return (0);
}

20
libft/is/ft_islower.c

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_islower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 10:04:18 by narnaud #+# #+# */
/* Updated: 2022/03/24 10:07:46 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_islower(char c)
{
if (c >= 'a' && c <= 'z')
return (1);
return (0);
}

26
libft/is/ft_isnumber.c

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isnumber.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/18 16:18:15 by narnaud@stude #+# #+# */
/* Updated: 2022/05/18 16:21:52 by narnaud@stude ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isnumber(char *str)
{
if (*str == '-' || *str == '+')
str++;
while (*str)
{
if (!ft_isdigit(*str))
return (0);
str++;
}
return (1);
}

21
libft/is/ft_isprint.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:14:02 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isprint(int ch)
{
if (ch > 31 && ch < 127)
return (1);
else
return (0);
}

22
libft/is/ft_isspace.c

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 10:05:51 by narnaud #+# #+# */
/* Updated: 2022/05/03 09:47:28 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isspace(int c)
{
if (c > 8 && c < 14)
return (1);
if (c == ' ')
return (1);
return (0);
}

20
libft/is/ft_isupper.c

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 10:06:18 by narnaud #+# #+# */
/* Updated: 2022/03/24 10:07:15 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_isupper(char c)
{
if (c >= 'A' && c <= 'Z')
return (1);
return (0);
}

108
libft/libft.h

@ -0,0 +1,108 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */
/* Updated: 2022/05/18 16:25:27 by narnaud@stude ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.h>
# include <stdlib.h>
# include <unistd.h>
typedef struct s_slist
{
void *content;
struct s_slist *next;
} t_slist;
typedef struct s_i_slist
{
int nb;
struct s_i_slist *next;
} t_i_slist;
typedef struct s_dlist
{
void *content;
struct s_dlist *next;
struct s_dlist *previous;
} t_dlist;
int ft_atoi(const char *str);
void ft_bzero(void *s, size_t n);
void *ft_calloc( size_t num, size_t size );
int ft_isalpha(int ch);
int ft_isascii(int ch);
int ft_isdigit(int ch);
int ft_isprint(int ch);
int ft_isalnum(int ch);
int ft_isspace(int ch);
int ft_isnumber(char *str);
void *ft_memchr(const void *b, 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_memmove(void *dst, const void *src, size_t n);
void ft_free_split(char **split);
size_t ft_ilen(int nbr);
size_t ft_ulen(unsigned int nbr);
size_t ft_longbaselen(long nbr, size_t base);
int ft_croissant(int a, int b);
int ft_decroissant(int a, int b);
int ft_max(int a, int b);
int ft_min(int a, int b);
void *ft_memset(void *b, int c, size_t len);
char *ft_strchr(const char *s, int c);
char *ft_strdup(const char *src);
size_t ft_strlcat(char *dst, const char *src, size_t dstsize);
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
size_t ft_strlen(const char *s);
size_t ft_strlen_to(const char *str, char ch);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strnstr(const char *haystack, const char *needle, size_t len);
char *ft_strrchr(const char *s, int c);
void ft_strrev(char **str, unsigned int neg);
int ft_tolower(int c);
int ft_toupper(int c);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(const char *s1, const char *s2);
char *ft_strjoin_with(char *s1, char *s2, char *inter);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *str, char ch);
char *ft_itoa(int n);
char *ft_itox(unsigned long n, const char *base);
char *ft_utoa(unsigned int n);
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);
t_slist *ft_slst_new(void *content);
void ft_slst_add_front(t_slist **alst, t_slist *new);
int ft_slst_size(t_slist *lst);
t_slist *ft_slst_last(t_slist *lst);
void ft_slst_add_back(t_slist **alst, t_slist *new);
void ft_slst_delone(t_slist *lst, void (*del)(void *));
void ft_slst_clear(t_slist **lst, void (*del)(void *));
void ft_slst_iter(t_slist *lst, void (*f)(void *));
t_slist *ft_slst_map(t_slist *lst, void *(*f)(void *), void (*del)(void *));
size_t ft_ilst_first(t_i_slist *lst, int (*fct)(int a, int b));
void ft_ilst_free(t_i_slist *list);
int ft_ilst_is_in(int value, t_i_slist lst);
t_dlist *ft_dlst_add(t_dlist *prev, void *content);
t_dlist *ft_dlst_n(t_dlist *lst, size_t n);
char **ft_dlst_to_arr(t_dlist *ptr);
#endif

22
libft/mem/ft_bzero.c

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 11:46:58 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_bzero(void *s, size_t n)
{
size_t i;
i = 0;
while (i < n)
*(char *)(s + i++) = 0;
}

24
libft/mem/ft_calloc.c

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:11:11 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void *ft_calloc(size_t count, size_t size)
{
void *ret;
ret = malloc(count * size);
if (!ret)
return (NULL);
ft_bzero(ret, count * size);
return (ret);
}

26
libft/mem/ft_free_split.c

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/25 08:55:20 by narnaud #+# #+# */
/* Updated: 2022/03/25 08:57:26 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_free_split(char **split)
{
int i;
i = 0;
while (split[i])
{
free(split[i]);
i++;
}
free(split);
}

27
libft/mem/ft_memchr.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:15:55 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void *ft_memchr(const void *b, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (*(unsigned char *)(b + i) == (unsigned char)c)
return ((void *)(b + i));
i++;
}
return (NULL);
}

29
libft/mem/ft_memcmp.c

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:16:23 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
int diff;
i = 0;
while (i < n)
{
diff = *(unsigned char *)(s1 + i) - *(unsigned char *)(s2 + i);
if (diff)
return (diff);
i++;
}
return (0);
}

28
libft/mem/ft_memcpy.c

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:16:47 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
size_t i;
i = 0;
if (!src && !dst)
return (NULL);
while (i < n)
{
*(char *)(dst + i) = *(char *)(src + i);
i++;
}
return (dst);
}

33
libft/mem/ft_memmove.c

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:17:21 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void *ft_memmove(void *dst, const void *src, size_t n)
{
size_t i;
unsigned char *ptr_dst;
unsigned char *ptr_src;
ptr_dst = (unsigned char *)dst;
ptr_src = (unsigned char *)src;
i = 0;
if (!dst && !src)
return (NULL);
if (ptr_src < ptr_dst)
while (++i <= n)
ptr_dst[n - i] = ptr_src[n - i];
else
while (n--)
*(ptr_dst++) = *(ptr_src++);
return (dst);
}

23
libft/mem/ft_memset.c

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 11:43:44 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void *ft_memset(void *b, int c, size_t len)
{
size_t i;
i = 0;
while (i < len)
*(unsigned char *)(b + i++) = (unsigned char)c;
return ((void *)b);
}

18
libft/nbr/ft_croissant.c

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_croissant.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/25 09:14:45 by narnaud #+# #+# */
/* Updated: 2022/03/25 09:15:32 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_croissant(int a, int b)
{
return (a <= b);
}

18
libft/nbr/ft_decroissant.c

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_decroissant.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/25 09:14:45 by narnaud #+# #+# */
/* Updated: 2022/03/25 10:53:14 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_decroissant(int a, int b)
{
return (a > b);
}

21
libft/nbr/ft_max.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_max.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/07 22:56:56 by narnaud@stude #+# #+# */
/* Updated: 2022/05/07 22:57:21 by narnaud@stude ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_max(int a, int b)
{
if (a > b)
return (a);
else
return (b);
}

21
libft/nbr/ft_min.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_min.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/07 22:56:56 by narnaud@stude #+# #+# */
/* Updated: 2022/05/09 08:58:50 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_min(int a, int b)
{
if (a < b)
return (a);
else
return (b);
}

50
libft/nbr/ft_nbrlen.c

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nbrlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/21 09:42:43 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
size_t ft_ilen(int nbr)
{
size_t i;
i = 0;
if (nbr == 0)
return (1);
else if (nbr < 0)
i++;
while (nbr && ++i)
nbr = nbr / 10;
return (i);
}
size_t ft_ulen(unsigned int nbr)
{
size_t i;
i = 0;
while (nbr && ++i)
nbr = nbr / 10;
return (i);
}
size_t ft_longbaselen(long nbr, size_t base)
{
size_t i;
if (nbr <= 0)
i = 1;
else
i = 0;
while (nbr && ++i)
nbr = nbr / base;
return (i);
}

18
libft/put/ft_putchar_fd.c

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 16:32:52 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

21
libft/put/ft_putendl_fd.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:11:11 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_putendl_fd(char *s, int fd)
{
if (!s)
return ;
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

61
libft/put/ft_putnbr_fd.c

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:12:01 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
static size_t get_nbrlength(int nbr)
{
size_t i;
i = 0;
if (nbr == 0)
return (1);
while (nbr && ++i)
nbr = nbr / 10;
return (i);
}
static int pow_10(size_t n)
{
int ret;
if (!n)
return (1);
ret = 10;
while (--n)
ret *= 10;
return (ret);
}
void ft_putnbr_fd(int n, int fd)
{
int fact;
if (n == -2147483648)
{
ft_putstr_fd("-2147483648", fd);
return ;
}
if (n < 0)
{
ft_putchar_fd('-', fd);
n = -n;
}
fact = pow_10(get_nbrlength(n) - 1);
while (fact >= 10)
{
ft_putchar_fd(n / fact + '0', fd);
n = n % fact;
fact /= 10;
}
ft_putchar_fd(n + '0', fd);
}

21
libft/put/ft_putstr_fd.c

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 15:10:39 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_putstr_fd(char *s, int fd)
{
if (!s)
return ;
while (*s)
ft_putchar_fd(*(s++), fd);
}

28
libft/slist/ft_add_back.c

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:51:11 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_slst_add_back(t_slist **alst, t_slist *new)
{
t_slist *i_cell;
if (!alst || !new)
return ;
if (*alst)
{
i_cell = ft_slst_last(*alst);
i_cell->next = new;
}
else
*alst = new;
}

22
libft/slist/ft_add_front.c

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:48:11 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_slst_add_front(t_slist **alst, t_slist *new)
{
if (!alst || !new)
return ;
if (*alst)
new->next = *alst;
*alst = new;
}

27
libft/slist/ft_clear.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:53:04 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_slst_clear(t_slist **lst, void (*del)(void *))
{
t_slist *next_cell;
if (!del)
return ;
while (lst && *lst)
{
next_cell = (*lst)->next;
ft_slst_delone(*lst, del);
*lst = next_cell;
}
}

22
libft/slist/ft_delone.c

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:51:53 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_slst_delone(t_slist *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
(*del)(lst->content);
free(lst);
lst = NULL;
}

22
libft/slist/ft_iter.c

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:54:03 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_slst_iter(t_slist *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
(*f)(lst->content);
if (lst->next)
ft_slst_iter(lst->next, f);
}

22
libft/slist/ft_last.c

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:50:21 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
t_slist *ft_slst_last(t_slist *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

29
libft/slist/ft_map.c

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:54:31 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
t_slist *ft_slst_map(t_slist *lst, void *(*f)(void *), void (*del)(void *))
{
t_slist *new_lst;
if (!lst || !f)
return (NULL);
new_lst = ft_slst_new((*f)(lst->content));
if (!new_lst)
{
ft_slst_clear(&new_lst, del);
return (NULL);
}
new_lst->next = ft_slst_map(lst->next, f, del);
return (new_lst);
}

25
libft/slist/ft_new.c

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 17:36:09 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
t_slist *ft_slst_new(void *content)
{
t_slist *i_cell;
i_cell = (t_slist *)malloc(sizeof(*i_cell));
if (!i_cell)
return (NULL);
i_cell->content = content;
i_cell->next = NULL;
return (i_cell);
}

23
libft/slist/ft_size.c

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 14:49:32 by narnaud #+# #+# */
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_slst_size(t_slist *lst)
{
size_t lst_size;
lst_size = 0;
while (lst && ++lst_size)
lst = lst->next;
return (lst_size);
}

27
libft/str/ft_chr.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 16:21:06 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strchr(const char *s, int c)
{
size_t i;
i = 0;
while (s[i] || c == '\0')
{
if (s[i] == (c % 256))
return ((char *)(s + i));
i++;
}
return (0);
}

31
libft/str/ft_dup.c

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:07:50 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strdup(const char *src)
{
char *dup;
size_t i;
i = 0;
dup = malloc((ft_strlen(src) + 1) * sizeof(char));
if (!dup)
return (NULL);
while (src[i])
{
dup[i] = src[i];
i++;
}
dup[i] = '\0';
return (dup);
}

27
libft/str/ft_iteri.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 16:23:41 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
i = 0;
if (!s || !f)
return ;
while (s[i])
{
f(i, s + i);
i++;
}
}

41
libft/str/ft_join.c

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 06:49:25 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strjoin(const char *s1, const char *s2)
{
char *ret;
size_t i;
size_t j;
if (!s1 || !s2)
return (NULL);
ret = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (ret == NULL)
return (NULL);
i = 0;
while (s1[i])
{
ret[i] = s1[i];
i++;
}
j = 0;
while (s2[j])
{
ret[i] = s2[j];
i++;
j++;
}
ret[i] = '\0';
return (ret);
}

27
libft/str/ft_join_with.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_join_with.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/17 09:44:59 by narnaud #+# #+# */
/* Updated: 2022/05/17 09:56:37 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strjoin_with(char *s1, char *s2, char *inter)
{
int path_length;
char *ret;
path_length = ft_strlen(s1) + ft_strlen(s2) + ft_strlen(inter) + 1;
ret = ft_calloc(path_length, sizeof(char));
ft_memcpy(ret, s1, ft_strlen(s1));
ft_memcpy(ret + ft_strlen(s1), inter, ft_strlen(inter));
ft_memcpy(ret + ft_strlen(s1) + ft_strlen(inter), \
s2, ft_strlen(s2));
return (ret);
}

37
libft/str/ft_lcat.c

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 16:13:02 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (dst[i])
i++;
while (dstsize && i + j < dstsize - 1 && src[j])
{
dst[i + j] = src[j];
j++;
}
if (dstsize)
dst[i + j] = '\0';
while (src[j])
j++;
if (i > dstsize)
return (dstsize + j);
else
return (i + j);
}

30
libft/str/ft_lcpy.c

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 16:08:31 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
{
size_t i;
i = 0;
while (dstsize && i < dstsize - 1 && src[i])
{
dst[i] = src[i];
i++;
}
if (dstsize)
dst[i] = '\0';
while (src[i])
i++;
return (i);
}

33
libft/str/ft_len.c

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 16:04:47 by narnaud #+# #+# */
/* Updated: 2022/03/25 11:57:59 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
size_t ft_strlen_to(const char *str, char ch)
{
size_t i;
i = 0;
while (str[i] && str[i] != ch)
i++;
return (i);
}
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}

32
libft/str/ft_mapi.c

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 16:12:32 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *ret;
if (!s)
return (NULL);
ret = (char *)ft_calloc(ft_strlen(s) + 1, sizeof(char));
if (!ret)
return (NULL);
i = 0;
while (s[i])
{
ret[i] = (*f)(i, s[i]);
i++;
}
return (ret);
}

34
libft/str/ft_ncmp.c

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 16:16:37 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
int ret;
unsigned char *s1cp;
unsigned char *s2cp;
if (!n)
return (0);
i = 0;
s1cp = (unsigned char *)s1;
s2cp = (unsigned char *)s2;
ret = s1cp[i] - s2cp[i];
while ((!ret && i < n) && (s1[i] || s2[i]))
{
ret = s1cp[i] - s2cp[i];
i++;
}
return (ret);
}

42
libft/str/ft_nstr.c

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 16:52:56 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
int found;
i = 0;
j = 0;
found = 0;
while (haystack[i] && i < len && needle[j])
{
if (haystack[i] == needle[j])
{
found = 1;
j++;
}
else if (found)
{
found = 0;
i -= j;
j = 0;
}
i++;
}
if (!needle[j])
return ((char *)(haystack + i - j));
return (0);
}

33
libft/str/ft_rchr.c

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 16:47:21 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strrchr(const char *s, int c)
{
size_t i;
int found;
i = 0;
found = -1;
while (s[i])
{
if (s[i] == (c % 256))
found = i;
i++;
}
if (c == '\0')
return ((char *)(s + i));
if (found >= 0)
return ((char *)(s + found));
return (0);
}

32
libft/str/ft_rev.c

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrev.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 09:36:21 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
void ft_strrev(char **str, unsigned int neg)
{
char ch;
size_t size;
size_t i;
size = ft_strlen(*str);
if (neg)
(*str)[size++] = '-';
i = 0;
while (i < size / 2)
{
ch = (*str)[i];
(*str)[i] = (*str)[size - i - 1];
(*str)[size - i - 1] = ch;
i++;
}
}

42
libft/str/ft_split.c

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 15:43:48 by narnaud #+# #+# */
/* Updated: 2022/03/25 14:56:37 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char **ft_split(const char *str, char c)
{
t_dlist *words;
char *word;
int i;
int j;
i = -1;
word = NULL;
words = NULL;
if (!str)
return (NULL);
while (str[++i])
{
if (!word && str[i] != c)
j = 0;
if (!word && str[i] != c)
word = ft_calloc((int)ft_strlen_to(str + i, c) + 1, sizeof(char));
if (word && str[i] == c)
words = ft_dlst_add(words, word);
if (word && str[i] == c)
word = NULL;
if (str[i] != c)
word[j++] = str[i];
}
words = ft_dlst_add(words, word);
return (ft_dlst_to_arr(words));
}

42
libft/str/ft_sub.c

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 06:45:32 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *ret;
size_t s_size;
if (!s)
return (NULL);
s_size = ft_strlen(s);
if (start > s_size)
{
start = s_size;
len = 0;
}
else if (s_size < start + len)
len = s_size - start;
ret = ft_calloc(len + 1, sizeof(char));
if (!ret)
return (NULL);
i = 0;
while (i < len && s[start + i])
{
ret[i] = s[start + i];
i++;
}
ret[i] = '\0';
return (ret);
}

53
libft/str/ft_trim.c

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 13:49:37 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
static int ft_search_char(char c, char const *set)
{
size_t i;
size_t set_len;
if (!c)
return (0);
i = 0;
set_len = ft_strlen(set);
while (i < set_len)
if (c == set[i++])
return (1);
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
unsigned int i;
unsigned int b_out;
unsigned int e_out;
char *ret;
if (!s1)
return (NULL);
i = 0;
b_out = 0;
e_out = ft_strlen(s1);
while (ft_search_char(s1[b_out], set))
b_out++;
while (e_out > b_out && ft_search_char(s1[e_out - 1], set))
e_out--;
ret = malloc((e_out - b_out + 1) * sizeof(char));
if (!ret)
return (NULL);
while (b_out < e_out)
ret[i++] = s1[b_out++];
ret[i] = '\0';
return (ret);
}

88
mlx/.github/workflows/ci.yml

@ -0,0 +1,88 @@
name: Build
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ${{ matrix.os }}
env:
DISPLAY: ":99"
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
timeout-minutes: 20
steps:
- uses: actions/checkout@v2
- name: Install mlx dependencies
run: |
set -x
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt-get update -qq
sudo apt-get install -y -qq gcc make xorg libxext-dev libbsd-dev
elif [ "$RUNNER_OS" == "macOS" ]; then
brew install xquartz
echo "/usr/X11/bin" >> $GITHUB_PATH
else
echo "$RUNNER_OS not supported"
exit 1
fi
- name: Setup x11 headless testing environment
run: |
set -x
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt-get install xvfb xdotool valgrind
Xvfb $DISPLAY -screen 0 1280x1024x24 &
elif [ "$RUNNER_OS" == "macOS" ]; then
brew install xdotool
defaults write org.x.X11 enable_test_extensions -boolean true
sudo Xvfb $DISPLAY -screen 0 1280x1024x24 &
else
echo "$RUNNER_OS not supported"
exit 1
fi
- name: Run ./configure
run: ./configure
- name: make check Linux
if: matrix.os == 'ubuntu-latest'
run: make -f Makefile.gen check
- name: make check MacOS
continue-on-error: true
if: matrix.os == 'macos-latest'
run: make -f Makefile.gen check
# Didn't find a way to simulate inputs on Macos. libxdo seem to no longer work on macos.
# It can be partially fixed writing proper unit-tests, thus avoiding the need of libxdo.
- name: Check leaks from binary "test/mlx-test"
run: |
cd test
if [ "$RUNNER_OS" == "Linux" ]; then
echo "Info: Still reachable doesn't matter. Valgrind will return success on thoses reports.
It is fine, we searching for lost pointers. Valgrind will return exit status 42 if any block is lost."
valgrind --leak-check=full --show-leak-kinds=definite,indirect,possible --errors-for-leak-kinds=definite,indirect,possible --error-exitcode=42 ./mlx-test > /dev/null &
PID=$!
sleep 30
xdotool search --name Title3 windowfocus key Escape
xdotool search --name Title2 windowfocus key Escape
wait $PID
elif [ "$RUNNER_OS" == "macOS" ]; then
MallocStackLoggingNoCompact=1
./mlx-test &
sleep 30
leaks mlx-test
pkill mlx-test
fi
- name: Norminette, just for fun
continue-on-error: true
run: |
pip3 install Norminette
norminette *.c *.h
norminette --version

67
mlx/.gitignore

@ -0,0 +1,67 @@
## Mlx related
Makefile.gen
/test/mlx-test
## Editor
.vscode/*
*~
\#*\#
## Other
.DS_STORE
## Template from https://github.com/github/gitignore
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

25
mlx/LICENSE

@ -0,0 +1,25 @@
BSD 2-Clause License
Copyright (c) 2021, Ecole 42
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

22
mlx/Makefile

@ -0,0 +1,22 @@
##
## Makefile for MiniLibX in /home/boulon/work/c/raytraceur/minilibx
##
## Made by Olivier Crouzet
## Login <ol@epitech.net>
##
## Started on Tue Oct 5 15:56:43 2004 Olivier Crouzet
## Last update Tue May 15 15:44:41 2007 Olivier Crouzet
##
## Please use configure script
all : do_configure
do_configure :
./configure
clean :
./configure clean
re : clean all

66
mlx/Makefile.mk

@ -0,0 +1,66 @@
##
## Makefile for MiniLibX in /home/boulon/work/c/raytraceur/minilibx
##
## Made by Olivier Crouzet
## Login <ol@epitech.net>
##
## Started on Tue Oct 5 15:56:43 2004 Olivier Crouzet
## Last update Tue May 15 15:41:20 2007 Olivier Crouzet
##
## Please use configure script
INC =%%%%
UNAME = $(shell uname)
CC = gcc
ifeq ($(UNAME),FreeBSD)
CC = clang
endif
NAME = libmlx.a
NAME_UNAME = libmlx_$(UNAME).a
SRC = mlx_init.c mlx_new_window.c mlx_pixel_put.c mlx_loop.c \
mlx_mouse_hook.c mlx_key_hook.c mlx_expose_hook.c mlx_loop_hook.c \
mlx_int_anti_resize_win.c mlx_int_do_nothing.c \
mlx_int_wait_first_expose.c mlx_int_get_visual.c \
mlx_flush_event.c mlx_string_put.c mlx_set_font.c \
mlx_new_image.c mlx_get_data_addr.c \
mlx_put_image_to_window.c mlx_get_color_value.c mlx_clear_window.c \
mlx_xpm.c mlx_int_str_to_wordtab.c mlx_destroy_window.c \
mlx_int_param_event.c mlx_int_set_win_event_mask.c mlx_hook.c \
mlx_rgb.c mlx_destroy_image.c mlx_mouse.c mlx_screen_size.c \
mlx_destroy_display.c
OBJ_DIR = obj
OBJ = $(addprefix $(OBJ_DIR)/,$(SRC:%.c=%.o))
CFLAGS = -O3 -I$(INC)
all : $(NAME)
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) $(IFLAGS) -c $< -o $@
$(NAME) : $(OBJ)
ar -r $(NAME) $(OBJ)
ranlib $(NAME)
cp $(NAME) $(NAME_UNAME)
check: all
@test/run_tests.sh
show:
@printf "NAME : $(NAME)\n"
@printf "NAME_UNAME : $(NAME_UNAME)\n"
@printf "CC : $(CC)\n"
@printf "CFLAGS : $(CFLAGS)\n"
@printf "SRC :\n $(SRC)\n"
@printf "OBJ :\n $(OBJ)\n"
clean :
rm -rf $(OBJ_DIR)/ $(NAME) $(NAME_UNAME) *~ core *.core
.PHONY: all check show clean

55
mlx/README.md

@ -0,0 +1,55 @@
[![Build](https://github.com/42Paris/minilibx-linux/actions/workflows/ci.yml/badge.svg)](https://github.com/42Paris/minilibx-linux/actions/workflows/ci.yml)
This is the MinilibX, a simple X-Window (X11R6) programming API
in C, designed for students, suitable for X-beginners.
Contents
- source code in C to create the mlx library
- man pages (in man/ directory)
- a test program (in test/ directory) is built
with the library
- a public include file mlx.h
- a tiny configure script to generate an appropriate Makefile.gen
Requirements for Linux
- MinilibX only support TrueColor visual type (8,15,16,24 or 32 bits depth)
- gcc
- make
- X11 include files (package xorg)
- XShm extension must be present (package libxext-dev)
- Utility functions from BSD systems - development files (package libbsd-dev)
- **e.g. _sudo apt-get install gcc make xorg libxext-dev libbsd-dev_ (Debian/Ubuntu)**
Requirements for MacOS
- [Xquartz](https://www.xquartz.org/)
```bash
➜ ~ Brew install Xquartz
➜ ~ reboot
➜ ~ xeyes # run an hello world X11 app
```
MlX Color Opacity / Transparency / Alpha (32 bits depth)
- 0xFF (fully transparent) or 0x00 (fully opaque)
Compile MinilibX
- run ./configure or make
both will make a few tests, create Makefile.gen
and then automatically run make on this generated Makefile.gen .
libmlx.a and libmlx_$(HOSTTYPE).a are created.
test/mlx-test binary is also created.
Install MinilibX
- no installation script is provided. You may want to install
- libmlx.a and/or libmlx_$(HOSTTYPE).a in /usr/X11/lib or /usr/local/lib
- mlx.h in /usr/X11/include or /usr/local/include
- man/man3/mlx*.1 in /usr/X11/man/man3 or /usr/local/man/man3
Olivier CROUZET - 2014-01-06 -

126
mlx/configure

@ -0,0 +1,126 @@
#!/usr/bin/env sh
set -e
BOLD="\033[1m"
RESET="\033[0m"
LIGHT_RED="\033[91m"
LIGHT_GREEN="\033[92m"
LIGHT_CYAN="\033[96m"
logging(){
local type=$1; shift
printf "${LIGHT_CYAN}${BOLD}configure${RESET} [%b] : %b\n" "$type" "$*"
}
log_info(){
logging "${LIGHT_GREEN}info${RESET}" "$@"
}
log_error(){
logging "${LIGHT_RED}error${RESET}" "$@" >&2
}
# find and print x11 header path
get_xlib_include_path(){
local result=""
for inc in \
/usr/X11/include \
/usr/X11R6/include \
/usr/X11R5/include \
/usr/X11R4/include \
\
/usr/include \
/usr/include/X11 \
/usr/include/X11R6 \
/usr/include/X11R5 \
/usr/include/X11R4 \
\
/usr/local/X11/include \
/usr/local/X11R6/include \
/usr/local/X11R5/include \
/usr/local/X11R4/include \
\
/usr/local/include/X11 \
/usr/local/include/X11R6 \
/usr/local/include/X11R5 \
/usr/local/include/X11R4 \
\
/usr/X386/include \
/usr/x386/include \
/usr/XFree86/include/X11 \
\
/usr/local/include \
/usr/athena/include \
/usr/local/x11r5/include \
/usr/lpp/Xamples/include \
\
/usr/openwin/include \
/usr/openwin/share/include
do
if [ -f "$inc/X11/Xlib.h" -a -f "$inc/X11/extensions/XShm.h" ]; then
result=$inc
break
fi
done
echo $result
}
show_help(){
cat <<EOF
Usage :
$0 Auto-configure and make MinilibX
$0 clean Execute the clean rule of both Makefile.gen
EOF
}
clean(){
log_info 'Execute "make clean" from "makefile.gen"'
${MAKE} -f Makefile.gen clean
log_info 'Execute "make clean" from "test/makefile.gen"'
${MAKE} -f Makefile.gen -C test/ --no-print-directory clean
}
parse_args(){
case "$1" in
--help | -h)
show_help
exit 0;;
clean)
clean
exit 0;;
"") return;;
*)
log_error "unknown command \"$1\"\nRun \"./configure --help\" for usage."
exit 1;;
esac
}
main(){
local xlib_inc="$(get_xlib_include_path)"
case $(uname) in
FreeBSD) MAKE=gmake ;;
*) MAKE=make ;;
esac
parse_args "$@"
if [ -z "$xlib_inc" ]; then
log_error "Can't find a suitable X11 include directory."
exit 1
fi
log_info "Found X11 include path directory: $xlib_inc"
log_info 'Generate "makefile.gen" from template "makefile.mk"'
echo "INC=$xlib_inc" > Makefile.gen
cat Makefile.mk | grep -v %%%% >> Makefile.gen
log_info 'Generate "test/makefile.gen" from template "test/makefile.mk"'
echo "INC=$xlib_inc" > test/Makefile.gen
cat test/Makefile.mk | grep -v %%%% >> test/Makefile.gen
log_info 'Execute "make all" from file "makefile.gen"'
${MAKE} -f Makefile.gen all
log_info 'Execute "make all" from file "test/makefile.gen"'
(cd test ; ${MAKE} -f Makefile.gen all )
}
main "$@"

93
mlx/man/man1/mlx.1

@ -0,0 +1,93 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Simple X-Window Interface Library for students
.SH SYNOPSYS
#include <mlx.h>
.nf
.I void *
.fi
.B mlx_init
();
.SH DESCRIPTION
MiniLibX is an easy way to create graphical software,
without any X-Window programming knowledge. It provides
simple window creation, a drawing tool, image and basic events
management.
.SH X-WINDOW CONCEPT
X-Window is a network-oriented graphical system for Unix.
It is based on two main parts:
.br
On one side, your software wants to draw something on the screen and/or
get keyboard & mouse entries.
.br
On the other side, the X-Server manages the screen, keyboard and mouse
(It is often refered to as a "display").
.br
A network connection must be established between these two entities to send
drawing orders (from the software to the X-Server), and keyboard/mouse
events (from the X-Server to the software).
.SH INCLUDE FILE
.B mlx.h
should be included for a correct use of the MiniLibX API.
It only contains function prototypes, no structure is needed.
.SH LIBRARY FUNCTIONS
.P
First of all, you need to initialize the connection
between your software and the display.
Once this connection is established, you'll be able to
use other MiniLibX functions to send the X-Server messages,
like "I want to draw a yellow pixel in this window" or "did the
user hit a key?".
.P
The
.B mlx_init
function will create this connection. No parameters are needed, ant it will
return a
.I "void *"
identifier, used for further calls to the library routines.
.P
All other MiniLibX functions are described in the following man pages:
.TP 20
.B mlx_new_window
: manage windows
.TP 20
.B mlx_pixel_put
: draw inside window
.TP 20
.B mlx_new_image
: manipulate images
.TP 20
.B mlx_loop
: handle keyboard or mouse events
.SH LINKING MiniLibX
To use MiniLibX functions, you'll need to link
your software with several libraries, including the MiniLibX library itself.
To do this, simply add the following arguments at linking time:
.B -lmlx -lXext -lX11
You may also need to specify the path to these libraries, using
the
.B -L
flag.
.SH RETURN VALUES
If
.B mlx_init()
fails to set up the connection to the X server, it will return NULL, otherwise
a non-null pointer is returned as a connection identifier.
.SH SEE ALSO
mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

141
mlx/man/man1/mlx_loop.1

@ -0,0 +1,141 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Handle events
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_loop
(
.I void *mlx_ptr
);
.nf
.I int
.fi
.B mlx_key_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_mouse_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_expose_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_loop_hook
(
.I void *mlx_ptr, int (*funct_ptr)(), void *param
);
.SH X-WINDOW EVENTS
The X-Window system is bi-directionnal. On one hand, the program sends orders to
the screen to display pixels, images, and so on. On the other hand,
it can get information from the keyboard and mouse associated to
the screen. To do so, the program receives "events" from the keyboard or the
mouse.
.SH DESCRIPTION
To receive events, you must use
.B mlx_loop
(). This function never returns. It is an infinite loop that waits for
an event, and then calls a user-defined function associated with this event.
A single parameter is needed, the connection identifier
.I mlx_ptr
(see the
.B mlx manual).
You can assign different functions to the three following events:
.br
- A key is pressed
.br
- The mouse button is pressed
.br
- A part of the window should be re-drawn
(this is called an "expose" event, and it is your program's job to handle it).
.br
Each window can define a different function for the same event.
The three functions
.B mlx_key_hook
(),
.B mlx_mouse_hook
() and
.B mlx_expose_hook
() work exactly the same way.
.I funct_ptr
is a pointer to the function you want to be called
when an event occurs. This assignment is specific to the window defined by the
.I win_ptr
identifier. The
.I param
adress will be passed to the function everytime it is called, and should be
used to store the parameters it might need.
The syntax for the
.B mlx_loop_hook
() function is identical to the previous ones, but the given function will be
called when no event occurs.
When it catches an event, the MiniLibX calls the corresponding function
with fixed parameters:
.nf
expose_hook(void *param);
key_hook(int keycode,void *param);
mouse_hook(int button,int x,int y,void *param);
loop_hook(void *param);
.fi
These function names are arbitrary. They here are used to distinguish
parameters according to the event. These functions are NOT part of the
MiniLibX.
.I param
is the address specified in the mlx_*_hook calls. This address is never
used nor modified by the MiniLibX. On key and mouse events, additional
information is passed:
.I keycode
tells you which key is pressed (look for the X11 include file "keysymdef.h"),
(
.I x
,
.I y
) are the coordinates of the mouse click in the window, and
.I button
tells you which mouse button was pressed.
.SH GOING FURTHER WITH EVENTS
The MiniLibX provides a much generic access to all X-Window events. The
.I mlx.h
include define
.B mlx_hook()
in the same manner mlx_*_hook functions work. The event and mask values
will be taken from the X11 include file "X.h".
See source code of mlx_int_param_event.c to find out how the MiniLibX will
call your own function for a specific event.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

192
mlx/man/man1/mlx_new_image.1

@ -0,0 +1,192 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Manipulating images
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_image
(
.I void *mlx_ptr, int width, int height
);
.nf
.I char *
.fi
.B mlx_get_data_addr
(
.I void *img_ptr, int *bits_per_pixel, int *size_line, int *endian
);
.nf
.I int
.fi
.B mlx_put_image_to_window
(
.I void *mlx_ptr, void *win_ptr, void *img_ptr, int x, int y
);
.nf
.I unsigned int
.fi
.B mlx_get_color_value
(
.I void *mlx_ptr, int color
);
.nf
.I void *
.fi
.B mlx_xpm_to_image
(
.I void *mlx_ptr, char **xpm_data, int *width, int *height
);
.nf
.I void *
.fi
.B mlx_xpm_file_to_image
(
.I void *mlx_ptr, char *filename, int *width, int *height
);
.nf
.I int
.fi
.B mlx_destroy_image
(
.I void *mlx_ptr, void *img_ptr
);
.SH DESCRIPTION
.B mlx_new_image
() creates a new image in memory. It returns a
.I void *
identifier needed to manipulate this image later. It only needs
the size of the image to be created, using the
.I width
and
.I height
parameters, and the
.I mlx_ptr
connection identifier (see the
.B mlx
manual).
The user can draw inside the image (see below), and
can dump the image inside a specified window at any time to
display it on the screen. This is done using
.B mlx_put_image_to_window
(). Three identifiers are needed here, for the connection to the
display, the window to use, and the image (respectively
.I mlx_ptr
,
.I win_ptr
and
.I img_ptr
). The (
.I x
,
.I y
) coordinates define where the image should be placed in the window.
.B mlx_get_data_addr
() returns information about the created image, allowing a user
to modify it later. The
.I img_ptr
parameter specifies the image to use. The three next parameters should
be the addresses of three different valid integers.
.I bits_per_pixel
will be filled with the number of bits needed to represent a pixel color
(also called the depth of the image).
.I size_line
is the number of bytes used to store one line of the image in memory.
This information is needed to move from one line to another in the image.
.I endian
tells you wether the pixel color in the image needs to be stored in
little endian (
.I endian
== 0), or big endian (
.I endian
== 1).
.B mlx_get_data_addr
returns a
.I char *
address that represents the begining of the memory area where the image
is stored. From this adress, the first
.I bits_per_pixel
bits represent the color of the first pixel in the first line of
the image. The second group of
.I bits_per_pixel
bits represent the second pixel of the first line, and so on.
Add
.I size_line
to the adress to get the begining of the second line. You can reach any
pixels of the image that way.
.B mlx_destroy_image
destroys the given image (
.I img_ptr
).
.SH STORING COLOR INSIDE IMAGES
Depending on the display, the number of bits used to store a pixel color
can change. The user usually represents a color in RGB mode, using
one byte for each component (see
.B mlx_pixel_put
manual). This must be translated to fit the
.I bits_per_pixel
requirement of the image, and make the color understandable to the X-Server.
That is the purpose of the
.B mlx_get_color_value
() function. It takes a standard RGB
.I color
parameter, and returns an
.I unsigned int
value.
The
.I bits_per_pixel
least significant bits of this value can be stored in the image.
Keep in mind that the least significant bits position depends on the local
computer's endian. If the endian of the image (in fact the endian of
the X-Server's computer) differs from the local endian, then the value should
be transformed before being used.
.SH XPM IMAGES
The
.B mlx_xpm_to_image
() and
.B mlx_xpm_file_to_image
() functions will create a new image the same way.
They will fill it using the specified
.I xpm_data
or
.I filename
, depending on which function is used.
Note that MiniLibX does not use the standard
Xpm library to deal with xpm images. You may not be able to
read all types of xpm images. It however handles transparency.
.SH RETURN VALUES
The three functions that create images,
.B mlx_new_image()
,
.B mlx_xpm_to_image()
and
.B mlx_xpm_file_to_image()
, will return NULL if an error occurs. Otherwise they return a non-null pointer
as an image identifier.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

79
mlx/man/man1/mlx_new_window.1

@ -0,0 +1,79 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Managing windows
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_window
(
.I void *mlx_ptr, int size_x, int size_y, char *title
);
.nf
.I int
.fi
.B mlx_clear_window
(
.I void *mlx_ptr, void *win_ptr
);
.nf
.I int
.fi
.B mlx_destroy_window
(
.I void *mlx_ptr, void *win_ptr
);
.SH DESCRIPTION
The
.B mlx_new_window
() function creates a new window on the screen, using the
.I size_x
and
.I size_y
parameters to determine its size, and
.I title
as the text that should be displayed in the window's title bar.
The
.I mlx_ptr
parameter is the connection identifier returned by
.B mlx_init
() (see the
.B mlx
man page).
.B mlx_new_window
() returns a
.I void *
window identifier that can be used by other MiniLibX calls.
Note that the MiniLibX
can handle an arbitrary number of separate windows.
.B mlx_clear_window
() and
.B mlx_destroy_window
() respectively clear (in black) and destroy the given window. They both have
the same parameters:
.I mlx_ptr
is the screen connection identifier, and
.I win_ptr
is a window identifier.
.SH RETURN VALUES
If
.B mlx_new_window()
fails to create a new window (for wathever reason), it will return NULL,
otherwise a non-null pointer is returned as a window identifier.
.B mlx_clear_window
and
.B mlx_destroy_window
right now return nothing.
.SH SEE ALSO
mlx(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

84
mlx/man/man1/mlx_pixel_put.1

@ -0,0 +1,84 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Drawing inside windows
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_pixel_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color
);
.nf
.I int
.fi
.B mlx_string_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color, char *string
);
.SH DESCRIPTION
The
.B mlx_pixel_put
() function draws a defined pixel in the window
.I win_ptr
using the (
.I x
,
.I y
) coordinates, and the specified
.I color
\&. The origin (0,0) is the upper left corner of the window, the x and y axis
respectively pointing right and down. The connection
identifier,
.I mlx_ptr
, is needed (see the
.B mlx
man page).
Parameters for
.B mlx_string_put
() have the same meaning. Instead of a simple pixel, the specified
.I string
will be displayed at (
.I x
,
.I y
).
In both functions, it is impossible to display anything outside the
specified window, nor display in another window in front of the selected one.
.SH COLOR MANAGEMENT
The
.I color
parameter has an integer type. The displayed color needs to be encoded
in this integer, following a defined scheme. All displayable colors
can be split in 3 basic colors: red, green and blue. Three associated
values, in the 0-255 range, represent how much of each color is mixed up
to create the original color. Theses three values must be set inside the
integer to display the right color. The three least significant bytes of
this integer are filled as shown in the picture below:
.TS
allbox;
c s s s s
r c c c c.
Color Integer
Interpretation \[*a] R G B
Bit numbers 31..24 23..16 15..8 7..0
.TE
While filling the integer, make sure you avoid endian problems. Remember
that the "blue" byte should always be the least significant one.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

93
mlx/man/man3/mlx.3

@ -0,0 +1,93 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Simple X-Window Interface Library for students
.SH SYNOPSYS
#include <mlx.h>
.nf
.I void *
.fi
.B mlx_init
();
.SH DESCRIPTION
MiniLibX is an easy way to create graphical software,
without any X-Window programming knowledge. It provides
simple window creation, a drawing tool, image and basic events
management.
.SH X-WINDOW CONCEPT
X-Window is a network-oriented graphical system for Unix.
It is based on two main parts:
.br
On one side, your software wants to draw something on the screen and/or
get keyboard & mouse entries.
.br
On the other side, the X-Server manages the screen, keyboard and mouse
(It is often refered to as a "display").
.br
A network connection must be established between these two entities to send
drawing orders (from the software to the X-Server), and keyboard/mouse
events (from the X-Server to the software).
.SH INCLUDE FILE
.B mlx.h
should be included for a correct use of the MiniLibX API.
It only contains function prototypes, no structure is needed.
.SH LIBRARY FUNCTIONS
.P
First of all, you need to initialize the connection
between your software and the display.
Once this connection is established, you'll be able to
use other MiniLibX functions to send the X-Server messages,
like "I want to draw a yellow pixel in this window" or "did the
user hit a key?".
.P
The
.B mlx_init
function will create this connection. No parameters are needed, ant it will
return a
.I "void *"
identifier, used for further calls to the library routines.
.P
All other MiniLibX functions are described in the following man pages:
.TP 20
.B mlx_new_window
: manage windows
.TP 20
.B mlx_pixel_put
: draw inside window
.TP 20
.B mlx_new_image
: manipulate images
.TP 20
.B mlx_loop
: handle keyboard or mouse events
.SH LINKING MiniLibX
To use MiniLibX functions, you'll need to link
your software with several libraries, including the MiniLibX library itself.
To do this, simply add the following arguments at linking time:
.B -lmlx -lXext -lX11
You may also need to specify the path to these libraries, using
the
.B -L
flag.
.SH RETURN VALUES
If
.B mlx_init()
fails to set up the connection to the X server, it will return NULL, otherwise
a non-null pointer is returned as a connection identifier.
.SH SEE ALSO
mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

141
mlx/man/man3/mlx_loop.3

@ -0,0 +1,141 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Handle events
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_loop
(
.I void *mlx_ptr
);
.nf
.I int
.fi
.B mlx_key_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_mouse_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_expose_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_loop_hook
(
.I void *mlx_ptr, int (*funct_ptr)(), void *param
);
.SH X-WINDOW EVENTS
The X-Window system is bi-directionnal. On one hand, the program sends orders to
the screen to display pixels, images, and so on. On the other hand,
it can get information from the keyboard and mouse associated to
the screen. To do so, the program receives "events" from the keyboard or the
mouse.
.SH DESCRIPTION
To receive events, you must use
.B mlx_loop
(). This function never returns. It is an infinite loop that waits for
an event, and then calls a user-defined function associated with this event.
A single parameter is needed, the connection identifier
.I mlx_ptr
(see the
.B mlx manual).
You can assign different functions to the three following events:
.br
- A key is pressed
.br
- The mouse button is pressed
.br
- A part of the window should be re-drawn
(this is called an "expose" event, and it is your program's job to handle it).
.br
Each window can define a different function for the same event.
The three functions
.B mlx_key_hook
(),
.B mlx_mouse_hook
() and
.B mlx_expose_hook
() work exactly the same way.
.I funct_ptr
is a pointer to the function you want to be called
when an event occurs. This assignment is specific to the window defined by the
.I win_ptr
identifier. The
.I param
adress will be passed to the function everytime it is called, and should be
used to store the parameters it might need.
The syntax for the
.B mlx_loop_hook
() function is identical to the previous ones, but the given function will be
called when no event occurs.
When it catches an event, the MiniLibX calls the corresponding function
with fixed parameters:
.nf
expose_hook(void *param);
key_hook(int keycode,void *param);
mouse_hook(int button,int x,int y,void *param);
loop_hook(void *param);
.fi
These function names are arbitrary. They here are used to distinguish
parameters according to the event. These functions are NOT part of the
MiniLibX.
.I param
is the address specified in the mlx_*_hook calls. This address is never
used nor modified by the MiniLibX. On key and mouse events, additional
information is passed:
.I keycode
tells you which key is pressed (look for the X11 include file "keysymdef.h"),
(
.I x
,
.I y
) are the coordinates of the mouse click in the window, and
.I button
tells you which mouse button was pressed.
.SH GOING FURTHER WITH EVENTS
The MiniLibX provides a much generic access to all X-Window events. The
.I mlx.h
include define
.B mlx_hook()
in the same manner mlx_*_hook functions work. The event and mask values
will be taken from the X11 include file "X.h".
See source code of mlx_int_param_event.c to find out how the MiniLibX will
call your own function for a specific event.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

192
mlx/man/man3/mlx_new_image.3

@ -0,0 +1,192 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Manipulating images
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_image
(
.I void *mlx_ptr, int width, int height
);
.nf
.I char *
.fi
.B mlx_get_data_addr
(
.I void *img_ptr, int *bits_per_pixel, int *size_line, int *endian
);
.nf
.I int
.fi
.B mlx_put_image_to_window
(
.I void *mlx_ptr, void *win_ptr, void *img_ptr, int x, int y
);
.nf
.I unsigned int
.fi
.B mlx_get_color_value
(
.I void *mlx_ptr, int color
);
.nf
.I void *
.fi
.B mlx_xpm_to_image
(
.I void *mlx_ptr, char **xpm_data, int *width, int *height
);
.nf
.I void *
.fi
.B mlx_xpm_file_to_image
(
.I void *mlx_ptr, char *filename, int *width, int *height
);
.nf
.I int
.fi
.B mlx_destroy_image
(
.I void *mlx_ptr, void *img_ptr
);
.SH DESCRIPTION
.B mlx_new_image
() creates a new image in memory. It returns a
.I void *
identifier needed to manipulate this image later. It only needs
the size of the image to be created, using the
.I width
and
.I height
parameters, and the
.I mlx_ptr
connection identifier (see the
.B mlx
manual).
The user can draw inside the image (see below), and
can dump the image inside a specified window at any time to
display it on the screen. This is done using
.B mlx_put_image_to_window
(). Three identifiers are needed here, for the connection to the
display, the window to use, and the image (respectively
.I mlx_ptr
,
.I win_ptr
and
.I img_ptr
). The (
.I x
,
.I y
) coordinates define where the image should be placed in the window.
.B mlx_get_data_addr
() returns information about the created image, allowing a user
to modify it later. The
.I img_ptr
parameter specifies the image to use. The three next parameters should
be the addresses of three different valid integers.
.I bits_per_pixel
will be filled with the number of bits needed to represent a pixel color
(also called the depth of the image).
.I size_line
is the number of bytes used to store one line of the image in memory.
This information is needed to move from one line to another in the image.
.I endian
tells you wether the pixel color in the image needs to be stored in
little endian (
.I endian
== 0), or big endian (
.I endian
== 1).
.B mlx_get_data_addr
returns a
.I char *
address that represents the begining of the memory area where the image
is stored. From this adress, the first
.I bits_per_pixel
bits represent the color of the first pixel in the first line of
the image. The second group of
.I bits_per_pixel
bits represent the second pixel of the first line, and so on.
Add
.I size_line
to the adress to get the begining of the second line. You can reach any
pixels of the image that way.
.B mlx_destroy_image
destroys the given image (
.I img_ptr
).
.SH STORING COLOR INSIDE IMAGES
Depending on the display, the number of bits used to store a pixel color
can change. The user usually represents a color in RGB mode, using
one byte for each component (see
.B mlx_pixel_put
manual). This must be translated to fit the
.I bits_per_pixel
requirement of the image, and make the color understandable to the X-Server.
That is the purpose of the
.B mlx_get_color_value
() function. It takes a standard RGB
.I color
parameter, and returns an
.I unsigned int
value.
The
.I bits_per_pixel
least significant bits of this value can be stored in the image.
Keep in mind that the least significant bits position depends on the local
computer's endian. If the endian of the image (in fact the endian of
the X-Server's computer) differs from the local endian, then the value should
be transformed before being used.
.SH XPM IMAGES
The
.B mlx_xpm_to_image
() and
.B mlx_xpm_file_to_image
() functions will create a new image the same way.
They will fill it using the specified
.I xpm_data
or
.I filename
, depending on which function is used.
Note that MiniLibX does not use the standard
Xpm library to deal with xpm images. You may not be able to
read all types of xpm images. It however handles transparency.
.SH RETURN VALUES
The three functions that create images,
.B mlx_new_image()
,
.B mlx_xpm_to_image()
and
.B mlx_xpm_file_to_image()
, will return NULL if an error occurs. Otherwise they return a non-null pointer
as an image identifier.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

79
mlx/man/man3/mlx_new_window.3

@ -0,0 +1,79 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Managing windows
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_window
(
.I void *mlx_ptr, int size_x, int size_y, char *title
);
.nf
.I int
.fi
.B mlx_clear_window
(
.I void *mlx_ptr, void *win_ptr
);
.nf
.I int
.fi
.B mlx_destroy_window
(
.I void *mlx_ptr, void *win_ptr
);
.SH DESCRIPTION
The
.B mlx_new_window
() function creates a new window on the screen, using the
.I size_x
and
.I size_y
parameters to determine its size, and
.I title
as the text that should be displayed in the window's title bar.
The
.I mlx_ptr
parameter is the connection identifier returned by
.B mlx_init
() (see the
.B mlx
man page).
.B mlx_new_window
() returns a
.I void *
window identifier that can be used by other MiniLibX calls.
Note that the MiniLibX
can handle an arbitrary number of separate windows.
.B mlx_clear_window
() and
.B mlx_destroy_window
() respectively clear (in black) and destroy the given window. They both have
the same parameters:
.I mlx_ptr
is the screen connection identifier, and
.I win_ptr
is a window identifier.
.SH RETURN VALUES
If
.B mlx_new_window()
fails to create a new window (for wathever reason), it will return NULL,
otherwise a non-null pointer is returned as a window identifier.
.B mlx_clear_window
and
.B mlx_destroy_window
right now return nothing.
.SH SEE ALSO
mlx(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

81
mlx/man/man3/mlx_pixel_put.3

@ -0,0 +1,81 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Drawing inside windows
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_pixel_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color
);
.nf
.I int
.fi
.B mlx_string_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color, char *string
);
.SH DESCRIPTION
The
.B mlx_pixel_put
() function draws a defined pixel in the window
.I win_ptr
using the (
.I x
,
.I y
) coordinates, and the specified
.I color
\&. The origin (0,0) is the upper left corner of the window, the x and y axis
respectively pointing right and down. The connection
identifier,
.I mlx_ptr
, is needed (see the
.B mlx
man page).
Parameters for
.B mlx_string_put
() have the same meaning. Instead of a simple pixel, the specified
.I string
will be displayed at (
.I x
,
.I y
).
In both functions, it is impossible to display anything outside the
specified window, nor display in another window in front of the selected one.
.SH COLOR MANAGEMENT
The
.I color
parameter has an integer type. The displayed color needs to be encoded
in this integer, following a defined scheme. All displayable colors
can be split in 3 basic colors: red, green and blue. Three associated
values, in the 0-255 range, represent how much of each color is mixed up
to create the original color. Theses three values must be set inside the
integer to display the right color. The three least significant bytes of
this integer are filled as shown in the picture below:
.nf
| 0 | R | G | B | color integer
+---+---+---+---+
.fi
While filling the integer, make sure you avoid endian problems. Remember
that the "blue" byte should always be the least significant one.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

139
mlx/mlx.h

@ -0,0 +1,139 @@
/*
** mlx.h for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:37:50 2000 Charlie Root
** Last update Tue May 15 16:23:28 2007 Olivier Crouzet
*/
/*
** MinilibX - Please report bugs
*/
/*
** FR msg - FR msg - FR msg
**
** La MinilibX utilise 2 librairies supplementaires qu'il
** est necessaire de rajouter a la compilation :
** -lmlx -lXext -lX11
**
** La MinilibX permet le chargement des images de type Xpm.
** Notez que cette implementation est incomplete.
** Merci de communiquer tout probleme de chargement d'image
** de ce type.
*/
#ifndef MLX_H
#define MLX_H
void *mlx_init();
/*
** needed before everything else.
** return (void *)0 if failed
*/
/*
** Basic actions
*/
void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title);
/*
** return void *0 if failed
*/
int mlx_clear_window(void *mlx_ptr, void *win_ptr);
int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color);
/*
** origin for x & y is top left corner of the window
** y down is positive
** color is 0x00RRGGBB
*/
/*
** Image stuff
*/
void *mlx_new_image(void *mlx_ptr,int width,int height);
/*
** return void *0 if failed
** obsolete : image2 data is stored using bit planes
** void *mlx_new_image2(void *mlx_ptr,int width,int height);
*/
char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel,
int *size_line, int *endian);
/*
** endian : 0 = sever X is little endian, 1 = big endian
** for mlx_new_image2, 2nd arg of mlx_get_data_addr is number_of_planes
*/
int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr,
int x, int y);
int mlx_get_color_value(void *mlx_ptr, int color);
/*
** dealing with Events
*/
int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param);
int mlx_loop (void *mlx_ptr);
int mlx_loop_end (void *mlx_ptr);
/*
** hook funct are called as follow :
**
** expose_hook(void *param);
** key_hook(int keycode, void *param);
** mouse_hook(int button, int x,int y, void *param);
** loop_hook(void *param);
**
*/
/*
** Usually asked...
*/
int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color,
char *string);
void mlx_set_font(void *mlx_ptr, void *win_ptr, char *name);
void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data,
int *width, int *height);
void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename,
int *width, int *height);
int mlx_destroy_window(void *mlx_ptr, void *win_ptr);
int mlx_destroy_image(void *mlx_ptr, void *img_ptr);
int mlx_destroy_display(void *mlx_ptr);
/*
** generic hook system for all events, and minilibX functions that
** can be hooked. Some macro and defines from X11/X.h are needed here.
*/
int mlx_hook(void *win_ptr, int x_event, int x_mask,
int (*funct)(), void *param);
int mlx_do_key_autorepeatoff(void *mlx_ptr);
int mlx_do_key_autorepeaton(void *mlx_ptr);
int mlx_do_sync(void *mlx_ptr);
int mlx_mouse_get_pos(void *mlx_ptr, void *win_ptr, int *x, int *y);
int mlx_mouse_move(void *mlx_ptr, void *win_ptr, int x, int y);
int mlx_mouse_hide(void *mlx_ptr, void *win_ptr);
int mlx_mouse_show(void *mlx_ptr, void *win_ptr);
int mlx_get_screen_size(void *mlx_ptr, int *sizex, int *sizey);
#endif /* MLX_H */

21
mlx/mlx_clear_window.c

@ -0,0 +1,21 @@
/*
** mlx_clear_window.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Sep 7 19:46:15 2000 Charlie Root
** Last update Tue Sep 25 17:11:19 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_clear_window(t_xvar *xvar,t_win_list *win)
{
XClearWindow(xvar->display,win->window);
if (xvar->do_flush)
XFlush(xvar->display);
}

18
mlx/mlx_destroy_display.c

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mlx_destroy_display.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mg <mg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/03 18:56:35 by mg #+# #+# */
/* Updated: 2020/10/04 01:55:35 by mg ### ########.fr */
/* */
/* ************************************************************************** */
#include "mlx_int.h"
int mlx_destroy_display(t_xvar *xvar)
{
XCloseDisplay(xvar->display);
}

31
mlx/mlx_destroy_image.c

@ -0,0 +1,31 @@
/*
** mlx_destroy_image.c for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Mar 12 10:25:15 2002 Charlie Root
** Last update Tue May 15 16:45:54 2007 Olivier Crouzet
*/
#include "mlx_int.h"
int mlx_destroy_image(t_xvar *xvar, t_img *img)
{
if (img->type == MLX_TYPE_SHM_PIXMAP ||
img->type == MLX_TYPE_SHM)
{
XShmDetach(xvar->display, &(img->shm));
shmdt(img->shm.shmaddr);
/* shmctl IPC_RMID already done */
}
XDestroyImage(img->image); /* For image & shm-image. Also free img->data */
XFreePixmap(xvar->display, img->pix);
if (img->gc)
XFreeGC(xvar->display, img->gc);
free(img);
if (xvar->do_flush)
XFlush(xvar->display);
}

38
mlx/mlx_destroy_window.c

@ -0,0 +1,38 @@
/*
** mlx_destroy_window.c for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Mar 12 10:25:15 2002 Charlie Root
** Last update Tue May 15 16:46:08 2007 Olivier Crouzet
*/
#include "mlx_int.h"
int mlx_destroy_window(t_xvar *xvar,t_win_list *win)
{
t_win_list *w;
t_win_list *prev;
t_win_list first;
first.next = xvar->win_list;
prev = &first;
w = prev->next;
while (w)
{
if (w==win)
prev->next = w->next;
else
prev = w;
w = w->next;
}
xvar->win_list = first.next;
XDestroyWindow(xvar->display,win->window);
XFreeGC(xvar->display,win->gc);
free(win);
if (xvar->do_flush)
XFlush(xvar->display);
}

22
mlx/mlx_expose_hook.c

@ -0,0 +1,22 @@
/*
** mlx_expose_hook.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Aug 3 11:49:06 2000 Charlie Root
** Last update Fri Feb 23 17:07:42 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_expose_hook(t_win_list *win,int (*funct)(),void *param)
{
win->hooks[Expose].hook = funct;
win->hooks[Expose].param = param;
win->hooks[Expose].mask = ExposureMask;
}

104
mlx/mlx_ext_randr.c

@ -0,0 +1,104 @@
#include "mlx_int.h"
#include <unistd.h>
#include <X11/extensions/Xrandr.h>
/* global for independant extension */
RRMode saved_mode = 0;
int mlx_ext_fullscreen(t_xvar *xvar, t_win_list *win, int fullscreen)
{
XWindowAttributes watt;
int i;
int j;
XRRScreenResources *res;
XRROutputInfo *o_info;
XRRCrtcInfo *crtc;
RRMode mode_candidate;
int idx_output;
int idx_candidate;
if (!XGetWindowAttributes(xvar->display, win->window, &watt))
return (0);
res = XRRGetScreenResources(xvar->display, xvar->root);
o_info = NULL;
idx_output = -1;
i = res->noutput;
while (i--)
{
o_info = XRRGetOutputInfo(xvar->display, res, res->outputs[i]);
if (o_info->connection == RR_Connected)
{
idx_output = i;
i = 0;
}
else
XRRFreeOutputInfo(o_info);
}
if (!o_info)
{
XRRFreeScreenResources(res);
return (0);
}
idx_candidate = -1;
i = o_info->nmode;
while (i--)
{
j = res->nmode;
while (j--)
if (res->modes[j].id == o_info->modes[i])
if (res->modes[j].width >= watt.width && res->modes[j].height >= watt.height &&
(idx_candidate == -1 || res->modes[idx_candidate].width > res->modes[j].width ||
res->modes[idx_candidate].height > res->modes[j].height) )
idx_candidate = i;
}
if (idx_candidate < 0)
{
XRRFreeOutputInfo(o_info);
XRRFreeScreenResources(res);
return (0);
}
if (!fullscreen && saved_mode == -1)
idx_candidate = 0; /* if no clue, uses first mode, usually part of npreferred */
mode_candidate = o_info->modes[idx_candidate];
if (!fullscreen)
mode_candidate = saved_mode;
crtc = XRRGetCrtcInfo(xvar->display, res, o_info->crtc);
saved_mode = crtc->mode;
i = XRRSetCrtcConfig(xvar->display, res, o_info->crtc, CurrentTime, 0, 0, mode_candidate,
crtc->rotation, &res->outputs[idx_output], 1);
if (fullscreen)
printf("found mode : %d x %d\n Status %d\n", res->modes[idx_candidate].width, res->modes[idx_candidate].height, i);
else
printf("back previous mode\n");
XMoveWindow(xvar->display, win->window, 0, 0);
XMapRaised(xvar->display, win->window);
if (fullscreen)
{
// XGrabPointer(xvar->display, win->window, True, 0, GrabModeAsync, GrabModeAsync, win->window, 0L, CurrentTime);
XGrabKeyboard(xvar->display, win->window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
}
else
{
XUngrabPointer(xvar->display, CurrentTime);
XUngrabKeyboard(xvar->display, CurrentTime);
}
XSync(xvar->display, False);
sleep(1);
XRRFreeCrtcInfo(crtc);
XRRFreeOutputInfo(o_info);
XRRFreeScreenResources(res);
}

25
mlx/mlx_flush_event.c

@ -0,0 +1,25 @@
/*
** mlx_flush_event.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Wed Aug 2 18:58:11 2000 Charlie Root
** Last update Fri Feb 23 17:08:48 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_flush_event(t_xvar *xvar)
{
XEvent ev;
while (XPending(xvar->display))
{
XNextEvent(xvar->display,&ev);
}
}

33
mlx/mlx_get_color_value.c

@ -0,0 +1,33 @@
/*
** mlx_get_color_value.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 19:01:33 2000 Charlie Root
** Last update Thu Oct 4 15:04:13 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_get_color_value(t_xvar *xvar,int color)
{
return(mlx_int_get_good_color(xvar,color));
}
int mlx_int_get_good_color(t_xvar *xvar,int color)
{
XColor xc;
if (xvar->depth>=24)
return (color);
xc.red = (color>>8)&0xFF00;
xc.green = color&0xFF00;
xc.blue = (color<<8)&0xFF00;
xc.pixel = ((xc.red>>(16-xvar->decrgb[1]))<<xvar->decrgb[0])+
((xc.green>>(16-xvar->decrgb[3]))<<xvar->decrgb[2])+
((xc.blue>>(16-xvar->decrgb[5]))<<xvar->decrgb[4]);
return (xc.pixel);
}

23
mlx/mlx_get_data_addr.c

@ -0,0 +1,23 @@
/*
** mlx_get_data_addr.c for MiniLibX in raytraceur
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Aug 14 15:45:57 2000 Charlie Root
** Last update Thu Sep 27 19:05:25 2001 Charlie Root
*/
#include "mlx_int.h"
char *mlx_get_data_addr(t_img *img,int *bits_per_pixel,
int *size_line,int *endian)
{
*bits_per_pixel = img->bpp;
*size_line = img->size_line;
*endian = img->image->byte_order;
return (img->data);
}

40
mlx/mlx_hook.c

@ -0,0 +1,40 @@
/*
** mlx_hook.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Aug 3 11:49:06 2000 Charlie Root
** Last update Fri Jan 28 17:05:28 2005 Olivier Crouzet
*/
#include "mlx_int.h"
int mlx_hook(t_win_list *win, int x_event, int x_mask,
int (*funct)(),void *param)
{
win->hooks[x_event].hook = funct;
win->hooks[x_event].param = param;
win->hooks[x_event].mask = x_mask;
}
int mlx_do_key_autorepeatoff(t_xvar *xvar)
{
XAutoRepeatOff(xvar->display);
}
int mlx_do_key_autorepeaton(t_xvar *xvar)
{
XAutoRepeatOn(xvar->display);
}
int mlx_do_sync(t_xvar *xvar)
{
XSync(xvar->display, False);
}

99
mlx/mlx_init.c

@ -0,0 +1,99 @@
/*
** mlx_init.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:52:42 2000 Charlie Root
** Last update Fri Jan 28 17:05:09 2005 Olivier Crouzet
*/
#include "mlx_int.h"
void *mlx_init()
{
t_xvar *xvar;
if (!(xvar = malloc(sizeof(*xvar))))
return ((void*)0);
if ((xvar->display = XOpenDisplay("")) == 0)
{
free(xvar);
return ((void*)0);
}
xvar->screen = DefaultScreen(xvar->display);
xvar->root = DefaultRootWindow(xvar->display);
xvar->cmap = DefaultColormap(xvar->display,xvar->screen);
xvar->depth = DefaultDepth(xvar->display,xvar->screen);
if (mlx_int_get_visual(xvar)==-1)
{
printf(ERR_NO_TRUECOLOR);
exit(1);
}
xvar->win_list = 0;
xvar->loop_hook = 0;
xvar->loop_param = (void *)0;
xvar->do_flush = 1;
xvar->wm_delete_window = XInternAtom (xvar->display, "WM_DELETE_WINDOW", False);
xvar->wm_protocols = XInternAtom (xvar->display, "WM_PROTOCOLS", False);
mlx_int_deal_shm(xvar);
if (xvar->private_cmap)
xvar->cmap = XCreateColormap(xvar->display,xvar->root,
xvar->visual,AllocNone);
mlx_int_rgb_conversion(xvar);
xvar->end_loop = 0;
return (xvar);
}
/*
** pshm_format of -1 : Not XYBitmap|XYPixmap|ZPixmap
** alpha libX need a check of the DISPLAY env var, or shm is allowed
** in remote Xserver connections.
*/
int mlx_int_deal_shm(t_xvar *xvar)
{
int use_pshm;
int bidon;
char *dpy;
char buff[33];
xvar->use_xshm = XShmQueryVersion(xvar->display,&bidon,&bidon,&(use_pshm));
if (xvar->use_xshm && use_pshm)
xvar->pshm_format = XShmPixmapFormat(xvar->display);
else
xvar->pshm_format = -1;
gethostname(buff,32);
dpy = getenv(ENV_DISPLAY);
if (dpy && strlen(dpy) && *dpy!=':' && strncmp(dpy,buff,strlen(buff)) &&
strncmp(dpy,LOCALHOST,strlen(LOCALHOST)) )
{
xvar->pshm_format = -1;
xvar->use_xshm = 0;
}
}
/*
** TrueColor Visual is needed to have *_mask correctly set
*/
int mlx_int_rgb_conversion(t_xvar *xvar)
{
bzero(xvar->decrgb,sizeof(int)*6);
while (!(xvar->visual->red_mask&1))
{ xvar->visual->red_mask >>= 1; xvar->decrgb[0] ++; }
while (xvar->visual->red_mask&1)
{ xvar->visual->red_mask >>= 1; xvar->decrgb[1] ++; }
while (!(xvar->visual->green_mask&1))
{ xvar->visual->green_mask >>= 1; xvar->decrgb[2] ++; }
while (xvar->visual->green_mask&1)
{ xvar->visual->green_mask >>= 1; xvar->decrgb[3] ++; }
while (!(xvar->visual->blue_mask&1))
{ xvar->visual->blue_mask >>= 1; xvar->decrgb[4] ++; }
while (xvar->visual->blue_mask&1)
{ xvar->visual->blue_mask >>= 1; xvar->decrgb[5] ++; }
}

140
mlx/mlx_int.h

@ -0,0 +1,140 @@
/*
** mlx_int.h for mlx in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:45:48 2000 Charlie Root
** Last update Wed May 25 16:44:16 2011 Olivier Crouzet
*/
/*
** Internal settings for MiniLibX
*/
#ifndef MLX_INT_H
# define MLX_INT_H
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include <fcntl.h>
# include <sys/mman.h>
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include <sys/ipc.h>
# include <sys/shm.h>
# include <X11/extensions/XShm.h>
# include <X11/XKBlib.h>
/* #include <X11/xpm.h> */
# define MLX_TYPE_SHM_PIXMAP 3
# define MLX_TYPE_SHM 2
# define MLX_TYPE_XIMAGE 1
# define MLX_MAX_EVENT LASTEvent
# define ENV_DISPLAY "DISPLAY"
# define LOCALHOST "localhost"
# define ERR_NO_TRUECOLOR "MinilibX Error : No TrueColor Visual available.\n"
# define WARN_SHM_ATTACH "MinilibX Warning : X server can't attach shared memory.\n"
typedef struct s_xpm_col
{
int name;
int col;
} t_xpm_col;
struct s_col_name
{
char *name;
int color;
};
typedef struct s_event_list
{
int mask;
int (*hook)();
void *param;
} t_event_list;
typedef struct s_win_list
{
Window window;
GC gc;
struct s_win_list *next;
int (*mouse_hook)();
int (*key_hook)();
int (*expose_hook)();
void *mouse_param;
void *key_param;
void *expose_param;
t_event_list hooks[MLX_MAX_EVENT];
} t_win_list;
typedef struct s_img
{
XImage *image;
Pixmap pix;
GC gc;
int size_line;
int bpp;
int width;
int height;
int type;
int format;
char *data;
XShmSegmentInfo shm;
} t_img;
typedef struct s_xvar
{
Display *display;
Window root;
int screen;
int depth;
Visual *visual;
Colormap cmap;
int private_cmap;
t_win_list *win_list;
int (*loop_hook)();
void *loop_param;
int use_xshm;
int pshm_format;
int do_flush;
int decrgb[6];
Atom wm_delete_window;
Atom wm_protocols;
int end_loop;
} t_xvar;
int mlx_int_do_nothing();
int mlx_get_color_value();
int mlx_int_get_good_color();
int mlx_int_find_in_pcm();
int mlx_int_anti_resize_win();
int mlx_int_wait_first_expose();
int mlx_int_rgb_conversion();
int mlx_int_deal_shm();
void *mlx_int_new_xshm_image();
char **mlx_int_str_to_wordtab();
void *mlx_new_image();
int shm_att_pb();
int mlx_int_get_visual(t_xvar *xvar);
int mlx_int_set_win_event_mask(t_xvar *xvar);
int mlx_int_str_str_cote(char *str,char *find,int len);
int mlx_int_str_str(char *str,char *find,int len);
#endif

28
mlx/mlx_int_anti_resize_win.c

@ -0,0 +1,28 @@
/*
** mlx_int_anti_resize_win.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Aug 8 14:31:05 2000 Charlie Root
** Last update Tue Sep 25 15:56:58 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_int_anti_resize_win(t_xvar *xvar,Window win,int w,int h)
{
XSizeHints hints;
long toto;
XGetWMNormalHints(xvar->display,win,&hints,&toto);
hints.width = w;
hints.height = h;
hints.min_width = w;
hints.min_height = h;
hints.max_width = w;
hints.max_height = h;
hints.flags = PPosition | PSize | PMinSize | PMaxSize;
XSetWMNormalHints(xvar->display,win,&hints);
}

16
mlx/mlx_int_do_nothing.c

@ -0,0 +1,16 @@
/*
** mlx_int_do_nothing.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Aug 8 12:58:24 2000 Charlie Root
** Last update Tue Sep 25 15:56:22 2001 Charlie Root
*/
int mlx_int_do_nothing(void *param)
{
}

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save