diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-16 19:41:26 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-16 19:41:26 +0100 |
commit | a8c9c04ec24a71f076891bdc1c26860db7c5c9c3 (patch) | |
tree | a5dc6905f6987c4d5fcd27276f7fc338f2af8f0b /src | |
parent | Cleaner memdels (diff) | |
parent | Merge branch 'master' into back-to-pthread (diff) | |
download | 42-cub3d-a8c9c04ec24a71f076891bdc1c26860db7c5c9c3.tar.gz 42-cub3d-a8c9c04ec24a71f076891bdc1c26860db7c5c9c3.tar.bz2 42-cub3d-a8c9c04ec24a71f076891bdc1c26860db7c5c9c3.tar.xz 42-cub3d-a8c9c04ec24a71f076891bdc1c26860db7c5c9c3.tar.zst 42-cub3d-a8c9c04ec24a71f076891bdc1c26860db7c5c9c3.zip |
Merge branch 'back-to-pthread'
Diffstat (limited to 'src')
-rw-r--r-- | src/ft_draw_scene.c | 1 | ||||
-rw-r--r-- | src/ft_exit.c | 58 | ||||
-rw-r--r-- | src/ft_get_music.c | 3 | ||||
-rw-r--r-- | src/ft_init_lists.c | 2 | ||||
-rw-r--r-- | src/ft_init_map.c | 5 | ||||
-rw-r--r-- | src/ft_init_sfx.c | 40 | ||||
-rw-r--r-- | src/ft_key_loop.c | 21 | ||||
-rw-r--r-- | src/ft_music.c | 25 | ||||
-rw-r--r-- | src/ft_sfx_death.c | 26 | ||||
-rw-r--r-- | src/ft_sfx_footstep.c | 45 | ||||
-rw-r--r-- | src/ft_sfx_new_level.c | 26 | ||||
-rw-r--r-- | src/ft_sfx_pain.c | 49 | ||||
-rw-r--r-- | src/ft_sfx_trap.c | 41 | ||||
-rw-r--r-- | src/ft_suffer_animation.c | 4 | ||||
-rw-r--r-- | src/ft_warp_level.c | 9 | ||||
-rw-r--r-- | src/main.c | 1 |
16 files changed, 242 insertions, 114 deletions
diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index 0f34567..c7b07a1 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -13,6 +13,7 @@ #include <libft.h> #include <cub3d.h> #include <mlx.h> +#include <stdlib.h> #include <stdint.h> static int8_t diff --git a/src/ft_exit.c b/src/ft_exit.c index 2e997fb..61f925e 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_some_extras(t_cub *clist) @@ -53,12 +51,13 @@ 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); - ft_free_words(clist->sfx.new_lvl); - ft_free_words(clist->sfx.trap); + 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); ft_free_sprites(clist->mlist.sprite_path); if (!clist->wlist.inited) ft_memdel((void*)&clist->wlist.winptr); @@ -89,25 +88,28 @@ static void } static void - ft_kill_forks(t_cub *clist) + ft_cancel_threads(t_cub *clist) { - pid_t tmp; - - if (!(tmp = waitpid(clist->mpid, NULL, WNOHANG))) - kill(clist->mpid, SIGTERM); - wait(&clist->mpid); - 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); + 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.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); } int @@ -121,7 +123,7 @@ int mlx_destroy_image(clist->wlist.wlx, clist->img.img); mlx_destroy_window(clist->wlist.wlx, clist->wlist.winptr); } - ft_kill_forks(clist); + ft_cancel_threads(clist); ft_free_lists(clist); ft_free_some_extras(clist); ft_printf("Exiting program\n"); diff --git a/src/ft_get_music.c b/src/ft_get_music.c index 6d4f299..cf105a3 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_lists.c b/src/ft_init_lists.c index 1b12b7e..692f3ce 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -71,7 +71,7 @@ static int8_t i = -1; while (++i < 5) cl->key_input[i] = -1; - cl->mpid = 0; + cl->mtid = 0; cl->ishud = 0; cl->isoldmus = 0; cl->doicast = 1; diff --git a/src/ft_init_map.c b/src/ft_init_map.c index 4fcff7e..8b97906 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_path(mlist) < 0) return (-1); mlist->map_w = 0; diff --git a/src/ft_init_sfx.c b/src/ft_init_sfx.c index 1a06d11..35ab57d 100644 --- a/src/ft_init_sfx.c +++ b/src/ft_init_sfx.c @@ -17,39 +17,41 @@ #include <stdlib.h> #include <stdint.h> #include <unistd.h> +#include <pthread.h> static int8_t - ft_split_sfx(char ***target, const char *path) + ft_init_sfx_cmd(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)))) + if (!(*target = (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); + ft_sprintf(*target, FT_SND_CMD, path); return (0); } int8_t ft_init_sfx(t_sfx *sfx) { - sfx->death_pid = 0; - sfx->new_lvl_pid = 0; - sfx->pain_pid = 0; - sfx->trap_pid = 0; - 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) + 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); return (0); } diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index c16655e..74ff775 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -82,20 +82,23 @@ 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++; } - if (cl->key_input[0] != -1) - ft_draw_scene(cl); + ft_draw_scene(cl); return (0); } diff --git a/src/ft_music.c b/src/ft_music.c index 234f86e..1a3da63 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -11,21 +11,28 @@ /* ************************************************************************** */ #include <cub3d.h> -#include <unistd.h> +#include <stddef.h> +#include <stdlib.h> +#include <pthread.h> -void - ft_music_fork(char **mcmd_words, char *const envp[]) +static void + *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_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_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_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_pain.c b/src/ft_sfx_pain.c new file mode 100644 index 0000000..cb68108 --- /dev/null +++ b/src/ft_sfx_pain.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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) + { + pthread_mutex_lock(&sfx->pain_mutex); + ref = (ref > 201) ? (0) : (ref + 1); + if (ref % 3) + system(sfx->pain_one); + else + system(sfx->pain_two); + } + return (NULL); +} + +void + ft_sfx_pain(t_cub *cl) +{ + static uint8_t ref = 0; + + ref = (ref > 201) ? (0) : (ref + 1); + if (ref % 2) + pthread_mutex_unlock(&cl->sfx.pain_mutex); +} diff --git a/src/ft_sfx_trap.c b/src/ft_sfx_trap.c index 1499ecf..d55dd57 100644 --- a/src/ft_sfx_trap.c +++ b/src/ft_sfx_trap.c @@ -10,40 +10,31 @@ /* */ /* ************************************************************************** */ -#include <libft.h> #include <cub3d.h> -#include <unistd.h> -#include <stdint.h> +#include <stdlib.h> +#include <pthread.h> -static void - ft_sfx_pain(t_cub *cl) +void + *ft_sfx_trap_thread(void *vargp) { - static uint8_t ref = 0; + t_sfx *sfx; - ref += 1; - if (ref > 201) - ref = 0; - if (ref % 2) + 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) { - 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); - } + 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); - else - { - ft_sfx_pain(cl); - } + pthread_mutex_unlock(&cl->sfx.trap_mutex); } 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 diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index bc903f0..fadbb2a 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 @@ -13,6 +13,7 @@ #include <libft.h> #include <mlx.h> #include <cub3d.h> +#include <stdlib.h> int main(int argc, const char *argv[], char *const envp[]) |