Browse Source

Final

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

47
Makefile

@ -0,0 +1,47 @@
NAME = fdf
LIBFT = libft.a
SRCS = fdf.c fdf_commands.c fdf_drawing.c fdf_utils.c fdf_parsing.c get_next_line.c get_next_line_utils.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):
@${MAKE} -C ./libft
@cp ./libft/libft.a .
%.o:%.c
@${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
all: $(NAME)
$(NAME): $(LIBFT) $(OBJS)
@echo "Making FdF."
@${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 -rf libft.a
@echo "Cleaning binary."
@${RM} ${NAME}
@echo "Done."
re: fclean all
.PHONY: all clean fclean re

81
fdf.c

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 07:43:17 by narnaud #+# #+# */
/* Updated: 2022/02/24 13:44:31 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
/* Function init_datas
* -------------------
* datas :
* fd :
*/
void init_map(t_datas *datas, int fd)
{
t_camera *cam;
t_map *map;
int y;
map = ft_calloc(1, sizeof(t_map));
map->altitude = ft_calloc(512000, sizeof(int *));
cam = ft_calloc(1, sizeof(t_camera));
datas->map = parse_file(fd, map);
map->z_mult = SCALE / (2 * map->hightest);
cam->ang.yaw = trigo_calc((DEF_YAW % 360) * 3.1415 / 180);
cam->ang.pitch = trigo_calc((DEF_PITCH % 360) * 3.1415 / 180);
cam->zoom = SCALE * WIN_Y_SZ / map->depth;
cam->center.x = WIN_X_SZ / 2;
cam->center.y = WIN_Y_SZ / 2;
y = 0;
cam->render = ft_calloc(datas->map->depth, sizeof(t_2di *));
while (y < map->depth)
{
cam->render[y] = (t_2di *)ft_calloc(datas->map->width, sizeof(t_2di));
y++;
}
datas->cam = cam;
}
void init_window(t_datas *datas)
{
void *mlx;
void *window;
mlx = mlx_init();
if (!mlx)
exit(error_msg("Mlx fail to init", 1));
datas->mlx = mlx;
window = mlx_new_window(mlx, WIN_X_SZ, WIN_Y_SZ, "FdF");
if (!window)
exit(error_msg("Mlx fail to create window", 1));
datas->win = window;
}
int main(int argc, char const *argv[])
{
t_datas *datas;
int fd;
if (argc != 2)
return (error_msg("Wrong arguments.\n", 0));
fd = open(argv[1], O_RDONLY);
if (fd < 0)
return (error_msg("File error:", 1));
else if (ft_strncmp(get_file_ext(argv[1]), "fdf", 4))
return (error_msg("Wrong file extension.\n", 0));
datas = (t_datas *)malloc(sizeof(t_datas));
init_window(datas);
init_map(datas, fd);
draw_map(datas);
mlx_key_hook(datas->win, key_hook_primary, datas);
mlx_loop(datas->mlx);
return (0);
}

154
fdf.h

@ -0,0 +1,154 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 08:19:48 by narnaud #+# #+# */
/* Updated: 2022/05/21 17:32:20 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FDF_H
# define FDF_H
# define SPLIT_SIZE 512
# define BUFFER_SIZE 4096
# define WIN_X_SZ 900
# define WIN_Y_SZ 600
# define DEF_YAW 0
# define DEF_PITCH -45
# define SCALE 0.9
# define LINES_LEN 20
# ifdef __linux__
# include "./mlx/mlx.h"
# elif __APPLE__
# include <mlx.h>
# endif
# include "mlx_keycode.h"
# include <math.h>
# include <stdio.h>
# include <stddef.h>
# include <sys/stat.h>
# include <fcntl.h>
# include "./libft/libft.h"
typedef struct s_2d_float
{
float x;
float y;
} t_2df;
typedef struct s_2d_int
{
int x;
int y;
int len;
} t_2di;
typedef struct s_trigo
{
float raw;
float cos;
float sin;
} t_trigo;
typedef struct s_3d_pos
{
int x;
int y;
float z;
t_trigo yaw;
t_trigo pitch;
} t_3d;
typedef struct s_axis
{
t_trigo yaw;
t_trigo pitch;
} t_axis;
typedef struct s_map
{
int **altitude;
float z_mult;
int hightest;
int width;
int depth;
} t_map;
typedef struct s_camera
{
t_2di **render;
t_2di center;
t_axis ang;
float zoom;
int color;
} t_camera;
typedef struct s_datas
{
t_camera *cam;
t_map *map;
void *mlx;
void *win;
void *img;
int pixel_bits;
int line_bytes;
int endian;
} t_datas;
typedef struct s_buffer
{
char *content;
size_t pos;
size_t read_length;
} t_buffer;
typedef struct s_line_split
{
char *content;
size_t i;
size_t count;
struct s_line_split *next;
} t_split;
//fdf.c
void init_map(t_datas *datas, int fd);
void init_window(t_datas *datas);
//fdf_parsing.c
void parse_line(t_map *map, t_2di p, char ***splited);
t_map *parse_file(int fd, t_map *map);
const char *get_file_ext(const char *filename);
//fdf_drawing.c
int get_color(t_datas *datas, t_2di coord, float coef);
void draw_horiz_line(t_datas *datas, t_2di coord, int *buffer);
void draw_vert_line(t_datas *datas, t_2di coord, int *buffer);
void get_coords(t_2di coord, t_datas *datas);
void draw_map(t_datas *datas);
//fdf_commands.c
void key_hook_third(int keycode, t_datas *datas);
void key_hook_second(int keycode, t_datas *datas);
int key_hook_primary(int keycode, void *param);
//fdf_utils.c
int error_msg(char *msg, int sys);
t_trigo trigo_calc(float rad);
void clean_exit(t_datas *datas);
void draw_infos(t_datas *datas);
//gnl
char *get_next_line(int fd);
t_split *ft_new_split(char *content);
t_split *ft_splitlast(t_split *lst);
void ft_splitadd_back(t_split **alst, t_split *new);
#endif

64
fdf_commands.c

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/05 13:59:02 by narnaud #+# #+# */
/* Updated: 2022/02/22 10:43:07 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
void key_hook_third(int keycode, t_datas *datas)
{
if (keycode == KEY_1)
datas->map->z_mult /= 2;
else if (keycode == KEY_2)
datas->map->z_mult *= 2;
else if (keycode == KEY_3 && datas->cam->zoom >= 0.5)
datas->cam->zoom /= 2;
else if (keycode == KEY_4)
datas->cam->zoom *= 2;
else if (keycode == KEY_ECHAP)
clean_exit(datas);
}
void key_hook_second(int keycode, t_datas *datas)
{
if (keycode == KEY_A)
datas->cam->ang.yaw.raw += 3.1415 / 36;
else if (keycode == KEY_D)
datas->cam->ang.yaw.raw -= 3.1415 / 36;
else if (keycode == KEY_W)
datas->cam->ang.pitch.raw += 3.1415 / 36;
else if (keycode == KEY_S)
datas->cam->ang.pitch.raw -= 3.1415 / 36;
else
return (key_hook_third(keycode, datas));
datas->cam->ang.yaw.cos = cos(datas->cam->ang.yaw.raw);
datas->cam->ang.yaw.sin = sin(datas->cam->ang.yaw.raw);
datas->cam->ang.pitch.cos = cos(datas->cam->ang.pitch.raw);
datas->cam->ang.pitch.sin = sin(datas->cam->ang.pitch.raw);
}
int key_hook_primary(int keycode, void *param)
{
t_datas *datas;
datas = (t_datas *)param;
if (keycode == KEY_ARROW_UP)
datas->cam->center.y -= 20;
else if (keycode == KEY_ARROW_DOWN)
datas->cam->center.y += 20;
else if (keycode == KEY_ARROW_LEFT)
datas->cam->center.x -= 20;
else if (keycode == KEY_ARROW_RIGHT)
datas->cam->center.x += 20;
else
key_hook_second(keycode, datas);
draw_map(datas);
return (1);
}

173
fdf_drawing.c

@ -0,0 +1,173 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf_drawing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/05 14:44:08 by narnaud #+# #+# */
/* Updated: 2022/02/24 13:45:03 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
/* Function : get_color
* --------------------
* return color as decimal value depending of altitude.
* point with an altitude of zero are green,
* points with negativ altitude tend to the blue
* points with positiv altitude tend to the red.
*
* db : map datas,
* x, y : points coordinates,
* coef : the position of the point through the line,
* negativ for vertical lines,
* positiv for horizontal lines,
*
* return : integer which give the color of the point formated as :
* Red * 256^2 + Green * 256 + Blue
* Red, Green and Blue beeing between 0 and 255.
*/
int get_color(t_datas *db, t_2di coord, float coef)
{
int z0;
float dz;
int height;
int color_coef;
if (coord.x > 0 && coef <= 0)
{
z0 = db->map->altitude[coord.y][coord.x - 1];
dz = (float)db->map->altitude[coord.y][coord.x] - (float)z0;
height = z0 - (int)round(coef * dz);
color_coef = (height * 254) / db->map->hightest + 1;
}
else
{
z0 = db->map->altitude[coord.y - 1][coord.x];
dz = (float)db->map->altitude[coord.y][coord.x] - (float)z0;
height = z0 + (int)round(coef * dz);
color_coef = (height * 254) / db->map->hightest + 1;
}
if (height >= 0)
return (256 * 255 + color_coef * 256 * 255);
else
return (256 * 255 - color_coef);
}
/* Function : draw_line
* --------------------
* datas :
* start :
* end :
* buffer :
*
* return :
*
*/
void draw_horiz_line(t_datas *datas, t_2di coord, int *buffer)
{
size_t i;
t_2df dp;
t_2df pos;
size_t len;
i = 0;
pos.x = (float)datas->cam->render[coord.y][coord.x - 1].x;
pos.y = (float)datas->cam->render[coord.y][coord.x - 1].y;
dp.x = (float)datas->cam->render[coord.y][coord.x].x - pos.x;
dp.y = (float)datas->cam->render[coord.y][coord.x].y - pos.y;
len = sqrt(dp.x * dp.x + dp.y * dp.y);
dp.x /= len;
dp.y /= len;
while (i < len)
{
pos.x += dp.x;
pos.y += dp.y;
if (pos.x >= 0 && pos.y >= 0 && \
pos.x < WIN_X_SZ && pos.y < WIN_Y_SZ)
buffer[datas->line_bytes * (int)floor(pos.y) + (int)floor(pos.x)] \
= get_color(datas, coord, -(float)i / (float)len);
i++;
}
}
void draw_vert_line(t_datas *datas, t_2di coord, int *buffer)
{
size_t i;
t_2df dp;
t_2df pos;
size_t len;
i = 0;
pos.x = (float)datas->cam->render[coord.y - 1][coord.x].x;
pos.y = (float)datas->cam->render[coord.y - 1][coord.x].y;
dp.x = (float)datas->cam->render[coord.y][coord.x].x - pos.x;
dp.y = (float)datas->cam->render[coord.y][coord.x].y - pos.y;
len = sqrt(dp.x * dp.x + dp.y * dp.y);
dp.x /= len;
dp.y /= len;
while (i < len)
{
pos.x += dp.x;
pos.y += dp.y;
if (pos.x >= 0 && pos.y >= 0 && \
pos.x < WIN_X_SZ && pos.y < WIN_Y_SZ)
buffer[datas->line_bytes * (int)floor(pos.y) + (int)floor(pos.x)] \
= get_color(datas, coord, (float)i / (float)len);
i++;
}
}
void get_coords(t_2di coord, t_datas *datas)
{
t_2df vars;
t_3d pt;
pt.x = coord.x - datas->map->width / 2;
pt.y = coord.y - datas->map->depth / 2;
pt.z = datas->map->altitude[coord.y][coord.x] * datas->map->z_mult;
pt.yaw.cos = datas->cam->ang.yaw.cos;
pt.yaw.sin = datas->cam->ang.yaw.sin;
pt.pitch.cos = datas->cam->ang.pitch.cos;
pt.pitch.sin = datas->cam->ang.pitch.sin;
vars.x = pt.yaw.cos * pt.x + pt.yaw.sin * pt.y;
vars.y = pt.pitch.sin * (pt.yaw.sin * pt.x - pt.yaw.cos * pt.y) - \
pt.pitch.cos * pt.z;
datas->cam->render[coord.y][coord.x].x = datas->cam->center.x + \
round(datas->cam->zoom * vars.x);
datas->cam->render[coord.y][coord.x].y = datas->cam->center.y + \
round(datas->cam->zoom * vars.y);
}
void draw_map(t_datas *datas)
{
t_2di coord;
int *buffer;
datas->img = mlx_new_image(datas->mlx, WIN_X_SZ, WIN_Y_SZ);
buffer = (int *)mlx_get_data_addr(datas->img, \
&datas->pixel_bits, &datas->line_bytes, &datas->endian);
datas->line_bytes /= 4;
coord.y = 0;
while (coord.y < datas->map->depth)
{
coord.x = 0;
while (coord.x < datas->map->width)
{
get_coords(coord, datas);
if (coord.x > 0)
draw_horiz_line(datas, coord, buffer);
if (coord.y > 0)
draw_vert_line(datas, coord, buffer);
coord.x++;
}
coord.y++;
}
mlx_put_image_to_window(datas->mlx, datas->win, datas->img, 0, 0);
draw_infos(datas);
mlx_destroy_image(datas->mlx, datas->img);
}

92
fdf_parsing.c

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/22 09:16:28 by narnaud #+# #+# */
/* Updated: 2022/02/24 13:47:26 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
/* Function : parse_line
* ---------------------
* map :
* p :
* splitted
*/
void parse_line(t_map *map, t_2di p, char ***splited)
{
p.x = 0;
map->altitude[p.y] = ft_calloc(p.len, sizeof(int));
while ((*splited)[p.x])
{
map->altitude[p.y][p.x] = ft_atoi((*splited)[p.x]);
if (abs(map->altitude[p.y][p.x]) > abs(map->hightest))
map->hightest = abs(map->altitude[p.y][p.x]);
free((*splited)[p.x]);
p.x++;
}
if (map->hightest == 0)
map->hightest = 1;
free((*splited));
}
/* Function : parse_file
* ---------------------
* fd :
* line :
* splited :
*/
t_map *parse_file(int fd, t_map *map)
{
t_2di p;
char *line;
char **splited;
map->hightest = 0;
p.x = 0;
line = get_next_line(fd);
splited = ft_split(line, ' ');
free(line);
while (splited[p.x])
p.x++;
p.len = p.x;
p.y = 0;
while (splited)
{
parse_line(map, p, &(splited));
line = get_next_line(fd);
splited = ft_split(line, ' ');
free(line);
p.y++;
}
map->width = p.x;
map->depth = p.y;
close(fd);
return (map);
}
/* Function : get_file_ext
* --------------------------
* fd : file_descriptor
* return file extension
*/
const char *get_file_ext(const char *filename)
{
int i;
i = 0;
while (filename[i])
i++;
while (filename[--i])
if (filename[i] == '.')
return (filename + i + 1);
return (NULL);
}

79
fdf_utils.c

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/19 12:07:55 by narnaud #+# #+# */
/* Updated: 2022/02/24 13:49:50 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
char *get_info(char *ret, const char *name, int value)
{
int len;
len = ft_strlen(name);
ft_memcpy(ret, name, len);
ft_memcpy(ret + len, ft_itoa(value), 4);
ft_memcpy(ret + len + 4, "\0", 1);
return (ret);
}
void draw_infos(t_datas *datas)
{
char *str;
str = ft_calloc(255, sizeof(char));
mlx_string_put(datas->mlx, datas->win, 10, 10, 255 * 255 * 255, \
get_info(str, "Zoom: ", (int)datas->cam->zoom));
mlx_string_put(datas->mlx, datas->win, 10, 20, 255 * 255 * 255, \
get_info(str, "Z_multiplier: ", (int)datas->map->z_mult));
mlx_string_put(datas->mlx, datas->win, 10, 30, 255 * 255 * 255, \
get_info(str, "Yaw: ", (int)(datas->cam->ang.yaw.raw * 57.3)));
mlx_string_put(datas->mlx, datas->win, 10, 40, 255 * 255 * 255, \
get_info(str, "Pitch: ", (int)(datas->cam->ang.pitch.raw * 57.3)));
free(str);
}
int error_msg(char *msg, int sys)
{
if (sys)
perror(msg);
else
ft_putstr_fd(msg, 2);
return (1);
}
t_trigo trigo_calc(float rad)
{
t_trigo ret;
ret.raw = rad;
ret.cos = cos(rad);
ret.sin = sin(rad);
return (ret);
}
void clean_exit(t_datas *datas)
{
static int y = 0;
while (y < datas->map->depth)
{
free(datas->cam->render[y]);
free(datas->map->altitude[y]);
y++;
}
free(datas->map->altitude);
free(datas->cam->render);
free(datas->map);
free(datas->cam);
mlx_destroy_window(datas->mlx, datas->win);
free(datas->mlx);
free(datas);
exit(0);
}

113
get_next_line.c

@ -0,0 +1,113 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/25 10:00:41 by narnaud #+# #+# */
/* Updated: 2021/12/10 14:20:46 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int close_buffer(t_buffer **buffers)
{
t_buffer *buffer;
buffer = *buffers;
free(buffer->content);
free(buffer);
*buffers = NULL;
return (1);
}
static t_buffer *load_buffer(t_buffer **buffers)
{
t_buffer *buffer;
buffer = *buffers;
if (buffer)
return (buffer);
buffer = ft_calloc(1, sizeof(*buffer));
buffer->content = (char *)ft_calloc(BUFFER_SIZE + 1, sizeof(char));
buffer->pos = 0;
buffer->read_length = 0;
return (buffer);
}
int parse_buffer(t_buffer *buffer, t_split **line)
{
t_split *split;
split = ft_splitlast(*line);
while (buffer->content[buffer->pos])
{
if (buffer->pos >= buffer->read_length)
break ;
if (split->i == SPLIT_SIZE)
{
split = ft_new_split(ft_calloc(SPLIT_SIZE + 1, sizeof(char)));
ft_splitadd_back(line, split);
}
(split->content)[split->i] = buffer->content[buffer->pos];
if (buffer->content[buffer->pos++] == '\n')
return (1);
(split->i)++;
}
buffer->pos = 0;
return (0);
}
char *merge_splits(t_split **split)
{
t_split *next;
char *ret;
size_t i;
size_t count;
next = ft_splitlast(*split);
count = next->count;
ret = (char *)ft_calloc((count * SPLIT_SIZE) + 1, sizeof(char));
i = 0;
while (split && *split)
{
next = (*split)->next;
(*split)->i = 0;
while ((*split)->content[(*split)->i])
{
ret[i] = (*split)->content[(*split)->i];
i++;
((*split)->i)++;
}
free((*split)->content);
free(*split);
*split = next;
}
ret[i] = '\0';
return (ret);
}
char *get_next_line(int fd)
{
static t_buffer *buffer;
t_split *line;
char *ret;
buffer = load_buffer(&buffer);
if (read(fd, buffer->content, 0) < 0 && close_buffer(&buffer))
return (NULL);
line = ft_new_split((char *)ft_calloc(SPLIT_SIZE + 1, sizeof(char)));
while (!parse_buffer(buffer, &line))
{
buffer->read_length = read(fd, buffer->content, BUFFER_SIZE);
if (!buffer->read_length && close_buffer(&buffer))
break ;
}
ret = merge_splits(&line);
if (*ret)
return (ret);
free(ret);
return (NULL);
}

65
get_next_line_utils.c

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/29 08:54:42 by narnaud #+# #+# */
/* Updated: 2021/12/10 14:21:30 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
void *ft_calloc(size_t count, size_t size)
{
void *ret;
size_t i;
i = 0;
ret = malloc(count * size);
if (ret)
while (i < (count * size))
*(char *)(ret + i++) = 0;
return (ret);
}
t_split *ft_splitlast(t_split *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}
t_split *ft_new_split(char *content)
{
t_split *empty_lst;
empty_lst = (t_split *)malloc(sizeof(*empty_lst));
if (!empty_lst)
return (NULL);
empty_lst->content = content;
empty_lst->next = NULL;
empty_lst->i = 0;
empty_lst->count = 1;
return (empty_lst);
}
void ft_splitadd_back(t_split **alst, t_split *new)
{
t_split *i_cell;
if (!alst || !new)
return ;
if (*alst)
{
i_cell = ft_splitlast(*alst);
i_cell->next = new;
new->count = i_cell->count + 1;
}
else
*alst = new;
}

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);
}

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

Loading…
Cancel
Save