aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ft_exit.c8
-rw-r--r--src/ft_init_sfx.c21
-rw-r--r--src/ft_music.c2
-rw-r--r--src/ft_sfx_pain.c50
-rw-r--r--src/ft_sfx_trap.c26
-rw-r--r--src/ft_suffer_animation.c4
6 files changed, 76 insertions, 35 deletions
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