From 63f82601408c75be665d428304d9a81b41083b49 Mon Sep 17 00:00:00 2001 From: Quinn Date: Fri, 27 Jun 2025 13:01:28 +0200 Subject: [PATCH] add input timeout and input keypresses --- src/io/input.c | 11 +++++++---- src/io/input.h | 4 +++- src/io/window.c | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/io/input.c b/src/io/input.c index 561a4a6..f5fa2e1 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -2,10 +2,11 @@ #include #include +#include +#include "../game/time.h" #include "window.h" -// TODO: this'll likely be insufficient, due to no code delays being applied, or it registers *just* the keydown event __attribute__((const)) static int procscancode(SDL_Scancode code) { switch (code) { case SDL_SCANCODE_Q: @@ -37,15 +38,17 @@ __attribute__((const)) static int procscancode(SDL_Scancode code) { } } -int input_getdat(void) { - int movdat = 0; +int input_getdat(time_t time) { + static int movdat = 0; 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: movdat &= ~procscancode(e.key.keysym.scancode); break; } } - return movdat; + static time_t timeout = 0; + return movdat & (-!!time_poll(time, 64, &timeout)); } diff --git a/src/io/input.h b/src/io/input.h index 8077472..a28da16 100644 --- a/src/io/input.h +++ b/src/io/input.h @@ -1,5 +1,7 @@ #pragma once +#include + /* 8 bit enumeration storing the movement data */ enum movdat { MOVL = 1, // move left @@ -12,4 +14,4 @@ enum movdat { /* returns an OR'd string from `enum movdat`, containing the movement data. * assumes that SDL has been initialized and a window has successfully been created. */ -__attribute__((__pure__)) int input_getdat(void); +__attribute__((__pure__)) int input_getdat(time_t time); diff --git a/src/io/window.c b/src/io/window.c index d84901e..d182eb9 100644 --- a/src/io/window.c +++ b/src/io/window.c @@ -46,7 +46,7 @@ void window_free(void) { void window_open(void) { while (!close) { size_t time = time_pull(); - game_update(input_getdat(), time); + game_update(input_getdat(time), time); render_update(); static time_t timeout = 0;