diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | inc/cub3d.h | 4 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 7 | ||||
-rw-r--r-- | map/lvl_one.cub | 2 | ||||
-rw-r--r-- | src/ft_exit.c | 8 | ||||
-rw-r--r-- | src/ft_init_sfx.c | 21 | ||||
-rw-r--r-- | src/ft_music.c | 2 | ||||
-rw-r--r-- | src/ft_sfx_pain.c | 50 | ||||
-rw-r--r-- | src/ft_sfx_trap.c | 26 | ||||
-rw-r--r-- | src/ft_suffer_animation.c | 4 |
10 files changed, 86 insertions, 42 deletions
@@ -92,8 +92,9 @@ 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_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 #--------------------------------------------------------------------------------------------------# @@ -128,7 +129,6 @@ CFLAGS += -Wall CFLAGS += -Wextra CFLAGS += -Werror CFLAGS += -pedantic -CFLAGS += -pthread #--------------------------------------------------------------------------------------------------# ifdef ASAN CFLAGS += ${DEBUG} diff --git a/inc/cub3d.h b/inc/cub3d.h index 6c9a010..a2c2aba 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -150,8 +150,10 @@ 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_new_level(t_cub *cl); +void ft_sfx_pain(t_cub *cl); +void ft_sfx_trap(t_cub *cl); +void *ft_sfx_pain_thread(void *vargp); /* ** ====== OTHER ====== diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index aaf8498..01c9b07 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -59,13 +59,14 @@ typedef struct s_sfx { char **death; char **new_lvl; - char **pain_one; - char **pain_two; + char *pain_one; + char *pain_two; char **trap; pid_t death_pid; pid_t new_lvl_pid; - pid_t pain_pid; + pthread_t pain_tid; pid_t trap_pid; + pthread_mutex_t pain_mutex; } t_sfx; typedef struct s_bmp_rgb diff --git a/map/lvl_one.cub b/map/lvl_one.cub index e6b93c7..688f216 100644 --- a/map/lvl_one.cub +++ b/map/lvl_one.cub @@ -14,7 +14,7 @@ SH 4 L ./map/lvl_two.cub LT ./media/img/crapaud.xpm -MU ./media/sound/sfx/death_screen.wav +MU ./media/sound/BITURE-ET-MELANCOLIE.wav 1111111 111111111111L00001 diff --git a/src/ft_exit.c b/src/ft_exit.c index 60e950a..3cd933a 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -39,8 +39,8 @@ static void ft_memdel((void**)&clist->mlist.mapl); ft_free_words(clist->mlist.map); ft_free_words(clist->sfx.death); - ft_free_words(clist->sfx.pain_one); - ft_free_words(clist->sfx.pain_two); + ft_memdel((void**)&clist->sfx.pain_one); + ft_memdel((void**)&clist->sfx.pain_two); ft_free_words(clist->sfx.trap); ft_free_sprites(clist->mlist.sprite_path); if (!clist->wlist.inited) @@ -81,15 +81,13 @@ static void pthread_cancel(clist->mtid); pthread_join(clist->mtid, NULL); } + pthread_cancel(clist->sfx.pain_tid); 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); diff --git a/src/ft_init_sfx.c b/src/ft_init_sfx.c index 728e410..658fa6c 100644 --- a/src/ft_init_sfx.c +++ b/src/ft_init_sfx.c @@ -17,6 +17,7 @@ #include <stdlib.h> #include <stdint.h> #include <unistd.h> +#include <pthread.h> static int8_t ft_split_sfx(char ***target, const char *path) @@ -38,13 +39,29 @@ static int8_t return (0); } +static int8_t + ft_init_sfx_cmd(char **target, const char *path) +{ + uint8_t len; + + len = ft_strlen(path); + len += ft_strlen(FT_SND_CMD) - 2; + if (!(*target = (char *)malloc((len + 1) * sizeof(char)))) + return (-1); + ft_sprintf(*target, FT_SND_CMD, path); + return (0); +} + int8_t ft_init_sfx(t_sfx *sfx) { + if (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) + return (-1); + pthread_mutex_init(&sfx->pain_mutex, NULL); + pthread_create(&sfx->pain_tid, NULL, ft_sfx_pain_thread, sfx); 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) return (-1); return (0); diff --git a/src/ft_music.c b/src/ft_music.c index 13da479..1a3da63 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -15,7 +15,7 @@ #include <stdlib.h> #include <pthread.h> -void +static void *ft_music_thread(void *vargp) { t_cub *cl; diff --git a/src/ft_sfx_pain.c b/src/ft_sfx_pain.c new file mode 100644 index 0000000..780cb73 --- /dev/null +++ b/src/ft_sfx_pain.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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) + { + ref += 1; + if (ref > 201) + ref = 0; + pthread_mutex_lock(&sfx->pain_mutex); + if (ref % 2) + { + if (ref % 3) + system(sfx->pain_one); + else + system(sfx->pain_two); + } + } + return (NULL); +} + +void + ft_sfx_pain(t_cub *cl) +{ + pthread_mutex_unlock(&cl->sfx.pain_mutex); +} diff --git a/src/ft_sfx_trap.c b/src/ft_sfx_trap.c index 1499ecf..4b3dcf9 100644 --- a/src/ft_sfx_trap.c +++ b/src/ft_sfx_trap.c @@ -10,40 +10,14 @@ /* */ /* ************************************************************************** */ -#include <libft.h> #include <cub3d.h> #include <unistd.h> #include <stdint.h> -static void - ft_sfx_pain(t_cub *cl) -{ - static uint8_t ref = 0; - - ref += 1; - if (ref > 201) - ref = 0; - if (ref % 2) - { - 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); - } - } -} - 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); - } } 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 |