summaryrefslogtreecommitdiff
path: root/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'input.c')
-rw-r--r--input.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/input.c b/input.c
new file mode 100644
index 0000000..6e9994d
--- /dev/null
+++ b/input.c
@@ -0,0 +1,59 @@
+#include <SDL2/SDL.h>
+#include "./constants.h"
+#include "./structs.h"
+
+extern App app;
+
+static void handle_key_down(SDL_KeyboardEvent *event) {
+ if(event->repeat == 0) {
+ if(event->keysym.sym == SDLK_ESCAPE) {
+ app.input |= INPUT_RESET;
+ }
+ if(event->keysym.sym == SDLK_a) {
+ app.input |= PADDLE_1_UP;
+ }
+ if(event->keysym.sym == SDLK_z) {
+ app.input |= PADDLE_1_DOWN;
+ }
+ if(event->keysym.sym == SDLK_QUOTE) {
+ app.input |= PADDLE_2_UP;
+ }
+ if(event->keysym.sym == SDLK_SLASH) {
+ app.input |= PADDLE_2_DOWN;
+ }
+ }
+}
+
+static void handle_key_up(SDL_KeyboardEvent *event) {
+ if(event->repeat == 0) {
+ if(event->keysym.sym == SDLK_a) {
+ app.input ^= PADDLE_1_UP;
+ }
+ if(event->keysym.sym == SDLK_z) {
+ app.input ^= PADDLE_1_DOWN;
+ }
+ if(event->keysym.sym == SDLK_QUOTE) {
+ app.input ^= PADDLE_2_UP;
+ }
+ if(event->keysym.sym == SDLK_SLASH) {
+ app.input ^= PADDLE_2_DOWN;
+ }
+ }
+}
+
+void process_input() {
+ SDL_Event event;
+ while(SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_QUIT:
+ app.input |= INPUT_RESET;
+ break;
+ case SDL_KEYDOWN:
+ handle_key_down(&event.key);
+ break;
+ case SDL_KEYUP:
+ handle_key_up(&event.key);
+ break;
+ }
+ }
+}