From 699a39ad492c6e7c5b7048378819d2ca8a98581f Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 11:01:22 +0200 Subject: Make changes to Makefile and header --- include/so_long.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/so_long.h b/include/so_long.h index bff3115..aea3bb8 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser 147 bytes textures/player.xpm | 25 ------------------------- 8 files changed, 39 insertions(+), 62 deletions(-) create mode 100644 textures/player.png delete mode 100644 textures/player.xpm (limited to 'include') diff --git a/Makefile b/Makefile index 7d5e6c2..d851d7b 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) all: $(NAME) -$(NAME): libmlx $(OBJ) | libft +$(NAME): $(OBJ) | libft libmlx @$(CC) $(CFLAGS) $(HEADERS) $^ -o $@ $(LIBS) @echo "[$(NAME)] Created binary." diff --git a/include/so_long.h b/include/so_long.h index aea3bb8..e4e4f57 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser mlx, game->window); - if (game->input_direction & UP) - mlx_string_put(game->mlx, game->window, 150, 100, 0x00FFFFFF, "^"); - if (game->input_direction & DOWN) - mlx_string_put(game->mlx, game->window, 150, 200, 0x00FFFFFF, "v"); - if (game->input_direction & LEFT) - mlx_string_put(game->mlx, game->window, 100, 150, 0x00FFFFFF, "<"); - if (game->input_direction & RIGHT) - mlx_string_put(game->mlx, game->window, 200, 150, 0x00FFFFFF, ">"); - mlx_string_put(game->mlx, game->window, 500, 500, 0x00FFFFFF, - ft_itoa(game->player.direction.x)); - mlx_string_put(game->mlx, game->window, 550, 500, 0x00FFFFFF, - ft_itoa(game->player.direction.y)); - mlx_put_image_to_window(game->mlx, game->window, game->player.img, - game->player.position.x, game->player.position.y); + game->player.img->instances[0].x = game->player.position.x; + game->player.img->instances[0].y = game->player.position.y; return (0); } diff --git a/src/init.c b/src/init.c index 55e8fe0..5efec32 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser mlx = mlx_init(); - game->window = mlx_new_window(game->mlx, 1920, 1080, "so_long"); + game->mlx = mlx_init(1920, 1080, "so_long", false); // TODO: make size and title dynamic game->input_direction = ZERO; init_hooks(game); init_player(&game->player, 960, 540); // TODO: make player spawn point dynamic - int w, h; - game->player.img = mlx_xpm_file_to_image(game->mlx, "textures/player.xpm", &w, &h); + mlx_texture_t *texture = mlx_load_png("textures/player.png"); + game->player.img = mlx_texture_to_image(game->mlx, texture); + mlx_image_to_window(game->mlx, game->player.img, game->player.position.x, game->player.position.y); return (0); } static void init_hooks(t_game *game) { mlx_loop_hook(game->mlx, loop, game); - mlx_hook(game->window, 2, 0, on_key_down, game); - mlx_hook(game->window, 3, 0, on_key_up, game); + mlx_key_hook(game->mlx, on_key_down, game); + /* mlx_hook(game->window, 2, 0, on_key_down, game); */ + /* mlx_hook(game->window, 3, 0, on_key_up, game); */ } static void init_player(t_player *player, int x, int y) diff --git a/src/input.c b/src/input.c index fe1f219..7582226 100644 --- a/src/input.c +++ b/src/input.c @@ -6,14 +6,27 @@ /* By: dkaiser input_direction |= UP; else if (keycode == 0 || keycode == 123) @@ -22,7 +35,6 @@ int on_key_down(int keycode, t_game *game) game->input_direction |= DOWN; else if (keycode == 2 || keycode == 124) game->input_direction |= RIGHT; - return (0); } int on_key_up(int keycode, t_game *game) diff --git a/src/loop.c b/src/loop.c index 69c0712..e994bc0 100644 --- a/src/loop.c +++ b/src/loop.c @@ -6,16 +6,18 @@ /* By: dkaiser player.direction.x = ((game->input_direction & RIGHT) != 0) - ((game->input_direction & LEFT) != 0); game->player.direction.y = ((game->input_direction & DOWN) != 0) @@ -25,5 +27,4 @@ int loop(t_game *game) game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED; draw(game); - return (0); } diff --git a/textures/player.png b/textures/player.png new file mode 100644 index 0000000..03f6517 Binary files /dev/null and b/textures/player.png differ diff --git a/textures/player.xpm b/textures/player.xpm deleted file mode 100644 index c2f06b1..0000000 --- a/textures/player.xpm +++ /dev/null @@ -1,25 +0,0 @@ -/* XPM */ -static char *fd469a8551244943fd42519ee02c19fdyQYtXZkzcWf5Mc13[] = { -/* columns rows colors chars-per-pixel */ -"16 16 3 1 ", -" c black", -". c #FFF200", -"X c white", -/* pixels */ -"XXXXXXXXXXXXXXXX", -"X..............X", -"X..............X", -"X.. .... ..X", -"X.. .... ..X", -"X.. .... ..X", -"X..............X", -"X..............X", -"X..............X", -"X.. ...... ..X", -"X.. ...... ..X", -"X.. ..X", -"X.. ..X", -"X..............X", -"X..............X", -"XXXXXXXXXXXXXXXX" -}; -- cgit v1.2.3 From b114117e5f62761dfecd618432a44e75263a929c Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 12:12:24 +0200 Subject: Change input handling to MLX42 approach --- include/so_long.h | 5 ++--- src/init.c | 4 ++-- src/input.c | 53 ++++++++++++++++++++++------------------------------- 3 files changed, 26 insertions(+), 36 deletions(-) (limited to 'include') diff --git a/include/so_long.h b/include/so_long.h index e4e4f57..b2bf7d7 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser mlx, loop, game); - mlx_key_hook(game->mlx, on_key_down, game); + mlx_key_hook(game->mlx, on_key_input, game); /* mlx_hook(game->window, 2, 0, on_key_down, game); */ /* mlx_hook(game->window, 3, 0, on_key_up, game); */ } diff --git a/src/input.c b/src/input.c index 7582226..40c5670 100644 --- a/src/input.c +++ b/src/input.c @@ -6,46 +6,37 @@ /* By: dkaiser input_direction |= UP; + else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) + game->input_direction |= LEFT; + else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) + game->input_direction |= DOWN; + else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) + game->input_direction |= RIGHT; + } + else if (event.action == MLX_RELEASE) + { + if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP) + game->input_direction &= ~UP; + else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) + game->input_direction &= ~LEFT; + else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) + game->input_direction &= ~DOWN; + else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) + game->input_direction &= ~RIGHT; } - - if (keycode == 13 || keycode == 126) - game->input_direction |= UP; - else if (keycode == 0 || keycode == 123) - game->input_direction |= LEFT; - else if (keycode == 1 || keycode == 125) - game->input_direction |= DOWN; - else if (keycode == 2 || keycode == 124) - game->input_direction |= RIGHT; -} - -int on_key_up(int keycode, t_game *game) -{ - if (keycode == 13 || keycode == 126) - game->input_direction &= ~UP; - else if (keycode == 0 || keycode == 123) - game->input_direction &= ~LEFT; - else if (keycode == 1 || keycode == 125) - game->input_direction &= ~DOWN; - else if (keycode == 2 || keycode == 124) - game->input_direction &= ~RIGHT; - return (0); } -- cgit v1.2.3 From 461f71e2660e6286ffc757ca6b7077c70c80b6cf Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 12:24:33 +0200 Subject: Update Include path --- Makefile | 4 ++-- include/so_long.h | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/Makefile b/Makefile index d851d7b..4ce3ecf 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,9 @@ OBJ_DIR := obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) -all: $(NAME) +all: libmlx $(NAME) -$(NAME): $(OBJ) | libft libmlx +$(NAME): $(OBJ) | libft @$(CC) $(CFLAGS) $(HEADERS) $^ -o $@ $(LIBS) @echo "[$(NAME)] Created binary." diff --git a/include/so_long.h b/include/so_long.h index b2bf7d7..7365a30 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser