code cleanup

This commit is contained in:
2025-07-01 11:43:26 +02:00
parent 9d7d82a9b6
commit f124076037

View File

@@ -5,6 +5,7 @@
#include <time.h>
#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;
}