added debounce time

This commit is contained in:
asus
2024-01-13 14:38:02 +01:00
parent dabb4acf7a
commit a2f5f90fce
9 changed files with 80 additions and 37 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
fdf

Binary file not shown.

View File

@@ -3,39 +3,42 @@
# include "../libft/includes/libft.h"
# include <mlx.h>
# include <math.h> // for M_PI
# include <fcntl.h> // for open
# include <math.h> // for M_PI
# include <fcntl.h> // for open
# include <time.h> // for time
# include <sys/time.h> // for gettimeofday
typedef struct s_fdf
{
void *mlx_ptr;
void *win_ptr;
void *img_ptr;
char *img_addr;
int **map;
int offset;
int min_z;
int max_z;
int z_amplitude;
int altitude;
int win_size_x;
int win_size_y;
int img_size_x;
int img_size_y;
int map_size_x;
int map_size_y;
int map_width;
int map_height;
int rot_x;
int rot_y;
double rad_x;
double rad_y;
int mov_x;
int mov_y;
double zoom;
int img_bpp;
int img_sizel;
int img_endian;
void *mlx_ptr;
void *win_ptr;
void *img_ptr;
char *img_addr;
int **map;
int offset;
int min_z;
int max_z;
int z_amplitude;
int altitude;
int win_size_x;
int win_size_y;
int img_size_x;
int img_size_y;
int map_size_x;
int map_size_y;
int map_width;
int map_height;
int rot_x;
int rot_y;
double rad_x;
double rad_y;
int mov_x;
int mov_y;
double zoom;
int img_bpp;
int img_sizel;
int img_endian;
struct timeval last_keypress_time;
} t_fdf;
// fdf.c
@@ -71,16 +74,17 @@ int **parse_map(t_fdf *fdf, int fd);
// steps size for the hight transform with p and o
// must be > 0
// 100 is small, 1 is the biggest
# define Z_HEIGHT 10
# define Z_HEIGHT 50
// color for altitude
# define COLOR_START 0xffffff
# define COLOR_END 0x00d700
// R,G,B colors
// ex for col = 0x236ad0
# define COLOR_R(col) ((col & 0xff0000) >> 16) // output 23
# define COLOR_G(col) ((col & 0x00ff00) >> 8) // output 6a
# define COLOR_B(col) (col & 0x0000ff) // output d0
/*
# define COLOR_END 0x1423e6 // blue
*/
# define COLOR_END 0x00d700 // green
// minimum time in milliseconds between two keypress
# define DEBOUNCE_TIME 60
# define ESCAPE 65307
# define UP 65362

View File

@@ -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(&current_time, NULL);
fdf->last_keypress_time = current_time;
init_server(fdf);
draw_image(fdf);
}

View File

@@ -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(&current_time, NULL);
if (should_ignore_keypress(&current_time, fdf))
return (0);
if (keycode == ESCAPE)
shut_down(fdf);
else if (keycode == LEFT)