aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_get_fps_count.c
blob: f7f0177f0dd2922be7de24c5471adbcaad22c4ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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);
}

static void
	ft_handle_firing(clock_t dt, t_cub *cl)
{
	static clock_t	curr_time = 0;

	curr_time += dt;
	if (dt > 0 && ft_clock_to_ms(curr_time) > 300.0)
	{
		cl->plist.fire = 0;
		curr_time -= CLOCKS_PER_SEC;
	}
}

void
	ft_get_fps_count(clock_t dt, t_cub *cl)
{
	static clock_t	curr_time = 0;
	static uint16_t	ticks = 0;

	curr_time += dt;
	ticks += 1;
	if (cl->plist.fire == 1)
		ft_handle_firing(dt, cl);
	if (dt > 0 && ft_clock_to_ms(curr_time) > 1000.0)
	{
		ft_sprintf(cl->fps_count, "fps: %hu", ticks);
		ticks = 0;
		curr_time -= CLOCKS_PER_SEC;
	}
}