summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2024-05-09 17:49:54 +0200
committerDominik Kaiser2024-05-09 17:49:54 +0200
commit14ffe0479dc33cb5ed96f85535f84fa91d56c48b (patch)
tree1b052671c87cdd54bef9d6b9e9e8fab39039fc45
parent8cec16bdb6f980565b29d5b55379e481018753f6 (diff)
downloadso_long-14ffe0479dc33cb5ed96f85535f84fa91d56c48b.tar.gz
so_long-14ffe0479dc33cb5ed96f85535f84fa91d56c48b.zip
Add input to direction handling
-rw-r--r--include/so_long.h42
-rw-r--r--src/init.c29
-rw-r--r--src/input.c39
-rw-r--r--src/loop.c21
4 files changed, 113 insertions, 18 deletions
diff --git a/include/so_long.h b/include/so_long.h
index 98b4711..11d9084 100644
--- a/include/so_long.h
+++ b/include/so_long.h
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 16:13:43 by dkaiser ### ########.fr */
+/* Updated: 2024/05/09 17:38:18 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,15 +16,39 @@
# include "libft.h"
# include "mlx.h"
+enum e_direction
+{
+ ZERO = 0,
+ UP = 1,
+ DOWN = 2,
+ LEFT = 4,
+ RIGHT = 8
+};
+
+typedef struct s_vector
+{
+ int x;
+ int y;
+} t_vector;
+
+typedef struct s_player
+{
+ t_vector position;
+ t_vector direction;
+ t_vector velocity;
+} t_player;
+
typedef struct s_game
{
- void *mlx;
- void *window;
-} t_game;
-
-int init(t_game *game);
-int loop(t_game *game);
-int on_key_down(int key, t_game *game);
-int on_key_up(int keycode, t_game *game);
+ void *mlx;
+ void *window;
+ t_player player;
+ int input_direction;
+} t_game;
+
+int init(t_game *game);
+int loop(t_game *game);
+int on_key_down(int key, t_game *game);
+int on_key_up(int keycode, t_game *game);
#endif // SO_LONG_H
diff --git a/src/init.c b/src/init.c
index 569bed8..7c9645b 100644
--- a/src/init.c
+++ b/src/init.c
@@ -6,26 +6,39 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 16:13:22 by dkaiser ### ########.fr */
+/* Updated: 2024/05/09 17:48:10 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "mlx.h"
#include "so_long.h"
-static void init_hooks(t_game *game)
-{
- mlx_loop_hook(game->mlx, loop, 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_hooks(t_game *game);
+static void init_player(t_player *player, int x, int y);
int init(t_game *game)
{
game->mlx = mlx_init();
game->window = mlx_new_window(game->mlx, 1920, 1080, "so_long");
// 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
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);
+}
+
+static void init_player(t_player *player, int x, int y)
+{
+ player->position.x = x;
+ player->position.y = y;
+ player->velocity.x = 0;
+ player->velocity.y = 0;
+}
diff --git a/src/input.c b/src/input.c
new file mode 100644
index 0000000..2fcdc4e
--- /dev/null
+++ b/src/input.c
@@ -0,0 +1,39 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* input.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/09 15:37:40 by dkaiser #+# #+# */
+/* Updated: 2024/05/09 17:48:20 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "so_long.h"
+
+int on_key_down(int keycode, t_game *game)
+{
+ if (keycode == 13)
+ game->input_direction |= UP;
+ else if (keycode == 0)
+ game->input_direction |= LEFT;
+ else if (keycode == 1)
+ game->input_direction |= DOWN;
+ else if (keycode == 2)
+ game->input_direction |= RIGHT;
+ return (0);
+}
+
+int on_key_up(int keycode, t_game *game)
+{
+ if (keycode == 13)
+ game->input_direction &= ~UP;
+ else if (keycode == 0)
+ game->input_direction &= ~LEFT;
+ else if (keycode == 1)
+ game->input_direction &= ~DOWN;
+ else if (keycode == 2)
+ game->input_direction &= ~RIGHT;
+ return (0);
+}
diff --git a/src/loop.c b/src/loop.c
index 506dad1..a55360b 100644
--- a/src/loop.c
+++ b/src/loop.c
@@ -6,13 +6,32 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:09:24 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 15:10:15 by dkaiser ### ########.fr */
+/* Updated: 2024/05/09 17:48:13 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "libft.h"
+#include "mlx.h"
#include "so_long.h"
int loop(t_game *game)
{
+ mlx_clear_window(game->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, ">");
+ game->player.direction.x = ((game->input_direction & RIGHT) != 0)
+ - ((game->input_direction & LEFT) != 0);
+ game->player.direction.y = ((game->input_direction & DOWN) != 0)
+ - ((game->input_direction & UP) != 0);
+ 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));
return (0);
}