From fab7338891394c08f30eeb1cedc268ebed1478b2 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 18 Jun 2023 22:22:16 +0200 Subject: [PATCH] axis now keeps a history for both keys --- src/corelib/input.c | 16 +++++++++------- src/corelib/input.h | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/corelib/input.c b/src/corelib/input.c index 571de49..df5c666 100644 --- a/src/corelib/input.c +++ b/src/corelib/input.c @@ -23,7 +23,8 @@ void add_key_listener(SDL_Scancode negative, SDL_Scancode positive, g_key_listeners_endptr->axis.delegate = delegate; g_key_listeners_endptr->axis.positive = positive; g_key_listeners_endptr->axis.negative = negative; - g_key_listeners_endptr->axis.last = 0; + g_key_listeners_endptr->axis.last_positive = + g_key_listeners_endptr->axis.last_negative = 0; ++g_key_listeners_endptr; } @@ -101,13 +102,14 @@ static inline void _handle_key_event(const SDL_Event event) { for(input_listener_t* listener = g_key_listeners; listener < g_key_listeners_endptr; ++listener) { if(listener->type == INPUT_LISTENER_AXIS) { - if(listener->axis.positive == event.key.keysym.scancode - || listener->axis.negative == event.key.keysym.scancode) { - const SDL_Scancode scode = event.key.keysym.scancode; - const int value = (event.type == SDL_KEYDOWN ? 1 : -1) * (scode == listener->axis.negative ? -1 : 1); - listener->axis.last += value; - listener->axis.delegate(listener->axis.last); + const SDL_Scancode scode = event.key.keysym.scancode; + if(listener->axis.positive == scode) { + listener->axis.last_positive = event.key.state == SDL_PRESSED; } + if(listener->axis.negative == scode) { + listener->axis.last_negative = event.key.state == SDL_PRESSED; + } + listener->axis.delegate(listener->axis.last_positive - listener->axis.last_negative); } } } diff --git a/src/corelib/input.h b/src/corelib/input.h index 85d3cc8..9be54d6 100644 --- a/src/corelib/input.h +++ b/src/corelib/input.h @@ -25,7 +25,7 @@ typedef struct input_listener_t { struct { input_axis_delegate_t delegate; SDL_Scancode positive, negative; - int last; + int last_positive, last_negative; } axis; struct { input_mouse_delegate_t delegate; -- 2.34.1