From 5db327ac690c7cbdbd992a041640d328242fbe87 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Mon, 6 Oct 2025 21:53:43 +0100 Subject: [PATCH 1/5] Add gamepad support to woodeneye-008 --- .../demo/02-woodeneye-008/woodeneye-008.c | 80 ++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/examples/demo/02-woodeneye-008/woodeneye-008.c b/examples/demo/02-woodeneye-008/woodeneye-008.c index 2c180cda013d2..ccc0b1f519971 100644 --- a/examples/demo/02-woodeneye-008/woodeneye-008.c +++ b/examples/demo/02-woodeneye-008/woodeneye-008.c @@ -15,6 +15,7 @@ typedef struct { SDL_MouseID mouse; SDL_KeyboardID keyboard; + SDL_Gamepad *gamepad; double pos[3]; double vel[3]; unsigned int yaw; @@ -60,6 +61,15 @@ static int whoseKeyboard(SDL_KeyboardID keyboard, const Player players[], int pl return -1; } +static int whoseGamepad(SDL_JoystickID gamepad, const Player players[], int players_len) +{ + int i; + for (i = 0; i < players_len; i++) { + if (SDL_GetGamepadID(players[i].gamepad) == gamepad) return i; + } + return -1; +} + static void shoot(int shooter, Player players[], int players_len) { int i, j; @@ -288,6 +298,7 @@ static void initPlayers(Player *players, int len) players[i].wasd = 0; players[i].mouse = 0; players[i].keyboard = 0; + players[i].gamepad = NULL; players[i].color[0] = (1 << (i / 2)) & 2 ? 0 : 0xff; players[i].color[1] = (1 << (i / 2)) & 1 ? 0 : 0xff; players[i].color[2] = (1 << (i / 2)) & 4 ? 0 : 0xff; @@ -344,7 +355,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) *appstate = as; } - if (!SDL_Init(SDL_INIT_VIDEO)) { + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { return SDL_APP_FAILURE; } if (!SDL_CreateWindowAndRenderer("examples/demo/woodeneye-008", 640, 480, SDL_WINDOW_RESIZABLE, &as->window, &as->renderer)) { @@ -386,6 +397,25 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) } } break; + case SDL_EVENT_GAMEPAD_REMOVED: + for (i = 0; i < player_count; i++) { + if (players[i].gamepad && (SDL_GetGamepadID(players[i].gamepad) == event->gdevice.which)) { + SDL_CloseGamepad(players[i].gamepad); + players[i].gamepad = NULL; + } + } + break; + case SDL_EVENT_GAMEPAD_ADDED: + for (i = 0; i < player_count; i++) { + if (players[i].gamepad == NULL) { + players[i].gamepad = SDL_OpenGamepad(event->gdevice.which); + if (!players[i].gamepad) + SDL_Log("Failed to open gamepad ID %u: %s", (unsigned int) event->gdevice.which, SDL_GetError()); + else + as->player_count = SDL_max(as->player_count, i + 1); + } + } + break; case SDL_EVENT_MOUSE_MOTION: { SDL_MouseID id = event->motion.which; int index = whoseMouse(id, players, player_count); @@ -446,6 +476,44 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) } break; } + case SDL_EVENT_GAMEPAD_AXIS_MOTION: { + SDL_JoystickID id = event->gaxis.which; + int index = whoseGamepad(id, players, player_count); + if (index >= 0) { + if (event->gaxis.axis == SDL_GAMEPAD_AXIS_LEFTX) + players[index].yaw -= ((int)event->gaxis.value) * 0x00000800; + if (event->gaxis.axis == SDL_GAMEPAD_AXIS_LEFTY) + players[index].pitch = SDL_max(-0x40000000, SDL_min(0x40000000, players[index].pitch - ((int)event->gaxis.value) * 0x00000800)); + } + break; + } + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: { + Uint8 button = event->gbutton.button; + SDL_JoystickID id = event->gbutton.which; + int index = whoseGamepad(id, players, player_count); + if (index >= 0) { + if (button == SDL_GAMEPAD_BUTTON_DPAD_UP) players[index].wasd |= 1; + if (button == SDL_GAMEPAD_BUTTON_DPAD_LEFT) players[index].wasd |= 2; + if (button == SDL_GAMEPAD_BUTTON_DPAD_DOWN) players[index].wasd |= 4; + if (button == SDL_GAMEPAD_BUTTON_DPAD_RIGHT) players[index].wasd |= 8; + if (button == SDL_GAMEPAD_BUTTON_SOUTH) players[index].wasd |= 16; + if (button == SDL_GAMEPAD_BUTTON_EAST) shoot(index, players, player_count); + } + break; + } + case SDL_EVENT_GAMEPAD_BUTTON_UP: { + Uint8 button = event->gbutton.button; + SDL_JoystickID id = event->gbutton.which; + int index = whoseGamepad(id, players, player_count); + if (index >= 0) { + if (button == SDL_GAMEPAD_BUTTON_DPAD_UP) players[index].wasd &= 30; + if (button == SDL_GAMEPAD_BUTTON_DPAD_LEFT) players[index].wasd &= 29; + if (button == SDL_GAMEPAD_BUTTON_DPAD_DOWN) players[index].wasd &= 27; + if (button == SDL_GAMEPAD_BUTTON_DPAD_RIGHT) players[index].wasd &= 23; + if (button == SDL_GAMEPAD_BUTTON_SOUTH) players[index].wasd &= 15; + } + break; + } } return SDL_APP_CONTINUE; } @@ -476,5 +544,13 @@ SDL_AppResult SDL_AppIterate(void *appstate) void SDL_AppQuit(void *appstate, SDL_AppResult result) { + AppState *as = appstate; + Player *players = as->players; + int player_count = as->player_count; + int i; + for (i = 0; i < player_count; i++) { + if (players[i].gamepad) + SDL_CloseGamepad(players[i].gamepad); + } SDL_free(appstate); // just free the memory, SDL will clean up the window/renderer for us. -} \ No newline at end of file +} From 39c5dce189235a58f7552e252056f0b4f3e788ff Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 8 Nov 2025 13:51:22 -0500 Subject: [PATCH 2/5] examples/demo/02-woodeneye-008: check gamepad sticks each frame, not in events. --- .../demo/02-woodeneye-008/woodeneye-008.c | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/examples/demo/02-woodeneye-008/woodeneye-008.c b/examples/demo/02-woodeneye-008/woodeneye-008.c index ccc0b1f519971..7c6268e8ef8ed 100644 --- a/examples/demo/02-woodeneye-008/woodeneye-008.c +++ b/examples/demo/02-woodeneye-008/woodeneye-008.c @@ -111,9 +111,29 @@ static void shoot(int shooter, Player players[], int players_len) } } +static void updatePlayerGamepad(Player *player) +{ + if (player->gamepad) { + const int leftx = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_LEFTX); + const int lefty = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_LEFTY); + player->yaw -= leftx * 0x00000800; + player->pitch = SDL_max(-0x40000000, SDL_min(0x40000000, player->pitch - lefty * 0x00000800)); + } +} + static void update(Player *players, int players_len, Uint64 dt_ns) { + static int gamepad_update_ticks = 0; + const Uint64 now = SDL_GetTicks(); int i; + + if ((now - gamepad_update_ticks) >= 16) { /* only update joysticks at about 60Hz so framerate doesn't matter. */ + gamepad_update_ticks += 16; + for (i = 0; i < players_len; i++) { + updatePlayerGamepad(&players[i]); /* check current gamepad state before we do any processing. */ + } + } + for (i = 0; i < players_len; i++) { Player *player = &players[i]; double rate = 6.0; @@ -476,17 +496,9 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) } break; } - case SDL_EVENT_GAMEPAD_AXIS_MOTION: { - SDL_JoystickID id = event->gaxis.which; - int index = whoseGamepad(id, players, player_count); - if (index >= 0) { - if (event->gaxis.axis == SDL_GAMEPAD_AXIS_LEFTX) - players[index].yaw -= ((int)event->gaxis.value) * 0x00000800; - if (event->gaxis.axis == SDL_GAMEPAD_AXIS_LEFTY) - players[index].pitch = SDL_max(-0x40000000, SDL_min(0x40000000, players[index].pitch - ((int)event->gaxis.value) * 0x00000800)); - } - break; - } + + /* We query the gamepad sticks every frame, so we don't check for SDL_EVENT_GAMEPAD_AXIS_MOTION here. */ + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: { Uint8 button = event->gbutton.button; SDL_JoystickID id = event->gbutton.which; From 32563c2048e4d78d05857dc4135c841fedb9f00a Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 8 Nov 2025 13:57:52 -0500 Subject: [PATCH 3/5] examples/demo/02-woodeneye-008: use right stick for camera, not left. This matches the standard for console FPS titles. --- examples/demo/02-woodeneye-008/woodeneye-008.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/demo/02-woodeneye-008/woodeneye-008.c b/examples/demo/02-woodeneye-008/woodeneye-008.c index 7c6268e8ef8ed..43f1f72ae926e 100644 --- a/examples/demo/02-woodeneye-008/woodeneye-008.c +++ b/examples/demo/02-woodeneye-008/woodeneye-008.c @@ -114,10 +114,10 @@ static void shoot(int shooter, Player players[], int players_len) static void updatePlayerGamepad(Player *player) { if (player->gamepad) { - const int leftx = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_LEFTX); - const int lefty = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_LEFTY); - player->yaw -= leftx * 0x00000800; - player->pitch = SDL_max(-0x40000000, SDL_min(0x40000000, player->pitch - lefty * 0x00000800)); + const int rightx = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_RIGHTX); + const int righty = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_RIGHTY); + player->yaw -= rightx * 0x00000800; + player->pitch = SDL_max(-0x40000000, SDL_min(0x40000000, player->pitch - righty * 0x00000800)); } } From 3e2ff3f1059ca6fa030d508582f52e0a0bf4c6de Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 8 Nov 2025 14:02:24 -0500 Subject: [PATCH 4/5] examples/demo/02-woodeneye-008: Minor coding style tweaks. --- examples/demo/02-woodeneye-008/woodeneye-008.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/demo/02-woodeneye-008/woodeneye-008.c b/examples/demo/02-woodeneye-008/woodeneye-008.c index 43f1f72ae926e..7c613dc8ee6e6 100644 --- a/examples/demo/02-woodeneye-008/woodeneye-008.c +++ b/examples/demo/02-woodeneye-008/woodeneye-008.c @@ -47,7 +47,9 @@ static int whoseMouse(SDL_MouseID mouse, const Player players[], int players_len { int i; for (i = 0; i < players_len; i++) { - if (players[i].mouse == mouse) return i; + if (players[i].mouse == mouse) { + return i; + } } return -1; } @@ -56,7 +58,9 @@ static int whoseKeyboard(SDL_KeyboardID keyboard, const Player players[], int pl { int i; for (i = 0; i < players_len; i++) { - if (players[i].keyboard == keyboard) return i; + if (players[i].keyboard == keyboard) { + return i; + } } return -1; } @@ -65,7 +69,9 @@ static int whoseGamepad(SDL_JoystickID gamepad, const Player players[], int play { int i; for (i = 0; i < players_len; i++) { - if (SDL_GetGamepadID(players[i].gamepad) == gamepad) return i; + if (SDL_GetGamepadID(players[i].gamepad) == gamepad) { + return i; + } } return -1; } @@ -561,8 +567,9 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) int player_count = as->player_count; int i; for (i = 0; i < player_count; i++) { - if (players[i].gamepad) + if (players[i].gamepad) { SDL_CloseGamepad(players[i].gamepad); + } } SDL_free(appstate); // just free the memory, SDL will clean up the window/renderer for us. } From 90874fabcd9f565b93505d89f3dfa35eca4882ef Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 8 Nov 2025 14:09:45 -0500 Subject: [PATCH 5/5] demo/02-woodeneye-008/woodeneye-008: ignore gamepad stick deadzones. This prevents an idle gamepad from overriding mouse input. --- examples/demo/02-woodeneye-008/woodeneye-008.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/demo/02-woodeneye-008/woodeneye-008.c b/examples/demo/02-woodeneye-008/woodeneye-008.c index 7c613dc8ee6e6..d87e4cb3f4fe9 100644 --- a/examples/demo/02-woodeneye-008/woodeneye-008.c +++ b/examples/demo/02-woodeneye-008/woodeneye-008.c @@ -122,8 +122,10 @@ static void updatePlayerGamepad(Player *player) if (player->gamepad) { const int rightx = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_RIGHTX); const int righty = (int)SDL_GetGamepadAxis(player->gamepad, SDL_GAMEPAD_AXIS_RIGHTY); - player->yaw -= rightx * 0x00000800; - player->pitch = SDL_max(-0x40000000, SDL_min(0x40000000, player->pitch - righty * 0x00000800)); + if ((SDL_abs(rightx) > 0x1000) || (SDL_abs(righty) > 0x1000)) { /* ignore if stick is near center, since it might be noise and the user is actually using the mouse, etc. */ + player->yaw -= rightx * 0x00000800; + player->pitch = SDL_max(-0x40000000, SDL_min(0x40000000, player->pitch - righty * 0x00000800)); + } } }