feat: Added delta time variable

This commit is contained in:
Theo Champion 2025-07-31 14:07:56 +02:00
parent 02940e9ecf
commit 2bc214103b
6 changed files with 53 additions and 3 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <bits/types/struct_timeval.h>
#include <stdbool.h>
#include <X11/keysym.h>
#include <X11/X.h>
#include <unistd.h>
#include <fcntl.h>
#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);
}

View file

@ -6,11 +6,12 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:26:53 by tchampio #+# #+# */
/* Updated: 2025/07/31 13:28:03 by tchampio ### ########.fr */
/* Updated: 2025/07/31 14:01:39 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "time.h"
#include "../../libft/includes/libft.h"
#include "../../mlx/mlx.h"
#include "../map/map_checker.h"
@ -35,4 +36,5 @@ void init_cub3d_data(t_cub3d_data *data, char **argv)
&data->img_data->endian);
init_player(&data->player, data->map);
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
data->delta = get_milliseconds();
}

23
src/utils/time.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* time.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:51:49 by tchampio #+# #+# */
/* Updated: 2025/07/31 14:12:48 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "time.h"
#include <sys/time.h>
#include <stdlib.h>
int get_milliseconds(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_usec / 1000);
}

18
src/utils/time.h Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* time.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:53:44 by tchampio #+# #+# */
/* Updated: 2025/07/31 13:54:08 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TIME_H
# define TIME_H
int get_milliseconds(void);
#endif