diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-06 22:10:16 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-06 22:10:16 +0100 |
commit | 079a4b6e9389b05db2c672594cfb2f2b197e35dd (patch) | |
tree | f4a2bc882dd7667e2c9758e2be38e3484879e225 | |
parent | qwe (diff) | |
parent | okok (diff) | |
download | 42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.gz 42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.bz2 42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.xz 42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.zst 42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.zip |
Merge branch 'fork-music'
-rw-r--r-- | Makefile | 17 | ||||
-rw-r--r-- | inc/cub3d.h | 6 | ||||
-rw-r--r-- | inc/cub3d_defines.h | 4 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 4 | ||||
-rw-r--r-- | map/lvl_one.cub | 2 | ||||
-rw-r--r-- | src/ft_exit.c | 9 | ||||
-rw-r--r-- | src/ft_get_music.c | 11 | ||||
-rw-r--r-- | src/ft_init_map.c | 5 | ||||
-rw-r--r-- | src/ft_music.c | 24 | ||||
-rw-r--r-- | src/ft_parse_map.c | 10 | ||||
-rw-r--r-- | src/ft_treat_args.c | 10 | ||||
-rw-r--r-- | src/ft_warp_level.c | 17 |
12 files changed, 68 insertions, 51 deletions
@@ -113,27 +113,24 @@ ifdef ASAN CFLAGS += ${FSANITIZE} endif #--------------------------------------------------------------------------------------------------# -CDEFS = -DFT_SCR_SIZE=\"${SCR_SZE}\" +CDEFS = -D_POSIX_C_SOURCE +CDEFS += -DFT_SCR_SIZE=\"${SCR_SZE}\" ifeq (${OS}, Darwin) CDEFS += -DFT_OS=1 else CDEFS += -DFT_OS=2 endif #--------------------------------------------------------------------------------------------------# -ifeq (${OS}, Darwin) - NAME = Cub3D -else - NAME = a.out -endif +NAME = Cub3D #==================================================================================================# #------------------------------------------ Rules -------------------------------------------------# #==================================================================================================# ${OBJS_DIR}%.o: ${SRCS_DIR}%.c ${INCS_DIR} @${MKDIR} ${OBJS_DIR} ifeq (${OS}, Darwin) - ${CC} -c -pthread ${CFLAGS} ${CDEFS} -I${LFT_DIR}${INCS_DIR} -I${MLX_DIR} -I${INCS_DIR} -o $@ $< + ${CC} -c ${CFLAGS} ${CDEFS} -I${LFT_DIR}${INCS_DIR} -I${MLX_DIR} -I${INCS_DIR} -o $@ $< else - ${CC} -c -pthread ${CFLAGS} ${CDEFS} -I${LFT_DIR}${INCS_DIR} -I${INCS_DIR} -o $@ $< + ${CC} -c ${CFLAGS} ${CDEFS} -I${LFT_DIR}${INCS_DIR} -I${INCS_DIR} -o $@ $< endif #--------------------------------------------------------------------------------------------------# ${NAME}: ${OBJS} ${LFT_SRCS} ${LFT_DIR}${INCS_DIR}libft.h @@ -149,10 +146,10 @@ else @$(MAKE) --no-print-directory -C ${LFT_DIR} all endif ifeq (${OS}, Darwin) - ${CC} -pthread ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm -lpthread \ + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm \ -framework OpenGL -framework AppKit else - ${CC} -pthread ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd -lpthread + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd endif #--------------------------------------------------------------------------------------------------# all: ${NAME} diff --git a/inc/cub3d.h b/inc/cub3d.h index 7b57942..6078801 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -112,14 +112,16 @@ size_t ft_get_line_len(char *line); */ uint8_t ft_check_map_arg(int argc, const char *argv[]); -uint8_t ft_use_args(int argc, const char *argv[], t_cub *clist); +uint8_t ft_use_args(int argc, const char *argv[], + char *const envp[], t_cub *clist); /* ** ====== OTHER ====== */ void ft_set_minimap_scale(t_cub *clist); -void *ft_music_thread(void *vargp); +void ft_enable_music(t_cub *cl); +void ft_music_fork(char **mcmd_words, char *const envp[]); void ft_detect(t_cub *cl); void ft_castray(t_cub *cl); int8_t ft_save_to_bmp(t_cub *cl); diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index fb50474..1153225 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -83,9 +83,9 @@ enum */ # if FT_OS == 1 -# define FT_MUS_CMD "afplay --volume 0.2 %s" +# define FT_MUS_CMD "/usr/bin/afplay --volume 0.2 %s" # else -# define FT_MUS_CMD "aplay -f cd -t wav -q %s" +# define FT_MUS_CMD "/usr/bin/aplay -f cd -t wav -q %s" # endif /* diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index c74a9e0..d53cdb9 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -152,6 +152,7 @@ typedef struct s_map char *music_cmd; char *mapl; char **map; + char **mcmd_words; int8_t x_step; int8_t y_step; size_t map_w; @@ -175,9 +176,10 @@ typedef struct s_cub uint8_t isoldmus; uint16_t currlvl; uint16_t i; + char *const *envp; char errmsg[64]; int32_t key_input[5]; - pthread_t tid; + pid_t mpid; int (*key_ptr[6])(struct s_cub*); int8_t (*get_ptr[11])(char**, struct s_cub*); struct s_win wlist; diff --git a/map/lvl_one.cub b/map/lvl_one.cub index 330ffbc..c6c7cc0 100644 --- a/map/lvl_one.cub +++ b/map/lvl_one.cub @@ -1,4 +1,4 @@ -R 1500 1000 +R 1500 900 NO ./media/img/BRIQUASSE_3.xpm SO ./media/img/BRIQUASSE_3.xpm diff --git a/src/ft_exit.c b/src/ft_exit.c index ca5201c..be4d46e 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -15,9 +15,9 @@ #include <mlx.h> #include <stddef.h> #include <stdlib.h> -#include <stdint.h> #include <signal.h> -#include <pthread.h> +#include <sys/wait.h> +#include <stdint.h> static void ft_free_lists(t_cub *clist) @@ -34,6 +34,7 @@ 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); if (!clist->wlist.inited) ft_memdel((void**)&clist->wlist.winptr); } @@ -67,8 +68,8 @@ int } if (clist->isoldmus && clist->wlist.inited) { - pthread_cancel(clist->tid); - pthread_join(clist->tid, NULL); + kill(clist->mpid, SIGTERM); + wait(&clist->mpid); } ft_free_lists(clist); ft_printf("Exiting program\n"); diff --git a/src/ft_get_music.c b/src/ft_get_music.c index cdd7039..2383e0a 100644 --- a/src/ft_get_music.c +++ b/src/ft_get_music.c @@ -15,7 +15,7 @@ #include <stdlib.h> #include <stdint.h> -static void +static int8_t ft_set_music_cmd(t_map *mlist) { uint8_t len; @@ -24,9 +24,13 @@ static void len += ft_strlen(FT_MUS_CMD) - 2; ft_memdel((void**)&mlist->music_cmd); if (!(mlist->music_cmd = (char *)malloc((len + 1) * sizeof(char)))) - return ; + return (-1); ft_sprintf(mlist->music_cmd, FT_MUS_CMD, mlist->music_path); + ft_free_words(mlist->mcmd_words); + if (!(mlist->mcmd_words = ft_split(mlist->music_cmd, ' '))) + return (-1); + return (0); } int8_t @@ -54,6 +58,5 @@ int8_t return (-1); } clist->mlist.ismusic = 1; - ft_set_music_cmd(&clist->mlist); - return (0); + return ((ft_set_music_cmd(&clist->mlist) == 0) ? (0) : (-1)); } diff --git a/src/ft_init_map.c b/src/ft_init_map.c index 4fa127c..1c12189 100644 --- a/src/ft_init_map.c +++ b/src/ft_init_map.c @@ -30,7 +30,9 @@ 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->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)))) return (-1); return (0); } @@ -41,6 +43,7 @@ int8_t if (ft_init_map_callocs(mlist) < 0) return (-1); mlist->map[1] = 0; + mlist->mcmd_words[1] = 0; mlist->map_w = 0; mlist->map_h = 0; mlist->mapl_len = 0; diff --git a/src/ft_music.c b/src/ft_music.c index aec5e58..c3b90b1 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -10,21 +10,23 @@ /* */ /* ************************************************************************** */ -#include <libft.h> #include <cub3d.h> -#include <stddef.h> -#include <stdlib.h> -#include <pthread.h> +#include <unistd.h> +#include <libft.h> void - *ft_music_thread(void *vargp) + ft_music_fork(char **mcmd_words, char *const envp[]) { - t_cub *cl; + execve(*(mcmd_words + 0), mcmd_words, envp); +} - cl = (t_cub *)vargp; +void + ft_enable_music(t_cub *cl) +{ cl->isoldmus = 1; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); - while (1) - system(cl->mlist.music_cmd); - return (NULL); + cl->mpid = fork(); + if (cl->mpid == 0) + { + ft_music_fork(cl->mlist.mcmd_words, cl->envp); + } } diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c index 3c1fd9c..36d1ec5 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -75,13 +75,13 @@ static int8_t } void - ft_save_name(const char *map_path, t_map *mlist, t_cub *clist) + ft_save_name(const char *map_path, t_cub *clist) { - ft_memdel((void**)&mlist->filename); - if (!(mlist->filename = + ft_memdel((void**)&clist->mlist.filename); + if (!(clist->mlist.filename = (char*)malloc((ft_strlen(map_path) + 1) * sizeof(char)))) ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, clist); - ft_sprintf(mlist->filename, "%s", map_path); + ft_sprintf(clist->mlist.filename, "%s", map_path); } void @@ -95,7 +95,7 @@ void fd = open(map_path, O_RDONLY); if (fd < 0) ft_error(FT_RET_NO_MAP, FT_ERR_NO_MAP, clist); - ft_save_name(map_path, &clist->mlist, clist); + ft_save_name(map_path, clist); ret = 1; while (ret != 12 && ret >= 0) ret = ft_parse_it(fd, clist); diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c index 2571709..a47f9f7 100644 --- a/src/ft_treat_args.c +++ b/src/ft_treat_args.c @@ -14,7 +14,6 @@ #include <cub3d.h> #include <stddef.h> #include <stdint.h> -#include <pthread.h> #include <unistd.h> uint8_t @@ -30,15 +29,20 @@ uint8_t } uint8_t - ft_use_args(int argc, const char *argv[], t_cub *clist) + ft_use_args(int argc, const char *argv[], char *const envp[], t_cub *clist) { + clist->envp = envp; if (argc < 3) { if (ft_init_winptr(clist) < 0) + { return (ft_exit(FT_RET_FAILED_MLX, clist)); + } ft_draw_scene(clist); if (clist->mlist.ismusic) - pthread_create(&clist->tid, NULL, ft_music_thread, clist); + { + ft_enable_music(clist); + } ft_hooks_and_loops(&clist->wlist, clist); } else if (argc == 3 && !ft_strncmp("--save", argv[2], 7)) diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index 808b346..37789a5 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -15,13 +15,15 @@ #include <stdlib.h> #include <stddef.h> #include <stdint.h> -#include <mlx.h> +#include <unistd.h> #include <signal.h> -#include <pthread.h> +#include <sys/wait.h> +#include <mlx.h> static void ft_del_map(t_map *ml) { + ft_memdel((void**)&ml->filename); ft_memdel((void**)&ml->no_tex_path); ft_memdel((void**)&ml->so_tex_path); ft_memdel((void**)&ml->ea_tex_path); @@ -33,6 +35,7 @@ static void ft_memdel((void**)&ml->music_cmd); ft_memdel((void**)&ml->mapl); ft_free_words(ml->map); + ft_free_words(ml->mcmd_words); } static void @@ -66,16 +69,16 @@ static void { if (isoldmus && !cl->mlist.ismusic) { - pthread_cancel(cl->tid); - pthread_join(cl->tid, NULL); + kill(cl->mpid, SIGTERM); + wait(&cl->mpid); cl->isoldmus = 0; } else if (isoldmus && cl->mlist.ismusic && ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) { - pthread_cancel(cl->tid); - pthread_join(cl->tid, NULL); - pthread_create(&cl->tid, NULL, ft_music_thread, cl); + kill(cl->mpid, SIGTERM); + wait(&cl->mpid); + ft_enable_music(cl); } else if (isoldmus && cl->mlist.ismusic && !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) |