From f124076037344636773fdf60eb6412e4bc1c09eb Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 1 Jul 2025 11:43:26 +0200 Subject: [PATCH] code cleanup --- src/io/input.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/io/input.c b/src/io/input.c index ad54c2b..0848394 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -5,6 +5,7 @@ #include #include "../game/time.h" +#include "../util/types.h" #include "window.h" /* processes an incoming scancode, returns the associated movement data, or performs the close action directly */ @@ -39,24 +40,40 @@ __attribute__((const)) static int procscancode(SDL_Scancode code) { } } +static int timeout_mask(time_t time) { + static time_t timeout = 0, timeout_roll = 0; + int msk = 0; + msk |= ((MOVR | MOVL | MOVD) & -!!time_poll(time, 64, &timeout)); + msk |= ((MOVRL | MOVRR) & -!!time_poll(time, 100, &timeout_roll)); + return msk; +} + +// NOTE: if an action is mapped to multiple keys, pressing both and releasing one will cause the action to be disabled. Minor issue, Won't fix. int input_getdat(time_t time) { - static int movdat = 0, nmovdat = 0; + static u8 movdat = 0, nmovdat = 0; + int mov = movdat, nmov = nmovdat; + + // process the event SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: window_close(); break; - case SDL_KEYDOWN: movdat |= procscancode(e.key.keysym.scancode); break; - case SDL_KEYUP: nmovdat |= procscancode(e.key.keysym.scancode); break; + case SDL_KEYDOWN: mov |= procscancode(e.key.keysym.scancode); break; + case SDL_KEYUP: nmov |= procscancode(e.key.keysym.scancode); break; } } - static time_t timeout = 0, timeout_roll = 0; - int mask = 0; - mask |= ((MOVR | MOVL | MOVD) & -!!time_poll(time, 64, &timeout)); - mask |= ((MOVRL | MOVRR) & -!!time_poll(time, 164, &timeout_roll)); - + // compute the current movement + int mask = timeout_mask(time); int cmov = movdat & mask; + + // handle releasing of keys nmovdat &= movdat; movdat &= ~(nmovdat & mask); + + // write to static variables (shrinking the values, and memory usage) + movdat = mov; + nmovdat = nmov; + return cmov; }