From 5ee5292e8810492753e80e1c170570ba725574ea Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 16 Jul 2023 23:38:36 +0200 Subject: [PATCH] key listeners are now only triggered if their value changed --- src/corelib/input.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/corelib/input.c b/src/corelib/input.c index bb99222..34fcdde 100644 --- a/src/corelib/input.c +++ b/src/corelib/input.c @@ -131,13 +131,20 @@ 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) { const SDL_Scancode scode = event.key.keysym.scancode; + int changed = 0; if(listener->axis.positive == scode) { - listener->axis.last_positive = event.key.state == SDL_PRESSED; + int new = event.key.state == SDL_PRESSED; + changed = new != listener->axis.last_positive; + listener->axis.last_positive = new; } - if(listener->axis.negative == scode) { - listener->axis.last_negative = event.key.state == SDL_PRESSED; + else if(listener->axis.negative == scode) { + int new = event.key.state == SDL_PRESSED; + changed = new != listener->axis.last_negative; + listener->axis.last_negative = new; } - listener->axis.delegate(listener->axis.last_positive - listener->axis.last_negative); + + if(changed) + listener->axis.delegate(listener->axis.last_positive - listener->axis.last_negative); } } }