diff options
Diffstat (limited to 'src/input.c')
| -rw-r--r-- | src/input.c | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/src/input.c b/src/input.c index 66c8b2d..9e48729 100644 --- a/src/input.c +++ b/src/input.c @@ -1,14 +1,22 @@ #include <SDL3/SDL_log.h> +#include <SDL3/SDL_mouse.h> #include "input.h" -struct pw_input *pw_input_init(void) { +struct pw_input *pw_input_init(SDL_Window *window) { + if (!window) { + SDL_InvalidParamError("window"); + return NULL; + } + struct pw_input *input = SDL_malloc(sizeof(*input)); if (!input) { return NULL; } SDL_zerop(input); + input->window = window; + return input; } @@ -218,6 +226,19 @@ static bool handle_key(struct pw_input *input, SDL_KeyboardEvent *event) { return true; } +static int32_t saturating_add(int32_t a, int32_t b) { + int64_t result = (int64_t)a + (int64_t)b; + return (int32_t)SDL_clamp(result, INT32_MIN, INT32_MAX); +} + +static bool handle_wheel(struct pw_input *input, SDL_MouseWheelEvent *wheel) { + input->mouse.wheel.dx = + saturating_add(input->mouse.wheel.dx, wheel->integer_x); + input->mouse.wheel.dy = + saturating_add(input->mouse.wheel.dy, wheel->integer_y); + return true; +} + bool pw_input_event(struct pw_input *input, struct pw_players *players, SDL_Event *event) { if (!input) { @@ -245,6 +266,8 @@ bool pw_input_event(struct pw_input *input, struct pw_players *players, case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_UP: return handle_key(input, &event->key); + case SDL_EVENT_MOUSE_WHEEL: + return handle_wheel(input, &event->wheel); default: return true; } @@ -400,6 +423,27 @@ enum pw_mouse_button pw_input_mouse(struct pw_input *input, return input->mouse.button; } +void pw_input_wheel(struct pw_input *input, int32_t *dx, int32_t *dy) { + static struct pw_mouse_wheel zero = {0}; + + struct pw_mouse_wheel *w; + if (input) { + w = &input->mouse.wheel; + } else { + SDL_InvalidParamError("input"); + w = &zero; + } + + if (dx) { + *dx = w->dx; + w->dx = 0; + } + if (dy) { + *dy = w->dy; + w->dy = 0; + } +} + bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { if (!input) { SDL_InvalidParamError("input"); @@ -442,3 +486,11 @@ bool pw_input_poll(struct pw_input *input, union pw_input_event *event) { return false; } + +bool pw_input_set_pointer_lock(struct pw_input *input, bool lock) { + return SDL_SetWindowRelativeMouseMode(input->window, lock); +} + +bool pw_input_get_pointer_lock(struct pw_input *input) { + return SDL_GetWindowRelativeMouseMode(input->window); +} |
