mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
feat: Added delta time variable
This commit is contained in:
parent
02940e9ecf
commit
2bc214103b
6 changed files with 53 additions and 3 deletions
1
Makefile
1
Makefile
|
|
@ -10,6 +10,7 @@ IFLAGS = -I./mlx -I./libft
|
||||||
|
|
||||||
SOURCEFILES = \
|
SOURCEFILES = \
|
||||||
src/utils/inits.c \
|
src/utils/inits.c \
|
||||||
|
src/utils/time.c \
|
||||||
src/draw/draw_map.c \
|
src/draw/draw_map.c \
|
||||||
src/draw/drawutils.c \
|
src/draw/drawutils.c \
|
||||||
src/main.c \
|
src/main.c \
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:59:37 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_player player;
|
||||||
t_keypresses keypresses;
|
t_keypresses keypresses;
|
||||||
int *screen_matrix;
|
int *screen_matrix;
|
||||||
|
int delta;
|
||||||
|
int last_tick;
|
||||||
} t_cub3d_data;
|
} t_cub3d_data;
|
||||||
|
|
||||||
#endif // CUB3D_DATA_H
|
#endif // CUB3D_DATA_H
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:14:30 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 "raycast/ray.h"
|
||||||
#include "utils/hooks.h"
|
#include "utils/hooks.h"
|
||||||
#include "utils/inits.h"
|
#include "utils/inits.h"
|
||||||
|
#include <bits/types/struct_timeval.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <X11/keysym.h>
|
#include <X11/keysym.h>
|
||||||
#include <X11/X.h>
|
#include <X11/X.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include "utils/time.h"
|
||||||
|
|
||||||
int game_loop(t_cub3d_data *data)
|
int game_loop(t_cub3d_data *data)
|
||||||
{
|
{
|
||||||
t_ray ray;
|
t_ray ray;
|
||||||
|
|
||||||
|
data->delta = (get_milliseconds() - data->last_tick);
|
||||||
mlx_destroy_image(data->mlx, data->img_data->img);
|
mlx_destroy_image(data->mlx, data->img_data->img);
|
||||||
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
|
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
|
||||||
reset_matrix(data);
|
reset_matrix(data);
|
||||||
|
|
@ -41,6 +44,7 @@ int game_loop(t_cub3d_data *data)
|
||||||
mlx_put_image_to_window(data->mlx, data->mlx_win,
|
mlx_put_image_to_window(data->mlx, data->mlx_win,
|
||||||
data->img_data->img, 0, 0);
|
data->img_data->img, 0, 0);
|
||||||
mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT);
|
mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT);
|
||||||
|
data->last_tick = get_milliseconds();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,12 @@
|
||||||
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/31 13:26:53 by tchampio #+# #+# */
|
/* 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 "../cub3d_data.h"
|
||||||
|
#include "time.h"
|
||||||
#include "../../libft/includes/libft.h"
|
#include "../../libft/includes/libft.h"
|
||||||
#include "../../mlx/mlx.h"
|
#include "../../mlx/mlx.h"
|
||||||
#include "../map/map_checker.h"
|
#include "../map/map_checker.h"
|
||||||
|
|
@ -35,4 +36,5 @@ void init_cub3d_data(t_cub3d_data *data, char **argv)
|
||||||
&data->img_data->endian);
|
&data->img_data->endian);
|
||||||
init_player(&data->player, data->map);
|
init_player(&data->player, data->map);
|
||||||
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
|
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
|
||||||
|
data->delta = get_milliseconds();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
23
src/utils/time.c
Normal file
23
src/utils/time.c
Normal 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
18
src/utils/time.h
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue