feat: add timedloop()

This commit is contained in:
mcolonna 2024-03-26 15:55:11 +01:00
parent 78a6bbbb68
commit 4bd12ffcf1
6 changed files with 78 additions and 4 deletions

View file

@ -2,7 +2,7 @@
NAME = so_long
SRCS = src/
INCLUDES = includes/
CODE = main error env input \
CODE = main error env input timedloop \
data_assets data_sprites \
display1 display2 display_utils sprite \
room room_utils object

View file

@ -1,3 +1,5 @@
/!\ NEEDS UPDATE
data\_assets
- `NB_ASSETS`
- `t_assetinfo`

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */
/* Updated: 2024/03/26 13:20:06 by mcolonna ### ########.fr */
/* Updated: 2024/03/26 15:48:24 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
# define WINDOW_WIDTH 600
# define WINDOW_HEIGHT 400
# define WINDOW_TITLE "undretale"
# define FPS 10
typedef struct s_env
{

18
includes/timedloop.h Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* timedloop.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 15:02:16 by mcolonna #+# #+# */
/* Updated: 2024/03/26 15:02:50 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TIMEDLOOP_H
# define TIMEDLOOP_H
void timedloop(void (*f)(void));
#endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
/* Updated: 2024/03/26 13:26:58 by mcolonna ### ########.fr */
/* Updated: 2024/03/26 15:59:54 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@
#include "data_assets.h"
#include "room.h"
#include "input.h"
#include "timedloop.h"
#include <X11/Xlib.h>
int expose_hook(void)
@ -32,9 +33,20 @@ int close_hook(void)
return (0);
}
static bool g_tac = false;
static void loop(void)
{
if (g_tac)
print_line(err, 1, " tac");
else
print_line(err, 1, "tic");
g_tac = !g_tac;
}
int loop_hook(void)
{
tf_printf("%i %i %i %i\n", g_env.up, g_env.down, g_env.left, g_env.right);
timedloop(loop);
return (0);
}

41
src/timedloop.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* timedloop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 14:54:49 by mcolonna #+# #+# */
/* Updated: 2024/03/26 15:51:04 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <time.h>
#include "env.h"
static long get_nanos(void)
{
struct timespec ts;
timespec_get(&ts, TIME_UTC);
return ((long)ts.tv_sec * 1000000000L + ts.tv_nsec);
}
void timedloop(void (*f)(void))
{
static long last_time = 0;
const long new_time = get_nanos();
static bool checked = false;
if (new_time - last_time >= 1000000000L / FPS)
{
if (checked)
last_time += 1000000000L / FPS;
else
last_time = new_time;
checked = false;
f();
}
else
checked = true;
}