diff options
122 files changed, 4110 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..08b56ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.o +so_long +minilibx/ +*.d +so_long_bonus diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8b60c8a --- /dev/null +++ b/Makefile @@ -0,0 +1,62 @@ +CC = gcc +LD = gcc +CFLAGS = -Wall -Wextra -Werror -MMD +LDFLAGS = -Lminilibx -lmlx -Llibft -lft -lXext -lX11 -lm +SRC := badeline.c game.c game_draw.c main.c map.c map_count.c map_draw.c \ + map_get.c map_pathfind.c map_spawn.c map_verify.c player.c \ + player_collect.c player_collide_badeline.c player_collide.c \ + player_collide_exit.c player_erase.c readall.c sily.c sily_clear.c \ + sily_draw_text.c sily_expose.c sily_input.c sprite.c spritesheet.c +MSRC := sily_time.c +BSRC := sily_time_bonus.c +OBJ := $(patsubst %.c,%.o,$(SRC)) +MOBJ := $(patsubst %.c,%.o,$(MSRC)) +BOBJ := $(patsubst %.c,%.o,$(BSRC)) +DEP := $(patsubst %.c,%.d,$(SRC) $(MSRC) $(BSRC)) +NAME = so_long +LIBS.A = minilibx/libmlx.a libft/libft.a + +all: $(NAME) + +bonus: $(NAME)_bonus + +$(NAME): $(LIBS.A) $(OBJ) $(MOBJ) + $(LD) -o $(NAME) $(OBJ) $(MOBJ) $(LDFLAGS) + +$(NAME)_bonus: $(LIBS.A) $(OBJ) $(BOBJ) + $(LD) -o $(NAME)_bonus $(OBJ) $(BOBJ) $(LDFLAGS) + +%.o: %.c minilibx/mlx.h + $(CC) $(CFLAGS) -c -o $@ $< + +minilibx/mlx.h: + rm -rf minilibx + tar xvf minilibx.tgz + mv minilibx-linux minilibx + +minilibx/libmlx.a: minilibx/mlx.h + make -C minilibx + +libft/libft.a: + make -C libft + +run: $(NAME) + ./$(NAME) map.ber + +clean: + rm -f $(OBJ) $(MOBJ) $(BOBJ) $(DEP) + make -C libft clean + rm -f minilibx/*.o + +fclean: + rm -f $(OBJ) $(MOBJ) $(BOBJ) $(DEP) $(NAME) $(NAME)_bonus + make -C libft fclean + rm -rf minilibx + +re: + make fclean + make all + +.PHONY: all bonus run clean fclean re + +-include $(DEP) diff --git a/badeline.c b/badeline.c new file mode 100644 index 0000000..97dc486 --- /dev/null +++ b/badeline.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* badeline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/17 11:32:46 by kdx #+# #+# */ +/* Updated: 2022/10/17 11:32:46 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "badeline.h" +#include "player.h" +#include "sprite.h" +#include "game.h" +#include <math.h> + +void badeline_update(t_badeline *badeline) +{ + int i; + + if (!badeline->active) + return ; + if (badeline->queue_size >= BADELINE_DELAY) + { + badeline->erase[0] = badeline->queue[0][0]; + badeline->erase[1] = badeline->queue[0][1]; + } + i = 0; + while (i < badeline->queue_size - 1) + { + badeline->queue[i][0] = badeline->queue[i + 1][0]; + badeline->queue[i][1] = badeline->queue[i + 1][1]; + i += 1; + } + badeline->queue[i][0] = badeline->follow->pos[0]; + badeline->queue[i][1] = badeline->follow->pos[1]; + badeline->queue_size += (badeline->queue_size < BADELINE_DELAY); +} + +void badeline_erase(t_sily *sily, t_badeline *badeline) +{ + if (badeline->erase[0] && badeline->erase[1]) + player_erase(sily, badeline->erase); +} + +void badeline_draw(t_sily *sily, t_badeline *badeline) +{ + int x; + int y; + + x = round(badeline->queue[0][0]); + y = round(badeline->queue[0][1]); + if (badeline->queue_size >= BADELINE_DELAY && x && y) + sprite_draw(sily, sily->game->s_badeline, x, y); +} diff --git a/badeline.h b/badeline.h new file mode 100644 index 0000000..d18455d --- /dev/null +++ b/badeline.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* badeline.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/16 01:27:51 by kdx #+# #+# */ +/* Updated: 2022/10/16 01:54:13 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BADELINE_H +# include "sily.h" +# include <stdbool.h> +# define BADELINE_H +# define BADELINE_DELAY 30 + +typedef struct s_player t_player; +typedef struct s_badeline +{ + t_player *follow; + double queue[BADELINE_DELAY][2]; + double erase[2]; + int queue_size; + bool active; +} t_badeline; + +void badeline_update(t_badeline *badeline); +void badeline_erase(t_sily *sily, t_badeline *badeline); +void badeline_draw(t_sily *sily, t_badeline *badeline); + +#endif diff --git a/badeline.xpm b/badeline.xpm new file mode 100644 index 0000000..6752990 --- /dev/null +++ b/badeline.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * badeline_xpm[] = { +"16 16 2 1", +" c #FF00FF", +". c #4F397B", +" . .. .. . ", +".. . . ..", +" .. .. ", +" .......... ", +" .... .... ", +"....... .......", +". . . . . .", +" . . . ", +" . .. . ", +". . . . . .", +"....... .......", +" .... .... ", +" .......... ", +" .. .. ", +".. . . ..", +" . .. .. . "}; @@ -0,0 +1,64 @@ +111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +100000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000001 +100000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000001 +100000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000001 +100000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000001 +100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000001 +100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000001 +100000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000001 +100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000001 +100000000000000000000000000CCCCCCC00000000000000000000000000000000000000000000000000000000000001 +111000000000000000000000000C000000CCCCCCCCCCCCC0000000000000000000000000000000000000000000000001 +10011000000000000000000000C00000000000000000000CCCCCCCCCCCCC000000000000000000000000000000000001 +10000110000000000000000000C000000000000000000000000000000000CCCCCCCCCCCCC00000000000000000000001 +10000001000000000000000000C0000000000000000000000000000000000000000000000CCCCCCC0000000000000001 +1000000011000000000000000C00000000000000000000000000000000001000000000000000000C0000000000000001 +1000000000110000000000000C00000000000000000000000000000000001000000000000000000C0000000000000001 +1000000000001100000000000C0000000000000000000000000000000001000000000000000000C00000000000000001 +1000000000000011000000000C0000000000000000000000000000000010000000000000000000C00000000000000001 +100000000000000010000000C00000000000000000000000000000000010000000000000000000C00000000000000001 +100000000000000001100000C0000000000000000000000000000000010000000000000000000C000000000000000001 +100000000000000000010000C0000000000000000000000000000000100000000000000000000C000000000000000001 +10000000000000000000000C00000000000000000000000000000000100000000000000000000C000000000000000001 +10000000000000000000000C0000000000000000000000000000000100000000000000000000C0000000000000000001 +10000000000000000000000C0000000000000000000000000000001000000000000000000000C0000000000000000001 +1000000000000000000000C000010P0000000000000000000000001000000000000000000000C0000000000000000001 +1000000000000000000000C00000110000000000000000000000010000000000000000000000C0000000000000000001 +1000000000000000000000C0000000100000000000000000000010000000000000000000000C00000000000000000001 +1000000000000000000000C0000000011000000000000000000010000000000000000000000C00000000000000000001 +100000000000000000000C00000000000110000000000000000100000000000000000000000C00000000000000000001 +100000000000000000000C0000000000000110000000000000100000000000000000000000C000000000000000000001 +100000000000000000000C0000000000000001100000000000100000000000000000000000C000000000000000000001 +10000000000000000000C00000000000000000010000000001010000000000000000000000C000000000000000000001 +10000000000000000000C0000000000000000000110000001000110000000000000000000C0000000000000000000001 +10000000000000000000C0000000000000000000001100010000001100000000000000000C0000000000000000000001 +1000000000000000000C00000000000000000000000011010000000011000000000000000C0000000000000000000001 +1000000000000000000C0000000000000000000000000010000000000011000000000000C00000000000000000000001 +1000000000000000000C0000000000000000000000000100000000000000110000000000C00000000000000000000001 +100000000000000000C00000000000000000000000000100000000000000001100000000C00000000000000000000001 +100000000000000000C0000000000000000000000000100000000000000000001100000C000000000000000000000001 +100000000000000000C0000000000000000000000001000000000000000000000011000C000000000000000000000001 +100000000000000000C0000000000000000000000001000000000000000000000000000C000000000000000000000001 +10000000000000000C0000000000000000000000001000000000000000000000000000C0000000000000000000000001 +10000000000000000C0000000000000000000000010000000000000000000000000000C0000000000000000000000001 +10000000000000000C0000000000000000000000010000000000000000000000000000C0000000000000000000000001 +1000000000000000C00000000000000000000000100000000000000000000000000000C0000010000000000000000001 +1000000000000000CCCCCC00000000000000000000000000000000000000000000000C00000001100000000000000001 +1000000000000000000000CCCCCCCCCC0000000000000000000000000000000000000C00000000011000000000000001 +10000000000000000000000000000000CCCCCCCCCC000000000000000000000000000C00000000000110000000000001 +100000000000000000000000000000000000000000CCCCCCCCCC0000000000000000C00000000000000110E000000001 +1000000000000000000000000000000000000000000000000000CCCCCC0000000000C000000000000000011000000001 +1000000000000000000000000000000000000000000000000000000000CCCC000000C000000000000000000110000001 +10000000000000000000000000000000000000000000000000000000000000CCCC0C0000000000000000000001100001 +100000000000000000000000000000000000000000000000000000000000000000CC0000000000000000000000011001 +100000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000111 +100000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001 +100000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000001 +111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 diff --git a/exit/0.xpm b/exit/0.xpm new file mode 100644 index 0000000..7158d16 --- /dev/null +++ b/exit/0.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * 0_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #FFFFFF", +" ", +" .. ", +" .... ", +" ...... ", +" ... ... ", +" ... ... ", +" ... ... ", +" ... ... ", +" ... ... ", +" ... ... ", +" ... ... ", +" ... ... ", +" ...... ", +" .... ", +" .. ", +" "}; diff --git a/exit/1.xpm b/exit/1.xpm new file mode 100644 index 0000000..45a4f54 --- /dev/null +++ b/exit/1.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * 1_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #FFFFFF", +" ", +" ", +" ... ", +" ....... ", +" ....... .. ", +" ... .. ", +" .. .. ", +" .. .. ", +" .. .. ", +" .. .. ", +" .. ... ", +" .. ....... ", +" ....... ", +" ... ", +" ", +" "}; diff --git a/exit/2.xpm b/exit/2.xpm new file mode 100644 index 0000000..f1dea38 --- /dev/null +++ b/exit/2.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * 2_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #FFFFFF", +" ", +" .. ", +" ..... ", +" ...... ", +" .. .... ", +" ... ...... ", +" .... ... ", +" ... .. ", +" .. ... ", +" ... .... ", +" ...... ... ", +" .... .. ", +" ...... ", +" ..... ", +" .. ", +" "}; @@ -0,0 +1,125 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 07:30:46 by kdx #+# #+# */ +/* Updated: 2022/10/16 01:47:03 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include "game.h" +#include "sily.h" +#include "sprite.h" +#include "spritesheet.h" +#include <stdbool.h> + +static t_err load_sprite(t_game *game, t_sprite **dst, char *path, int size) +{ + *dst = sprite_load(game->sily, path, size & 255, size / 256); + return (*dst == NULL); +} + +static t_err load_tileset(t_game *game, const char *pattern, int size, + const char *tiles) +{ + int i; + char *path; + char *path_x; + unsigned char tile; + + if (ft_strchr(pattern, 'X') == NULL) + return (1); + path = ft_strdup(pattern); + if (path == NULL) + return (1); + path_x = ft_strchr(path, 'X'); + i = -1; + while (tiles[++i] != '\0') + { + tile = tiles[i]; + *path_x = tile; + if (load_sprite(game, &game->s_tiles[tile], path, size)) + { + ft_free(path); + return (1); + } + } + ft_free(path); + return (0); +} + +static t_game *load_sprites(t_game *game) +{ + if (load_tileset(game, "tiles/X.xpm", TSIZE + TSIZE * 256, + "01ECabcdefghijklmnop")) + return (game_destroy(game)); + if (load_sprite(game, &game->s_player, "player.xpm", TSIZE + TSIZE * 256)) + return (game_destroy(game)); + if (load_sprite(game, &game->s_badeline, "badeline.xpm", + TSIZE + TSIZE * 256)) + return (game_destroy(game)); + game->ss_exit = spritesheet_load(game->sily, "exit/X.xpm", 3); + if (game->ss_exit == NULL) + return (game_destroy(game)); + return (game); +} + +t_game *game_new(const char *map_path) +{ + t_game *game; + t_err err; + + if (ft_alloc(&game, sizeof(t_game))) + return (NULL); + err = map_load(&game->map, map_path); + if (err) + ft_printf("Error\n%s\n", map_load_error(err)); + if (err) + return (game_destroy(game)); + game->collectibles = map_count(&game->map, TILE_COLLECTIBLE); + game->sily = ft_calloc(1, sizeof(t_sily)); + if (game->sily == NULL) + return (game_destroy(game)); + if (sily_init(game->sily, game->map.width * TSIZE, game->map.height * TSIZE, + "SO LONG")) + return (game_destroy(game)); + game->sily->game = game; + ft_bzero(game->s_tiles, sizeof(game->s_tiles)); + if (load_sprites(game) == NULL) + return (NULL); + map_spawn(game, &game->map); + game->badeline.follow = &game->player; + game->badeline.active = false; + return (game); +} + +void *game_destroy(t_game *game) +{ + int i; + + if (game != NULL) + { + i = 0; + spritesheet_destroy(game->sily, game->ss_exit); + while (i < 256) + { + sprite_destroy(game->sily, game->s_tiles[i]); + i++; + } + sprite_destroy(game->sily, game->s_badeline); + sprite_destroy(game->sily, game->s_player); + if (game->sily) + { + mlx_do_key_autorepeaton(game->sily->ctx); + sily_deinit(game->sily); + } + ft_free(game->sily); + map_destroy(&game->map); + ft_free(game); + } + return (NULL); +} @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 07:29:21 by kdx #+# #+# */ +/* Updated: 2022/10/16 01:44:41 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GAME_H +# define GAME_H +# include "sprite.h" +# include "spritesheet.h" +# include "sily.h" +# include "player.h" +# include "badeline.h" +# include "map.h" + +typedef struct s_game +{ + t_sily *sily; + t_player player; + t_badeline badeline; + t_map map; + t_sprite *s_player; + t_sprite *s_badeline; + t_sprite *s_tiles[256]; + t_spritesheet *ss_exit; + int redraw; + int x; + int y; + size_t collectibles; + int victory; +} t_game; + +t_game *game_new(const char *map_path); +void *game_destroy(t_game *game); +void game_draw(t_game *game); + +#endif diff --git a/game_draw.c b/game_draw.c new file mode 100644 index 0000000..ed91aa4 --- /dev/null +++ b/game_draw.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game_draw.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/12 10:10:28 by kdx #+# #+# */ +/* Updated: 2022/10/14 03:28:39 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "game.h" +#include "map.h" +#include "player.h" +#include "sprite.h" +#include "badeline.h" + +void game_draw(t_game *game) +{ + t_sprite *s_exit; + + if (game->redraw) + { + game->redraw = 0; + map_draw(game->sily, &game->map); + sily_input_draw(game->sily); + } + s_exit = game->ss_exit->frame[(game->sily->tick / 10) + % game->ss_exit->frame_count]; + badeline_erase(game->sily, &game->badeline); + player_erase(game->sily, game->player.old_pos); + sprite_draw(game->sily, s_exit, game->map.exit[0], game->map.exit[1]); + badeline_draw(game->sily, &game->badeline); + player_draw(game->sily, &game->player); + if (game->sily->prev_move_events != game->sily->move_events) + sily_input_draw(game->sily); +} diff --git a/libft/.gitignore b/libft/.gitignore new file mode 100644 index 0000000..b32c3ed --- /dev/null +++ b/libft/.gitignore @@ -0,0 +1,4 @@ +*.a +*.o +a.out +libft.so diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..47a51ce --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,49 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/09/26 12:10:27 by kdx #+# #+# # +# Updated: 2022/10/12 10:00:34 by kdx ### ########.fr # +# # +# **************************************************************************** # + +CC = gcc +CFLAGS = -Wall -Wextra -Werror +NAME = libft.a +SRC = ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c \ + ft_strlen.c ft_memset.c ft_bzero.c ft_memcpy.c ft_memmove.c ft_strlcpy.c \ + ft_strlcat.c ft_toupper.c ft_tolower.c ft_strchr.c ft_strrchr.c ft_strncmp.c \ + ft_memchr.c ft_memcmp.c ft_strnstr.c ft_atoi.c ft_calloc.c ft_strdup.c \ + ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \ + ft_striteri.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \ + ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c \ + ft_lstadd_back.c ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c \ + ft_puti128_fd.c ft_putu128x_fd.c ft_vdprintf.c ft_dprintf.c ft_vprintf.c \ + ft_printf.c ft_free.c ft_alloc.c +OBJ := $(patsubst %.c,%.o,$(SRC)) + +all: $(NAME) + +$(NAME): $(OBJ) + ar -rc $(NAME) $(OBJ) + +bonus: $(NAME) + +%.o: %.c libft.h + $(CC) $(CFLAGS) -c -o $@ $< + +so: $(OBJ) + $(CC) -nostartfiles -shared -o libft.so $(OBJ) + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) libft.so + +re: fclean all + +.PHONY: bonus so clean fclean re diff --git a/libft/ft_alloc.c b/libft/ft_alloc.c new file mode 100644 index 0000000..99eeadb --- /dev/null +++ b/libft/ft_alloc.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_alloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/12 09:59:21 by kdx #+# #+# */ +/* Updated: 2022/10/12 10:01:51 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_err ft_alloc(void *ptr, size_t size) +{ + *((void **)ptr) = ft_calloc(1, size); + return (*((void **)ptr) == NULL); +} diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..53c0e5a --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/25 16:53:01 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:31:22 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <limits.h> + +int ft_atoi(const char *nptr) +{ + long result; + int sign; + + while (*nptr == ' ' || *nptr == '\n' || *nptr == '\t' + || *nptr == '\r' || *nptr == '\f' || *nptr == '\v') + nptr += 1; + sign = 1; + if (*nptr == '+' || *nptr == '-') + { + if (*nptr == '-') + sign = -sign; + nptr += 1; + } + result = 0; + while (*nptr >= '0' && *nptr <= '9') + { + result = result * 10 + (*nptr - '0') * sign; + nptr += 1; + if (result > INT_MAX) + return (-1); + if (result < INT_MIN) + return (0); + } + return (result); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100644 index 0000000..f5320e7 --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 04:18:59 by kdx #+# #+# */ +/* Updated: 2022/09/27 05:15:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c new file mode 100644 index 0000000..b7c710d --- /dev/null +++ b/libft/ft_calloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 05:18:38 by kdx #+# #+# */ +/* Updated: 2022/09/29 20:49:25 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <stdint.h> + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ptr; + + if (nmemb != 0 && (size_t)(nmemb * size) / nmemb != size) + return (NULL); + ptr = malloc(nmemb * size); + if (ptr == NULL) + return (ptr); + ft_bzero(ptr, nmemb * size); + return (ptr); +} diff --git a/libft/ft_dprintf.c b/libft/ft_dprintf.c new file mode 100644 index 0000000..2a2234a --- /dev/null +++ b/libft/ft_dprintf.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 14:29:46 by kdx #+# #+# */ +/* Updated: 2022/10/06 14:33:24 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_i32 ft_dprintf(int fd, const char *fmt, ...) +{ + va_list va; + t_i32 rv; + + va_start(va, fmt); + rv = ft_vdprintf(fd, fmt, va); + va_end(va); + return (rv); +} diff --git a/libft/ft_free.c b/libft/ft_free.c new file mode 100644 index 0000000..1713236 --- /dev/null +++ b/libft/ft_free.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/07 13:31:11 by kdx #+# #+# */ +/* Updated: 2022/10/07 14:51:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_free(void *ptr) +{ + if (ptr != NULL) + free(ptr); + return (NULL); +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100644 index 0000000..75c9552 --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 12:35:09 by kdx #+# #+# */ +/* Updated: 2022/09/26 15:05:32 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100644 index 0000000..8e3ac7b --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 12:23:43 by kdx #+# #+# */ +/* Updated: 2022/09/26 16:59:14 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100644 index 0000000..4ffcc7d --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:15:42 by kdx #+# #+# */ +/* Updated: 2022/09/26 15:05:45 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (c >= 0 && c <= 127); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100644 index 0000000..760e7fe --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 12:32:05 by kdx #+# #+# */ +/* Updated: 2022/09/28 04:43:31 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100644 index 0000000..65226a0 --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:40:45 by kdx #+# #+# */ +/* Updated: 2022/09/26 17:19:40 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (c >= ' ' && c <= '~'); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100644 index 0000000..09d0319 --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 19:00:04 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:43:21 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *_itoa(long v, int sign, long div, char *ptr) +{ + size_t i; + + i = 0; + if (sign == -1) + { + ptr[i] = '-'; + i += 1; + } + while (div / 10 > 0) + { + div /= 10; + ptr[i] = '0' + v / div % 10; + i += 1; + } + ptr[i] = '\0'; + return (ptr); +} + +char *ft_itoa(int n) +{ + long v; + int sign; + long div; + size_t i; + char *ptr; + + if (n == 0) + return (ft_strdup("0")); + v = n; + sign = (v > 0) - (v < 0); + v *= sign; + div = 1; + i = (sign == -1); + while (div <= v && i + 1) + { + i += 1; + div *= 10; + } + ptr = malloc(i + 1); + if (ptr == NULL) + return (NULL); + return (_itoa(v, sign, div, ptr)); +} diff --git a/libft/ft_lstadd_back.c b/libft/ft_lstadd_back.c new file mode 100644 index 0000000..953f771 --- /dev/null +++ b/libft/ft_lstadd_back.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:06:14 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:10:36 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *last; + + if (lst == NULL || new == NULL) + return ; + last = ft_lstlast(*lst); + if (last == NULL) + *lst = new; + else + last->next = new; +} diff --git a/libft/ft_lstadd_front.c b/libft/ft_lstadd_front.c new file mode 100644 index 0000000..ca3b6ca --- /dev/null +++ b/libft/ft_lstadd_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:58:48 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:00:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **list, t_list *new) +{ + if (new == NULL) + return ; + new->next = *list; + *list = new; +} diff --git a/libft/ft_lstclear.c b/libft/ft_lstclear.c new file mode 100644 index 0000000..a262722 --- /dev/null +++ b/libft/ft_lstclear.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:23:00 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:26:07 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *next; + + if (lst == NULL || del == NULL) + return ; + while (*lst != NULL) + { + next = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = next; + } +} diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c new file mode 100644 index 0000000..6975f88 --- /dev/null +++ b/libft/ft_lstdelone.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:11:51 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:21:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (del == NULL || lst == NULL) + return ; + if (lst != NULL) + { + if (lst->content != NULL) + del(lst->content); + free(lst); + } +} diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c new file mode 100644 index 0000000..4f66050 --- /dev/null +++ b/libft/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:30:19 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:31:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (f == NULL) + return ; + while (lst != NULL) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/libft/ft_lstlast.c b/libft/ft_lstlast.c new file mode 100644 index 0000000..926b5d3 --- /dev/null +++ b/libft/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:02:22 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:03:20 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c new file mode 100644 index 0000000..e962de6 --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:32:46 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:40:19 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *root; + t_list *last; + + if (f == NULL || del == NULL) + return (NULL); + root = ft_lstnew(f(lst->content)); + if (root == NULL) + return (NULL); + last = root; + lst = lst->next; + while (lst != NULL) + { + last->next = ft_lstnew(f(lst->content)); + if (last->next == NULL) + { + ft_lstclear(&root, del); + return (NULL); + } + lst = lst->next; + last = last->next; + } + return (root); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100644 index 0000000..6ccb7a8 --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:49:36 by kdx #+# #+# */ +/* Updated: 2022/09/29 02:50:55 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *lst; + + lst = malloc(sizeof(t_list)); + if (lst == NULL) + return (NULL); + lst->content = content; + lst->next = NULL; + return (lst); +} diff --git a/libft/ft_lstsize.c b/libft/ft_lstsize.c new file mode 100644 index 0000000..6bd2025 --- /dev/null +++ b/libft/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:00:40 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:32:19 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int count; + + count = 0; + while (lst != NULL) + { + count += 1; + lst = lst->next; + } + return (count); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100644 index 0000000..13ee6fc --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 23:53:11 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:32:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + const unsigned char *data; + unsigned char ch; + size_t i; + + data = s; + ch = c; + i = 0; + while (i < n) + { + if (data[i] == ch) + return ((void *)s + i); + i += 1; + } + return (NULL); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100644 index 0000000..442d3d0 --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 05:37:18 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:32:42 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <stdint.h> + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + const uint8_t *data_1; + const uint8_t *data_2; + size_t i; + + if (n == 0) + return (0); + data_1 = s1; + data_2 = s2; + i = 0; + while (i + 1 < n) + { + if (data_1[i] != data_2[i]) + return (data_1[i] - data_2[i]); + i += 1; + } + return (data_1[i] - data_2[i]); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100644 index 0000000..aa62b88 --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 06:34:25 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:27:38 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + if (dest == src) + return (dest); + while (n > 0) + { + n -= 1; + ((char *)dest)[n] = ((char *)src)[n]; + } + return (dest); +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100644 index 0000000..75797d1 --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 08:42:29 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:33:27 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + size_t i; + + if (dest == src) + return (dest); + if (dest < src) + { + i = 0; + while (i < n) + { + ((char *)dest)[i] = ((char *)src)[i]; + i += 1; + } + } + else + ft_memcpy(dest, src, n); + return (dest); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100644 index 0000000..b37c11c --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 01:05:33 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:33:40 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + unsigned char *data; + size_t i; + + data = s; + i = 0; + while (i < n) + { + data[i] = c; + i += 1; + } + return (data); +} diff --git a/libft/ft_printf.c b/libft/ft_printf.c new file mode 100644 index 0000000..49643cb --- /dev/null +++ b/libft/ft_printf.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 14:29:46 by kdx #+# #+# */ +/* Updated: 2022/10/06 14:34:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_i32 ft_printf(const char *fmt, ...) +{ + va_list va; + t_i32 rv; + + va_start(va, fmt); + rv = ft_vprintf(fmt, va); + va_end(va); + return (rv); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c new file mode 100644 index 0000000..120c9cc --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:11:58 by kdx #+# #+# */ +/* Updated: 2022/10/04 04:54:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <unistd.h> + +size_t ft_putchar_fd(char c, int fd) +{ + return (write(fd, &c, 1) == 1); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100644 index 0000000..9fc0cb3 --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:23:50 by kdx #+# #+# */ +/* Updated: 2022/09/29 02:24:32 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libft/ft_puti128_fd.c b/libft/ft_puti128_fd.c new file mode 100644 index 0000000..a8bc0fe --- /dev/null +++ b/libft/ft_puti128_fd.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_puti128_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:29:50 by kdx #+# #+# */ +/* Updated: 2022/10/04 05:53:17 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_puti128_fd(t_i128 n, int fd) +{ + t_u128 v; + t_u128 div; + size_t rv; + + rv = 0; + v = n; + if (n < 0) + { + rv += ft_putchar_fd('-', fd); + v = -n; + } + div = 1; + while (div <= v / 10) + div *= 10; + while (div > 0) + { + rv += ft_putchar_fd('0' + v / div % 10, fd); + div /= 10; + } + return (rv); +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..7705180 --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:29:50 by kdx #+# #+# */ +/* Updated: 2022/10/04 04:54:24 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + long v; + long div; + + if (n == 0) + { + ft_putchar_fd('0', fd); + return ; + } + v = n; + if (v < 0) + { + ft_putchar_fd('-', fd); + v *= -1; + } + div = 1; + while (div <= v) + div *= 10; + while (div / 10 > 0) + { + div /= 10; + ft_putchar_fd('0' + v / div % 10, fd); + } +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100644 index 0000000..e971974 --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:16:42 by kdx #+# #+# */ +/* Updated: 2022/10/04 07:56:40 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <unistd.h> + +size_t ft_putstr_fd(char *s, int fd) +{ + if (s == NULL) + return (ft_putstr_fd("(null)", fd)); + else + write(fd, s, ft_strlen(s)); + return (ft_strlen(s)); +} diff --git a/libft/ft_putu128x_fd.c b/libft/ft_putu128x_fd.c new file mode 100644 index 0000000..01989b4 --- /dev/null +++ b/libft/ft_putu128x_fd.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putu128x_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:29:50 by kdx #+# #+# */ +/* Updated: 2022/10/04 05:58:16 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_putu128x_fd(t_u128 n, int fd, int capitalize) +{ + t_u128 div; + size_t rv; + const char *set; + + set = "0123456789abcdef"; + if (capitalize) + set = "0123456789ABCDEF"; + rv = 0; + div = 1; + while (div <= n / 16) + div *= 16; + while (div > 0) + { + rv += ft_putchar_fd(set[n / div % 16], fd); + div /= 16; + } + return (rv); +} diff --git a/libft/ft_split.c b/libft/ft_split.c new file mode 100644 index 0000000..bce2f72 --- /dev/null +++ b/libft/ft_split.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 16:55:02 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:34:28 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <stdbool.h> + +static void *free_array(char **a) +{ + size_t i; + + if (a != NULL) + { + i = 0; + while (a[i] != NULL) + { + free(a[i]); + i += 1; + } + free(a); + } + return (NULL); +} + +static size_t skip_char(const char **s, char c, bool invert) +{ + size_t i; + const char *p; + + i = 0; + p = *s; + while (((!invert && *p == c) || (invert && *p != c)) && *p != '\0') + { + p += 1; + i += 1; + } + *s = p; + return (i); +} + +static size_t count_elems(const char *s, char c) +{ + size_t n_elem; + + n_elem = 0; + skip_char(&s, c, false); + while (*s != '\0') + { + skip_char(&s, c, true); + skip_char(&s, c, false); + n_elem += 1; + } + return (n_elem); +} + +char **ft_split(char const *s, char c) +{ + char **array; + size_t n_elem; + size_t arr_i; + const char *rem_s; + size_t len; + + if (s == NULL) + return (NULL); + n_elem = count_elems(s, c); + array = ft_calloc(n_elem + 1, sizeof(char *)); + if (array == NULL) + return (array); + skip_char(&s, c, false); + arr_i = 0; + while (arr_i < n_elem) + { + rem_s = s; + len = skip_char(&s, c, true); + array[arr_i] = ft_substr(rem_s, 0, len); + if (array[arr_i] == NULL) + return (free_array(array)); + arr_i += 1; + skip_char(&s, c, false); + } + return (array); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100644 index 0000000..373c5bb --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 23:37:56 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:25:14 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s != c) + { + if (*s == '\0') + return (NULL); + s += 1; + } + return ((char *)s); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100644 index 0000000..0bbfc2e --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 05:21:01 by kdx #+# #+# */ +/* Updated: 2022/09/28 05:28:25 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *copy; + size_t s_len; + + s_len = ft_strlen(s) + 1; + copy = malloc(s_len); + if (copy == NULL) + return (NULL); + ft_memcpy(copy, s, s_len); + return (copy); +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100644 index 0000000..d7ed90b --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:06:44 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:25:22 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + size_t i; + + if (s == NULL || f == NULL) + return ; + i = 0; + while (s[i] != '\0') + { + f(i, s + i); + i += 1; + } +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..30447d1 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 07:29:30 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:28:15 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t s1_len; + size_t s2_len; + char *ptr; + + if (s1 == NULL || s2 == NULL) + return (NULL); + s1_len = ft_strlen(s1); + s2_len = ft_strlen(s2); + ptr = malloc(s1_len + s2_len + 1); + if (ptr == NULL) + return (NULL); + ptr[s1_len + s2_len] = '\0'; + while (s2_len > 0) + { + s2_len -= 1; + ptr[s1_len + s2_len] = s2[s2_len]; + } + while (s1_len > 0) + { + s1_len -= 1; + ptr[s1_len] = s1[s1_len]; + } + return (ptr); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100644 index 0000000..cd1f70b --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 09:02:56 by kdx #+# #+# */ +/* Updated: 2022/09/28 17:03:21 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t dest_size; + + if (size == 0) + return (ft_strlen(src)); + dest_size = ft_strlen(dst); + if (dest_size >= size) + return (ft_strlen(src) + size); + return (dest_size + ft_strlcpy(dst + dest_size, src, size - dest_size)); +} diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c new file mode 100644 index 0000000..16016ae --- /dev/null +++ b/libft/ft_strlcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 08:46:49 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:25:35 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + while (i + 1 < size && src[i] != '\0') + { + dst[i] = src[i]; + i += 1; + } + if (size) + dst[i] = '\0'; + return (ft_strlen(src)); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..bd11faf --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 14:34:15 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:25:47 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t len; + + len = 0; + while (s[len] != '\0') + len += 1; + return (len); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100644 index 0000000..f769325 --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 20:06:57 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:25:55 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *copy; + size_t i; + + if (s == NULL || f == NULL) + return (NULL); + copy = ft_strdup(s); + if (copy == NULL) + return (NULL); + i = 0; + while (copy[i] != '\0') + { + copy[i] = f(i, copy[i]); + i += 1; + } + return (copy); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..7861842 --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/18 18:00:30 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:34:13 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + if (n == 0) + return (0); + n -= 1; + while (n > 0 && (*s1 != '\0' || *s2 != '\0')) + { + if (*s1 != *s2) + return ((unsigned char)*s1 - (unsigned char)*s2); + s1 += 1; + s2 += 1; + n -= 1; + } + return ((unsigned char)*s1 - (unsigned char)*s2); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..93427d2 --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 00:07:22 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:26:20 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t little_len; + size_t cmp_len; + + little_len = ft_strlen(little); + if (little_len == 0 || (big == little && little_len <= len)) + return ((char *)big); + if (len == 0) + return (NULL); + i = 0; + while (big[i] != '\0' && i < len && little_len <= len - i) + { + cmp_len = len - i; + if (little_len < cmp_len) + cmp_len = little_len; + if (ft_strncmp(big + i, little, cmp_len) == 0) + return ((char *)big + i); + i += 1; + } + return (NULL); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100644 index 0000000..df363a2 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 23:37:56 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:37:31 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t i; + + i = ft_strlen(s) + 1; + while (i > 0) + { + i -= 1; + if (s[i] == c) + return ((char *)(s + i)); + } + return (NULL); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..d844fb4 --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 09:23:36 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:37:46 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t begin; + size_t to_copy; + size_t len; + + if (s1 == NULL || set == NULL) + return (NULL); + len = ft_strlen(s1); + if (len == 0) + return (ft_strdup("")); + begin = 0; + while (ft_strchr(set, s1[begin]) != NULL) + begin += 1; + to_copy = len - begin; + while (ft_strchr(set, s1[begin + to_copy - 1]) != NULL && to_copy) + to_copy -= 1; + return (ft_substr(s1, begin, to_copy)); +} diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100644 index 0000000..283db6e --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 06:35:57 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:37:58 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + ssize_t size; + char *ptr; + + if (s == NULL) + return (NULL); + size = ft_strlen(s); + size -= start; + if (size < 0) + size = 0; + if ((size_t)size > len) + size = len; + ptr = malloc(size + 1); + if (ptr == NULL) + return (NULL); + ptr[size] = '\0'; + while (size > 0) + { + size -= 1; + ptr[size] = s[start + size]; + } + return (ptr); +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100644 index 0000000..5c5d65b --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 09:39:00 by kdx #+# #+# */ +/* Updated: 2022/09/27 09:43:03 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c - 'A' + 'a'); + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100644 index 0000000..ba6983a --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 09:39:00 by kdx #+# #+# */ +/* Updated: 2022/09/27 09:39:52 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c + 'A' - 'a'); + return (c); +} diff --git a/libft/ft_vdprintf.c b/libft/ft_vdprintf.c new file mode 100644 index 0000000..3d6b5da --- /dev/null +++ b/libft/ft_vdprintf.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vdprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/30 05:04:45 by kdx #+# #+# */ +/* Updated: 2022/10/14 02:26:57 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <unistd.h> + +static t_i32 put_ptr(void *ptr, int fd) +{ + t_i32 rv; + + rv = 0; + if (ptr != NULL) + { + rv += ft_putstr_fd("0x", fd); + rv += ft_putu128x_fd((t_u64)ptr, fd, 0); + } + else + rv += ft_putstr_fd("(nil)", fd); + return (rv); +} + +static t_i32 put_element(t_i32 fd, const char *fmt, va_list va) +{ + if (*fmt == '%') + return (ft_putchar_fd(*fmt, fd)); + if (*fmt == 'c') + return (ft_putchar_fd(va_arg(va, int), fd)); + if (*fmt == 'd' || *fmt == 'i') + return (ft_puti128_fd(va_arg(va, int), fd)); + if (*fmt == 'u') + return (ft_puti128_fd(va_arg(va, unsigned int), fd)); + if (*fmt == 'p') + return (put_ptr(va_arg(va, void *), fd)); + if (*fmt == 'x' || *fmt == 'X') + return (ft_putu128x_fd(va_arg(va, unsigned int), fd, *fmt == 'X')); + if (*fmt == 's') + return (ft_putstr_fd(va_arg(va, char *), fd)); + return (0); +} + +t_i32 ft_vdprintf(t_i32 fd, const char *fmt, va_list va) +{ + t_i32 rv; + + rv = 0; + while (*fmt != '\0') + { + if (*fmt == '%' && ft_strchr("cdiupxXs%", fmt[1]) != NULL) + { + fmt += 1; + rv += put_element(fd, fmt, va); + } + else + rv += ft_putchar_fd(*fmt, fd); + fmt += 1; + } + return (rv); +} diff --git a/libft/ft_vprintf.c b/libft/ft_vprintf.c new file mode 100644 index 0000000..db53f97 --- /dev/null +++ b/libft/ft_vprintf.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 14:29:46 by kdx #+# #+# */ +/* Updated: 2022/10/06 14:33:43 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_i32 ft_vprintf(const char *fmt, va_list va) +{ + return (ft_vdprintf(1, fmt, va)); +} diff --git a/libft/libft.h b/libft/libft.h new file mode 100644 index 0000000..09e82d3 --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 04:34:06 by kdx #+# #+# */ +/* Updated: 2022/10/12 10:01:24 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include <stdlib.h> +# include <stdint.h> +# include <stdarg.h> + +typedef int8_t t_i8; +typedef int16_t t_i16; +typedef int32_t t_i32; +typedef int64_t t_i64; +typedef __int128_t t_i128; +typedef uint8_t t_u8; +typedef uint16_t t_u16; +typedef uint32_t t_u32; +typedef uint64_t t_u64; +typedef __uint128_t t_u128; +typedef t_u32 t_err; + +int ft_atoi(const char *nptr); +void ft_bzero(void *s, size_t n); +void *ft_calloc(size_t nmemb, size_t size); +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *s, int c, size_t n); +char *ft_strchr(const char *s, int c); +char *ft_strdup(const char *s); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlen(const char *s); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strrchr(const char *s, int c); +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(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +size_t ft_putchar_fd(char c, int fd); +size_t ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +size_t ft_puti128_fd(t_i128 n, int fd); +size_t ft_putu128x_fd(t_u128 n, int fd, int capitalize); + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +t_i32 ft_vdprintf(int fd, const char *fmt, va_list va); +t_i32 ft_dprintf(int fd, const char *fmt, ...); +t_i32 ft_vprintf(const char *fmt, va_list va); +t_i32 ft_printf(const char *fmt, ...); + +void *ft_free(void *ptr); +t_err ft_alloc(void *ptr, size_t size); + +#endif @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/07 01:51:52 by kdx #+# #+# */ +/* Updated: 2022/10/14 14:45:59 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "badeline.h" +#include "libft/libft.h" +#include "game.h" +#include "sily.h" +#include "sprite.h" +#include "player.h" +#include <stdlib.h> + +static void update(t_sily *sily) +{ + sily_input_update(sily); + player_update(sily, &sily->game->player); + badeline_update(&sily->game->badeline); + if (sily->game->victory) + { + ft_putendl_fd("Victory!", 1); + game_destroy(sily->game); + exit(0); + } +} + +static void draw(t_sily *sily) +{ + game_draw(sily->game); +} + +int main(int argc, char **argv) +{ + t_game *game; + + if (argc != 2) + return (ft_printf("Error\nusage: so_long <map.ber>\n")); + game = game_new(argv[1]); + if (game == NULL) + return (1); + game->redraw = 1; + sily_loop(game->sily, update, draw); + return (0); +} @@ -0,0 +1,16 @@ +1111111111111111 +11111000000C0001 +1000000000C00001 +10000000CC000E01 +1C00000C00000111 +10CC00C000011111 +1000CC0000111001 +1100000001110001 +1111000000000001 +1111110000000001 +1000111000C0C001 +1000000000101001 +100000000C0C0C01 +1000000001010101 +100P000000000001 +1111111111111111 @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 07:35:33 by kdx #+# #+# */ +/* Updated: 2022/10/16 00:21:32 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" +#include "readall.h" +#include <unistd.h> +#include <fcntl.h> + +static t_err check_path(const char *path) +{ + if (ft_strlen(path) <= ft_strlen(".ber")) + return (1); + if (ft_strncmp(path + ft_strlen(path) - ft_strlen(".ber"), ".ber", 4)) + return (1); + return (0); +} + +static t_err find_exit(t_map *map) +{ + size_t i; + + i = 0; + while (i < (map->width + 1) * map->height) + { + if (map->data[i] == TILE_EXIT) + { + map->exit[0] = (i % (map->width + 1)) * TSIZE; + map->exit[1] = (i / (map->width + 1)) * TSIZE; + return (0); + } + i++; + } + return (1); +} + +t_err map_load(t_map *map, const char *path) +{ + int fd; + t_err err; + + if (check_path(path)) + return (ERR_FILENAME); + fd = open(path, O_RDONLY); + if (fd < 0) + return (ERR_OPEN); + map->data = (unsigned char *)readall(fd); + close(fd); + if (map->data == NULL) + return (ERR_READ); + err = map_verify(map, (unsigned const char *)map->data); + if (err) + return (err); + if (find_exit(map)) + return (ERR_MISSING); + return (ERR_NONE); +} + +void map_destroy(t_map *map) +{ + ft_free(map->data); + map->data = NULL; +} + +const char *map_load_error(t_err err) +{ + if (err == ERR_OPEN) + return ("Failed to open file."); + if (err == ERR_READ) + return ("Failed to read file."); + if (err == ERR_MALLOC) + return ("Failed to malloc."); + if (err == ERR_INVALID_CHAR) + return ("Found invalid character in loaded map."); + if (err == ERR_WRONG_DIMENSION) + return ("Wrong map dimensions."); + if (err == ERR_FILENAME) + return ("Invalid filename."); + if (err == ERR_MISSING) + return ("Missing required character."); + if (err == ERR_DUPLICATE) + return ("Duplicate unique character."); + if (err == ERR_OPEN_BORDER) + return ("Borders aren't closed properly."); + if (err == ERR_NO_PATH) + return ("Map is impossible."); + return ("No error."); +} @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 07:29:31 by kdx #+# #+# */ +/* Updated: 2022/10/16 00:12:59 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MAP_H +# define MAP_H +# include "libft/libft.h" +# include "sily.h" +# define TSIZE 16 + +enum +{ + TILE_NONE = '0', + TILE_WALL = '1', + TILE_COLLECTIBLE = 'C', + TILE_EXIT = 'E', + TILE_PLAYER = 'P', +}; + +enum +{ + ERR_NONE, + ERR_OPEN, + ERR_READ, + ERR_MALLOC, + ERR_INVALID_CHAR, + ERR_WRONG_DIMENSION, + ERR_FILENAME, + ERR_MISSING, + ERR_DUPLICATE, + ERR_OPEN_BORDER, + ERR_NO_PATH, +}; + +typedef struct s_map +{ + unsigned char *data; + size_t width; + size_t height; + size_t exit[2]; +} t_map; + +t_err map_load(t_map *map, const char *path); +void map_destroy(t_map *map); +const char *map_load_error(t_err err); +void map_draw_tile(t_sily *sily, t_map *map, int x, int y); +void map_draw(t_sily *sily, t_map *map); +void map_spawn(t_game *game, t_map *map); +unsigned char map_get(t_map *map, int x, int y); +void map_set(t_map *map, int x, int y, unsigned char v); +size_t map_count(t_map *map, int v); + +t_err map_verify(t_map *map, const unsigned char *data); +t_err map_pathfind(t_map *map); + +#endif diff --git a/map_count.c b/map_count.c new file mode 100644 index 0000000..7c0167e --- /dev/null +++ b/map_count.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_count.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 14:33:55 by kdx #+# #+# */ +/* Updated: 2022/10/14 14:34:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" + +size_t map_count(t_map *map, int v) +{ + size_t count; + size_t i; + + count = 0; + i = 0; + while (i < (map->width + 1) * map->height) + { + count += (map->data[i] == v); + i++; + } + return (count); +} diff --git a/map_draw.c b/map_draw.c new file mode 100644 index 0000000..3c43203 --- /dev/null +++ b/map_draw.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_draw.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 19:43:45 by kdx #+# #+# */ +/* Updated: 2022/10/12 07:09:49 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" +#include "sily.h" +#include "game.h" + +static char map_get_grid(t_map *map, int x, int y) +{ + return (map_get(map, x * TSIZE, y * TSIZE)); +} + +static char wall_spr(t_map *map, int x, int y) +{ + char spr; + + spr = 0; + spr |= (x <= 0 || map_get_grid(map, x - 1, y) == TILE_WALL) + << 0; + spr |= (y <= 0 || map_get_grid(map, x, y - 1) == TILE_WALL) + << 1; + spr |= (x + 1 >= (int)map->width || map_get_grid(map, x + 1, y) + == TILE_WALL) + << 2; + spr |= (y + 1 >= (int)map->height || map_get_grid(map, x, y + 1) + == TILE_WALL) + << 3; + return (spr + 'a'); +} + +void map_draw_tile(t_sily *sily, t_map *map, int x, int y) +{ + unsigned char tile; + + x /= TSIZE; + y /= TSIZE; + tile = map_get_grid(map, x, y); + if (tile == TILE_WALL) + tile = wall_spr(map, x, y); + sprite_draw(sily, sily->game->s_tiles[tile], x * TSIZE, y * TSIZE); +} + +void map_draw(t_sily *sily, t_map *map) +{ + size_t x; + size_t y; + unsigned char tile; + + y = -1; + while (++y < map->height) + { + x = -1; + while (++x < map->width) + { + tile = map_get_grid(map, x, y); + if (tile == TILE_WALL) + tile = wall_spr(map, x, y); + sprite_draw(sily, sily->game->s_tiles[tile], x * TSIZE, y * TSIZE); + } + } +} diff --git a/map_get.c b/map_get.c new file mode 100644 index 0000000..30337d1 --- /dev/null +++ b/map_get.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 21:28:49 by kdx #+# #+# */ +/* Updated: 2022/10/11 00:26:11 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" + +unsigned char map_get(t_map *map, int x, int y) +{ + return (map->data[(x / TSIZE) + (y / TSIZE) * (map->width + 1)]); +} + +void map_set(t_map *map, int x, int y, unsigned char v) +{ + map->data[(x / TSIZE) + (y / TSIZE) * (map->width + 1)] = v; +} diff --git a/map_pathfind.c b/map_pathfind.c new file mode 100644 index 0000000..c1b0eb8 --- /dev/null +++ b/map_pathfind.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_pathfind.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/15 23:59:55 by kdx #+# #+# */ +/* Updated: 2022/10/16 00:45:47 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" + +static t_err map_fill(t_map *map, int x, int y) +{ + unsigned char tile; + t_err rv; + + if (x < 0 || y < 0 || (size_t)x >= map->width || (size_t)y >= map->height + || (map_get(map, x * TSIZE, y * TSIZE) & 128) != 0) + return (0); + tile = map_get(map, x * TSIZE, y * TSIZE); + map_set(map, x * TSIZE, y * TSIZE, tile | 128); + if (tile == TILE_WALL) + return (0); + if (tile == TILE_EXIT || tile == TILE_COLLECTIBLE) + rv = 1; + else + rv = 0; + return (rv + map_fill(map, x - 1, y) + map_fill(map, x + 1, y) + + map_fill(map, x, y - 1) + map_fill(map, x, y + 1)); +} + +static void find_player(t_map *map, int *x, int *y) +{ + *y = 0; + while ((size_t)(*y) < map->height) + { + *x = 0; + while ((size_t)(*x) < map->width) + { + if (map_get(map, *x * TSIZE, *y * TSIZE) == TILE_PLAYER) + return ; + *x += 1; + } + if (map_get(map, *x * TSIZE, *y * TSIZE) == TILE_PLAYER) + return ; + *y += 1; + } +} + +static t_err expected_result(t_map *map) +{ + size_t i; + t_err expected; + + i = 0; + expected = 0; + while (map->data[i] != '\0') + { + if (map->data[i] == TILE_EXIT + || map->data[i] == TILE_COLLECTIBLE) + expected += 1; + i += 1; + } + return (expected); +} + +t_err map_pathfind(t_map *map) +{ + int x; + int y; + t_err rv; + size_t i; + + find_player(map, &x, &y); + rv = map_fill(map, x, y); + i = 0; + while (map->data[i] != '\0') + { + map->data[i] &= 127; + i += 1; + } + return (rv == expected_result(map)); +} diff --git a/map_spawn.c b/map_spawn.c new file mode 100644 index 0000000..521eeef --- /dev/null +++ b/map_spawn.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_spawn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 21:42:00 by kdx #+# #+# */ +/* Updated: 2022/10/11 00:27:03 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" +#include "game.h" + +void map_spawn(t_game *game, t_map *map) +{ + size_t x; + size_t y; + int tile; + + y = -1; + while (++y < map->height) + { + x = -1; + while (++x < map->width) + { + tile = map_get(map, x * TSIZE, y * TSIZE); + if (tile == TILE_PLAYER) + { + game->player.pos[0] = x * TSIZE; + game->player.pos[1] = y * TSIZE; + map_set(map, x * TSIZE, y * TSIZE, '0'); + } + } + } +} diff --git a/map_verify.c b/map_verify.c new file mode 100644 index 0000000..a3ccc5a --- /dev/null +++ b/map_verify.c @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_verify.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 08:14:30 by kdx #+# #+# */ +/* Updated: 2022/10/16 00:14:35 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" + +static t_err only_chars(const unsigned char *data) +{ + int accepted[256]; + size_t i; + + i = 0; + while (i < 256) + accepted[i++] = 0; + accepted[TILE_NONE] = 1; + accepted[TILE_WALL] = 1; + accepted[TILE_COLLECTIBLE] = 1; + accepted[TILE_EXIT] = 1; + accepted[TILE_PLAYER] = 1; + accepted['\n'] = 1; + i = 0; + while (data[i] != '\0') + if (!accepted[data[i++]]) + return (1); + return (0); +} + +static t_err find_uniques(const unsigned char *data) +{ + int uniques[256]; + size_t i; + size_t to_be_found; + + i = 0; + while (i < 256) + uniques[i++] = 0; + uniques['P'] = 1; + uniques['E'] = 1; + uniques['C'] = 5; + to_be_found = 3; + i = 0; + while (data[i] != '\0') + { + if (uniques[data[i]] && uniques[data[i]] != 7) + { + if (uniques[data[i]] == 3) + return (ERR_DUPLICATE); + if ((uniques[data[i]] & 2) == 0) + to_be_found--; + uniques[data[i]] |= 2; + } + i++; + } + return (ERR_MISSING + (to_be_found == 0) * (ERR_NONE - ERR_MISSING)); +} + +static t_err sensible_dimensions(t_map *map, const unsigned char *data) +{ + size_t prev_newline; + + if (ft_strchr((const char *)data, '\n') == NULL) + return (1); + prev_newline = 0; + map->width = 0; + map->height = 0; + while (*data != '\0') + { + if (*data++ == '\n') + { + if (map->width == 0) + map->width = prev_newline; + if (prev_newline == 0 || map->width != prev_newline) + return (1); + prev_newline = 0; + map->height++; + } + else + prev_newline++; + } + if (prev_newline != 0 || map->width == 0 || map->height == 0) + return (1); + return (0); +} + +static t_err valid_borders(t_map *map) +{ + size_t x; + size_t y; + + x = 0; + while (x < map->width) + { + if (map->data[x] != TILE_WALL + || map_get(map, x * TSIZE, (map->height - 1) * TSIZE) != TILE_WALL) + return (ERR_OPEN_BORDER); + x += 1; + } + y = 0; + while (y < map->height) + { + if (map_get(map, 0, y * TSIZE) != TILE_WALL + || map_get(map, (map->width - 1) * TSIZE, y * TSIZE) != TILE_WALL) + return (ERR_OPEN_BORDER); + y += 1; + } + return (ERR_NONE); +} + +t_err map_verify(t_map *map, const unsigned char *data) +{ + if (only_chars(data)) + return (ERR_INVALID_CHAR); + if (find_uniques(data)) + return (find_uniques(data)); + if (sensible_dimensions(map, data)) + return (ERR_WRONG_DIMENSION); + if (valid_borders(map)) + return (ERR_OPEN_BORDER); + if (!map_pathfind(map)) + return (ERR_NO_PATH); + return (ERR_NONE); +} diff --git a/masterrace.ber b/masterrace.ber new file mode 100644 index 0000000..6c5c75d --- /dev/null +++ b/masterrace.ber @@ -0,0 +1,20 @@ +11111111111111111111 +10000000111100000001 +1000000001100C000001 +1000000000000C000001 +100000C001100C000C01 +100000C0101000000C01 +100000C110100000C001 +100000010000000CC001 +10000000000000CC0001 +100CC0000000CC0C0001 +1000CCC0CCCC000C0001 +100000CCC00C00CC0001 +11110000C000C0C00001 +11111000CC000C000001 +111110000CCCC0000001 +11111000000000000001 +11110000000000000001 +10000000000000000001 +1000000P0000000E0001 +11111111111111111111 diff --git a/minilibx.tgz b/minilibx.tgz Binary files differnew file mode 100644 index 0000000..cb7c5a4 --- /dev/null +++ b/minilibx.tgz diff --git a/miscellany.h b/miscellany.h new file mode 100644 index 0000000..cd5c5ae --- /dev/null +++ b/miscellany.h @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* miscellany.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 04:18:22 by kdx #+# #+# */ +/* Updated: 2022/10/09 04:19:22 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MISCELLANY_H +# define MISCELLANY_H +# define XK_MISCELLANY +# define XK_LATIN1 +#endif diff --git a/player.c b/player.c new file mode 100644 index 0000000..68f77ff --- /dev/null +++ b/player.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 08:29:41 by kdx #+# #+# */ +/* Updated: 2022/10/14 14:36:25 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "player.h" +#include "sprite.h" +#include "game.h" +#include "map.h" +#include <math.h> +#include <stdbool.h> + +static void player_move(t_player *player, t_map *map) +{ + int i; + int sign; + + if (player_collide(map, player->pos[0], player->pos[1], TILE_WALL)) + return ; + i = -1; + while (++i < 2) + { + sign = (player->vel[i] > 0) - (player->vel[i] < 0); + player->pos[i] += player->vel[i]; + while (player_collide(map, player->pos[0], player->pos[1], TILE_WALL)) + { + player->vel[i] = 0.0; + player->pos[i] = round(player->pos[i]) - sign; + } + } +} + +void player_init(t_player *player, double x, double y) +{ + player->pos[0] = x; + player->pos[1] = y; + player->vel[0] = 0.0; + player->vel[1] = 0.0; +} + +static void player_update_end(t_sily *sily, t_player *player) +{ + player_move(player, &sily->game->map); + player_collect(sily->game, player, &sily->game->map); + player_collide_exit(sily, player, &sily->game->map); + if (player_collide_badeline(player, &sily->game->badeline)) + { + ft_putendl_fd("oof", 1); + sily->quit = true; + } +} + +void player_update(t_sily *sily, t_player *player) +{ + double dir[2]; + int i; + + dir[0] = sily->input[K_RIGHT] - sily->input[K_LEFT]; + dir[1] = sily->input[K_DOWN] - sily->input[K_UP]; + player->invincible -= (player->invincible > 0); + if ((dir[0] || dir[1]) && !sily->game->badeline.active) + { + sily->game->badeline.active = true; + player->invincible = PLAYER_INVINCIBLE; + } + i = 0; + while (i < 2) + { + player->old_pos[i] = player->pos[i]; + if (dir[0] && dir[1]) + dir[i] /= 1.4142135623730951; + i++; + } + player->vel[0] *= 1.0 - PLAYER_FRICTION; + player->vel[0] += PLAYER_ACCELERATION * dir[0]; + player->vel[1] += PLAYER_THRUSTER * dir[1]; + player->vel[1] += PLAYER_GRAVITY; + player_update_end(sily, player); +} + +void player_draw(t_sily *sily, t_player *player) +{ + sprite_draw(sily, sily->game->s_player, round(player->pos[0]), + round(player->pos[1])); +} diff --git a/player.h b/player.h new file mode 100644 index 0000000..df32bc6 --- /dev/null +++ b/player.h @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 08:29:41 by kdx #+# #+# */ +/* Updated: 2022/10/14 14:36:55 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PLAYER_H +# define PLAYER_H +# include "sily.h" +# include "map.h" +# include "badeline.h" +# define PLAYER_FRICTION 0.02 +# define PLAYER_ACCELERATION 0.4 +# define PLAYER_THRUSTER 0.6 +# define PLAYER_GRAVITY 0.2 +# define PLAYER_INVINCIBLE 60 + +typedef struct s_player { + int invincible; + double old_pos[2]; + double pos[2]; + double vel[2]; +} t_player; + +typedef struct s_game t_game; + +void player_init(t_player *player, double x, double y); +void player_update(t_sily *sily, t_player *player); +void player_erase(t_sily *sily, double pos[2]); +void player_draw(t_sily *sily, t_player *player); +int player_collide(t_map *map, double x, double y, int v); +void player_collect(t_game *game, t_player *player, t_map *map); +void player_collide_exit(t_sily *sily, t_player *player, t_map *map); +bool player_collide_badeline(t_player *player, t_badeline *bdl); + +#endif diff --git a/player.xpm b/player.xpm new file mode 100644 index 0000000..8786c7a --- /dev/null +++ b/player.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * player_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #A3BDE9", +" ...... ...... ", +".. ...... ..", +". ........ .", +". .......... .", +". ............ .", +"....... .......", +"...... ......", +" .... . .... ", +" .... .. .... ", +"...... ......", +"....... .......", +". ............ .", +". .......... .", +". ........ .", +".. ...... ..", +" ...... ...... "}; diff --git a/player_collect.c b/player_collect.c new file mode 100644 index 0000000..7ba9df2 --- /dev/null +++ b/player_collect.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player_collect.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 02:51:33 by kdx #+# #+# */ +/* Updated: 2022/10/14 14:37:15 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "player.h" +#include "map.h" +#include "game.h" +#include <math.h> + +void player_collect(t_game *game, t_player *player, t_map *map) +{ + int pos[2]; + + pos[0] = round(player->pos[0]) + 8; + pos[1] = round(player->pos[1]) + 8; + if (map_get(map, pos[0], pos[1]) == TILE_COLLECTIBLE) + { + map_set(map, pos[0], pos[1], TILE_NONE); + game->collectibles -= 1; + } +} diff --git a/player_collide.c b/player_collide.c new file mode 100644 index 0000000..f94046e --- /dev/null +++ b/player_collide.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player_collide.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/11 00:32:12 by kdx #+# #+# */ +/* Updated: 2022/10/11 00:34:58 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "player.h" +#include "map.h" +#include <math.h> + +int player_collide(t_map *map, double x, double y, int v) +{ + int tl[2]; + int br[2]; + + tl[0] = round(x) + 1; + tl[1] = round(y) + 1; + br[0] = tl[0] + 13; + br[1] = tl[1] + 13; + return (map_get(map, tl[0], tl[1]) == v + || map_get(map, br[0], tl[1]) == v + || map_get(map, tl[0], br[1]) == v + || map_get(map, br[0], br[1]) == v); +} diff --git a/player_collide_badeline.c b/player_collide_badeline.c new file mode 100644 index 0000000..10033a5 --- /dev/null +++ b/player_collide_badeline.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player_collide_badeline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/17 11:32:49 by kdx #+# #+# */ +/* Updated: 2022/10/17 13:36:50 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "player.h" +#include "badeline.h" +#include <stdbool.h> +#include <math.h> + +bool player_collide_badeline(t_player *player, t_badeline *bdl) +{ + double diff; + + if (!bdl->active || player->invincible > 0) + return (false); + diff = fabs(player->pos[0] - bdl->queue[0][0]); + diff += fabs(player->pos[1] - bdl->queue[0][1]); + return (diff < 16.0); +} diff --git a/player_collide_exit.c b/player_collide_exit.c new file mode 100644 index 0000000..5ba0cb3 --- /dev/null +++ b/player_collide_exit.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player_collide_exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 14:28:38 by kdx #+# #+# */ +/* Updated: 2022/10/16 01:24:41 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "player.h" +#include "sily.h" +#include "game.h" +#include "map.h" + +void player_collide_exit(t_sily *sily, t_player *player, t_map *map) +{ + if (sily->game->collectibles == 0 && player_collide(map, player->pos[0], + player->pos[1], TILE_EXIT)) + sily->game->victory = 1; +} diff --git a/player_erase.c b/player_erase.c new file mode 100644 index 0000000..bc3a5e1 --- /dev/null +++ b/player_erase.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player_erase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/17 11:32:56 by kdx #+# #+# */ +/* Updated: 2022/10/17 11:32:58 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "player.h" +#include "game.h" +#include "map.h" +#include "sily.h" +#include <math.h> + +void player_erase(t_sily *sily, double pos[2]) +{ + int x; + int y; + + x = round(pos[0]); + y = round(pos[1]); + map_draw_tile(sily, &sily->game->map, x, y); + map_draw_tile(sily, &sily->game->map, x + TSIZE, y + TSIZE); + map_draw_tile(sily, &sily->game->map, x + TSIZE, y); + map_draw_tile(sily, &sily->game->map, x, y + TSIZE); +} diff --git a/readall.c b/readall.c new file mode 100644 index 0000000..3dd976f --- /dev/null +++ b/readall.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* readall.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 07:51:08 by kdx #+# #+# */ +/* Updated: 2022/10/10 08:07:42 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include "readall.h" +#include <unistd.h> + +/* always free src */ +static char *realloc_buf(char *src, size_t size) +{ + char *cpy; + size_t i; + + cpy = ft_calloc((size + 1) * READALL_BSIZE, sizeof(char)); + if (cpy == NULL) + return (ft_free(src)); + i = -1; + while (++i < size * READALL_BSIZE) + cpy[i] = src[i]; + ft_free(src); + return (cpy); +} + +char *readall(int fd) +{ + char *buf; + int read_rv; + size_t read_cnt; + + buf = realloc_buf(NULL, 0); + if (buf == NULL) + return (NULL); + read_cnt = 0; + read_rv = 1; + while (read_rv && buf != NULL) + { + read_rv = read(fd, buf + read_cnt * READALL_BSIZE, READALL_BSIZE); + read_cnt++; + if (read_rv < 0) + return (ft_free(buf)); + else + buf = realloc_buf(buf, read_cnt); + if (read_rv != READALL_BSIZE) + return (buf); + } + return (buf); +} diff --git a/readall.h b/readall.h new file mode 100644 index 0000000..81b701f --- /dev/null +++ b/readall.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* readall.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 07:50:18 by kdx #+# #+# */ +/* Updated: 2022/10/10 08:01:33 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef READALL_H +# define READALL_H +# define READALL_BSIZE 1024 + +/* readall NEVER closes fd */ +char *readall(int fd); + +#endif @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/07 04:15:41 by kdx #+# #+# */ +/* Updated: 2022/10/16 01:26:55 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "sily.h" +#include "game.h" +#include "minilibx/mlx.h" +#include <X11/X.h> +#include <stdlib.h> + +static int sily_hook_destroy(t_sily *sily) +{ + game_destroy(sily->game); + exit(0); + return (0); +} + +t_err sily_init(t_sily *sily, int width, int height, char *title) +{ + ft_bzero(sily, sizeof(*sily)); + if (width <= 0) + return (1 | ft_dprintf(2, "width is < 0\n")); + if (height <= 0) + return (1 | ft_dprintf(2, "height is < 0\n")); + if (title == NULL) + return (1 | ft_dprintf(2, "title is NULL\n")); + sily->width = width; + sily->height = height; + sily->ctx = mlx_init(); + if (sily->ctx == NULL) + return (1 | ft_dprintf(2, "mlx_init failed\n")); + sily->window = mlx_new_window(sily->ctx, width, height, title); + if (sily->window == NULL) + return (1 | ft_dprintf(2, "mlx_new_window failed\n")); + mlx_expose_hook(sily->window, sily_expose_hook, sily); + mlx_key_hook(sily->window, sily_input_release_ev, sily); + mlx_hook(sily->window, KeyPress, KeyPressMask, sily_input_press_ev, + sily); + mlx_hook(sily->window, DestroyNotify, StructureNotifyMask, + sily_hook_destroy, sily); + mlx_do_key_autorepeatoff(sily->ctx); + sily->next_time = sily_get_time(); + return (0); +} + +void sily_deinit(t_sily *sily) +{ + if (sily->ctx != NULL) + mlx_loop_end(sily->ctx); + if (sily->window != NULL) + mlx_destroy_window(sily->ctx, sily->window); + if (sily->ctx != NULL) + mlx_destroy_display(sily->ctx); + free(sily->ctx); + ft_bzero(sily, sizeof(*sily)); +} + +static int sily_update(t_sily *sily) +{ + t_u64 cur_time; + + if (sily->quit) + game_destroy(sily->game); + else + { + sily->update(sily); + sily->next_time += MIN_DT; + sily->draw(sily); + cur_time = sily_get_time(); + if (sily->next_time <= cur_time) + sily->next_time = cur_time; + else + sily_sleep(sily->next_time - cur_time); + } + sily->tick++; + return (0); +} + +void sily_loop(t_sily *sily, void (*u)(t_sily *), void (*d)(t_sily *)) +{ + mlx_loop_hook(sily->ctx, sily_update, sily); + sily->update = u; + sily->draw = d; + mlx_loop(sily->ctx); +} @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/07 04:32:07 by kdx #+# #+# */ +/* Updated: 2022/10/14 03:26:49 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* 102761446 */ + +#ifndef SILY_H +# define SILY_H +# include "minilibx/mlx.h" +# include "libft/libft.h" +# include <stdbool.h> +# define MIN_DT 33 + +typedef struct s_game t_game; +typedef struct s_sily t_sily; +struct s_sily +{ + void *ctx; + void *window; + int width; + int height; + bool quit; + void (*update)(t_sily *); + void (*draw)(t_sily *); + t_u64 tick; + t_u64 next_time; + t_u64 prev_move_events; + t_u64 move_events; + int input[4]; + int input_queue[4]; + t_game *game; +}; + +t_err sily_init(t_sily *s, int width, int height, char *title); +void sily_deinit(t_sily *s); +void sily_loop(t_sily *s, void (*update)(t_sily *), void (*draw)(t_sily *)); + +t_u64 sily_get_time(void); +void sily_sleep(t_u64 millis); + +enum { + K_LEFT = 0, + K_RIGHT = 1, + K_DOWN = 2, + K_UP = 3, +}; + +void sily_input_update(t_sily *sily); +int sily_input_press_ev(int key, t_sily *sily); +int sily_input_release_ev(int key, t_sily *sily); +void sily_input_draw(t_sily *sily); +void sily_clear(t_sily *sily); +int sily_expose_hook(t_sily *sily); +void sily_erase_text(t_sily *silly, char *str, int x, int y); +void sily_draw_text(t_sily *sily, char *str, int x, int y); + +#endif diff --git a/sily_clear.c b/sily_clear.c new file mode 100644 index 0000000..d2c72d8 --- /dev/null +++ b/sily_clear.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 07:45:27 by kdx #+# #+# */ +/* Updated: 2022/10/09 07:46:09 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "sily.h" +#include "minilibx/mlx.h" + +void sily_clear(t_sily *sily) +{ + mlx_clear_window(sily->ctx, sily->window); +} diff --git a/sily_draw_text.c b/sily_draw_text.c new file mode 100644 index 0000000..de4516a --- /dev/null +++ b/sily_draw_text.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily_draw_text.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 03:11:27 by kdx #+# #+# */ +/* Updated: 2022/10/14 03:26:27 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "sily.h" +#include "minilibx/mlx.h" + +void sily_erase_text(t_sily *sily, char *text, int x, int y) +{ + mlx_string_put(sily->ctx, sily->window, x, y, 0x004f397b, text); +} + +void sily_draw_text(t_sily *sily, char *text, int x, int y) +{ + mlx_string_put(sily->ctx, sily->window, x, y, 0x00ffffff, text); +} diff --git a/sily_expose.c b/sily_expose.c new file mode 100644 index 0000000..32fd314 --- /dev/null +++ b/sily_expose.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily_expose.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/13 22:21:19 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:21:25 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include "sily.h" +#include "game.h" + +int sily_expose_hook(t_sily *sily) +{ + sily->game->redraw = 1; + return (0); +} diff --git a/sily_input.c b/sily_input.c new file mode 100644 index 0000000..1afaa32 --- /dev/null +++ b/sily_input.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily_input.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/07 07:32:30 by kdx #+# #+# */ +/* Updated: 2022/11/02 17:29:25 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include "sily.h" +#include "miscellany.h" +#include "game.h" +#include <X11/keysymdef.h> + +static void press_key(t_sily *sily, int k) +{ + if (sily->input_queue[k] == 0) + { + sily->input_queue[k] = 1; + sily->move_events += 1; + ft_printf("moves: %u\n", (unsigned)sily->move_events); + } +} + +void sily_input_update(t_sily *sily) +{ + int i; + + i = 0; + while (i < 4) + { + sily->input[i] = sily->input_queue[i]; + i++; + } +} + +int sily_input_press_ev(int key, t_sily *sily) +{ + if (key == XK_Escape) + sily->quit = 1; + else if (key == 119 || key == 107 || key == XK_Up) + press_key(sily, K_UP); + else if (key == 97 || key == 104 || key == XK_Left) + press_key(sily, K_LEFT); + else if (key == 115 || key == 106 || key == XK_Down) + press_key(sily, K_DOWN); + else if (key == 100 || key == 108 || key == XK_Right) + press_key(sily, K_RIGHT); + return (0); +} + +int sily_input_release_ev(int key, t_sily *sily) +{ + if (key == 119 || key == 107 || key == XK_Up) + sily->input_queue[K_UP] = 0; + else if (key == 97 || key == 104 || key == XK_Left) + sily->input_queue[K_LEFT] = 0; + else if (key == 115 || key == 106 || key == XK_Down) + sily->input_queue[K_DOWN] = 0; + else if (key == 100 || key == 108 || key == XK_Right) + sily->input_queue[K_RIGHT] = 0; + return (0); +} + +void sily_input_draw(t_sily *sily) +{ + char *str; + + str = ft_itoa(sily->prev_move_events); + if (str == NULL) + return ; + sily_erase_text(sily, str, 2, 10); + free(str); + str = ft_itoa(sily->move_events); + if (str == NULL) + return ; + sily_draw_text(sily, str, 2, 10); + free(str); + sily->prev_move_events = sily->move_events; +} diff --git a/sily_time.c b/sily_time.c new file mode 100644 index 0000000..c4ac6e5 --- /dev/null +++ b/sily_time.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily_time.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/07 07:30:41 by kdx #+# #+# */ +/* Updated: 2022/10/07 07:30:42 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include "sily.h" + +t_u64 sily_get_time(void) +{ + return (0); +} + +void sily_sleep(t_u64 millis) +{ + size_t i; + + (void)millis; + i = 0; + while (i < 4000000) + { + ft_printf(""); + i++; + } +} diff --git a/sily_time_bonus.c b/sily_time_bonus.c new file mode 100644 index 0000000..ca1c859 --- /dev/null +++ b/sily_time_bonus.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily_time_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/17 11:33:01 by kdx #+# #+# */ +/* Updated: 2022/10/17 11:33:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "sily.h" +#include <sys/time.h> + +t_u64 sily_get_time(void) +{ + struct timeval te; + + gettimeofday(&te, NULL); + return (te.tv_sec * 1000LL + te.tv_usec / 1000); +} + +void sily_sleep(t_u64 millis) +{ + t_u64 start; + + start = sily_get_time(); + while (sily_get_time() - start < millis) + ; +} diff --git a/small.ber b/small.ber new file mode 100644 index 0000000..a9df096 --- /dev/null +++ b/small.ber @@ -0,0 +1,4 @@ +1111 +1CE1 +1PC1 +1111 diff --git a/sprite.c b/sprite.c new file mode 100644 index 0000000..09c9815 --- /dev/null +++ b/sprite.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sprite.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 07:15:24 by kdx #+# #+# */ +/* Updated: 2022/10/10 19:18:58 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include "minilibx/mlx.h" +#include "sprite.h" +#include "sily.h" + +t_sprite *sprite_load(t_sily *sily, char *path, int width, int height) +{ + t_sprite *sprite; + + sprite = ft_calloc(1, sizeof(t_sprite)); + if (sprite == NULL) + return (NULL); + sprite->width = width; + sprite->height = height; + sprite->img = mlx_xpm_file_to_image(sily->ctx, path, &sprite->width, + &sprite->height); + if (sprite->img == NULL) + return (ft_free(sprite)); + return (sprite); +} + +void sprite_destroy(t_sily *sily, t_sprite *sprite) +{ + if (sprite != NULL) + { + if (sprite->img != NULL) + mlx_destroy_image(sily->ctx, sprite->img); + ft_free(sprite); + } +} + +void sprite_draw(t_sily *sily, t_sprite *sprite, int x, int y) +{ + if (sprite == NULL) + { + ft_putendl_fd("error: sprite is NULL", 2); + return ; + } + mlx_put_image_to_window(sily->ctx, sily->window, sprite->img, x, y); +} diff --git a/sprite.h b/sprite.h new file mode 100644 index 0000000..7ad821d --- /dev/null +++ b/sprite.h @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sprite.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 07:15:24 by kdx #+# #+# */ +/* Updated: 2022/10/09 07:47:13 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SPRITE_H +# define SPRITE_H +# include "sily.h" + +typedef struct s_sprite +{ + void *img; + int width; + int height; +} t_sprite; + +t_sprite *sprite_load(t_sily *sily, char *path, int width, int height); +void sprite_destroy(t_sily *sily, t_sprite *sprite); +void sprite_draw(t_sily *sily, t_sprite *sprite, int x, int y); + +#endif diff --git a/spritesheet.c b/spritesheet.c new file mode 100644 index 0000000..dcf4780 --- /dev/null +++ b/spritesheet.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* spritesheet.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/12 09:43:47 by kdx #+# #+# */ +/* Updated: 2022/10/12 10:25:23 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include "spritesheet.h" +#include "sprite.h" +#include "map.h" + +static t_sprite *load_sprite(t_sily *sily, char *path) +{ + return (sprite_load(sily, path, TSIZE, TSIZE)); +} + +t_spritesheet *spritesheet_load(t_sily *sily, const char *pattern, + size_t frames) +{ + t_spritesheet *sheet; + size_t i; + char *path; + char *path_x; + + if (frames > 10 || ft_strchr(pattern, 'X') == NULL) + return (NULL); + path = ft_strdup(pattern); + if (path == NULL) + return (NULL); + path_x = ft_strchr(path, 'X'); + if (ft_alloc(&sheet, sizeof(t_spritesheet))) + return (ft_free(path)); + i = -1; + while (++i < frames) + { + *path_x = '0' + i; + sheet->frame[sheet->frame_count] = load_sprite(sily, path); + if (sheet->frame[sheet->frame_count] == NULL) + ft_free(path); + if (sheet->frame[sheet->frame_count++] == NULL) + return (spritesheet_destroy(sily, sheet)); + } + ft_free(path); + return (sheet); +} + +void *spritesheet_destroy(t_sily *sily, t_spritesheet *sheet) +{ + if (sheet != NULL) + { + while (sheet->frame_count-- > 0) + sprite_destroy(sily, sheet->frame[sheet->frame_count]); + ft_free(sheet); + } + return (NULL); +} diff --git a/spritesheet.h b/spritesheet.h new file mode 100644 index 0000000..21d771d --- /dev/null +++ b/spritesheet.h @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* spritesheet.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/12 09:43:52 by kdx #+# #+# */ +/* Updated: 2022/10/12 09:56:59 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SPRITESHEET_H +# define SPRITESHEET_H +# include "sprite.h" +# include "sily.h" + +typedef struct s_spritesheet +{ + t_sprite *frame[10]; + size_t frame_count; +} t_spritesheet; + +t_spritesheet *spritesheet_load(t_sily *sily, const char *pattern, + size_t frames); +void *spritesheet_destroy(t_sily *sily, t_spritesheet *sheet); + +#endif diff --git a/tiles/0.xpm b/tiles/0.xpm new file mode 100644 index 0000000..3ac1b76 --- /dev/null +++ b/tiles/0.xpm @@ -0,0 +1,20 @@ +/* XPM */ +static char * 0_xpm[] = { +"16 16 1 1", +" c #4F397B", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" "}; diff --git a/tiles/1.xpm b/tiles/1.xpm new file mode 100644 index 0000000..55bcfe8 --- /dev/null +++ b/tiles/1.xpm @@ -0,0 +1,20 @@ +/* XPM */ +static char * 1_xpm[] = { +"16 16 1 1", +" c #00FFEF", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" "}; diff --git a/tiles/C.xpm b/tiles/C.xpm new file mode 100644 index 0000000..57817e8 --- /dev/null +++ b/tiles/C.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * C_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #FFFF00", +" ", +" ", +" ", +" ", +" ........ ", +" . . ", +" . .. . ", +" . .. . ", +" . . ", +" . . ", +" . . ", +" ........ ", +" ", +" ", +" ", +" "}; diff --git a/tiles/E.xpm b/tiles/E.xpm new file mode 100644 index 0000000..cc6a38f --- /dev/null +++ b/tiles/E.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * E_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #FFFFFF", +" ", +" .. ", +" .... ", +" ...... ", +" ........ ", +" .... .... ", +" .... .... ", +" .... .... ", +" .... .... ", +" .... .... ", +" .... .... ", +" ........ ", +" ...... ", +" .... ", +" .. ", +" "}; diff --git a/tiles/a.xpm b/tiles/a.xpm new file mode 100644 index 0000000..446da1b --- /dev/null +++ b/tiles/a.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * a_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #00FFEF", +" ............ ", +" .............. ", +".... ....", +"... ...", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +"... ...", +".... ....", +" .............. ", +" ............ "}; diff --git a/tiles/b.xpm b/tiles/b.xpm new file mode 100644 index 0000000..9aa1515 --- /dev/null +++ b/tiles/b.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * b_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ..", +" .", +"............ ", +"............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +"............. ", +"............ ", +" .", +" .."}; diff --git a/tiles/c.xpm b/tiles/c.xpm new file mode 100644 index 0000000..0e1947b --- /dev/null +++ b/tiles/c.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * c_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" .......... ", +" ........ ", +". .", +".. .."}; diff --git a/tiles/d.xpm b/tiles/d.xpm new file mode 100644 index 0000000..ae191d6 --- /dev/null +++ b/tiles/d.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * d_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #00FFEF", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ...", +" ....", +"............... ", +".............. "}; diff --git a/tiles/e.xpm b/tiles/e.xpm new file mode 100644 index 0000000..8d8fdfc --- /dev/null +++ b/tiles/e.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * e_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #00FFEF", +" ..............", +" ...............", +".... ", +"... ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +"... ", +".... ", +" ...............", +" .............."}; diff --git a/tiles/f.xpm b/tiles/f.xpm new file mode 100644 index 0000000..7dcdb8b --- /dev/null +++ b/tiles/f.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * f_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ", +" ", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +" ", +" "}; diff --git a/tiles/g.xpm b/tiles/g.xpm new file mode 100644 index 0000000..5a0620a --- /dev/null +++ b/tiles/g.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * g_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" .............", +" ............", +". ", +".. "}; diff --git a/tiles/h.xpm b/tiles/h.xpm new file mode 100644 index 0000000..afff907 --- /dev/null +++ b/tiles/h.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * h_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #00FFEF", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +"................", +"................"}; diff --git a/tiles/i.xpm b/tiles/i.xpm new file mode 100644 index 0000000..aba884b --- /dev/null +++ b/tiles/i.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * i_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #00FFEF", +" ............ ", +" .............. ", +".... ....", +"... ...", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. ..", +".. .."}; diff --git a/tiles/j.xpm b/tiles/j.xpm new file mode 100644 index 0000000..f926add --- /dev/null +++ b/tiles/j.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * j_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ..", +" .", +"............ ", +"............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. ", +".............. "}; diff --git a/tiles/k.xpm b/tiles/k.xpm new file mode 100644 index 0000000..5b694d9 --- /dev/null +++ b/tiles/k.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * k_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ ", +" ............ "}; diff --git a/tiles/l.xpm b/tiles/l.xpm new file mode 100644 index 0000000..1ab3901 --- /dev/null +++ b/tiles/l.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * l_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #00FFEF", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" ..", +" .."}; diff --git a/tiles/m.xpm b/tiles/m.xpm new file mode 100644 index 0000000..f3dcc75 --- /dev/null +++ b/tiles/m.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * m_xpm[] = { +"16 16 2 1", +" c #4F397B", +". c #00FFEF", +" ..............", +" ...............", +".... ", +"... ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. ", +".. "}; diff --git a/tiles/n.xpm b/tiles/n.xpm new file mode 100644 index 0000000..a805048 --- /dev/null +++ b/tiles/n.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * n_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ", +" ", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................", +"................"}; diff --git a/tiles/o.xpm b/tiles/o.xpm new file mode 100644 index 0000000..86186b7 --- /dev/null +++ b/tiles/o.xpm @@ -0,0 +1,21 @@ +/* XPM */ +static char * o_xpm[] = { +"16 16 2 1", +" c #00FFEF", +". c #4F397B", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" ..............", +" .............."}; diff --git a/tiles/p.xpm b/tiles/p.xpm new file mode 100644 index 0000000..846f59a --- /dev/null +++ b/tiles/p.xpm @@ -0,0 +1,20 @@ +/* XPM */ +static char * p_xpm[] = { +"16 16 1 1", +" c #4F397B", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" "}; |