support for gamepad events

This commit is contained in:
2025-07-12 22:38:18 -05:00
parent c87f85cf6c
commit 0d97b47728
2 changed files with 14 additions and 4 deletions

View File

@@ -877,7 +877,7 @@ int main(int argc, char **argv)
profile_enabled = 1; script_start = 2;
}
if (!SDL_Init(SDL_INIT_EVENTS)) {
if (!SDL_Init(SDL_INIT_EVENTS | SDL_INIT_GAMEPAD)) {
printf("CRITICAL ERROR: %s\n", SDL_GetError());
exit(1);
}

View File

@@ -653,16 +653,26 @@ static void event2wota_write(WotaBuffer *wb, const SDL_Event *e, int c) {
wota_write_text(wb, "which");
wota_write_number(wb, (double)e->gaxis.which);
wota_write_text(wb, "axis");
wota_write_number(wb, (double)e->gaxis.axis);
wota_write_text(wb, SDL_GetGamepadStringForAxis(e->gaxis.axis));
wota_write_text(wb, "value");
wota_write_number(wb, (double)e->gaxis.value);
// Normalize axis values
double normalized_value;
if (e->gaxis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
e->gaxis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
// Triggers: 0 to 32767 -> 0 to 1
normalized_value = (double)e->gaxis.value / 32767.0;
} else {
// Thumbsticks: -32768 to 32767 -> -1 to 1
normalized_value = (double)e->gaxis.value / 32767.0;
}
wota_write_number(wb, normalized_value);
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
wota_write_text(wb, "which");
wota_write_number(wb, (double)e->gbutton.which);
wota_write_text(wb, "button");
wota_write_number(wb, (double)e->gbutton.button);
wota_write_text(wb, SDL_GetGamepadStringForButton(e->gbutton.button));
wota_write_text(wb, "down");
wota_write_sym(wb, e->gbutton.down ? WOTA_TRUE : WOTA_FALSE);
break;