aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/ft_convert_keycode.c35
-rw-r--r--src/ft_key_loop.c53
-rw-r--r--src/ft_key_release.c55
3 files changed, 143 insertions, 0 deletions
diff --git a/src/ft_convert_keycode.c b/src/ft_convert_keycode.c
new file mode 100644
index 0000000..5a037c5
--- /dev/null
+++ b/src/ft_convert_keycode.c
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_convert_keycode.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/17 19:00:20 by rbousset #+# #+# */
+/* Updated: 2020/02/17 19:00:21 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <cub3d.h>
+#include <stdint.h>
+
+int32_t
+ ft_convert_keycode(const int32_t tmp_code)
+{
+ uint16_t keycode;
+
+ keycode = UINT16_MAX;
+ (tmp_code == FT_W_KEY) ? (keycode = 0) : 0;
+ (tmp_code == FT_A_KEY) ? (keycode = 1) : 0;
+ (tmp_code == FT_S_KEY) ? (keycode = 2) : 0;
+ (tmp_code == FT_D_KEY) ? (keycode = 3) : 0;
+ (tmp_code == FT_L_ARR_KEY) ? (keycode = 4) : 0;
+ (tmp_code == FT_R_ARR_KEY) ? (keycode = 5) : 0;
+ (tmp_code == 3) ? (keycode = UINT16_MAX) : 0;
+ (tmp_code == 4) ? (keycode = UINT16_MAX) : 0;
+ (tmp_code == 5) ? (keycode = UINT16_MAX) : 0;
+ (tmp_code == FT_ESC_KEY) ? (keycode = FT_ESC_KEY) : 0;
+ (tmp_code == FT_F1_KEY) ? (keycode = FT_F1_KEY) : 0;
+ (tmp_code == FT_TAB_KEY) ? (keycode = FT_TAB_KEY) : 0;
+ return (keycode);
+}
diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c
new file mode 100644
index 0000000..b902e33
--- /dev/null
+++ b/src/ft_key_loop.c
@@ -0,0 +1,53 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_key_loop.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/17 20:06:26 by rbousset #+# #+# */
+/* Updated: 2020/02/17 20:06:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <cub3d.h>
+#include <stdint.h>
+
+static void
+ ft_collision(float old_y, float old_x, t_player *pl, t_map *ml)
+{
+ const size_t x = pl->pos_x;
+ const size_t y = pl->pos_y;
+
+ if (ml->map[y][x] == '1')
+ {
+ pl->pos_y = old_y;
+ pl->pos_x = old_x;
+ }
+}
+
+int
+ ft_key_loop(t_cub *cl)
+{
+ int (*fun_ptr[6])(t_cub*);
+ uint8_t i;
+ const float old_y = cl->plist->pos_y;
+ const float old_x = cl->plist->pos_x;
+
+ fun_ptr[0] = ft_w_key;
+ fun_ptr[1] = ft_a_key;
+ fun_ptr[2] = ft_s_key;
+ fun_ptr[3] = ft_d_key;
+ fun_ptr[4] = ft_left_key;
+ fun_ptr[5] = ft_right_key;
+ i = 0;
+ while (i < 5 && cl->key_input[i] != -1 && cl->key_input[i] <= 5)
+ {
+ (*fun_ptr[cl->key_input[i]])(cl);
+ ft_collision(old_y, old_x, cl->plist, cl->mlist);
+ ft_draw_scene(cl);
+ i++;
+ }
+ return (0);
+}
diff --git a/src/ft_key_release.c b/src/ft_key_release.c
new file mode 100644
index 0000000..58eb2bc
--- /dev/null
+++ b/src/ft_key_release.c
@@ -0,0 +1,55 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_key_release.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/17 18:43:56 by rbousset #+# #+# */
+/* Updated: 2020/02/17 18:43:59 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <cub3d.h>
+#include <stdint.h>
+
+static void
+ ft_decale(t_cub *cl)
+{
+ uint8_t i;
+
+ i = 0;
+ while (i < 3)
+ {
+ if (cl->key_input[i] == -1 && cl->key_input[i + 1] != -1)
+ {
+ cl->key_input[i] = cl->key_input[i + 1];
+ cl->key_input[i + 1] = -1;
+ }
+ i++;
+ }
+}
+
+static void
+ ft_pop_key(uint16_t keycode, t_cub *clist)
+{
+ uint8_t i;
+
+ i = 0;
+ while (i < 5)
+ {
+ if (clist->key_input[i] == keycode)
+ clist->key_input[i] = -1;
+ i++;
+ }
+ ft_decale(clist);
+}
+
+int
+ ft_key_release(int keycode, t_cub *clist)
+{
+ keycode = ft_convert_keycode(keycode);
+ ft_pop_key(keycode, clist);
+ return (0);
+}