feat: add timedloop()
This commit is contained in:
parent
78a6bbbb68
commit
4bd12ffcf1
6 changed files with 78 additions and 4 deletions
16
src/main.c
16
src/main.c
|
|
@ -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
41
src/timedloop.c
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue