feat: add timedloop()
This commit is contained in:
parent
78a6bbbb68
commit
4bd12ffcf1
6 changed files with 78 additions and 4 deletions
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@
|
||||||
NAME = so_long
|
NAME = so_long
|
||||||
SRCS = src/
|
SRCS = src/
|
||||||
INCLUDES = includes/
|
INCLUDES = includes/
|
||||||
CODE = main error env input \
|
CODE = main error env input timedloop \
|
||||||
data_assets data_sprites \
|
data_assets data_sprites \
|
||||||
display1 display2 display_utils sprite \
|
display1 display2 display_utils sprite \
|
||||||
room room_utils object
|
room room_utils object
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
/!\ NEEDS UPDATE
|
||||||
|
|
||||||
data\_assets
|
data\_assets
|
||||||
- `NB_ASSETS`
|
- `NB_ASSETS`
|
||||||
- `t_assetinfo`
|
- `t_assetinfo`
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */
|
/* 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_WIDTH 600
|
||||||
# define WINDOW_HEIGHT 400
|
# define WINDOW_HEIGHT 400
|
||||||
# define WINDOW_TITLE "undretale"
|
# define WINDOW_TITLE "undretale"
|
||||||
|
# define FPS 10
|
||||||
|
|
||||||
typedef struct s_env
|
typedef struct s_env
|
||||||
{
|
{
|
||||||
|
|
18
includes/timedloop.h
Normal file
18
includes/timedloop.h
Normal 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
|
16
src/main.c
16
src/main.c
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
|
/* 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 "data_assets.h"
|
||||||
#include "room.h"
|
#include "room.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
#include "timedloop.h"
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
|
|
||||||
int expose_hook(void)
|
int expose_hook(void)
|
||||||
|
@ -32,9 +33,20 @@ int close_hook(void)
|
||||||
return (0);
|
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)
|
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);
|
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
Reference in a new issue