added debounce time
This commit is contained in:
@@ -53,6 +53,8 @@ void init_offset(t_fdf *fdf)
|
||||
|
||||
void init_fdf(t_fdf *fdf)
|
||||
{
|
||||
struct timeval current_time;
|
||||
|
||||
fdf->win_size_x = 700;
|
||||
fdf->win_size_y = 700;
|
||||
fdf->img_size_x = fdf->win_size_x;
|
||||
@@ -71,6 +73,8 @@ void init_fdf(t_fdf *fdf)
|
||||
fdf->mov_x = (fdf->win_size_x - fdf->map_size_x) / 2;
|
||||
fdf->mov_y = (fdf->win_size_y - fdf->map_size_y) / 2;
|
||||
fdf->zoom = 0;
|
||||
gettimeofday(¤t_time, NULL);
|
||||
fdf->last_keypress_time = current_time;
|
||||
init_server(fdf);
|
||||
draw_image(fdf);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,36 @@ void keypress_more(int keycode, t_fdf *fdf)
|
||||
fdf->altitude--;
|
||||
}
|
||||
|
||||
int should_ignore_keypress(const struct timeval *current_time, t_fdf *fdf)
|
||||
{
|
||||
int is_less;
|
||||
unsigned long current_milliseconds;
|
||||
unsigned long last_milliseconds;
|
||||
|
||||
current_milliseconds = (current_time->tv_sec % 1000) * 1000 + current_time->tv_usec / 1000;
|
||||
last_milliseconds = (fdf->last_keypress_time.tv_sec % 1000) * 1000 + fdf->last_keypress_time.tv_usec / 1000;
|
||||
is_less = (current_milliseconds - last_milliseconds) < DEBOUNCE_TIME;
|
||||
|
||||
/*
|
||||
[1 705 151 587 ,905 277,49579540-1000000]
|
||||
1 705 151 587 000
|
||||
1 705 151 587 905
|
||||
1 705 151 587 % 1000 = 587
|
||||
587 * 1000 + 905 277 / 1000 = 587 000 + 905 = 587905
|
||||
|
||||
[836 763,836 066]
|
||||
ft_putchar('[');
|
||||
ft_putnbr(current_milliseconds);
|
||||
ft_putchar(',');
|
||||
ft_putnbr(last_milliseconds);
|
||||
ft_putchar(']');
|
||||
*/
|
||||
|
||||
if (!is_less)
|
||||
fdf->last_keypress_time = *current_time;
|
||||
return is_less;
|
||||
}
|
||||
|
||||
/*
|
||||
** Q -> move left
|
||||
** D -> move right
|
||||
@@ -44,6 +74,11 @@ void keypress_more(int keycode, t_fdf *fdf)
|
||||
*/
|
||||
int keypress(int keycode, t_fdf *fdf)
|
||||
{
|
||||
struct timeval current_time;
|
||||
|
||||
gettimeofday(¤t_time, NULL);
|
||||
if (should_ignore_keypress(¤t_time, fdf))
|
||||
return (0);
|
||||
if (keycode == ESCAPE)
|
||||
shut_down(fdf);
|
||||
else if (keycode == LEFT)
|
||||
|
||||
Reference in New Issue
Block a user