reworked most input listener types to use OS events instead of checking state on update

this allows for them to be more reliable with low framerates
pull/8/head
Sara 2023-06-18 22:14:16 +02:00
parent b30df338ac
commit a93d09c96c
1 changed files with 47 additions and 46 deletions

View File

@ -70,21 +70,6 @@ void input_init() {
g_key_states = SDL_GetKeyboardState(NULL);
}
static inline
void _process_axis_listener(input_listener_t* listener) {
int val = 0;
if(g_key_states[listener->axis.negative]) {
val = -1;
}
if(g_key_states[listener->axis.positive]) {
val += 1;
}
if(val != listener->axis.last) {
listener->axis.last = val;
listener->axis.delegate(val);
}
}
static inline
void _process_mouse_listener(input_listener_t* listener, float dx, float dy) {
if(dx != 0.0 && dy != 0.0) {
@ -92,23 +77,6 @@ void _process_mouse_listener(input_listener_t* listener, float dx, float dy) {
}
}
static inline
void _process_button_listener(input_listener_t* listener) {
uint32_t state = SDL_GetMouseState(NULL, NULL);
int is_down = (state & (listener->button.button)) != 0;
if(is_down != listener->button.last) {
listener->button.delegate(is_down);
}
listener->button.last = is_down;
}
static inline
void _process_scroll_listener(input_listener_t* listener) {
if(_scroll_delta != 0) {
listener->scroll.delegate(_scroll_delta);
}
}
void update_input() {
float dx, dy;
int px, py;
@ -118,19 +86,8 @@ void update_input() {
dx = (float)(px - _last_mouse_x)/width; dy = (float)(py - _last_mouse_y)/width;
for(input_listener_t* listener = g_key_listeners; listener != g_key_listeners_endptr; ++listener) {
switch(listener->type) {
case INPUT_LISTENER_AXIS:
_process_axis_listener(listener);
break;
case INPUT_LISTENER_MOUSE:
if(listener->type == INPUT_LISTENER_MOUSE) {
_process_mouse_listener(listener, dx, dy);
break;
case INPUT_LISTENER_SCROLL:
_process_scroll_listener(listener);
break;
case INPUT_LISTENER_BUTTON:
_process_button_listener(listener);
break;
}
}
@ -140,13 +97,57 @@ void update_input() {
_scroll_delta = 0;
}
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);
}
}
}
}
static inline
void _handle_scroll_event(const SDL_Event event) {
_scroll_delta = event.wheel.y;
for(input_listener_t* listener = g_key_listeners; listener < g_key_listeners_endptr; ++listener) {
if(listener->type == INPUT_LISTENER_SCROLL) {
listener->scroll.delegate(_scroll_delta);
}
}
}
static inline
void _handle_mousebutton_event(const SDL_Event event) {
for(input_listener_t* listener = g_key_listeners; listener < g_key_listeners_endptr; ++listener) {
if(listener->type == INPUT_LISTENER_BUTTON
|| listener->button.button == event.button.button) {
listener->button.last = event.button.state == SDL_PRESSED;
listener->button.delegate(listener->button.last);
}
}
}
void input_notify_event(SDL_Event event) {
switch(event.type) {
default:
return;
case SDL_KEYUP:
case SDL_KEYDOWN:
_handle_key_event(event);
return;
case SDL_MOUSEWHEEL:
_scroll_delta = event.wheel.y;
break;
_handle_scroll_event(event);
return;
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
_handle_mousebutton_event(event);
return;
}
}