add input timeout and input keypresses

This commit is contained in:
2025-06-27 13:01:28 +02:00
parent d42e5c25f4
commit 63f8260140
3 changed files with 11 additions and 6 deletions

View File

@@ -2,10 +2,11 @@
#include <SDL_events.h> #include <SDL_events.h>
#include <SDL_scancode.h> #include <SDL_scancode.h>
#include <time.h>
#include "../game/time.h"
#include "window.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) { __attribute__((const)) static int procscancode(SDL_Scancode code) {
switch (code) { switch (code) {
case SDL_SCANCODE_Q: case SDL_SCANCODE_Q:
@@ -37,15 +38,17 @@ __attribute__((const)) static int procscancode(SDL_Scancode code) {
} }
} }
int input_getdat(void) { int input_getdat(time_t time) {
int movdat = 0; static int movdat = 0;
SDL_Event e; SDL_Event e;
while (SDL_PollEvent(&e)) { while (SDL_PollEvent(&e)) {
switch (e.type) { switch (e.type) {
case SDL_QUIT: window_close(); break; case SDL_QUIT: window_close(); break;
case SDL_KEYDOWN: movdat |= procscancode(e.key.keysym.scancode); 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));
} }

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <time.h>
/* 8 bit enumeration storing the movement data */ /* 8 bit enumeration storing the movement data */
enum movdat { enum movdat {
MOVL = 1, // move left MOVL = 1, // move left
@@ -12,4 +14,4 @@ enum movdat {
/* returns an OR'd string from `enum movdat`, containing the movement data. /* 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. */ * 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);

View File

@@ -46,7 +46,7 @@ void window_free(void) {
void window_open(void) { void window_open(void) {
while (!close) { while (!close) {
size_t time = time_pull(); size_t time = time_pull();
game_update(input_getdat(), time); game_update(input_getdat(time), time);
render_update(); render_update();
static time_t timeout = 0; static time_t timeout = 0;