From 59ea94739fe5205f29e75ceff97156607868e3d0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 14 Mar 2020 23:27:54 +0100 Subject: Looks like I have to pthread this shit --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index a7ff053..05f9828 100644 --- a/Makefile +++ b/Makefile @@ -128,6 +128,7 @@ CFLAGS += -Wall CFLAGS += -Wextra CFLAGS += -Werror CFLAGS += -pedantic +CFLAGS += -pthread #--------------------------------------------------------------------------------------------------# ifdef ASAN CFLAGS += ${DEBUG} @@ -164,10 +165,10 @@ 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} -lft -lX11 -lXext -lmlx -lm -lbsd + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd -lpthread endif #--------------------------------------------------------------------------------------------------# all: ${NAME} -- cgit v1.2.3 From 9f35e7fab50809cd74e3bfffff67a81023abc837 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 15 Mar 2020 01:02:32 +0100 Subject: I need a macOS --- inc/cub3d_structs.h | 5 ++--- map/lvl_one.cub | 2 +- src/ft_draw_scene.c | 1 + src/ft_exit.c | 9 +++++---- src/ft_get_music.c | 3 --- src/ft_init_map.c | 5 +---- src/ft_music.c | 23 +++++++++++++++-------- src/ft_warp_level.c | 9 ++++----- 8 files changed, 29 insertions(+), 28 deletions(-) diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 7545f3b..aaf8498 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -14,9 +14,9 @@ # define CUB3D_STRUCTS_H #include -#include #include #include +#include typedef struct s_win { @@ -189,7 +189,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 +229,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 688f216..e6b93c7 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/BITURE-ET-MELANCOLIE.wav +MU ./media/sound/sfx/death_screen.wav 1111111 111111111111L00001 diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index 1edef2a..a9ea6c5 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -13,6 +13,7 @@ #include #include #include +#include #include static int8_t diff --git a/src/ft_exit.c b/src/ft_exit.c index f1da643..60e950a 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -38,7 +38,6 @@ 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); @@ -77,9 +76,11 @@ static void { pid_t tmp; - if (!(tmp = waitpid(clist->mpid, NULL, WNOHANG))) - kill(clist->mpid, SIGTERM); - wait(&clist->mpid); + if (clist->isoldmus && clist->wlist.inited) + { + pthread_cancel(clist->mtid); + pthread_join(clist->mtid, NULL); + } if (!(tmp = waitpid(clist->sfx.death_pid, NULL, WNOHANG))) kill(clist->sfx.death_pid, SIGTERM); wait(&clist->sfx.death_pid); diff --git a/src/ft_get_music.c b/src/ft_get_music.c index 7a8d36f..7e6c6be 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_map.c b/src/ft_init_map.c index 813a795..cf5bebe 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(mlist) < 0) return (-1); mlist->map_w = 0; diff --git a/src/ft_music.c b/src/ft_music.c index 234f86e..13da479 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -11,21 +11,28 @@ /* ************************************************************************** */ #include -#include +#include +#include +#include void - ft_music_fork(char **mcmd_words, char *const envp[]) + *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_warp_level.c b/src/ft_warp_level.c index 56d4d67..dd4ed1b 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 -- cgit v1.2.3 From b992ca41965edf663f99bae19a95d59f2fd1ed97 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 15 Mar 2020 02:15:01 +0100 Subject: okok --- Makefile | 4 ++-- inc/cub3d.h | 4 +++- inc/cub3d_structs.h | 7 ++++--- map/lvl_one.cub | 2 +- src/ft_exit.c | 8 +++----- src/ft_init_sfx.c | 21 ++++++++++++++++++-- src/ft_music.c | 2 +- src/ft_sfx_pain.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++ src/ft_sfx_trap.c | 26 ------------------------ src/ft_suffer_animation.c | 4 +++- 10 files changed, 86 insertions(+), 42 deletions(-) create mode 100644 src/ft_sfx_pain.c diff --git a/Makefile b/Makefile index 05f9828..4274a9b 100644 --- a/Makefile +++ b/Makefile @@ -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 #include #include +#include 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 #include -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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 17:17:54 by rbousset #+# #+# */ +/* Updated: 2020/02/24 17:17:56 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +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 #include #include #include -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 #include #include -#include 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 -- cgit v1.2.3 From e22ae5588bf27756fe157f0bab363478136f3030 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 15 Mar 2020 02:17:56 +0100 Subject: Character is now FAT --- inc/cub3d_defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index c931b2d..d4979a7 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -114,7 +114,7 @@ enum ** ====== COLLISION ====== */ -# define FT_COLL_MULT 0.225 +# define FT_COLL_MULT 0.3 /* ** ====== SCREEN ====== -- cgit v1.2.3 From bcbe3db5345f86fcacb039039382f3130c933eea Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 15 Mar 2020 14:26:29 +0100 Subject: Sounds have been threaded --- inc/cub3d.h | 3 +++ inc/cub3d_structs.h | 15 +++++++++------ map/map_one.cub | 2 +- src/ft_exit.c | 26 +++++++++++++------------- src/ft_init_sfx.c | 37 +++++++++++-------------------------- src/ft_sfx_death.c | 26 +++++++++++++++++++++----- src/ft_sfx_new_level.c | 26 +++++++++++++++++++++----- 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 @@ -19,26 +19,6 @@ #include #include -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) { @@ -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 -#include +#include +#include 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 -#include +#include +#include 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 -#include -#include +#include +#include + +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); } -- cgit v1.2.3 From 4fe3a87d3c09a22da49fb1f3e49e26751e7d4471 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 15 Mar 2020 14:36:36 +0100 Subject: ok --- src/ft_key_loop.c | 2 +- src/ft_sfx_pain.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index c16655e..5b6fee4 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -95,7 +95,7 @@ int } i++; } - if (cl->key_input[0] != -1) + /* if (cl->key_input[0] != -1) */ ft_draw_scene(cl); return (0); } diff --git a/src/ft_sfx_pain.c b/src/ft_sfx_pain.c index 780cb73..962dcae 100644 --- a/src/ft_sfx_pain.c +++ b/src/ft_sfx_pain.c @@ -31,9 +31,9 @@ void ref += 1; if (ref > 201) ref = 0; - pthread_mutex_lock(&sfx->pain_mutex); if (ref % 2) { + pthread_mutex_lock(&sfx->pain_mutex); if (ref % 3) system(sfx->pain_one); else -- cgit v1.2.3 From 163bed0c8988ff020b746bf96dc01dd2058dc4f9 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 15 Mar 2020 15:09:59 +0100 Subject: Footsteps are bav --- Makefile | 1 + inc/cub3d.h | 4 +++- inc/cub3d_defines.h | 2 ++ inc/cub3d_structs.h | 4 ++++ media/sound/sfx/footstep_one.wav | Bin 0 -> 44892 bytes media/sound/sfx/footstep_two.wav | Bin 0 -> 42954 bytes src/ft_exit.c | 20 ++++++++++------- src/ft_init_sfx.c | 4 ++++ src/ft_key_loop.c | 18 ++++++++++------ src/ft_sfx_footstep.c | 45 +++++++++++++++++++++++++++++++++++++++ src/ft_sfx_pain.c | 4 +--- 11 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 media/sound/sfx/footstep_one.wav create mode 100644 media/sound/sfx/footstep_two.wav create mode 100644 src/ft_sfx_footstep.c diff --git a/Makefile b/Makefile index 4274a9b..e4ecd46 100644 --- a/Makefile +++ b/Makefile @@ -92,6 +92,7 @@ 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_footstep.c SRCS_NAME += ft_sfx_new_level.c SRCS_NAME += ft_sfx_pain.c SRCS_NAME += ft_sfx_trap.c diff --git a/inc/cub3d.h b/inc/cub3d.h index 6905688..aafe288 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -150,11 +150,13 @@ void ft_floor_cast_inits(uint16_t y, t_ray *rl, t_cub *cl); */ void ft_sfx_death(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_new_lvl_thread(void *vargp); 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); diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index d4979a7..286dbef 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" diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 473fe9f..2d1d00d 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -58,15 +58,19 @@ typedef struct s_bmp_info typedef struct s_sfx { 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; diff --git a/media/sound/sfx/footstep_one.wav b/media/sound/sfx/footstep_one.wav new file mode 100644 index 0000000..f8f4577 Binary files /dev/null and b/media/sound/sfx/footstep_one.wav differ diff --git a/media/sound/sfx/footstep_two.wav b/media/sound/sfx/footstep_two.wav new file mode 100644 index 0000000..4186e35 Binary files /dev/null and b/media/sound/sfx/footstep_two.wav differ diff --git a/src/ft_exit.c b/src/ft_exit.c index 625f86b..c3b5ebf 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -15,10 +15,8 @@ #include #include #include -#include -#include -#include #include +#include static void ft_free_lists(t_cub *clist) @@ -39,6 +37,9 @@ static void ft_memdel((void**)&clist->mlist.mapl); ft_free_words(clist->mlist.map); 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); @@ -72,7 +73,7 @@ static void } static void - ft_kill_forks(t_cub *clist) + ft_cancel_threads(t_cub *clist) { if (clist->isoldmus && clist->wlist.inited) { @@ -82,12 +83,15 @@ static void 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); - pthread_join(clist->sfx.pain_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); @@ -105,7 +109,7 @@ int mlx_destroy_window(clist->wlist.wlx, clist->wlist.winptr); clist->wlist.winptr = NULL; } - ft_kill_forks(clist); + ft_cancel_threads(clist); ft_free_lists(clist); ft_printf("Exiting program\n"); exit(exit_code); diff --git a/src/ft_init_sfx.c b/src/ft_init_sfx.c index e1e9b7f..35ab57d 100644 --- a/src/ft_init_sfx.c +++ b/src/ft_init_sfx.c @@ -36,16 +36,20 @@ int8_t ft_init_sfx(t_sfx *sfx) { 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); diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index 5b6fee4..df97380 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -82,15 +82,19 @@ 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++; 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 17:17:54 by rbousset #+# #+# */ +/* Updated: 2020/02/24 17:17:56 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +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_pain.c b/src/ft_sfx_pain.c index 962dcae..28bc95d 100644 --- a/src/ft_sfx_pain.c +++ b/src/ft_sfx_pain.c @@ -28,9 +28,7 @@ void pthread_mutex_lock(&sfx->pain_mutex); while (1) { - ref += 1; - if (ref > 201) - ref = 0; + ref = (ref > 201) ? (0) : (ref + 1); if (ref % 2) { pthread_mutex_lock(&sfx->pain_mutex); -- cgit v1.2.3 From 5a2b45a9c6d8235f5027a22132920fc28f2602c8 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 15 Mar 2020 16:06:39 +0100 Subject: Edited sounds, bug fix --- media/sound/sfx/death_screen.wav | Bin 1459980 -> 1255372 bytes media/sound/sfx/footstep_one.wav | Bin 44892 -> 24298 bytes media/sound/sfx/footstep_two.wav | Bin 42954 -> 25562 bytes media/sound/sfx/next_lvl.wav | Bin 792004 -> 547516 bytes media/sound/sfx/scream_one.wav | Bin 26392 -> 24112 bytes media/sound/sfx/scream_two.wav | Bin 34140 -> 28996 bytes media/sound/sfx/trap.wav | Bin 108532 -> 60508 bytes src/ft_sfx_pain.c | 19 ++++++++++--------- 8 files changed, 10 insertions(+), 9 deletions(-) diff --git a/media/sound/sfx/death_screen.wav b/media/sound/sfx/death_screen.wav index 61cf33d..5ee6f4d 100644 Binary files a/media/sound/sfx/death_screen.wav and b/media/sound/sfx/death_screen.wav differ diff --git a/media/sound/sfx/footstep_one.wav b/media/sound/sfx/footstep_one.wav index f8f4577..898363e 100644 Binary files a/media/sound/sfx/footstep_one.wav and b/media/sound/sfx/footstep_one.wav differ diff --git a/media/sound/sfx/footstep_two.wav b/media/sound/sfx/footstep_two.wav index 4186e35..01781e1 100644 Binary files a/media/sound/sfx/footstep_two.wav and b/media/sound/sfx/footstep_two.wav differ diff --git a/media/sound/sfx/next_lvl.wav b/media/sound/sfx/next_lvl.wav index 64cbc31..2b73389 100644 Binary files a/media/sound/sfx/next_lvl.wav and b/media/sound/sfx/next_lvl.wav differ diff --git a/media/sound/sfx/scream_one.wav b/media/sound/sfx/scream_one.wav index df9fc78..0bdab05 100644 Binary files a/media/sound/sfx/scream_one.wav and b/media/sound/sfx/scream_one.wav differ diff --git a/media/sound/sfx/scream_two.wav b/media/sound/sfx/scream_two.wav index 1d4ff78..3ccb161 100644 Binary files a/media/sound/sfx/scream_two.wav and b/media/sound/sfx/scream_two.wav differ diff --git a/media/sound/sfx/trap.wav b/media/sound/sfx/trap.wav index 7d237e5..ae41e07 100644 Binary files a/media/sound/sfx/trap.wav and b/media/sound/sfx/trap.wav differ diff --git a/src/ft_sfx_pain.c b/src/ft_sfx_pain.c index 28bc95d..cb68108 100644 --- a/src/ft_sfx_pain.c +++ b/src/ft_sfx_pain.c @@ -28,15 +28,12 @@ void pthread_mutex_lock(&sfx->pain_mutex); while (1) { + pthread_mutex_lock(&sfx->pain_mutex); ref = (ref > 201) ? (0) : (ref + 1); - if (ref % 2) - { - pthread_mutex_lock(&sfx->pain_mutex); - if (ref % 3) - system(sfx->pain_one); - else - system(sfx->pain_two); - } + if (ref % 3) + system(sfx->pain_one); + else + system(sfx->pain_two); } return (NULL); } @@ -44,5 +41,9 @@ void void ft_sfx_pain(t_cub *cl) { - pthread_mutex_unlock(&cl->sfx.pain_mutex); + static uint8_t ref = 0; + + ref = (ref > 201) ? (0) : (ref + 1); + if (ref % 2) + pthread_mutex_unlock(&cl->sfx.pain_mutex); } -- cgit v1.2.3