diff --git a/src/input.c b/src/input.c index 5d8e71d..bc94b86 100644 --- a/src/input.c +++ b/src/input.c @@ -1,75 +1,10 @@ #include "input.h" -#include "debug.h" -#include "list.h" -#include -typedef struct InputAction { - const uint8_t* positive; - const uint8_t* negative; - int last; - void* object_arg; - InputActionDelegate delegate; -} InputAction; - -static List _actions; - -static const uint8_t* _keys; void input_init() { - _actions = list_from_type(InputAction); - _keys = SDL_GetKeyboardState(NULL); + } void input_clean() { - list_empty(&_actions); -} - -static -void _key_changed_event(SDL_Event evt) { - const uint8_t* keyptr = _keys + evt.key.keysym.scancode; - list_foreach(InputAction, action, &_actions) { - if(keyptr == action->positive || keyptr == action->negative) { - int val = *action->positive - *action->negative; - if(val != action->last) { - action->last = val; - action->delegate(action->object_arg, val); - } - } - } -} - -void input_handle_event(SDL_Event event) { - switch(event.type) { - default:return; - case SDL_KEYDOWN: - case SDL_KEYUP: - _key_changed_event(event); - break; - } -} - -void input_add_axis_action(void* object, InputActionDelegate delegate, SDL_Scancode negative, SDL_Scancode positive) { - list_add(&_actions, &(InputAction){ - .negative = _keys + negative, - .positive = _keys + positive, - .delegate = delegate, - .object_arg = object, - }); -} - -void input_add_key_action(void* object, InputActionDelegate delegate, SDL_Scancode key) { - list_add(&_actions, &(InputAction) { - .negative = _keys + SDL_SCANCODE_UNKNOWN, - .positive = _keys + key, - .delegate = delegate, - .object_arg = object, - }); -} - -void input_remove_actions(void* object) { - for(size_t i = 0; i < _actions.len; ++i) { - if(list_at_as(InputAction, &_actions, i)->object_arg == object) { - list_erase(&_actions, i); - } - } + } diff --git a/src/input.h b/src/input.h index 0b7443e..35e4c61 100644 --- a/src/input.h +++ b/src/input.h @@ -1,12 +1,12 @@ #ifndef _fencer_input_h #define _fencer_input_h +#include "vmath.h" +#include "list.h" #include #include #include - -// input listener -typedef void (*InputActionDelegate)(void* object, int value); +#include extern void input_init(); extern void input_clean(); @@ -14,14 +14,4 @@ extern void input_clean(); // handle an input event extern void input_handle_event(SDL_Event event); -// create a new axis action triggering on a change of either 'negative' or 'positive' and invoking 'delegate' with first argument 'object' -extern void input_add_axis_action(void* object, InputActionDelegate delegate, SDL_Scancode negative, SDL_Scancode positive); -// create a new key action triggering on 'key' change invoking 'delegate' on 'object'. -extern void input_add_key_action(void* object, InputActionDelegate delegate, SDL_Scancode key); -// remove all actions related to 'object'. -extern void input_remove_actions(void* object); - -#define InputDelegate(__Listener)\ - ((InputActionDelegate)&__Listener) - #endif // !_fencer_input_h