From ad22993557296b02bc0c947cb3bb6247fac8f47f Mon Sep 17 00:00:00 2001 From: banana Date: Fri, 22 Mar 2024 13:46:36 -0700 Subject: Initial commit --- input.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 input.c (limited to 'input.c') 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 +#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; + } + } +} -- cgit v1.2.3