From 10920b30a614a336ecfe86aeeb42256e2f34bb0a Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Mon, 6 Oct 2025 21:53:43 +0100 Subject: [PATCH] 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 b97b06adc109b..7caf8305d73d2 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, 0, &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 +}