cleared input module
This commit is contained in:
parent
c9dcb508bd
commit
dc6a1acc34
69
src/input.c
69
src/input.c
|
@ -1,75 +1,10 @@
|
|||
#include "input.h"
|
||||
#include "debug.h"
|
||||
#include "list.h"
|
||||
#include <stdint.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
16
src/input.h
16
src/input.h
|
@ -1,12 +1,12 @@
|
|||
#ifndef _fencer_input_h
|
||||
#define _fencer_input_h
|
||||
|
||||
#include "vmath.h"
|
||||
#include "list.h"
|
||||
#include <SDL2/SDL_events.h>
|
||||
#include <SDL2/SDL_scancode.h>
|
||||
#include <SDL2/SDL_keyboard.h>
|
||||
|
||||
// input listener
|
||||
typedef void (*InputActionDelegate)(void* object, int value);
|
||||
#include <SDL2/SDL_joystick.h>
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue