]> git.dkaiser.de - 42/so_long.git/commitdiff
Add input to direction handling
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Thu, 9 May 2024 15:49:54 +0000 (17:49 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Thu, 9 May 2024 15:49:54 +0000 (17:49 +0200)
include/so_long.h
src/init.c
src/input.c [new file with mode: 0644]
src/loop.c

index 98b47117326de67147efd7d3ccf14ab2d25bda36..11d90842483cb46c227ef18ab5626e873579e00f 100644 (file)
@@ -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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 # 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
index 569bed8b4fa286183d8efe518fadcb3df9195a60..7c9645b5e79804224b94134c621cf23870964c66 100644 (file)
@@ -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 (file)
index 0000000..2fcdc4e
--- /dev/null
@@ -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);
+}
index 506dad1add8cf483b812380c2a4605ac57a3f8e7..a55360ba95d332773d319806b620f8c331202404 100644 (file)
@@ -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);
 }