aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-03-15 14:26:29 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-03-15 14:26:29 +0100
commitbcbe3db5345f86fcacb039039382f3130c933eea (patch)
tree36cb74b5e6d4c1b6ade5816ee821c58acbd4fd17 /src
parentCharacter is now FAT (diff)
download42-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
Diffstat (limited to 'src')
-rw-r--r--src/ft_exit.c26
-rw-r--r--src/ft_init_sfx.c37
-rw-r--r--src/ft_sfx_death.c26
-rw-r--r--src/ft_sfx_new_level.c26
-rw-r--r--src/ft_sfx_trap.c27
5 files changed, 88 insertions, 54 deletions
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);
}