mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
write code for input handling
This commit is contained in:
44
src/io/input.c
Normal file
44
src/io/input.c
Normal 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
15
src/io/input.h
Normal 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);
|
||||
Reference in New Issue
Block a user