diff options
28 files changed, 280 insertions, 133 deletions
@@ -93,8 +93,10 @@ SRCS_NAME += ft_draw_life_bar.c SRCS_NAME += ft_del_extra_sprites.c SRCS_NAME += ft_init_sfx.c SRCS_NAME += ft_sfx_death.c -SRCS_NAME += ft_sfx_trap.c +SRCS_NAME += ft_sfx_footstep.c SRCS_NAME += ft_sfx_new_level.c +SRCS_NAME += ft_sfx_pain.c +SRCS_NAME += ft_sfx_trap.c SRCS_NAME += ft_death_screen.c SRCS_NAME += ft_death_hooks.c SRCS_NAME += ft_init_sprites.c @@ -180,10 +182,11 @@ else @$(MAKE) --no-print-directory -C ${LFT_DIR} all endif ifeq (${OS}, Darwin) - ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm \ + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm -lpthread \ -framework OpenGL -framework AppKit else - ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_GNU_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_GNU_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd \ +-lpthread endif #--------------------------------------------------------------------------------------------------# all: ${NAME} diff --git a/inc/cub3d.h b/inc/cub3d.h index 6ca6f8b..5371305 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -151,8 +151,15 @@ void ft_floor_cast_inits(uint16_t y, t_ray *rl, t_cub *cl); */ void ft_sfx_death(t_cub *cl); -void ft_sfx_trap(t_cub *cl); +void ft_sfx_footstep(t_cub *cl); void ft_sfx_new_level(t_cub *cl); +void ft_sfx_pain(t_cub *cl); +void ft_sfx_trap(t_cub *cl); +void *ft_sfx_death_thread(void *vargp); +void *ft_sfx_footstep_thread(void *vargp); +void *ft_sfx_new_lvl_thread(void *vargp); +void *ft_sfx_pain_thread(void *vargp); +void *ft_sfx_trap_thread(void *vargp); /* ** ====== OTHER ====== diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index 0cd15fa..e52eb7a 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -86,6 +86,8 @@ enum */ # define FT_SFX_DEATH_PATH "./media/sound/sfx/death_screen.wav" +# define FT_SFX_FS_ONE_PATH "./media/sound/sfx/footstep_one.wav" +# define FT_SFX_FS_TWO_PATH "./media/sound/sfx/footstep_two.wav" # define FT_SFX_N_LVL_PATH "./media/sound/sfx/next_lvl.wav" # define FT_SFX_SCR_ONE_PATH "./media/sound/sfx/scream_one.wav" # define FT_SFX_SCR_TWO_PATH "./media/sound/sfx/scream_two.wav" @@ -114,7 +116,7 @@ enum ** ====== COLLISION ====== */ -# define FT_COLL_MULT 0.225 +# define FT_COLL_MULT 0.3 /* ** ====== SCREEN ====== diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 9ea3d08..92d928d 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -14,9 +14,9 @@ # define CUB3D_STRUCTS_H #include <stddef.h> -#include <stdlib.h> #include <stdint.h> #include <sys/types.h> +#include <pthread.h> typedef struct s_win { @@ -57,15 +57,23 @@ typedef struct s_bmp_info typedef struct s_sfx { - char **death; - char **new_lvl; - char **pain_one; - char **pain_two; - char **trap; - pid_t death_pid; - pid_t new_lvl_pid; - pid_t pain_pid; - pid_t trap_pid; + char *death; + char *footstep_one; + char *footstep_two; + char *new_lvl; + char *pain_one; + char *pain_two; + char *trap; + pthread_t death_tid; + pthread_t footstep_tid; + pthread_t new_lvl_tid; + pthread_t pain_tid; + pthread_t trap_tid; + pthread_mutex_t death_mutex; + pthread_mutex_t footstep_mutex; + pthread_mutex_t new_lvl_mutex; + pthread_mutex_t pain_mutex; + pthread_mutex_t trap_mutex; } t_sfx; typedef struct s_bmp_rgb @@ -189,7 +197,6 @@ typedef struct s_map char *mapl; char **sprite_path; char **map; - char **mcmd_words; int8_t x_step; int8_t y_step; size_t map_w; @@ -230,7 +237,7 @@ typedef struct s_cub char *const *envp; char errmsg[64]; int32_t key_input[5]; - pid_t mpid; + pthread_t mtid; int (*key_ptr[6])(struct s_cub*); int8_t (*get_ptr[14])(char**, struct s_cub*); char ref[22][3]; diff --git a/map/lvl_one.cub b/map/lvl_one.cub index e9c51ca..688f216 100644 --- a/map/lvl_one.cub +++ b/map/lvl_one.cub @@ -17,8 +17,8 @@ LT ./media/img/crapaud.xpm MU ./media/sound/BITURE-ET-MELANCOLIE.wav 1111111 -111111111111LW0001 +111111111111L00001 120000000211111101 -100000000000000001 +1E0000000000000001 120000000211T10011 111111111111111111 diff --git a/media/sound/sfx/death_screen.wav b/media/sound/sfx/death_screen.wav Binary files differindex 61cf33d..5ee6f4d 100644 --- a/media/sound/sfx/death_screen.wav +++ b/media/sound/sfx/death_screen.wav diff --git a/media/sound/sfx/footstep_one.wav b/media/sound/sfx/footstep_one.wav Binary files differnew file mode 100644 index 0000000..898363e --- /dev/null +++ b/media/sound/sfx/footstep_one.wav diff --git a/media/sound/sfx/footstep_two.wav b/media/sound/sfx/footstep_two.wav Binary files differnew file mode 100644 index 0000000..01781e1 --- /dev/null +++ b/media/sound/sfx/footstep_two.wav diff --git a/media/sound/sfx/next_lvl.wav b/media/sound/sfx/next_lvl.wav Binary files differindex 64cbc31..2b73389 100644 --- a/media/sound/sfx/next_lvl.wav +++ b/media/sound/sfx/next_lvl.wav diff --git a/media/sound/sfx/scream_one.wav b/media/sound/sfx/scream_one.wav Binary files differindex df9fc78..0bdab05 100644 --- a/media/sound/sfx/scream_one.wav +++ b/media/sound/sfx/scream_one.wav diff --git a/media/sound/sfx/scream_two.wav b/media/sound/sfx/scream_two.wav Binary files differindex 1d4ff78..3ccb161 100644 --- a/media/sound/sfx/scream_two.wav +++ b/media/sound/sfx/scream_two.wav diff --git a/media/sound/sfx/trap.wav b/media/sound/sfx/trap.wav Binary files differindex 7d237e5..ae41e07 100644 --- a/media/sound/sfx/trap.wav +++ b/media/sound/sfx/trap.wav diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index 0f34567..c7b07a1 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -13,6 +13,7 @@ #include <libft.h> #include <cub3d.h> #include <mlx.h> +#include <stdlib.h> #include <stdint.h> static int8_t diff --git a/src/ft_exit.c b/src/ft_exit.c index 2e997fb..61f925e 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -15,10 +15,8 @@ #include <mlx.h> #include <stddef.h> #include <stdlib.h> -#include <signal.h> -#include <sys/types.h> -#include <sys/wait.h> #include <stdint.h> +#include <pthread.h> static void ft_free_some_extras(t_cub *clist) @@ -53,12 +51,13 @@ static void ft_memdel((void*)&clist->mlist.music_cmd); ft_memdel((void*)&clist->mlist.mapl); ft_free_words(clist->mlist.map); - ft_free_words(clist->mlist.mcmd_words); - ft_free_words(clist->sfx.death); - ft_free_words(clist->sfx.pain_one); - ft_free_words(clist->sfx.pain_two); - ft_free_words(clist->sfx.new_lvl); - ft_free_words(clist->sfx.trap); + ft_memdel((void*)&clist->sfx.death); + ft_memdel((void*)&clist->sfx.footstep_one); + ft_memdel((void*)&clist->sfx.footstep_two); + ft_memdel((void*)&clist->sfx.new_lvl); + ft_memdel((void*)&clist->sfx.pain_one); + ft_memdel((void*)&clist->sfx.pain_two); + ft_memdel((void*)&clist->sfx.trap); ft_free_sprites(clist->mlist.sprite_path); if (!clist->wlist.inited) ft_memdel((void*)&clist->wlist.winptr); @@ -89,25 +88,28 @@ static void } static void - ft_kill_forks(t_cub *clist) + ft_cancel_threads(t_cub *clist) { - pid_t tmp; - - if (!(tmp = waitpid(clist->mpid, NULL, WNOHANG))) - kill(clist->mpid, SIGTERM); - wait(&clist->mpid); - if (!(tmp = waitpid(clist->sfx.death_pid, NULL, WNOHANG))) - kill(clist->sfx.death_pid, SIGTERM); - wait(&clist->sfx.death_pid); - if (!(tmp = waitpid(clist->sfx.new_lvl_pid, NULL, WNOHANG))) - kill(clist->sfx.new_lvl_pid, SIGTERM); - wait(&clist->sfx.new_lvl_pid); - if (!(tmp = waitpid(clist->sfx.pain_pid, NULL, WNOHANG))) - kill(clist->sfx.pain_pid, SIGTERM); - wait(&clist->sfx.pain_pid); - if (!(tmp = waitpid(clist->sfx.trap_pid, NULL, WNOHANG))) - kill(clist->sfx.trap_pid, SIGTERM); - wait(&clist->sfx.trap_pid); + if (clist->isoldmus && clist->wlist.inited) + { + pthread_cancel(clist->mtid); + pthread_join(clist->mtid, NULL); + } + pthread_mutex_unlock(&clist->sfx.death_mutex); + pthread_cancel(clist->sfx.death_tid); + pthread_join(clist->sfx.death_tid, NULL); + pthread_mutex_unlock(&clist->sfx.footstep_mutex); + pthread_cancel(clist->sfx.footstep_tid); + pthread_join(clist->sfx.footstep_tid, NULL); + pthread_mutex_unlock(&clist->sfx.new_lvl_mutex); + pthread_cancel(clist->sfx.new_lvl_tid); + pthread_join(clist->sfx.new_lvl_tid, NULL); + pthread_mutex_unlock(&clist->sfx.pain_mutex); + pthread_cancel(clist->sfx.pain_tid); + pthread_join(clist->sfx.pain_tid, NULL); + pthread_mutex_unlock(&clist->sfx.trap_mutex); + pthread_cancel(clist->sfx.trap_tid); + pthread_join(clist->sfx.trap_tid, NULL); } int @@ -121,7 +123,7 @@ int mlx_destroy_image(clist->wlist.wlx, clist->img.img); mlx_destroy_window(clist->wlist.wlx, clist->wlist.winptr); } - ft_kill_forks(clist); + ft_cancel_threads(clist); ft_free_lists(clist); ft_free_some_extras(clist); ft_printf("Exiting program\n"); diff --git a/src/ft_get_music.c b/src/ft_get_music.c index 6d4f299..cf105a3 100644 --- a/src/ft_get_music.c +++ b/src/ft_get_music.c @@ -27,9 +27,6 @@ static int8_t return (-1); ft_sprintf(mlist->music_cmd, FT_SND_CMD, mlist->music_path); - ft_free_words(mlist->mcmd_words); - if (!(mlist->mcmd_words = ft_split(mlist->music_cmd, ' '))) - return (-1); return (0); } diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c index 1b12b7e..692f3ce 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -71,7 +71,7 @@ static int8_t i = -1; while (++i < 5) cl->key_input[i] = -1; - cl->mpid = 0; + cl->mtid = 0; cl->ishud = 0; cl->isoldmus = 0; cl->doicast = 1; diff --git a/src/ft_init_map.c b/src/ft_init_map.c index 4fcff7e..8b97906 100644 --- a/src/ft_init_map.c +++ b/src/ft_init_map.c @@ -34,9 +34,7 @@ static int8_t !(mlist->music_cmd = (char*)ft_calloc(1, sizeof(char))) || !(mlist->mapl = (char*)ft_calloc(1, sizeof(char))) || !(mlist->map = (char**)ft_calloc(2, sizeof(char*))) || - !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char))) || - !(mlist->mcmd_words = (char**)ft_calloc(2, sizeof(char*))) || - !(mlist->mcmd_words[0] = (char*)ft_calloc(1, sizeof(char)))) + !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char)))) return (-1); return (0); } @@ -75,7 +73,6 @@ int8_t if (ft_init_map_callocs(mlist) < 0) return (-1); mlist->map[1] = 0; - mlist->mcmd_words[1] = 0; if (ft_calloc_sprites_path(mlist) < 0) return (-1); mlist->map_w = 0; diff --git a/src/ft_init_sfx.c b/src/ft_init_sfx.c index 1a06d11..35ab57d 100644 --- a/src/ft_init_sfx.c +++ b/src/ft_init_sfx.c @@ -17,39 +17,41 @@ #include <stdlib.h> #include <stdint.h> #include <unistd.h> +#include <pthread.h> static int8_t - ft_split_sfx(char ***target, const char *path) + ft_init_sfx_cmd(char **target, const char *path) { uint8_t len; - char *tmp; len = ft_strlen(path); len += ft_strlen(FT_SND_CMD) - 2; - if (!(tmp = (char *)malloc((len + 1) * sizeof(char)))) + if (!(*target = (char *)malloc((len + 1) * sizeof(char)))) return (-1); - ft_sprintf(tmp, FT_SND_CMD, path); - if (!(*target = ft_split(tmp, ' '))) - { - ft_memdel((void*)&tmp); - return (-1); - } - ft_memdel((void*)&tmp); + ft_sprintf(*target, FT_SND_CMD, path); return (0); } int8_t ft_init_sfx(t_sfx *sfx) { - sfx->death_pid = 0; - sfx->new_lvl_pid = 0; - sfx->pain_pid = 0; - sfx->trap_pid = 0; - if (ft_split_sfx(&sfx->death, FT_SFX_DEATH_PATH) < 0 || - ft_split_sfx(&sfx->new_lvl, FT_SFX_N_LVL_PATH) < 0 || - ft_split_sfx(&sfx->pain_one, FT_SFX_SCR_ONE_PATH) < 0 || - ft_split_sfx(&sfx->pain_two, FT_SFX_SCR_TWO_PATH) < 0 || - ft_split_sfx(&sfx->trap, FT_SFX_TRAP_PATH) < 0) + if (ft_init_sfx_cmd(&sfx->death, FT_SFX_DEATH_PATH) < 0 || + ft_init_sfx_cmd(&sfx->footstep_one, FT_SFX_FS_ONE_PATH) < 0 || + ft_init_sfx_cmd(&sfx->footstep_two, FT_SFX_FS_TWO_PATH) < 0 || + ft_init_sfx_cmd(&sfx->new_lvl, FT_SFX_N_LVL_PATH) < 0 || + ft_init_sfx_cmd(&sfx->pain_one, FT_SFX_SCR_ONE_PATH) < 0 || + ft_init_sfx_cmd(&sfx->pain_two, FT_SFX_SCR_TWO_PATH) < 0 || + ft_init_sfx_cmd(&sfx->trap, FT_SFX_TRAP_PATH) < 0) return (-1); + pthread_mutex_init(&sfx->death_mutex, NULL); + pthread_mutex_init(&sfx->footstep_mutex, NULL); + pthread_mutex_init(&sfx->new_lvl_mutex, NULL); + pthread_mutex_init(&sfx->pain_mutex, NULL); + pthread_mutex_init(&sfx->trap_mutex, NULL); + pthread_create(&sfx->death_tid, NULL, ft_sfx_death_thread, sfx); + pthread_create(&sfx->footstep_tid, NULL, ft_sfx_footstep_thread, sfx); + pthread_create(&sfx->new_lvl_tid, NULL, ft_sfx_new_lvl_thread, sfx); + pthread_create(&sfx->pain_tid, NULL, ft_sfx_pain_thread, sfx); + pthread_create(&sfx->trap_tid, NULL, ft_sfx_trap_thread, sfx); return (0); } diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index c16655e..74ff775 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -82,20 +82,23 @@ int while (i < 5 && cl->key_input[i] != -1 && cl->key_input[i] <= 5) { cl->key_ptr[cl->key_input[i]](cl); - ft_collision(old_y, old_x, cl->key_input[i], cl); - if (cl->mlist.isnlvl) + if (cl->key_input[i] >= 0 && cl->key_input[i] <= 3) { - if ((uint32_t)cl->plist.pos_x == cl->mlist.nlx && - (uint32_t)cl->plist.pos_y == cl->mlist.nly) + ft_sfx_footstep(cl); + ft_collision(old_y, old_x, cl->key_input[i], cl); + if (cl->mlist.isnlvl) { - ft_sfx_new_level(cl); - return ((ft_warp_level(cl->mlist.nlevel_path, cl) < 0) ? - (ft_exit(FT_RET_FAILED_STRUCTS, cl)) : (0)); + if ((uint32_t)cl->plist.pos_x == cl->mlist.nlx && + (uint32_t)cl->plist.pos_y == cl->mlist.nly) + { + ft_sfx_new_level(cl); + return ((ft_warp_level(cl->mlist.nlevel_path, cl) < 0) ? + (ft_exit(FT_RET_FAILED_STRUCTS, cl)) : (0)); + } } } i++; } - if (cl->key_input[0] != -1) - ft_draw_scene(cl); + ft_draw_scene(cl); return (0); } diff --git a/src/ft_music.c b/src/ft_music.c index 234f86e..1a3da63 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -11,21 +11,28 @@ /* ************************************************************************** */ #include <cub3d.h> -#include <unistd.h> +#include <stddef.h> +#include <stdlib.h> +#include <pthread.h> -void - ft_music_fork(char **mcmd_words, char *const envp[]) +static void + *ft_music_thread(void *vargp) { - execve(*(mcmd_words + 0), mcmd_words, envp); + t_cub *cl; + + if (FT_OS == 1) + pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + else + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + cl = (t_cub *)vargp; + while (1) + system(cl->mlist.music_cmd); + return(NULL); } void ft_enable_music(t_cub *cl) { cl->isoldmus = 1; - cl->mpid = fork(); - if (cl->mpid == 0) - { - ft_music_fork(cl->mlist.mcmd_words, cl->envp); - } + pthread_create(&cl->mtid, NULL, ft_music_thread, cl); } diff --git a/src/ft_sfx_death.c b/src/ft_sfx_death.c index 2566358..1680f2d 100644 --- a/src/ft_sfx_death.c +++ b/src/ft_sfx_death.c @@ -11,14 +11,30 @@ /* ************************************************************************** */ #include <cub3d.h> -#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> void - ft_sfx_death(t_cub *cl) + *ft_sfx_death_thread(void *vargp) { - cl->sfx.death_pid = fork(); - if (cl->sfx.death_pid == 0) + t_sfx *sfx; + + if (FT_OS == 1) + pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + else + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + sfx = (t_sfx *)vargp; + pthread_mutex_lock(&sfx->death_mutex); + while (1) { - execve(*(cl->sfx.death + 0), cl->sfx.death, cl->envp); + pthread_mutex_lock(&sfx->death_mutex); + system(sfx->death); } + return (NULL); +} + +void + ft_sfx_death(t_cub *cl) +{ + pthread_mutex_unlock(&cl->sfx.death_mutex); } diff --git a/src/ft_sfx_footstep.c b/src/ft_sfx_footstep.c new file mode 100644 index 0000000..a2eef1d --- /dev/null +++ b/src/ft_sfx_footstep.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sfx_footstep.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 17:17:54 by rbousset #+# #+# */ +/* Updated: 2020/02/24 17:17:56 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <cub3d.h> +#include <stdlib.h> +#include <pthread.h> + +void + *ft_sfx_footstep_thread(void *vargp) +{ + t_sfx *sfx; + static uint8_t ref = 0; + + if (FT_OS == 1) + pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + else + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + sfx = (t_sfx *)vargp; + pthread_mutex_lock(&sfx->footstep_mutex); + while (1) + { + ref = (ref > 201) ? (0) : (ref + 1); + pthread_mutex_lock(&sfx->footstep_mutex); + if (ref % 2) + system(sfx->footstep_one); + else + system(sfx->footstep_two); + } + return (NULL); +} + +void + ft_sfx_footstep(t_cub *cl) +{ + pthread_mutex_unlock(&cl->sfx.footstep_mutex); +} diff --git a/src/ft_sfx_new_level.c b/src/ft_sfx_new_level.c index 05e9059..ec648e0 100644 --- a/src/ft_sfx_new_level.c +++ b/src/ft_sfx_new_level.c @@ -11,14 +11,30 @@ /* ************************************************************************** */ #include <cub3d.h> -#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> void - ft_sfx_new_level(t_cub *cl) + *ft_sfx_new_lvl_thread(void *vargp) { - cl->sfx.new_lvl_pid = fork(); - if (cl->sfx.new_lvl_pid == 0) + t_sfx *sfx; + + if (FT_OS == 1) + pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + else + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + sfx = (t_sfx *)vargp; + pthread_mutex_lock(&sfx->new_lvl_mutex); + while (1) { - execve(*(cl->sfx.new_lvl + 0), cl->sfx.new_lvl, cl->envp); + pthread_mutex_lock(&sfx->new_lvl_mutex); + system(sfx->new_lvl); } + return (NULL); +} + +void + ft_sfx_new_level(t_cub *cl) +{ + pthread_mutex_unlock(&cl->sfx.new_lvl_mutex); } diff --git a/src/ft_sfx_pain.c b/src/ft_sfx_pain.c new file mode 100644 index 0000000..cb68108 --- /dev/null +++ b/src/ft_sfx_pain.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sfx_trap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 17:17:54 by rbousset #+# #+# */ +/* Updated: 2020/02/24 17:17:56 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <cub3d.h> +#include <stdlib.h> +#include <pthread.h> + +void + *ft_sfx_pain_thread(void *vargp) +{ + t_sfx *sfx; + static uint8_t ref = 0; + + if (FT_OS == 1) + pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + else + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + sfx = (t_sfx *)vargp; + pthread_mutex_lock(&sfx->pain_mutex); + while (1) + { + pthread_mutex_lock(&sfx->pain_mutex); + ref = (ref > 201) ? (0) : (ref + 1); + if (ref % 3) + system(sfx->pain_one); + else + system(sfx->pain_two); + } + return (NULL); +} + +void + ft_sfx_pain(t_cub *cl) +{ + static uint8_t ref = 0; + + ref = (ref > 201) ? (0) : (ref + 1); + if (ref % 2) + pthread_mutex_unlock(&cl->sfx.pain_mutex); +} diff --git a/src/ft_sfx_trap.c b/src/ft_sfx_trap.c index 1499ecf..d55dd57 100644 --- a/src/ft_sfx_trap.c +++ b/src/ft_sfx_trap.c @@ -10,40 +10,31 @@ /* */ /* ************************************************************************** */ -#include <libft.h> #include <cub3d.h> -#include <unistd.h> -#include <stdint.h> +#include <stdlib.h> +#include <pthread.h> -static void - ft_sfx_pain(t_cub *cl) +void + *ft_sfx_trap_thread(void *vargp) { - static uint8_t ref = 0; + t_sfx *sfx; - ref += 1; - if (ref > 201) - ref = 0; - if (ref % 2) + if (FT_OS == 1) + pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + else + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + sfx = (t_sfx *)vargp; + pthread_mutex_lock(&sfx->trap_mutex); + while (1) { - cl->sfx.pain_pid = fork(); - if (cl->sfx.pain_pid == 0) - { - if (ref % 3) - execve(*(cl->sfx.pain_one + 0), cl->sfx.pain_one, cl->envp); - else - execve(*(cl->sfx.pain_two + 0), cl->sfx.pain_two, cl->envp); - } + pthread_mutex_lock(&sfx->trap_mutex); + system(sfx->trap); } + return (NULL); } void ft_sfx_trap(t_cub *cl) { - cl->sfx.trap_pid = fork(); - if (cl->sfx.trap_pid == 0) - execve(*(cl->sfx.trap + 0), cl->sfx.trap, cl->envp); - else - { - ft_sfx_pain(cl); - } + pthread_mutex_unlock(&cl->sfx.trap_mutex); } diff --git a/src/ft_suffer_animation.c b/src/ft_suffer_animation.c index d08fa54..a0a21a5 100644 --- a/src/ft_suffer_animation.c +++ b/src/ft_suffer_animation.c @@ -14,7 +14,6 @@ #include <cub3d.h> #include <mlx.h> #include <stdint.h> -#include <unistd.h> void ft_linux_suffer_animation(t_cub *cl) @@ -44,7 +43,10 @@ void ft_death_screen(cl); } else + { ft_sfx_trap(cl); + ft_sfx_pain(cl); + } } void diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index bc903f0..fadbb2a 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -39,7 +39,6 @@ static void ft_memdel((void*)&ml->mapl); ft_free_sprites(ml->sprite_path); ft_free_words(ml->map); - ft_free_words(ml->mcmd_words); } static void @@ -74,15 +73,15 @@ static void { if (isoldmus && !cl->mlist.ismusic) { - kill(cl->mpid, SIGTERM); - wait(&cl->mpid); + pthread_cancel(cl->mtid); + pthread_join(cl->mtid, NULL); cl->isoldmus = 0; } else if (isoldmus && cl->mlist.ismusic && ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) { - kill(cl->mpid, SIGTERM); - wait(&cl->mpid); + pthread_cancel(cl->mtid); + pthread_join(cl->mtid, NULL); ft_enable_music(cl); } else if (isoldmus && cl->mlist.ismusic @@ -13,6 +13,7 @@ #include <libft.h> #include <mlx.h> #include <cub3d.h> +#include <stdlib.h> int main(int argc, const char *argv[], char *const envp[]) |