diff options
56 files changed, 580 insertions, 308 deletions
@@ -93,10 +93,14 @@ 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_trap.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 SRCS_NAME += ft_death_screen.c SRCS_NAME += ft_death_hooks.c +SRCS_NAME += ft_init_sprites.c +SRCS_NAME += ft_get_fps_count.c #--------------------------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #--------------------------------------------------------------------------------------------------# @@ -179,10 +183,11 @@ else @$(MAKE) --no-print-directory -C ${LFT_DIR} all endif ifeq (${OS}, Darwin) - ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm \ + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm -lpthread \ -framework OpenGL -framework AppKit else - ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_GNU_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_GNU_DIR} -lft -lX11 -lXext -lmlx -lm -lbsd \ +-lpthread endif #--------------------------------------------------------------------------------------------------# all: ${NAME} @@ -206,8 +211,10 @@ endif #--------------------------------------------------------------------------------------------------# re: fclean all #--------------------------------------------------------------------------------------------------# +bonus: all +#--------------------------------------------------------------------------------------------------# run: all @./${NAME} "map/map_one.cub" #--------------------------------------------------------------------------------------------------# -.PHONY: all clean clean fclean re run default +.PHONY: all clean clean fclean re bonus run default #============================================== EOF ===============================================# @@ -1,9 +0,0 @@ -garder : -modifs a ft_draw_sprites et ft_draw_sprites_extra -sort sprites ajoutes au header - -virer : -map - -probleme : -les sprites differents se chevauchent, ca va finir par marcher diff --git a/inc/cub3d.h b/inc/cub3d.h index 9c906be..042faa3 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -17,6 +17,7 @@ #include <cub3d_structs.h> #include <stddef.h> #include <stdint.h> +#include <time.h> /* ** ====== STRUCTS ====== @@ -35,6 +36,7 @@ t_bmp_file ft_init_bmp(void); t_bmp_info ft_init_bmp_info(void); t_rgb ft_hex_to_og_rgb(uint32_t color); int8_t ft_init_sfx(t_sfx *sfx); +int8_t ft_init_sprites(t_sprite ***sprites); /* ** ====== HOOKS ====== @@ -150,8 +152,15 @@ void ft_floor_cast_inits(uint16_t y, t_ray *rl, t_cub *cl); */ void ft_sfx_death(t_cub *cl); -void ft_sfx_trap(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_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); /* ** ====== OTHER ====== @@ -171,5 +180,6 @@ uint32_t ft_rgb_to_hex(t_rgb rgb); t_bmp_rgb ft_hex_to_rgb(uint32_t color); uint32_t ft_darken(t_rgb rgb, t_cub *cl); void ft_death_screen(t_cub *cl); +void ft_get_fps_count(clock_t delta_time, t_cub *cl); # endif diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index 0cd15fa..1aa380f 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -86,11 +86,14 @@ 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" # define FT_SFX_TRAP_PATH "./media/sound/sfx/trap.wav" # define FT_DEATH_SCREEN_PATH "./media/img/death_screen.xpm" +# define FT_HUD_BACK_PATH "./media/img/plate_small.xpm" /* ** ====== SOUNDS ====== @@ -114,7 +117,7 @@ enum ** ====== COLLISION ====== */ -# define FT_COLL_MULT 0.225 +# define FT_COLL_MULT 0.3 /* ** ====== SCREEN ====== @@ -209,6 +212,7 @@ enum # define FT_ERR_MISS_SFX_SCR_TWO "missing sound effect scream two" # define FT_ERR_MISS_SFX_TRAP "missing sound effect trap" # define FT_ERR_MISS_DEATH_SCREEN "missing death screen image" +# define FT_ERR_MISS_HUD_BACK "missing HUD background image" /* ** ====== OTHER ====== diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 6db79de..66f139d 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -14,9 +14,9 @@ # define CUB3D_STRUCTS_H #include <stddef.h> -#include <stdlib.h> #include <stdint.h> #include <sys/types.h> +#include <pthread.h> typedef struct s_win { @@ -57,15 +57,23 @@ typedef struct s_bmp_info typedef struct s_sfx { - char **death; - char **new_lvl; - char **pain_one; - char **pain_two; - char **trap; - pid_t death_pid; - pid_t new_lvl_pid; - pid_t pain_pid; - pid_t trap_pid; + 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; } t_sfx; typedef struct s_bmp_rgb @@ -189,7 +197,6 @@ typedef struct s_map char *mapl; char **sprite_path; char **map; - char **mcmd_words; int8_t x_step; int8_t y_step; size_t map_w; @@ -230,7 +237,8 @@ typedef struct s_cub char *const *envp; char errmsg[64]; int32_t key_input[5]; - pid_t mpid; + char fps_count[9]; + pthread_t mtid; int (*key_ptr[6])(struct s_cub*); int8_t (*get_ptr[14])(char**, struct s_cub*); char ref[22][3]; @@ -242,8 +250,8 @@ typedef struct s_cub struct s_img death_screen; struct s_rgb f_rgb; struct s_rgb c_rgb; - struct s_img tlist[16]; - struct s_sprite sprites[8][4096]; + struct s_img tlist[17]; + struct s_sprite **sprites; struct s_sprite traps[512]; struct s_sfx sfx; } t_cub; diff --git a/map/map_one.cub b/map/map_one.cub index e928a6c..46f5aac 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -1,4 +1,4 @@ -R 800 800 +R 1600 1000 NO ./media/img/wood_wall_1.xpm SO ./media/img/wood_wall_1.xpm @@ -18,9 +18,9 @@ T ./media/img/spikes.xpm SH 2 111111111111111111 -140000000002220001 +140000000000020201 100000E00000000001 -100100000000333001 +100100000000030301 101100000111000001 111111111111110011 1000001 100001 diff --git a/media/sound/sfx/death_screen.wav b/media/sound/sfx/death_screen.wav Binary files differindex 61cf33d..5ee6f4d 100644 --- a/media/sound/sfx/death_screen.wav +++ b/media/sound/sfx/death_screen.wav diff --git a/media/sound/sfx/footstep_one.wav b/media/sound/sfx/footstep_one.wav Binary files differnew file mode 100644 index 0000000..898363e --- /dev/null +++ b/media/sound/sfx/footstep_one.wav diff --git a/media/sound/sfx/footstep_two.wav b/media/sound/sfx/footstep_two.wav Binary files differnew file mode 100644 index 0000000..01781e1 --- /dev/null +++ b/media/sound/sfx/footstep_two.wav diff --git a/media/sound/sfx/next_lvl.wav b/media/sound/sfx/next_lvl.wav Binary files differindex 64cbc31..2b73389 100644 --- a/media/sound/sfx/next_lvl.wav +++ b/media/sound/sfx/next_lvl.wav diff --git a/media/sound/sfx/scream_one.wav b/media/sound/sfx/scream_one.wav Binary files differindex df9fc78..0bdab05 100644 --- a/media/sound/sfx/scream_one.wav +++ b/media/sound/sfx/scream_one.wav diff --git a/media/sound/sfx/scream_two.wav b/media/sound/sfx/scream_two.wav Binary files differindex 1d4ff78..3ccb161 100644 --- a/media/sound/sfx/scream_two.wav +++ b/media/sound/sfx/scream_two.wav diff --git a/media/sound/sfx/trap.wav b/media/sound/sfx/trap.wav Binary files differindex 7d237e5..ae41e07 100644 --- a/media/sound/sfx/trap.wav +++ b/media/sound/sfx/trap.wav diff --git a/src/ft_check_missing.c b/src/ft_check_missing.c index a17bdea..4d06276 100644 --- a/src/ft_check_missing.c +++ b/src/ft_check_missing.c @@ -14,15 +14,6 @@ #include <cub3d.h> #include <unistd.h> -int - ft_missing_error(const char *err, t_cub *clist) -{ - ft_dprintf(STDERR_FILENO, "Error\n"); - ft_dprintf(STDERR_FILENO, - "\033[1;31m%s %s\033[0m\n", FT_ERR_MISS_ELEMENT, err); - return (ft_exit(FT_ERR_MISSING, clist)); -} - static int ft_check_missing_sfx(t_cub *clist) { @@ -60,6 +51,18 @@ static int } static int + ft_check_missing_tex_defines(t_cub *clist) +{ + if (ft_check_not_found(FT_DEATH_SCREEN_PATH) < 0 || + ft_check_ext(FT_DEATH_SCREEN_PATH, ".xpm") < 0) + return (ft_missing_error(FT_ERR_MISS_DEATH_SCREEN, clist)); + if (ft_check_not_found(FT_HUD_BACK_PATH) < 0 || + ft_check_ext(FT_HUD_BACK_PATH, ".xpm") < 0) + return (ft_missing_error(FT_ERR_MISS_HUD_BACK, clist)); + return (ft_check_missing_sprites(clist)); +} + +static int ft_check_missing_norme(t_cub *clist) { if (!clist->mlist.isftex) @@ -82,10 +85,7 @@ static int return (ft_missing_error(FT_ERR_MISS_NLVL_PATH, clist)); if (clist->mlist.istraps && !clist->mlist.traps_path[0]) return (ft_missing_error(FT_ERR_MISS_TRAP, clist)); - if (ft_check_not_found(FT_DEATH_SCREEN_PATH) < 0 || - ft_check_ext(FT_DEATH_SCREEN_PATH, ".xpm") < 0) - return (ft_missing_error(FT_ERR_MISS_DEATH_SCREEN, clist)); - return (ft_check_missing_sprites(clist)); + return (ft_check_missing_tex_defines(clist)); } int diff --git a/src/ft_death_screen.c b/src/ft_death_screen.c index cc299d1..55a61cb 100644 --- a/src/ft_death_screen.c +++ b/src/ft_death_screen.c @@ -25,10 +25,10 @@ static void while (y < (int32_t)cl->wlist.y_size) { x = 0; + cl->death_screen.tex_y = ((y * y_ratio) >> 16); while (x < (int32_t)cl->wlist.x_size) { cl->death_screen.tex_x = ((x * x_ratio) >> 16); - cl->death_screen.tex_y = ((y * y_ratio) >> 16); cl->img.ptr[x * 4 + (cl->img.sizeline * y)] = (uint8_t)cl->death_screen.ptr[cl->death_screen.tex_x * 4 + 4 * cl->death_screen.img_w * cl->death_screen.tex_y]; @@ -38,7 +38,6 @@ static void cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] = (uint8_t)cl->death_screen.ptr[cl->death_screen.tex_x * 4 + 4 * cl->death_screen.img_w * cl->death_screen.tex_y + 2]; - cl->img.ptr[x * 4 + cl->wlist.x_size * y + 3] = (char)0; x++; } y++; diff --git a/src/ft_draw_hud.c b/src/ft_draw_hud.c index eb329c6..121e85e 100644 --- a/src/ft_draw_hud.c +++ b/src/ft_draw_hud.c @@ -14,31 +14,44 @@ #include <stdint.h> static void - ft_draw_life_back(t_win *wl, t_cub *cl) + ft_put_image_back_to_ptr(uint16_t y, uint16_t x, t_cub *cl) { - const uint16_t scl = cl->mlist.scale; - uint32_t x; - uint32_t y; - int32_t col; + const uint32_t col = 0x00d2d4d6; - col = 0x00353535; - x = (cl->mlist.map_w * scl) + 20; - y = wl->y_size - (cl->mlist.map_h * scl) - 20; - while (x < ((cl->mlist.map_w * scl) + 20) + (3 * scl)) + *(cl->img.ptr + x * 4 + (cl->img.sizeline * y)) = + (uint8_t)*(cl->tlist[16].ptr + cl->tlist[16].tex_x * 4 + 4 * + cl->tlist[16].img_w * cl->tlist[16].tex_y) & col; + *(cl->img.ptr + x * 4 + (cl->img.sizeline * y) + 1) = + (uint8_t)*(cl->tlist[16].ptr + cl->tlist[16].tex_x * 4 + 4 * + cl->tlist[16].img_w * cl->tlist[16].tex_y + 1) & col; + *(cl->img.ptr + x * 4 + (cl->img.sizeline * y) + 2) = + (uint8_t)*(cl->tlist[16].ptr + cl->tlist[16].tex_x * 4 + 4 * + cl->tlist[16].img_w * cl->tlist[16].tex_y + 2) & col; +} + +static void + ft_draw_minimap_back_image(uint16_t h, uint16_t w, t_cub *cl) +{ + int32_t x_ratio; + int32_t y_ratio; + int16_t x; + int16_t y; + + x_ratio = (int)((cl->tlist[16].img_w << 16) / w) + 1; + y_ratio = (int)((cl->tlist[16].img_h << 16) / h) + 1; + y = cl->wlist.y_size - (cl->mlist.map_h * cl->mlist.scale) - 20; + while (y < (int32_t)cl->wlist.y_size) { - while (y < wl->y_size) + x = 0; + cl->tlist[16].tex_y = (y * y_ratio) >> 16; + while (x < (uint16_t)((cl->mlist.map_w + * cl->mlist.scale) + 20 + (3 * cl->mlist.scale))) { - *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = col; - if (!(y % 3)) - { - if (col < 0x00aaaaaa) - col += 0x00010101; - } - y++; + cl->tlist[16].tex_x = (x * x_ratio) >> 16; + ft_put_image_back_to_ptr(y, x, cl); + x++; } - col = 0x00353535; - y = wl->y_size - (cl->mlist.map_h * scl) - 20; - x++; + y++; } } @@ -46,29 +59,18 @@ static void ft_draw_minimap_back(size_t map_h, size_t map_w, t_win *wl, t_cub *cl) { const uint16_t scl = cl->mlist.scale; - uint32_t x; - uint32_t y; - int32_t col; + uint16_t x; + uint16_t y; - col = 0x00353535; x = 0; y = wl->y_size - (map_h * scl) - 20; - while (x < (map_w * scl) + 20) + while (x < (map_w * scl) + 20 + (3 * scl)) { while (y < wl->y_size) - { - *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = col; - if (!(y % 3)) - { - if (col < 0x00aaaaaa) - col += 0x00010101; - } y++; - } - col = 0x00353535; - y = wl->y_size - (map_h * scl) - 20; x++; } + ft_draw_minimap_back_image(y, x, cl); } static void @@ -107,7 +109,6 @@ int8_t ft_draw_minimap_back(clist->mlist.map_h, clist->mlist.map_w, &clist->wlist, clist); ft_draw_map(clist->mlist.map, clist); - ft_draw_life_back(&clist->wlist, clist); ft_draw_life_bar(&clist->wlist, clist); if (clist->mlist.isnlvl) { diff --git a/src/ft_draw_map.c b/src/ft_draw_map.c index 5d37566..286f69d 100644 --- a/src/ft_draw_map.c +++ b/src/ft_draw_map.c @@ -31,7 +31,7 @@ static void ft_draw_circle( (scale / 2) + (x * (scale)), - ft_y_offset(clist) - (scale) + (y * (scale)), + ft_y_offset(clist) - (scale + 9) + (y * (scale)), 0x009843fa, clist); } @@ -52,7 +52,7 @@ void if (ft_ischarset("1", map[y][x])) ft_draw_square(scale + 9 + (x * (scale)), ft_y_offset(clist) - 9 + (y * (scale)), 0x00ca5422, clist); - else if (map[y][x] == '2') + else if (ft_ischarset(FT_CHRST_SPRITES, map[y][x])) ft_draw_square(scale + 9 + (x * (scale)), ft_y_offset(clist) - 9 + (y * (scale)), 0x0033ccff, clist); else if (ft_ischarset(FT_CHRST_MAP_NON_WALL, map[y][x])) diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index 1edef2a..86009f6 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -13,8 +13,19 @@ #include <libft.h> #include <cub3d.h> #include <mlx.h> +#include <stdlib.h> #include <stdint.h> +static void + ft_put_fps(t_cub *clist) +{ + mlx_string_put(clist->wlist.wlx, + clist->wlist.winptr, + 10, 20, + 0x00eeeeee, + clist->fps_count); +} + static int8_t ft_put_stage(t_cub *clist) { @@ -34,7 +45,7 @@ static int8_t (uint32_t)x, y, 0x002288da, str); - ft_memdel((void**)&str); + ft_memdel((void*)&str); return (0); } @@ -60,9 +71,12 @@ void clist->wlist.winptr, clist->img.img, 0, 0); if (FT_OS == 2) mlx_destroy_image(clist->wlist.wlx, clist->img.img); - if (clist->ishud && clist->mlist.isnlvl) - if (ft_put_stage(clist) < 0) + if (clist->ishud) + { + ft_put_fps(clist); + if (clist->mlist.isnlvl && ft_put_stage(clist) < 0) ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, clist); + } } void diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c index 66d69e5..ceea280 100644 --- a/src/ft_draw_sprite.c +++ b/src/ft_draw_sprite.c @@ -86,7 +86,6 @@ static void cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline) + 2] = (uint8_t)cl->tlist[sprite->current_sprite].ptr[sprite->tex_x * 4 + 4 * cl->tlist[sprite->current_sprite].img_h * sprite->tex_y + 2] / calc; - cl->img.ptr[sprite->x * 4 + cl->wlist.x_size * sprite->y + 3] = (char)0; } void diff --git a/src/ft_draw_textures.c b/src/ft_draw_textures.c index 5fc3bdd..dcd36e7 100644 --- a/src/ft_draw_textures.c +++ b/src/ft_draw_textures.c @@ -41,7 +41,6 @@ void ft_draw_texture(t_cub *cl, int x, int y, int tex_y) cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] = (uint8_t)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 * cl->tlist[cl->w_side].img_h * tex_y + 2] / calc; - cl->img.ptr[x * 4 + cl->wlist.x_size * y + 3] = (char)0; } void ft_choose_tex(t_cub *clist) diff --git a/src/ft_draw_traps.c b/src/ft_draw_traps.c index abde37e..8bf2221 100644 --- a/src/ft_draw_traps.c +++ b/src/ft_draw_traps.c @@ -77,7 +77,6 @@ static void cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline) + 2] = (uint8_t)cl->tlist[15].ptr[sprite->tex_x * 4 + 4 * cl->tlist[15].img_h * sprite->tex_y + 2] / calc; - cl->img.ptr[sprite->x * 4 + cl->wlist.x_size * sprite->y + 3] = (char)0; } void diff --git a/src/ft_error.c b/src/ft_error.c index 87be01e..0f2bc97 100644 --- a/src/ft_error.c +++ b/src/ft_error.c @@ -34,3 +34,12 @@ int errmsg); return (ft_exit(FT_RET_MAP_ERR, clist)); } + +int + ft_missing_error(const char *err, t_cub *clist) +{ + ft_dprintf(STDERR_FILENO, "Error\n"); + ft_dprintf(STDERR_FILENO, + "\033[1;31m%s %s\033[0m\n", FT_ERR_MISS_ELEMENT, err); + return (ft_exit(FT_ERR_MISSING, clist)); +} diff --git a/src/ft_exit.c b/src/ft_exit.c index 80fd734..78b6d0a 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -15,39 +15,52 @@ #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) +{ + uint8_t i; + + i = 0; + while (i < 8) + { + ft_memdel((void*)&clist->sprites[i]); + i++; + } + ft_memdel((void*)&clist->sprites); + ft_memdel((void*)&clist); +} static void ft_free_lists(t_cub *clist) { - ft_memdel((void**)&clist->mlist.filename); - ft_memdel((void**)&clist->mlist.no_tex_path); - ft_memdel((void**)&clist->mlist.so_tex_path); - ft_memdel((void**)&clist->mlist.ea_tex_path); - ft_memdel((void**)&clist->mlist.we_tex_path); - ft_memdel((void**)&clist->mlist.nl_tex_path); - ft_memdel((void**)&clist->mlist.fl_tex_path); - ft_memdel((void**)&clist->mlist.ce_tex_path); - ft_memdel((void**)&clist->mlist.nlevel_path); - ft_memdel((void**)&clist->mlist.skybox_path); - ft_memdel((void**)&clist->mlist.traps_path); - ft_memdel((void**)&clist->mlist.music_path); - ft_memdel((void**)&clist->mlist.music_cmd); - ft_memdel((void**)&clist->mlist.mapl); + ft_memdel((void*)&clist->mlist.filename); + ft_memdel((void*)&clist->mlist.no_tex_path); + ft_memdel((void*)&clist->mlist.so_tex_path); + ft_memdel((void*)&clist->mlist.ea_tex_path); + ft_memdel((void*)&clist->mlist.we_tex_path); + ft_memdel((void*)&clist->mlist.nl_tex_path); + ft_memdel((void*)&clist->mlist.fl_tex_path); + ft_memdel((void*)&clist->mlist.ce_tex_path); + ft_memdel((void*)&clist->mlist.nlevel_path); + ft_memdel((void*)&clist->mlist.skybox_path); + ft_memdel((void*)&clist->mlist.traps_path); + ft_memdel((void*)&clist->mlist.music_path); + 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); - ft_memdel((void**)&clist); + ft_memdel((void*)&clist->wlist.winptr); } static void @@ -71,29 +84,34 @@ static void mlx_destroy_image(clist->wlist.wlx, clist->tlist[7].img); if (clist->mlist.istraps && clist->tlist[15].img) mlx_destroy_image(clist->wlist.wlx, clist->tlist[15].img); + if (clist->tlist[16].img) + mlx_destroy_image(clist->wlist.wlx, clist->tlist[16].img); ft_del_extra_sprites(clist); } 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 @@ -106,10 +124,10 @@ int if (FT_OS == 1) mlx_destroy_image(clist->wlist.wlx, clist->img.img); 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_free_some_extras(clist); ft_printf("Exiting program\n"); exit(exit_code); return (0); diff --git a/src/ft_floor_cast.c b/src/ft_floor_cast.c index 92dcb07..160a2c1 100644 --- a/src/ft_floor_cast.c +++ b/src/ft_floor_cast.c @@ -25,30 +25,30 @@ static void { float dist; float calc; - t_rgb rgb; dist = (cl->rlist.row_dist > 0) ? (cl->rlist.row_dist) : (0.0001); calc = (dist * 0.1 * cl->mlist.darklvl); calc = (calc >= 255) ? (255) : (calc); calc = (calc < 1) ? (1) : (calc); calc = (cl->mlist.darklvl > 0) ? (calc) : (1); - rgb.r = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x * 4 + 4 * - cl->tlist[tid].img_h * cl->tlist[tid].tex_y + 2] / calc; - rgb.g = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x * 4 + 4 * - cl->tlist[tid].img_h * cl->tlist[tid].tex_y + 1] / calc; - rgb.b = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x * 4 + 4 * - cl->tlist[tid].img_h * cl->tlist[tid].tex_y] / calc; - *(int*)(cl->img.ptr + - (x * 4 + (y * cl->img.sizeline))) = ft_rgb_to_hex(rgb); + *(cl->img.ptr + x * 4 + (cl->img.sizeline * y)) = + (uint8_t)*(cl->tlist[tid].ptr + cl->tlist[tid].tex_x * 4 + 4 * + cl->tlist[tid].img_w * cl->tlist[tid].tex_y) / calc; + *(cl->img.ptr + x * 4 + (cl->img.sizeline * y) + 1) = + (uint8_t)*(cl->tlist[tid].ptr + cl->tlist[tid].tex_x * 4 + 4 * + cl->tlist[tid].img_w * cl->tlist[tid].tex_y + 1) / calc; + *(cl->img.ptr + x * 4 + (cl->img.sizeline * y) + 2) = + (uint8_t)*(cl->tlist[tid].ptr + cl->tlist[tid].tex_x * 4 + 4 * + cl->tlist[tid].img_w * cl->tlist[tid].tex_y + 2) / calc; } static void ft_set_tex_xy(uint8_t tid, t_ray *rl, t_cub *cl) { cl->tlist[tid].tex_y = (int32_t)(cl->tlist[tid].img_w - * (rl->x_floor - rl->x_cell)) % (cl->tlist[tid].img_w); + * (rl->x_floor - rl->x_cell)); cl->tlist[tid].tex_x = (int32_t)(cl->tlist[tid].img_h - * (rl->y_floor - rl->y_cell)) % (cl->tlist[tid].img_h); + * (rl->y_floor - rl->y_cell)); cl->tlist[tid].tex_x = (cl->tlist[tid].tex_x > 0) ? (cl->tlist[tid].tex_x) : (-cl->tlist[tid].tex_x); cl->tlist[tid].tex_y = (cl->tlist[tid].tex_y > 0) diff --git a/src/ft_free_words.c b/src/ft_free_words.c index a90b959..3ca01c8 100644 --- a/src/ft_free_words.c +++ b/src/ft_free_words.c @@ -20,10 +20,10 @@ uint8_t i = 0; while (i < 9) { - ft_memdel((void**)&sprites[i]); + ft_memdel((void*)&sprites[i]); i++; } - ft_memdel((void**)&sprites); + ft_memdel((void*)&sprites); return (0); } @@ -35,9 +35,9 @@ uint8_t i = 0; while (words[i]) { - ft_memdel((void**)&words[i]); + ft_memdel((void*)&words[i]); i++; } - ft_memdel((void**)&words); + ft_memdel((void*)&words); return (0); } diff --git a/src/ft_get_fps_count.c b/src/ft_get_fps_count.c new file mode 100644 index 0000000..16306c1 --- /dev/null +++ b/src/ft_get_fps_count.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_loop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/17 20:06:26 by rbousset #+# #+# */ +/* Updated: 2020/02/17 20:06:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stddef.h> +#include <stdint.h> +#include <time.h> + +static float + ft_clock_to_ms(clock_t ticks) +{ + return ((ticks / (float)CLOCKS_PER_SEC) * 1000.0); +} + +#include <stdio.h> + +void + ft_get_fps_count(clock_t dt, t_cub *cl) +{ + static clock_t curr_time = 0; + + curr_time += dt; + if (dt > 0 && ft_clock_to_ms(curr_time) > 1000.0) + { + ft_sprintf(cl->fps_count, "fps: %ld", (uint32_t)CLOCKS_PER_SEC / dt); + curr_time -= CLOCKS_PER_SEC; + } +} diff --git a/src/ft_get_map.c b/src/ft_get_map.c index 08c9734..8602ce3 100644 --- a/src/ft_get_map.c +++ b/src/ft_get_map.c @@ -58,22 +58,22 @@ int clist->mlist.map_start = clist->mlist.line_chk; if (!line[0]) { - ft_memdel((void**)&line); + ft_memdel((void*)&line); ft_sprintf(clist->errmsg, FT_ERR_READ); return (-1); } if (ft_check_map_line(line, 1, clist) < 0) { - ft_memdel((void**)&line); + ft_memdel((void*)&line); return (-1); } - ft_memdel((void**)&clist->mlist.mapl); + ft_memdel((void*)&clist->mlist.mapl); if (ft_cat_mapl(line, clist) < 0) { - ft_memdel((void**)&line); + ft_memdel((void*)&line); return (-1); } - ft_memdel((void**)&line); + ft_memdel((void*)&line); return (1); } @@ -92,17 +92,17 @@ int { if (!line[0]) ft_sprintf(clist->errmsg, FT_ERR_MAP_EMPL); - ft_memdel((void**)&line); + ft_memdel((void*)&line); return (-1); } - ft_memdel((void**)&line); + ft_memdel((void*)&line); } - ft_memdel((void**)&line); + ft_memdel((void*)&line); clist->mlist.mapl[clist->mlist.mapl_len - 1] = '\0'; ft_free_words(clist->mlist.map); clist->mlist.map = ft_split(clist->mlist.mapl, '\n'); clist->mlist.map_h = ft_get_map_h(clist->mlist.map); clist->mlist.map_w = ft_get_map_w(clist->mlist.map); - ft_memdel((void**)&clist->mlist.mapl); + ft_memdel((void*)&clist->mlist.mapl); return (0); } diff --git a/src/ft_get_music.c b/src/ft_get_music.c index 7a8d36f..cf105a3 100644 --- a/src/ft_get_music.c +++ b/src/ft_get_music.c @@ -22,14 +22,11 @@ static int8_t len = ft_strlen(mlist->music_path); len += ft_strlen(FT_SND_CMD) - 2; - ft_memdel((void**)&mlist->music_cmd); + ft_memdel((void*)&mlist->music_cmd); if (!(mlist->music_cmd = (char *)malloc((len + 1) * sizeof(char)))) 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); } @@ -46,7 +43,7 @@ int8_t ft_sprintf(clist->errmsg, FT_ERR_NOT_A_WAV); return (-1); } - ft_memdel((void**)&clist->mlist.music_path); + ft_memdel((void*)&clist->mlist.music_path); if (!(clist->mlist.music_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); diff --git a/src/ft_get_path_nl.c b/src/ft_get_path_nl.c index 68de5c6..38f5b36 100644 --- a/src/ft_get_path_nl.c +++ b/src/ft_get_path_nl.c @@ -27,7 +27,7 @@ int8_t ft_sprintf(clist->errmsg, FT_ERR_NOT_A_CUB); return (-1); } - ft_memdel((void**)&clist->mlist.nlevel_path); + ft_memdel((void*)&clist->mlist.nlevel_path); if (!(clist->mlist.nlevel_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); diff --git a/src/ft_get_skybox.c b/src/ft_get_skybox.c index d2f1f02..1e55b85 100644 --- a/src/ft_get_skybox.c +++ b/src/ft_get_skybox.c @@ -27,7 +27,7 @@ int8_t ft_sprintf(clist->errmsg, FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.skybox_path); + ft_memdel((void*)&clist->mlist.skybox_path); if (!(clist->mlist.skybox_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c index ed44d9b..5a5ecd0 100644 --- a/src/ft_get_sprite.c +++ b/src/ft_get_sprite.c @@ -53,7 +53,7 @@ static int8_t static int8_t ft_get_sprite_norme(int8_t id, char **words, t_cub *clist) { - ft_memdel((void**)&clist->mlist.sprite_path[id]); + ft_memdel((void*)&clist->mlist.sprite_path[id]); if (!(clist->mlist.sprite_path[id] = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); diff --git a/src/ft_get_sprite_spawns.c b/src/ft_get_sprite_spawns.c index 3a44f56..da883b2 100644 --- a/src/ft_get_sprite_spawns.c +++ b/src/ft_get_sprite_spawns.c @@ -19,7 +19,7 @@ void ft_get_next_sprite(t_cub *clist, int s_n, char c, size_t x) { size_t y; - uint8_t i; + int16_t i; y = 0; i = 0; @@ -37,7 +37,8 @@ void } x = 0; } - if (clist->sprites[s_n][i - 1].s_pos_x != 0) + if (clist->sprites[s_n][(i - 1 < 0) ? (0) : (i - 1)].s_pos_x != 0 + && s_n + 1 < 7) { ft_get_next_sprite(clist, s_n + 1, c + 1, 0); } diff --git a/src/ft_get_tex.c b/src/ft_get_tex.c index f8a817c..21096eb 100644 --- a/src/ft_get_tex.c +++ b/src/ft_get_tex.c @@ -27,7 +27,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.no_tex_path); + ft_memdel((void*)&clist->mlist.no_tex_path); if (!(clist->mlist.no_tex_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); @@ -54,7 +54,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.so_tex_path); + ft_memdel((void*)&clist->mlist.so_tex_path); if (!(clist->mlist.so_tex_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); @@ -81,7 +81,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.ea_tex_path); + ft_memdel((void*)&clist->mlist.ea_tex_path); if (!(clist->mlist.ea_tex_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); @@ -108,7 +108,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.we_tex_path); + ft_memdel((void*)&clist->mlist.we_tex_path); if (!(clist->mlist.we_tex_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); diff --git a/src/ft_get_tex_extra.c b/src/ft_get_tex_extra.c index 25f9c23..77f053b 100644 --- a/src/ft_get_tex_extra.c +++ b/src/ft_get_tex_extra.c @@ -26,7 +26,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.fl_tex_path); + ft_memdel((void*)&clist->mlist.fl_tex_path); if (!(clist->mlist.fl_tex_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); @@ -54,7 +54,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.ce_tex_path); + ft_memdel((void*)&clist->mlist.ce_tex_path); if (!(clist->mlist.ce_tex_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); diff --git a/src/ft_get_tex_nl.c b/src/ft_get_tex_nl.c index bb071f7..0931df1 100644 --- a/src/ft_get_tex_nl.c +++ b/src/ft_get_tex_nl.c @@ -27,7 +27,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.nl_tex_path); + ft_memdel((void*)&clist->mlist.nl_tex_path); if (!(clist->mlist.nl_tex_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); diff --git a/src/ft_get_traps.c b/src/ft_get_traps.c index 0235bb5..b7d12e1 100644 --- a/src/ft_get_traps.c +++ b/src/ft_get_traps.c @@ -55,7 +55,7 @@ int8_t ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist.traps_path); + ft_memdel((void*)&clist->mlist.traps_path); if (!(clist->mlist.traps_path = ft_strdup(*(words + 1)))) { ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); diff --git a/src/ft_hooks_and_loops.c b/src/ft_hooks_and_loops.c index b8bdd36..9293496 100644 --- a/src/ft_hooks_and_loops.c +++ b/src/ft_hooks_and_loops.c @@ -15,7 +15,7 @@ #include <mlx.h> void - ft_hooks_and_loops(t_win *wl, t_cub *cl) +ft_hooks_and_loops(t_win *wl, t_cub *cl) { mlx_hook(wl->winptr, 2, (1L << 0), ft_key_event, cl); mlx_hook(wl->winptr, 3, (1L << 1), ft_key_release, cl); diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c index 3e4dc96..99ebd65 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -11,7 +11,6 @@ /* ************************************************************************** */ #include <libft.h> -#include <mlx.h> #include <cub3d.h> #include <stddef.h> #include <stdlib.h> @@ -72,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; @@ -85,22 +84,18 @@ static int8_t cl->walltexgood = 0; ft_init_funptr(cl); ft_init_ref(cl); + if (ft_init_sprites(&cl->sprites) < 0) + return (-1); return (0); } int8_t ft_init_cub3d(t_cub *clist) { - uint8_t i; if (ft_init_cub(clist) < 0) return (-1); if (ft_init_win(&clist->wlist) < 0) return (-1); - i = 0; - while (i < 9) - { - ft_bzero(clist->sprites[i], 4096); - i++; - } + ft_sprintf(clist->fps_count, "fps: 60"); return (0); } diff --git a/src/ft_init_map.c b/src/ft_init_map.c index 813a795..8b97906 100644 --- a/src/ft_init_map.c +++ b/src/ft_init_map.c @@ -34,15 +34,13 @@ 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); } static int8_t - ft_calloc_sprites(t_map *mlist) + ft_calloc_sprites_path(t_map *mlist) { uint8_t i; @@ -75,8 +73,7 @@ int8_t if (ft_init_map_callocs(mlist) < 0) return (-1); mlist->map[1] = 0; - mlist->mcmd_words[1] = 0; - if (ft_calloc_sprites(mlist) < 0) + if (ft_calloc_sprites_path(mlist) < 0) return (-1); mlist->map_w = 0; mlist->map_h = 0; diff --git a/src/ft_init_sfx.c b/src/ft_init_sfx.c index 9049250..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_init_sprites.c b/src/ft_init_sprites.c new file mode 100644 index 0000000..4fc9aed --- /dev/null +++ b/src/ft_init_sprites.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_init_lists.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:53 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:53 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stddef.h> +#include <stdlib.h> +#include <stdint.h> + +int8_t + ft_init_sprites(t_sprite ***sprites) +{ + uint8_t i; + + if (!(*sprites = (t_sprite**)ft_calloc(8, sizeof(t_sprite*)))) + return (-1); + i = 0; + while (i < 8) + { + if (!(*((*sprites) + i) = (t_sprite*)ft_calloc(4096, sizeof(t_sprite)))) + return (-1); + i++; + } + return (0); +} diff --git a/src/ft_init_winlx.c b/src/ft_init_winlx.c index dee4009..59d0b58 100644 --- a/src/ft_init_winlx.c +++ b/src/ft_init_winlx.c @@ -26,7 +26,7 @@ int int ft_init_winptr(t_cub *clist) { - ft_memdel((void**)&clist->wlist.winptr); + ft_memdel((void*)&clist->wlist.winptr); if (!(clist->wlist.winptr = mlx_new_window(clist->wlist.wlx, clist->wlist.x_size, clist->wlist.y_size, "Cub3D"))) return (-1); diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index c16655e..0108202 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -13,8 +13,9 @@ #include <libft.h> #include <cub3d.h> #include <mlx.h> -#include <stdint.h> #include <stddef.h> +#include <stdint.h> +#include <time.h> static uint64_t ft_find_x(int32_t key, const t_player *pl) @@ -77,25 +78,31 @@ int uint8_t i; const float old_y = cl->plist.pos_y; const float old_x = cl->plist.pos_x; + clock_t begin_frame; + begin_frame = clock(); i = 0; 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); + ft_get_fps_count(clock() - begin_frame, cl); return (0); } diff --git a/src/ft_music.c b/src/ft_music.c index 234f86e..7a2d800 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -11,21 +11,29 @@ /* ************************************************************************** */ #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) + if (system(cl->mlist.music_cmd) != 0) + pthread_exit(NULL); + 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_parse_map.c b/src/ft_parse_map.c index f1df73d..79c00d2 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -42,7 +42,7 @@ static void static int8_t ft_error_here(const char *errmsg, char *line, t_cub *clist) { - ft_memdel((void**)&line); + ft_memdel((void*)&line); return (ft_map_error(errmsg, clist)); } @@ -60,7 +60,7 @@ static int8_t return (ft_error_here(FT_ERR_UNFINISHED, line, clist)); if (!line[0]) { - ft_memdel((void**)&line); + ft_memdel((void*)&line); return (ft_parse_it(fd, clist)); } if (ft_ischarset("1 ", line[0])) @@ -70,14 +70,14 @@ static int8_t return (ft_error_here(FT_ERR_ILL_ENTRY, line, clist)); if ((ret = ft_select_get(words, clist)) == FT_PARSE_END_RET) return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (25)); - ft_memdel((void**)&line); + ft_memdel((void*)&line); return (ret); } void ft_save_name(const char *map_path, t_cub *clist) { - ft_memdel((void**)&clist->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); diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 743dc36..aa050c5 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -107,6 +107,6 @@ void ft_floor_cast(cl); ft_calc_sprite(cl); ft_calc_trap(cl); - ft_memdel((void**)&cl->rlist.wall_dist_tab); - ft_memdel((void**)&cl->rlist.wall_bz); + ft_memdel((void*)&cl->rlist.wall_dist_tab); + ft_memdel((void*)&cl->rlist.wall_bz); } diff --git a/src/ft_save_to_bmp.c b/src/ft_save_to_bmp.c index ae3a2b6..1737226 100644 --- a/src/ft_save_to_bmp.c +++ b/src/ft_save_to_bmp.c @@ -111,16 +111,16 @@ int8_t return (-1); if (!(bmp = ft_convert_image_to_bmp(cl->img, cl))) { - ft_memdel((void**)&fname); + ft_memdel((void*)&fname); return (-1); } if (ft_write_bmp(fname, cl->wlist.y_size, cl->wlist.x_size, bmp) < 0) { - ft_memdel((void**)&bmp); - ft_memdel((void**)&fname); + ft_memdel((void*)&bmp); + ft_memdel((void*)&fname); return (-1); } - ft_memdel((void**)&bmp); - ft_memdel((void**)&fname); + ft_memdel((void*)&bmp); + ft_memdel((void*)&fname); return (0); } diff --git a/src/ft_sfx_death.c b/src/ft_sfx_death.c index 2566358..32ad6fe 100644 --- a/src/ft_sfx_death.c +++ b/src/ft_sfx_death.c @@ -11,14 +11,31 @@ /* ************************************************************************** */ #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); + if (system(sfx->death)) + pthread_exit(NULL); } + 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..c0c9ffe --- /dev/null +++ b/src/ft_sfx_footstep.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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) + { + if (system(sfx->footstep_one)) + pthread_exit(NULL); + } + else if (system(sfx->footstep_two)) + pthread_exit(NULL); + } + 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..2cde15c 100644 --- a/src/ft_sfx_new_level.c +++ b/src/ft_sfx_new_level.c @@ -11,14 +11,31 @@ /* ************************************************************************** */ #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); + if (system(sfx->new_lvl)) + pthread_exit(NULL); } + 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..68340c5 --- /dev/null +++ b/src/ft_sfx_pain.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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) + { + if (system(sfx->pain_one)) + pthread_exit(NULL); + } + else if (system(sfx->pain_two)) + pthread_exit(NULL); + } + 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..a395450 100644 --- a/src/ft_sfx_trap.c +++ b/src/ft_sfx_trap.c @@ -10,40 +10,32 @@ /* */ /* ************************************************************************** */ -#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); + if (system(sfx->trap)) + pthread_exit(NULL); } + 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_tex_init.c b/src/ft_tex_init.c index 298fbfc..fe8aadd 100644 --- a/src/ft_tex_init.c +++ b/src/ft_tex_init.c @@ -35,6 +35,7 @@ ** 13 : sprite 8 ** 14 : sprite 9 ** 15 : trap +** 16 : HUD back */ static void @@ -72,6 +73,10 @@ static void cl->tlist[7].ptr = mlx_get_data_addr(cl->tlist[7].img, &cl->tlist[7].bpp, &cl->tlist[7].sizeline, &cl->tlist[7].endian); } + cl->tlist[16].img = mlx_xpm_file_to_image(cl->wlist.wlx, + FT_HUD_BACK_PATH, &cl->tlist[16].img_w, &cl->tlist[16].img_h); + cl->tlist[16].ptr = mlx_get_data_addr(cl->tlist[16].img, + &cl->tlist[16].bpp, &cl->tlist[16].sizeline, &cl->tlist[16].endian); cl->walltexgood = 1; if (cl->mlist.sprite_var > 1) { diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index 56d4d67..fadbb2a 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -23,41 +23,36 @@ 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); - ft_memdel((void**)&ml->we_tex_path); - ft_memdel((void**)&ml->nl_tex_path); - ft_memdel((void**)&ml->fl_tex_path); - ft_memdel((void**)&ml->ce_tex_path); - ft_memdel((void**)&ml->nlevel_path); - ft_memdel((void**)&ml->skybox_path); - ft_memdel((void**)&ml->traps_path); - ft_memdel((void**)&ml->music_path); - ft_memdel((void**)&ml->music_cmd); - ft_memdel((void**)&ml->mapl); + 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); + ft_memdel((void*)&ml->we_tex_path); + ft_memdel((void*)&ml->nl_tex_path); + ft_memdel((void*)&ml->fl_tex_path); + ft_memdel((void*)&ml->ce_tex_path); + ft_memdel((void*)&ml->nlevel_path); + ft_memdel((void*)&ml->skybox_path); + ft_memdel((void*)&ml->traps_path); + ft_memdel((void*)&ml->music_path); + ft_memdel((void*)&ml->music_cmd); + ft_memdel((void*)&ml->mapl); ft_free_sprites(ml->sprite_path); ft_free_words(ml->map); - ft_free_words(ml->mcmd_words); } static void ft_del_some(t_cub *cl) { - uint8_t i; + int8_t i; cl->plist = ft_init_player(); cl->f_rgb = ft_init_rgb(); cl->c_rgb = ft_init_rgb(); cl->rlist = ft_init_s_ray(); - i = 0; - while (i < 5) - { + i = -1; + while (++i < 5) mlx_destroy_image(cl->wlist.wlx, cl->tlist[i].img); - cl->tlist[i].ptr = NULL; - i++; - } if (cl->mlist.isnlvl && cl->tlist[5].img) mlx_destroy_image(cl->wlist.wlx, cl->tlist[5].img); if (cl->mlist.isftex && cl->tlist[6].img) @@ -67,6 +62,10 @@ static void if (cl->mlist.istraps && cl->tlist[15].img) mlx_destroy_image(cl->wlist.wlx, cl->tlist[15].img); ft_del_extra_sprites(cl); + i = -1; + while (++i < 8) + ft_memdel((void*)&cl->sprites[i]); + ft_memdel((void*)&cl->sprites); } 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 @@ -104,13 +103,15 @@ int8_t tmp_mup = ft_strdup(cl->mlist.music_path); ft_del_some(cl); ft_del_map(&cl->mlist); + if (ft_init_sprites(&cl->sprites) < 0) + return (-1); if (ft_init_map(&cl->mlist) < 0) return (-1); ft_parse_map(next_path, cl); ft_treat_music(isoldmus, tmp_mup, cl); if (isoldmus) - ft_memdel((void**)&tmp_mup); + ft_memdel((void*)&tmp_mup); ft_wall_tex_init(cl); - ft_memdel((void**)&next_path); + ft_memdel((void*)&next_path); return (0); } @@ -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[]) |