write code for input handling

This commit is contained in:
2025-06-24 19:51:45 +02:00
parent 046f7affaf
commit a7d5f1bb2a
2 changed files with 59 additions and 0 deletions

44
src/io/input.c Normal file
View File

@@ -0,0 +1,44 @@
#include "input.h"
#include <SDL_events.h>
#include <SDL_scancode.h>
#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;
}

15
src/io/input.h Normal file
View File

@@ -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);