From e89fa8741d349f69e6c46b381059332b00721932 Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 13 Jan 2023 15:14:47 +0100 Subject: school project very shrug-tier --- .gitignore | 5 ++ Makefile | 62 ++++++++++++++++++++++ badeline.c | 57 ++++++++++++++++++++ badeline.h | 33 ++++++++++++ badeline.xpm | 21 ++++++++ big.ber | 64 +++++++++++++++++++++++ exit/0.xpm | 21 ++++++++ exit/1.xpm | 21 ++++++++ exit/2.xpm | 21 ++++++++ game.c | 125 ++++++++++++++++++++++++++++++++++++++++++++ game.h | 43 +++++++++++++++ game_draw.c | 38 ++++++++++++++ libft/.gitignore | 4 ++ libft/Makefile | 49 +++++++++++++++++ libft/ft_alloc.c | 19 +++++++ libft/ft_atoi.c | 42 +++++++++++++++ libft/ft_bzero.c | 18 +++++++ libft/ft_calloc.c | 27 ++++++++++ libft/ft_dprintf.c | 24 +++++++++ libft/ft_free.c | 20 +++++++ libft/ft_isalnum.c | 18 +++++++ libft/ft_isalpha.c | 18 +++++++ libft/ft_isascii.c | 18 +++++++ libft/ft_isdigit.c | 18 +++++++ libft/ft_isprint.c | 18 +++++++ libft/ft_itoa.c | 59 +++++++++++++++++++++ libft/ft_lstadd_back.c | 26 ++++++++++ libft/ft_lstadd_front.c | 21 ++++++++ libft/ft_lstclear.c | 27 ++++++++++ libft/ft_lstdelone.c | 25 +++++++++ libft/ft_lstiter.c | 24 +++++++++ libft/ft_lstlast.c | 22 ++++++++ libft/ft_lstmap.c | 39 ++++++++++++++ libft/ft_lstnew.c | 25 +++++++++ libft/ft_lstsize.c | 26 ++++++++++ libft/ft_memchr.c | 31 +++++++++++ libft/ft_memcmp.c | 34 ++++++++++++ libft/ft_memcpy.c | 25 +++++++++ libft/ft_memmove.c | 33 ++++++++++++ libft/ft_memset.c | 28 ++++++++++ libft/ft_printf.c | 24 +++++++++ libft/ft_putchar_fd.c | 19 +++++++ libft/ft_putendl_fd.c | 19 +++++++ libft/ft_puti128_fd.c | 37 +++++++++++++ libft/ft_putnbr_fd.c | 39 ++++++++++++++ libft/ft_putstr_fd.c | 23 ++++++++ libft/ft_putu128x_fd.c | 34 ++++++++++++ libft/ft_split.c | 91 ++++++++++++++++++++++++++++++++ libft/ft_strchr.c | 24 +++++++++ libft/ft_strdup.c | 26 ++++++++++ libft/ft_striteri.c | 27 ++++++++++ libft/ft_strjoin.c | 40 ++++++++++++++ libft/ft_strlcat.c | 25 +++++++++ libft/ft_strlcpy.c | 28 ++++++++++ libft/ft_strlen.c | 23 ++++++++ libft/ft_strmapi.c | 32 ++++++++++++ libft/ft_strncmp.c | 29 +++++++++++ libft/ft_strnstr.c | 37 +++++++++++++ libft/ft_strrchr.c | 27 ++++++++++ libft/ft_strtrim.c | 33 ++++++++++++ libft/ft_substr.c | 38 ++++++++++++++ libft/ft_tolower.c | 20 +++++++ libft/ft_toupper.c | 20 +++++++ libft/ft_vdprintf.c | 67 ++++++++++++++++++++++++ libft/ft_vprintf.c | 18 +++++++ libft/libft.h | 93 +++++++++++++++++++++++++++++++++ main.c | 51 ++++++++++++++++++ map.ber | 16 ++++++ map.c | 96 ++++++++++++++++++++++++++++++++++ map.h | 64 +++++++++++++++++++++++ map_count.c | 28 ++++++++++ map_draw.c | 70 +++++++++++++++++++++++++ map_get.c | 23 ++++++++ map_pathfind.c | 86 ++++++++++++++++++++++++++++++ map_spawn.c | 37 +++++++++++++ map_verify.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++ masterrace.ber | 20 +++++++ minilibx.tgz | Bin 0 -> 267131 bytes miscellany.h | 17 ++++++ player.c | 92 ++++++++++++++++++++++++++++++++ player.h | 42 +++++++++++++++ player.xpm | 21 ++++++++ player_collect.c | 29 +++++++++++ player_collide.c | 30 +++++++++++ player_collide_badeline.c | 27 ++++++++++ player_collide_exit.c | 23 ++++++++ player_erase.c | 30 +++++++++++ readall.c | 56 ++++++++++++++++++++ readall.h | 20 +++++++ sily.c | 93 +++++++++++++++++++++++++++++++++ sily.h | 65 +++++++++++++++++++++++ sily_clear.c | 19 +++++++ sily_draw_text.c | 24 +++++++++ sily_expose.c | 21 ++++++++ sily_input.c | 84 ++++++++++++++++++++++++++++++ sily_time.c | 32 ++++++++++++ sily_time_bonus.c | 31 +++++++++++ small.ber | 4 ++ sprite.c | 52 +++++++++++++++++++ sprite.h | 28 ++++++++++ spritesheet.c | 62 ++++++++++++++++++++++ spritesheet.h | 28 ++++++++++ tiles/0.xpm | 20 +++++++ tiles/1.xpm | 20 +++++++ tiles/C.xpm | 21 ++++++++ tiles/E.xpm | 21 ++++++++ tiles/a.xpm | 21 ++++++++ tiles/b.xpm | 21 ++++++++ tiles/c.xpm | 21 ++++++++ tiles/d.xpm | 21 ++++++++ tiles/e.xpm | 21 ++++++++ tiles/f.xpm | 21 ++++++++ tiles/g.xpm | 21 ++++++++ tiles/h.xpm | 21 ++++++++ tiles/i.xpm | 21 ++++++++ tiles/j.xpm | 21 ++++++++ tiles/k.xpm | 21 ++++++++ tiles/l.xpm | 21 ++++++++ tiles/m.xpm | 21 ++++++++ tiles/n.xpm | 21 ++++++++ tiles/o.xpm | 21 ++++++++ tiles/p.xpm | 20 +++++++ 122 files changed, 4110 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 badeline.c create mode 100644 badeline.h create mode 100644 badeline.xpm create mode 100644 big.ber create mode 100644 exit/0.xpm create mode 100644 exit/1.xpm create mode 100644 exit/2.xpm create mode 100644 game.c create mode 100644 game.h create mode 100644 game_draw.c create mode 100644 libft/.gitignore create mode 100644 libft/Makefile create mode 100644 libft/ft_alloc.c create mode 100644 libft/ft_atoi.c create mode 100644 libft/ft_bzero.c create mode 100644 libft/ft_calloc.c create mode 100644 libft/ft_dprintf.c create mode 100644 libft/ft_free.c create mode 100644 libft/ft_isalnum.c create mode 100644 libft/ft_isalpha.c create mode 100644 libft/ft_isascii.c create mode 100644 libft/ft_isdigit.c create mode 100644 libft/ft_isprint.c create mode 100644 libft/ft_itoa.c create mode 100644 libft/ft_lstadd_back.c create mode 100644 libft/ft_lstadd_front.c create mode 100644 libft/ft_lstclear.c create mode 100644 libft/ft_lstdelone.c create mode 100644 libft/ft_lstiter.c create mode 100644 libft/ft_lstlast.c create mode 100644 libft/ft_lstmap.c create mode 100644 libft/ft_lstnew.c create mode 100644 libft/ft_lstsize.c create mode 100644 libft/ft_memchr.c create mode 100644 libft/ft_memcmp.c create mode 100644 libft/ft_memcpy.c create mode 100644 libft/ft_memmove.c create mode 100644 libft/ft_memset.c create mode 100644 libft/ft_printf.c create mode 100644 libft/ft_putchar_fd.c create mode 100644 libft/ft_putendl_fd.c create mode 100644 libft/ft_puti128_fd.c create mode 100644 libft/ft_putnbr_fd.c create mode 100644 libft/ft_putstr_fd.c create mode 100644 libft/ft_putu128x_fd.c create mode 100644 libft/ft_split.c create mode 100644 libft/ft_strchr.c create mode 100644 libft/ft_strdup.c create mode 100644 libft/ft_striteri.c create mode 100644 libft/ft_strjoin.c create mode 100644 libft/ft_strlcat.c create mode 100644 libft/ft_strlcpy.c create mode 100644 libft/ft_strlen.c create mode 100644 libft/ft_strmapi.c create mode 100644 libft/ft_strncmp.c create mode 100644 libft/ft_strnstr.c create mode 100644 libft/ft_strrchr.c create mode 100644 libft/ft_strtrim.c create mode 100644 libft/ft_substr.c create mode 100644 libft/ft_tolower.c create mode 100644 libft/ft_toupper.c create mode 100644 libft/ft_vdprintf.c create mode 100644 libft/ft_vprintf.c create mode 100644 libft/libft.h create mode 100644 main.c create mode 100644 map.ber create mode 100644 map.c create mode 100644 map.h create mode 100644 map_count.c create mode 100644 map_draw.c create mode 100644 map_get.c create mode 100644 map_pathfind.c create mode 100644 map_spawn.c create mode 100644 map_verify.c create mode 100644 masterrace.ber create mode 100644 minilibx.tgz create mode 100644 miscellany.h create mode 100644 player.c create mode 100644 player.h create mode 100644 player.xpm create mode 100644 player_collect.c create mode 100644 player_collide.c create mode 100644 player_collide_badeline.c create mode 100644 player_collide_exit.c create mode 100644 player_erase.c create mode 100644 readall.c create mode 100644 readall.h create mode 100644 sily.c create mode 100644 sily.h create mode 100644 sily_clear.c create mode 100644 sily_draw_text.c create mode 100644 sily_expose.c create mode 100644 sily_input.c create mode 100644 sily_time.c create mode 100644 sily_time_bonus.c create mode 100644 small.ber create mode 100644 sprite.c create mode 100644 sprite.h create mode 100644 spritesheet.c create mode 100644 spritesheet.h create mode 100644 tiles/0.xpm create mode 100644 tiles/1.xpm create mode 100644 tiles/C.xpm create mode 100644 tiles/E.xpm create mode 100644 tiles/a.xpm create mode 100644 tiles/b.xpm create mode 100644 tiles/c.xpm create mode 100644 tiles/d.xpm create mode 100644 tiles/e.xpm create mode 100644 tiles/f.xpm create mode 100644 tiles/g.xpm create mode 100644 tiles/h.xpm create mode 100644 tiles/i.xpm create mode 100644 tiles/j.xpm create mode 100644 tiles/k.xpm create mode 100644 tiles/l.xpm create mode 100644 tiles/m.xpm create mode 100644 tiles/n.xpm create mode 100644 tiles/o.xpm create mode 100644 tiles/p.xpm 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 + +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 +# 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", +" . .. .. . ", +".. . . ..", +" .. .. ", +" .......... ", +" .... .... ", +"....... .......", +". . . . . .", +" . . . ", +" . .. . ", +". . . . . .", +"....... .......", +" .... .... ", +" .......... ", +" .. .. ", +".. . . ..", +" . .. .. . "}; diff --git a/big.ber b/big.ber new file mode 100644 index 0000000..8413629 --- /dev/null +++ b/big.ber @@ -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", +" ", +" .. ", +" ..... ", +" ...... ", +" .. .... ", +" ... ...... ", +" .... ... ", +" ... .. ", +" .. ... ", +" ... .... ", +" ...... ... ", +" .... .. ", +" ...... ", +" ..... ", +" .. ", +" "}; diff --git a/game.c b/game.c new file mode 100644 index 0000000..96864ab --- /dev/null +++ b/game.c @@ -0,0 +1,125 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx + +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); +} diff --git a/game.h b/game.h new file mode 100644 index 0000000..490eda2 --- /dev/null +++ b/game.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/25 16:53:01 by kdx #+# #+# */ +/* Updated: 2022/10/13 22:31:22 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +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 + +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 = '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 = 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 = '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 = ' ' && 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 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 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 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 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 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 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 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 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 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 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 + +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 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 + +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 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 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 + +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 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 + +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 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 = 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 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 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 = '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 = '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 + +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 +# include +# include + +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 diff --git a/main.c b/main.c new file mode 100644 index 0000000..82c28e6 --- /dev/null +++ b/main.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx + +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 \n")); + game = game_new(argv[1]); + if (game == NULL) + return (1); + game->redraw = 1; + sily_loop(game->sily, update, draw); + return (0); +} diff --git a/map.ber b/map.ber new file mode 100644 index 0000000..6c50e9e --- /dev/null +++ b/map.ber @@ -0,0 +1,16 @@ +1111111111111111 +11111000000C0001 +1000000000C00001 +10000000CC000E01 +1C00000C00000111 +10CC00C000011111 +1000CC0000111001 +1100000001110001 +1111000000000001 +1111110000000001 +1000111000C0C001 +1000000000101001 +100000000C0C0C01 +1000000001010101 +100P000000000001 +1111111111111111 diff --git a/map.c b/map.c new file mode 100644 index 0000000..de3bcf1 --- /dev/null +++ b/map.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx +#include + +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."); +} diff --git a/map.h b/map.h new file mode 100644 index 0000000..b4ce641 --- /dev/null +++ b/map.h @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx 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 = (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 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 = 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 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 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 new file mode 100644 index 0000000..cb7c5a4 Binary files /dev/null and b/minilibx.tgz differ 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 +#include + +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 + +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 + +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 +#include + +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 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 + +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 + +/* 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 +#include + +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); +} diff --git a/sily.h b/sily.h new file mode 100644 index 0000000..398dd12 --- /dev/null +++ b/sily.h @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sily.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx +# 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 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 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 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 + +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 + +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 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 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