aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Makefile1
-rw-r--r--inc/cub3d.h3
-rw-r--r--inc/cub3d_defines.h8
-rw-r--r--map/map_one.cub2
-rw-r--r--src/ft_basic_keys.c8
-rw-r--r--src/ft_convert_keycode.c1
-rw-r--r--src/ft_draw_scene.c2
-rw-r--r--src/ft_extra_keys.c4
-rw-r--r--src/ft_hooks_and_loops.c5
-rw-r--r--src/ft_key_events.c39
-rw-r--r--src/ft_key_loop.c53
-rw-r--r--src/ft_key_release.c26
12 files changed, 96 insertions, 56 deletions
diff --git a/Makefile b/Makefile
index 13238d9..3a89cf8 100644
--- a/Makefile
+++ b/Makefile
@@ -56,6 +56,7 @@ SRCS_NAME += ft_check_ext.c
SRCS_NAME += ft_draw_textures.c
SRCS_NAME += ft_key_release.c
SRCS_NAME += ft_convert_keycode.c
+SRCS_NAME += ft_key_loop.c
#--------------------------------------------------------------------------------------------------#
SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME})
#--------------------------------------------------------------------------------------------------#
diff --git a/inc/cub3d.h b/inc/cub3d.h
index eacb997..2362d07 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -76,6 +76,7 @@ int ft_left_key(t_cub *clist);
int ft_right_key(t_cub *clist);
int ft_key_release(int keycode, t_cub *clist);
int8_t ft_check_ext(const char *filep, const char *ext);
-int32_t ft_convert_keycode(const int32_t tmp_code);
+int32_t ft_convert_keycode(const int32_t tmp_code);
+int ft_key_loop(t_cub *cl);
# endif
diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h
index 0c9a294..209d0bc 100644
--- a/inc/cub3d_defines.h
+++ b/inc/cub3d_defines.h
@@ -46,6 +46,14 @@
# endif
/*
+** ====== MOVE SPEED ======
+*/
+
+# define FT_MOVE_SPEED 0.1
+# define FT_STRAFE_SPEED 0.05
+# define FT_ROT_SPEED 0.07
+
+/*
** ====== SCREEN ======
*/
diff --git a/map/map_one.cub b/map/map_one.cub
index 7cb6311..da39c42 100644
--- a/map/map_one.cub
+++ b/map/map_one.cub
@@ -1,4 +1,4 @@
-R 1600 900
+R 1600 1060
NO ./map/img/crapaud.xpm
SO ./map/img/linus.xpm
diff --git a/src/ft_basic_keys.c b/src/ft_basic_keys.c
index d147486..ff74262 100644
--- a/src/ft_basic_keys.c
+++ b/src/ft_basic_keys.c
@@ -17,7 +17,7 @@ int
ft_w_key(t_cub *clist)
{
t_player *pl;
- const float move_speed = 0.2;
+ const float move_speed = FT_MOVE_SPEED;
pl = clist->plist;
pl->pos_y += move_speed * pl->dir_x;
@@ -29,7 +29,7 @@ int
ft_a_key(t_cub *clist)
{
t_player *pl;
- const float move_speed = 0.1;
+ const float move_speed = FT_STRAFE_SPEED;
pl = clist->plist;
pl->pos_y += move_speed * -pl->dir_y;
@@ -41,7 +41,7 @@ int
ft_s_key(t_cub *clist)
{
t_player *pl;
- const float move_speed = 0.2;
+ const float move_speed = FT_MOVE_SPEED;
pl = clist->plist;
pl->pos_y += move_speed * -pl->dir_x;
@@ -53,7 +53,7 @@ int
ft_d_key(t_cub *clist)
{
t_player *pl;
- const float move_speed = 0.1;
+ const float move_speed = FT_STRAFE_SPEED;
pl = clist->plist;
pl->pos_y += move_speed * pl->dir_y;
diff --git a/src/ft_convert_keycode.c b/src/ft_convert_keycode.c
index c676920..5a037c5 100644
--- a/src/ft_convert_keycode.c
+++ b/src/ft_convert_keycode.c
@@ -33,4 +33,3 @@ int32_t
(tmp_code == FT_TAB_KEY) ? (keycode = FT_TAB_KEY) : 0;
return (keycode);
}
-
diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c
index 966d357..ec2cc89 100644
--- a/src/ft_draw_scene.c
+++ b/src/ft_draw_scene.c
@@ -18,7 +18,7 @@
void
ft_draw_scene(t_cub *clist)
{
- /* mlx_clear_window(clist->wlist->wlx, clist->wlist->winptr); */
+ mlx_clear_window(clist->wlist->wlx, clist->wlist->winptr);
clist->img.img = mlx_new_image(clist->wlist->wlx,
clist->wlist->x_size, clist->wlist->y_size);
clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp,
diff --git a/src/ft_extra_keys.c b/src/ft_extra_keys.c
index bfa8386..39a297b 100644
--- a/src/ft_extra_keys.c
+++ b/src/ft_extra_keys.c
@@ -20,7 +20,7 @@ int
t_player *pl;
float sav_dir_x;
float sav_plane_x;
- const float rot_speed = 0.07;
+ const float rot_speed = FT_ROT_SPEED;
pl = clist->plist;
sav_dir_x = pl->dir_x;
@@ -38,7 +38,7 @@ int
t_player *pl;
float sav_dir_x;
float sav_plane_x;
- const float rot_speed = 0.07;
+ const float rot_speed = FT_ROT_SPEED;
pl = clist->plist;
sav_dir_x = pl->dir_x;
diff --git a/src/ft_hooks_and_loops.c b/src/ft_hooks_and_loops.c
index e7f9d58..b8bdd36 100644
--- a/src/ft_hooks_and_loops.c
+++ b/src/ft_hooks_and_loops.c
@@ -17,8 +17,9 @@
void
ft_hooks_and_loops(t_win *wl, t_cub *cl)
{
- mlx_hook(wl->winptr, 2, 1L << 0, ft_key_event, cl);
- mlx_key_hook(wl->winptr, ft_key_release, cl);
+ mlx_hook(wl->winptr, 2, (1L << 0), ft_key_event, cl);
+ mlx_hook(wl->winptr, 3, (1L << 1), ft_key_release, cl);
+ mlx_loop_hook(wl->wlx, ft_key_loop, cl);
mlx_hook(wl->winptr, 17, 0L, ft_click_close, cl);
mlx_loop(wl->wlx);
}
diff --git a/src/ft_key_events.c b/src/ft_key_events.c
index 54f3033..317715d 100644
--- a/src/ft_key_events.c
+++ b/src/ft_key_events.c
@@ -26,51 +26,16 @@ static void
clist->key_input[i] = keycode;
}
-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_event(int keycode, t_cub *clist)
{
- int (*fun_ptr[6])(t_cub*);
const int32_t tmp_code = keycode;
- const float old_y = clist->plist->pos_y;
- const float old_x = clist->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;
keycode = ft_convert_keycode(tmp_code);
- ft_insert_key(keycode, clist);
- ft_printf("keys [%d][%d][%d][%d][%d]\n",
- clist->key_input[0],
- clist->key_input[1],
- clist->key_input[2],
- clist->key_input[3],
- clist->key_input[4]);
- if (keycode <= 5)
- {
- (*fun_ptr[keycode])(clist);
- ft_collision(old_y, old_x, clist->plist, clist->mlist);
- ft_draw_scene(clist);
- return (0);
- }
- else if (keycode == FT_ESC_KEY)
+ if (keycode == FT_ESC_KEY)
return (ft_exit(0, (clist)));
else if (keycode == FT_F1_KEY || keycode == FT_TAB_KEY)
return (ft_f1_key(clist));
+ ft_insert_key(keycode, clist);
return (0);
}
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
index e56b9d0..58eb2bc 100644
--- a/src/ft_key_release.c
+++ b/src/ft_key_release.c
@@ -15,6 +15,23 @@
#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;
@@ -22,10 +39,11 @@ static void
i = 0;
while (i < 5)
{
- if (clist->key_input[i] != keycode)
+ if (clist->key_input[i] == keycode)
clist->key_input[i] = -1;
i++;
}
+ ft_decale(clist);
}
int
@@ -33,11 +51,5 @@ int
{
keycode = ft_convert_keycode(keycode);
ft_pop_key(keycode, clist);
- ft_printf("keys [%d][%d][%d][%d][%d]\n",
- clist->key_input[0],
- clist->key_input[1],
- clist->key_input[2],
- clist->key_input[3],
- clist->key_input[4]);
return (0);
}