diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-17 17:15:32 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-17 17:15:32 +0100 |
commit | 05ad2085ab6ce4ffb280b21ce3f321b2bd75aa9e (patch) | |
tree | c6c521c8f2ad8df4477005b8225427b7459daf32 | |
parent | Merge branch 'back-to-pthread' (diff) | |
download | 42-cub3d-05ad2085ab6ce4ffb280b21ce3f321b2bd75aa9e.tar.gz 42-cub3d-05ad2085ab6ce4ffb280b21ce3f321b2bd75aa9e.tar.bz2 42-cub3d-05ad2085ab6ce4ffb280b21ce3f321b2bd75aa9e.tar.xz 42-cub3d-05ad2085ab6ce4ffb280b21ce3f321b2bd75aa9e.tar.zst 42-cub3d-05ad2085ab6ce4ffb280b21ce3f321b2bd75aa9e.zip |
fps counter is bav
Diffstat (limited to '')
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | inc/cub3d.h | 2 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 1 | ||||
-rw-r--r-- | map/map_one.cub | 2 | ||||
-rw-r--r-- | src/ft_draw_scene.c | 17 | ||||
-rw-r--r-- | src/ft_get_fps_count.c | 38 | ||||
-rw-r--r-- | src/ft_hooks_and_loops.c | 2 | ||||
-rw-r--r-- | src/ft_key_loop.c | 6 | ||||
-rw-r--r-- | src/ft_music.c | 3 | ||||
-rw-r--r-- | src/ft_sfx_death.c | 3 | ||||
-rw-r--r-- | src/ft_sfx_footstep.c | 9 | ||||
-rw-r--r-- | src/ft_sfx_new_level.c | 3 | ||||
-rw-r--r-- | src/ft_sfx_pain.c | 9 | ||||
-rw-r--r-- | src/ft_sfx_trap.c | 3 |
14 files changed, 85 insertions, 15 deletions
@@ -100,6 +100,7 @@ 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}) #--------------------------------------------------------------------------------------------------# @@ -140,6 +141,7 @@ CFLAGS += -Wall CFLAGS += -Wextra CFLAGS += -Werror CFLAGS += -pedantic +CFLAGS += -march=ivybridge -O3 -pipe #--------------------------------------------------------------------------------------------------# ifdef DEBUG CFLAGS += ${DBG} diff --git a/inc/cub3d.h b/inc/cub3d.h index 5371305..61c4f01 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 ====== @@ -179,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_structs.h b/inc/cub3d_structs.h index 92d928d..9c61907 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -237,6 +237,7 @@ typedef struct s_cub char *const *envp; char errmsg[64]; int32_t key_input[5]; + char fps_count[9]; pthread_t mtid; int (*key_ptr[6])(struct s_cub*); int8_t (*get_ptr[14])(char**, struct s_cub*); diff --git a/map/map_one.cub b/map/map_one.cub index 3dad2cc..6123a8d 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -1,4 +1,4 @@ -R 1400 900 +R 1600 1000 NO ./media/img/wood_wall_1.xpm SO ./media/img/wood_wall_1.xpm diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index c7b07a1..86009f6 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -16,6 +16,16 @@ #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) { @@ -61,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_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_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_key_loop.c b/src/ft_key_loop.c index 74ff775..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,7 +78,9 @@ 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) { @@ -100,5 +103,6 @@ int i++; } 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 1a3da63..7a2d800 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -26,7 +26,8 @@ static void pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); cl = (t_cub *)vargp; while (1) - system(cl->mlist.music_cmd); + if (system(cl->mlist.music_cmd) != 0) + pthread_exit(NULL); return(NULL); } diff --git a/src/ft_sfx_death.c b/src/ft_sfx_death.c index 1680f2d..32ad6fe 100644 --- a/src/ft_sfx_death.c +++ b/src/ft_sfx_death.c @@ -28,7 +28,8 @@ void while (1) { pthread_mutex_lock(&sfx->death_mutex); - system(sfx->death); + if (system(sfx->death)) + pthread_exit(NULL); } return (NULL); } diff --git a/src/ft_sfx_footstep.c b/src/ft_sfx_footstep.c index a2eef1d..c0c9ffe 100644 --- a/src/ft_sfx_footstep.c +++ b/src/ft_sfx_footstep.c @@ -31,9 +31,12 @@ void ref = (ref > 201) ? (0) : (ref + 1); pthread_mutex_lock(&sfx->footstep_mutex); if (ref % 2) - system(sfx->footstep_one); - else - system(sfx->footstep_two); + { + if (system(sfx->footstep_one)) + pthread_exit(NULL); + } + else if (system(sfx->footstep_two)) + pthread_exit(NULL); } return (NULL); } diff --git a/src/ft_sfx_new_level.c b/src/ft_sfx_new_level.c index ec648e0..2cde15c 100644 --- a/src/ft_sfx_new_level.c +++ b/src/ft_sfx_new_level.c @@ -28,7 +28,8 @@ void while (1) { pthread_mutex_lock(&sfx->new_lvl_mutex); - system(sfx->new_lvl); + if (system(sfx->new_lvl)) + pthread_exit(NULL); } return (NULL); } diff --git a/src/ft_sfx_pain.c b/src/ft_sfx_pain.c index cb68108..68340c5 100644 --- a/src/ft_sfx_pain.c +++ b/src/ft_sfx_pain.c @@ -31,9 +31,12 @@ void pthread_mutex_lock(&sfx->pain_mutex); ref = (ref > 201) ? (0) : (ref + 1); if (ref % 3) - system(sfx->pain_one); - else - system(sfx->pain_two); + { + if (system(sfx->pain_one)) + pthread_exit(NULL); + } + else if (system(sfx->pain_two)) + pthread_exit(NULL); } return (NULL); } diff --git a/src/ft_sfx_trap.c b/src/ft_sfx_trap.c index d55dd57..a395450 100644 --- a/src/ft_sfx_trap.c +++ b/src/ft_sfx_trap.c @@ -28,7 +28,8 @@ void while (1) { pthread_mutex_lock(&sfx->trap_mutex); - system(sfx->trap); + if (system(sfx->trap)) + pthread_exit(NULL); } return (NULL); } |