aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ft_draw_scene.c17
-rw-r--r--src/ft_get_fps_count.c38
-rw-r--r--src/ft_hooks_and_loops.c2
-rw-r--r--src/ft_key_loop.c6
-rw-r--r--src/ft_music.c3
-rw-r--r--src/ft_sfx_death.c3
-rw-r--r--src/ft_sfx_footstep.c9
-rw-r--r--src/ft_sfx_new_level.c3
-rw-r--r--src/ft_sfx_pain.c9
-rw-r--r--src/ft_sfx_trap.c3
10 files changed, 79 insertions, 14 deletions
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);
}