diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-15 14:26:29 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-15 14:26:29 +0100 |
commit | bcbe3db5345f86fcacb039039382f3130c933eea (patch) | |
tree | 36cb74b5e6d4c1b6ade5816ee821c58acbd4fd17 | |
parent | Character is now FAT (diff) | |
download | 42-cub3d-bcbe3db5345f86fcacb039039382f3130c933eea.tar.gz 42-cub3d-bcbe3db5345f86fcacb039039382f3130c933eea.tar.bz2 42-cub3d-bcbe3db5345f86fcacb039039382f3130c933eea.tar.xz 42-cub3d-bcbe3db5345f86fcacb039039382f3130c933eea.tar.zst 42-cub3d-bcbe3db5345f86fcacb039039382f3130c933eea.zip |
Sounds have been threaded
-rw-r--r-- | inc/cub3d.h | 3 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 15 | ||||
-rw-r--r-- | map/map_one.cub | 2 | ||||
-rw-r--r-- | src/ft_exit.c | 26 | ||||
-rw-r--r-- | src/ft_init_sfx.c | 37 | ||||
-rw-r--r-- | src/ft_sfx_death.c | 26 | ||||
-rw-r--r-- | src/ft_sfx_new_level.c | 26 | ||||
-rw-r--r-- | src/ft_sfx_trap.c | 27 |
8 files changed, 101 insertions, 61 deletions
diff --git a/inc/cub3d.h b/inc/cub3d.h index a2c2aba..6905688 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -153,7 +153,10 @@ void ft_sfx_death(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_new_lvl_thread(void *vargp); +void *ft_sfx_death_thread(void *vargp); void *ft_sfx_pain_thread(void *vargp); +void *ft_sfx_trap_thread(void *vargp); /* ** ====== OTHER ====== diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 01c9b07..473fe9f 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -57,16 +57,19 @@ typedef struct s_bmp_info typedef struct s_sfx { - char **death; - char **new_lvl; + char *death; + char *new_lvl; char *pain_one; char *pain_two; - char **trap; - pid_t death_pid; - pid_t new_lvl_pid; + char *trap; + pthread_t death_tid; + pthread_t new_lvl_tid; pthread_t pain_tid; - pid_t trap_pid; + pthread_t trap_tid; + pthread_mutex_t death_mutex; + pthread_mutex_t new_lvl_mutex; pthread_mutex_t pain_mutex; + pthread_mutex_t trap_mutex; } t_sfx; typedef struct s_bmp_rgb diff --git a/map/map_one.cub b/map/map_one.cub index 3dad2cc..f3dc29b 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -19,7 +19,7 @@ T ./media/img/spikes.xpm SH 2 111111111111111111 140000000000300001 -1000E0000000000001 +1000E0T00000000001 100100000000300001 101100000111000001 111111111111110011 diff --git a/src/ft_exit.c b/src/ft_exit.c index 3cd933a..625f86b 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -38,10 +38,10 @@ 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->sfx.death); + ft_memdel((void**)&clist->sfx.death); ft_memdel((void**)&clist->sfx.pain_one); ft_memdel((void**)&clist->sfx.pain_two); - ft_free_words(clist->sfx.trap); + ft_memdel((void**)&clist->sfx.trap); ft_free_sprites(clist->mlist.sprite_path); if (!clist->wlist.inited) ft_memdel((void**)&clist->wlist.winptr); @@ -74,23 +74,23 @@ static void static void ft_kill_forks(t_cub *clist) { - pid_t tmp; - 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.pain_mutex); 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.trap_pid, NULL, WNOHANG))) - kill(clist->sfx.trap_pid, SIGTERM); - wait(&clist->sfx.trap_pid); + pthread_join(clist->sfx.pain_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.trap_mutex); + pthread_cancel(clist->sfx.trap_tid); + pthread_join(clist->sfx.trap_tid, NULL); } int diff --git a/src/ft_init_sfx.c b/src/ft_init_sfx.c index 658fa6c..e1e9b7f 100644 --- a/src/ft_init_sfx.c +++ b/src/ft_init_sfx.c @@ -20,26 +20,6 @@ #include <pthread.h> static int8_t - ft_split_sfx(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)))) - return (-1); - ft_sprintf(tmp, FT_SND_CMD, path); - if (!(*target = ft_split(tmp, ' '))) - { - ft_memdel((void**)&tmp); - return (-1); - } - ft_memdel((void**)&tmp); - return (0); -} - -static int8_t ft_init_sfx_cmd(char **target, const char *path) { uint8_t len; @@ -55,14 +35,19 @@ static int8_t 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) + if (ft_init_sfx_cmd(&sfx->death, FT_SFX_DEATH_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->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->new_lvl_tid, NULL, ft_sfx_new_lvl_thread, sfx); 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->trap, FT_SFX_TRAP_PATH) < 0) - return (-1); + pthread_create(&sfx->trap_tid, NULL, ft_sfx_trap_thread, sfx); return (0); } 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_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_trap.c b/src/ft_sfx_trap.c index 4b3dcf9..d55dd57 100644 --- a/src/ft_sfx_trap.c +++ b/src/ft_sfx_trap.c @@ -11,13 +11,30 @@ /* ************************************************************************** */ #include <cub3d.h> -#include <unistd.h> -#include <stdint.h> +#include <stdlib.h> +#include <pthread.h> + +void + *ft_sfx_trap_thread(void *vargp) +{ + 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->trap_mutex); + while (1) + { + 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); + pthread_mutex_unlock(&cl->sfx.trap_mutex); } |