From a7d5f1bb2a505d0829c409d3cf908ce4233b9f0a Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 24 Jun 2025 19:51:45 +0200 Subject: [PATCH] write code for input handling --- src/io/input.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/io/input.h | 15 +++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/io/input.c create mode 100644 src/io/input.h diff --git a/src/io/input.c b/src/io/input.c new file mode 100644 index 0000000..bebcf67 --- /dev/null +++ b/src/io/input.c @@ -0,0 +1,44 @@ +#include "input.h" + +#include +#include + +#include "window.h" + +__attribute__((const)) static int procscancode(SDL_Scancode code) { + switch (code) { + case SDL_SCANCODE_Q: return MOVRL; + case SDL_SCANCODE_E: return MOVRR; + + case SDL_SCANCODE_LEFT: + case SDL_SCANCODE_A: + return MOVL; + + case SDL_SCANCODE_RIGHT: + case SDL_SCANCODE_D: + return MOVR; + + case SDL_SCANCODE_DOWN: + case SDL_SCANCODE_S: + case SDL_SCANCODE_SPACE: + return MOVD; + + case SDL_SCANCODE_ESCAPE: + window_close(); + return 0; + default: return 0; + } +} + +int input_getdat(void) { + 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; + } + } + + return movdat; +} diff --git a/src/io/input.h b/src/io/input.h new file mode 100644 index 0000000..8077472 --- /dev/null +++ b/src/io/input.h @@ -0,0 +1,15 @@ +#pragma once + +/* 8 bit enumeration storing the movement data */ +enum movdat { + MOVL = 1, // move left + MOVR = 2, // move right + MOVD = 4, // move down + MOVRL = 8, // move roll left + MOVRR = 16, // move roll right + MOVPL = 32, // move place +}; + +/* 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);