aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-03-15 15:09:59 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-03-15 15:09:59 +0100
commit163bed0c8988ff020b746bf96dc01dd2058dc4f9 (patch)
tree724090f6319cc6d0336eb2a5b501bd032599d295
parentok (diff)
download42-cub3d-163bed0c8988ff020b746bf96dc01dd2058dc4f9.tar.gz
42-cub3d-163bed0c8988ff020b746bf96dc01dd2058dc4f9.tar.bz2
42-cub3d-163bed0c8988ff020b746bf96dc01dd2058dc4f9.tar.xz
42-cub3d-163bed0c8988ff020b746bf96dc01dd2058dc4f9.tar.zst
42-cub3d-163bed0c8988ff020b746bf96dc01dd2058dc4f9.zip
Footsteps are bav
-rw-r--r--Makefile1
-rw-r--r--inc/cub3d.h4
-rw-r--r--inc/cub3d_defines.h2
-rw-r--r--inc/cub3d_structs.h4
-rw-r--r--media/sound/sfx/footstep_one.wavbin0 -> 44892 bytes
-rw-r--r--media/sound/sfx/footstep_two.wavbin0 -> 42954 bytes
-rw-r--r--src/ft_exit.c20
-rw-r--r--src/ft_init_sfx.c4
-rw-r--r--src/ft_key_loop.c18
-rw-r--r--src/ft_sfx_footstep.c45
-rw-r--r--src/ft_sfx_pain.c4
11 files changed, 83 insertions, 19 deletions
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
--- /dev/null
+++ b/media/sound/sfx/footstep_one.wav
Binary files differ
diff --git a/media/sound/sfx/footstep_two.wav b/media/sound/sfx/footstep_two.wav
new file mode 100644
index 0000000..4186e35
--- /dev/null
+++ b/media/sound/sfx/footstep_two.wav
Binary files 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 <mlx.h>
#include <stddef.h>
#include <stdlib.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
#include <stdint.h>
+#include <pthread.h>
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 <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_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);