From 2bc214103bfb1479254b7f2287ec66d842195dc9 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Thu, 31 Jul 2025 14:07:56 +0200 Subject: [PATCH] feat: Added delta time variable --- Makefile | 1 + src/cub3d_data.h | 4 +++- src/main.c | 6 +++++- src/utils/inits.c | 4 +++- src/utils/time.c | 23 +++++++++++++++++++++++ src/utils/time.h | 18 ++++++++++++++++++ 6 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/utils/time.c create mode 100644 src/utils/time.h diff --git a/Makefile b/Makefile index f1539c3..1d4dcb0 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ IFLAGS = -I./mlx -I./libft SOURCEFILES = \ src/utils/inits.c \ + src/utils/time.c \ src/draw/draw_map.c \ src/draw/drawutils.c \ src/main.c \ diff --git a/src/cub3d_data.h b/src/cub3d_data.h index 83c6678..4cdc95f 100644 --- a/src/cub3d_data.h +++ b/src/cub3d_data.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ -/* Updated: 2025/07/31 13:23:32 by kcolin ### ########.fr */ +/* Updated: 2025/07/31 13:55:08 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,8 @@ typedef struct s_cub3d_data t_player player; t_keypresses keypresses; int *screen_matrix; + int delta; + int last_tick; } t_cub3d_data; #endif // CUB3D_DATA_H diff --git a/src/main.c b/src/main.c index 1ca5d62..91c4a01 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/31 13:30:05 by tchampio ### ########.fr */ +/* Updated: 2025/07/31 13:57:08 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,16 +21,19 @@ #include "raycast/ray.h" #include "utils/hooks.h" #include "utils/inits.h" +#include #include #include #include #include #include +#include "utils/time.h" int game_loop(t_cub3d_data *data) { t_ray ray; + data->delta = (get_milliseconds() - data->last_tick); mlx_destroy_image(data->mlx, data->img_data->img); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); reset_matrix(data); @@ -41,6 +44,7 @@ int game_loop(t_cub3d_data *data) mlx_put_image_to_window(data->mlx, data->mlx_win, data->img_data->img, 0, 0); mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT); + data->last_tick = get_milliseconds(); return (0); } diff --git a/src/utils/inits.c b/src/utils/inits.c index 694e161..48343e5 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -6,11 +6,12 @@ /* By: tchampio img_data->endian); init_player(&data->player, data->map); data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT); + data->delta = get_milliseconds(); } diff --git a/src/utils/time.c b/src/utils/time.c new file mode 100644 index 0000000..9632e39 --- /dev/null +++ b/src/utils/time.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* time.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#include + +int get_milliseconds(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return (tv.tv_usec / 1000); +} diff --git a/src/utils/time.h b/src/utils/time.h new file mode 100644 index 0000000..ab7e8f3 --- /dev/null +++ b/src/utils/time.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* time.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio