diff --git a/Makefile b/Makefile index f39b581..85a99b6 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/dev/map.md b/dev/map.md index 60d09fa..5d55507 100644 --- a/dev/map.md +++ b/dev/map.md @@ -1,3 +1,5 @@ +/!\ NEEDS UPDATE + data\_assets - `NB_ASSETS` - `t_assetinfo` diff --git a/includes/env.h b/includes/env.h index cc5b7b1..e361158 100644 --- a/includes/env.h +++ b/includes/env.h @@ -6,7 +6,7 @@ /* 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_HEIGHT 400 # define WINDOW_TITLE "undretale" +# define FPS 10 typedef struct s_env { diff --git a/includes/timedloop.h b/includes/timedloop.h new file mode 100644 index 0000000..3d14f9f --- /dev/null +++ b/includes/timedloop.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* timedloop.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/src/main.c b/src/main.c index c4462a3..18d2d54 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* 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 "room.h" #include "input.h" +#include "timedloop.h" #include 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); } diff --git a/src/timedloop.c b/src/timedloop.c new file mode 100644 index 0000000..a5228ef --- /dev/null +++ b/src/timedloop.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* timedloop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/26 14:54:49 by mcolonna #+# #+# */ +/* Updated: 2024/03/26 15:51:04 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#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; +}