diff options
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | inc/cub3d.h | 3 | ||||
-rw-r--r-- | inc/cub3d_defines.h | 8 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 1 | ||||
-rw-r--r-- | libft/Makefile | 6 | ||||
-rw-r--r-- | map/map_one.cub | 11 | ||||
-rw-r--r-- | src/ft_basic_keys.c | 8 | ||||
-rw-r--r-- | src/ft_extra_keys.c | 4 | ||||
-rw-r--r-- | src/ft_hooks_and_loops.c | 4 | ||||
-rw-r--r-- | src/ft_init_lists.c | 7 | ||||
-rw-r--r-- | src/ft_key_events.c | 54 | ||||
-rw-r--r-- | src/ft_raycasting.c | 13 |
12 files changed, 58 insertions, 70 deletions
@@ -54,6 +54,9 @@ SRCS_NAME += ft_tex_init.c SRCS_NAME += ft_detect.c 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}) #--------------------------------------------------------------------------------------------------# @@ -73,6 +76,9 @@ endif #==================================================================================================# #------------------------------------------ Compiler ----------------------------------------------# #==================================================================================================# +DEBUG = -glldb +FSANITIZE = -fsanitize=address +#--------------------------------------------------------------------------------------------------# CC = clang #--------------------------------------------------------------------------------------------------# CFLAGS = -std=c89 @@ -99,9 +105,6 @@ ifneq (${OS}, Darwin) CDEFS += -DFT_ESC_KEY=65307 endif #--------------------------------------------------------------------------------------------------# -DEBUG = -glldb -FSANITIZE = -fsanitize=address -#--------------------------------------------------------------------------------------------------# ifeq (${OS}, Darwin) NAME = cub3d else diff --git a/inc/cub3d.h b/inc/cub3d.h index a691023..2362d07 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -74,6 +74,9 @@ int ft_d_key(t_cub *clist); int ft_f1_key(t_cub *clist); 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); +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/inc/cub3d_structs.h b/inc/cub3d_structs.h index 00f85d7..908b007 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -111,6 +111,7 @@ typedef struct s_cub uint8_t w_side; uint8_t minimap; char errmsg[40]; + int32_t key_input[5]; struct s_win *wlist; struct s_player *plist; struct s_map *mlist; diff --git a/libft/Makefile b/libft/Makefile index 38b33d5..aef5f5d 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -111,6 +111,9 @@ OBJS = $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS}) #==============================================================================# #-------------------------------- Compiler ------------------------------------# #==============================================================================# +DEBUG = -glldb +FSANITIZE = -fsanitize=address +#------------------------------------------------------------------------------# CC = clang CFLAGS = -std=c89 CFLAGS += -Wall @@ -122,9 +125,6 @@ ifdef ASAN CFLAGS += ${FSANITIZE} endif #------------------------------------------------------------------------------# -DEBUG = -glldb -FSANITIZE = -fsanitize=address -#------------------------------------------------------------------------------# NAME = libft.a #==============================================================================# #-------------------------------- Library -------------------------------------# diff --git a/map/map_one.cub b/map/map_one.cub index 9be9d48..fec57b0 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -1,4 +1,4 @@ -R 1200 800 +R 1600 1060 NO ./map/img/crapaud.xpm SO ./map/img/linus.xpm @@ -10,12 +10,13 @@ C 50,100,200 F 150,150,124 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 N 0 0 1 +1 E 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 +1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 +1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 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_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 a494597..b8bdd36 100644 --- a/src/ft_hooks_and_loops.c +++ b/src/ft_hooks_and_loops.c @@ -17,7 +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_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_init_lists.c b/src/ft_init_lists.c index 95f0488..90b8638 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -67,6 +67,7 @@ static t_cub *ft_init_cub(void) { t_cub *clist; + uint8_t i; if (!(clist = (t_cub*)malloc(sizeof(t_cub)))) return (NULL); @@ -74,6 +75,12 @@ static t_cub !(clist->mlist = ft_init_map())) return (NULL); ft_bzero(clist->errmsg, 40); + i = 0; + while (i < 5) + { + clist->key_input[i] = -1; + i++; + } clist->minimap = 0; clist->f_rgb = ft_init_rgb(); clist->c_rgb = ft_init_rgb(); diff --git a/src/ft_key_events.c b/src/ft_key_events.c index 395761e..317715d 100644 --- a/src/ft_key_events.c +++ b/src/ft_key_events.c @@ -15,59 +15,27 @@ #include <stdlib.h> #include <stdint.h> -static uint16_t - ft_set_keycode(const uint16_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); -} - static void - ft_secure_player_pos(t_player *pl, t_map *ml) + ft_insert_key(uint16_t keycode, t_cub *clist) { - (pl->pos_y < 1) ? (pl->pos_y = 1.1) : 0; - (pl->pos_x < 1) ? (pl->pos_x = 1.1) : 0; - (pl->pos_y > ml->map_h - 1.4) ? (pl->pos_y = ml->map_h - 1.4) : 0; - (pl->pos_x > ml->map_w - 1.4) ? (pl->pos_x = ml->map_w - 1.4) : 0; + uint8_t i; + + i = 0; + while (i < 5 && clist->key_input[i] != keycode && clist->key_input[i] != -1) + i++; + clist->key_input[i] = keycode; } int ft_key_event(int keycode, t_cub *clist) { - int (*fun_ptr[6])(t_cub*); - const uint16_t tmp_code = keycode; + const int32_t tmp_code = keycode; - 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_set_keycode(tmp_code); - if (keycode <= 5) - { - (*fun_ptr[keycode])(clist); - ft_secure_player_pos(clist->plist, clist->mlist); - ft_draw_scene(clist); - return (0); - } - else if (keycode == FT_ESC_KEY) + keycode = ft_convert_keycode(tmp_code); + 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_raycasting.c b/src/ft_raycasting.c index e2530a1..ff73893 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -13,7 +13,6 @@ #include <libft.h> #include <cub3d.h> #include <stdint.h> -#include <stdio.h> #include <math.h> void @@ -29,15 +28,11 @@ void clist->rlist.tex_x = (int)(clist->rlist.wall_hit_x * (double)clist->tlist[clist->w_side].img_w); if (clist->rlist.side == 0 && clist->rlist.x_ray_dir > 0) - { - clist->rlist.tex_x = clist->tlist[clist->w_side].img_w - clist->rlist.tex_x - 1; - } + clist->rlist.tex_x = clist->tlist[clist->w_side].img_w + - clist->rlist.tex_x - 1; if (clist->rlist.side == 1 && clist->rlist.y_ray_dir < 0) - { - clist->rlist.tex_x = clist->tlist[clist->w_side].img_w - clist->rlist.tex_x - 1; - } - printf("wall_hit_x : %f\ntex_x : %d\nimg_w : %d\n", - clist->rlist.wall_hit_x, clist->rlist.tex_x, clist->tlist[clist->w_side].img_w); + clist->rlist.tex_x = clist->tlist[clist->w_side].img_w + - clist->rlist.tex_x - 1; } static void |