SDL2 recognizes my PS4 controller but not the button presses. Here is a minimal code example.
#include <SDL2/SDL.h>
#include <stdio.h>
int main(int argc, char** argv) {
if (SDL_Init(SDL_INIT_VIDEO) != 0 ) {
printf("Unable to initialize SDL (%s)\n",SDL_GetError());
printf("Exit\n");
return -1;
}
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
SDL_GameController *gameController = NULL;
int numJoysticks;
numJoysticks = SDL_NumJoysticks();
printf("%d joysticks found\n", numJoysticks);
if( numJoysticks <= 0 ) {
return -1;
}
if( ! SDL_IsGameController(0) ) {
printf("Error: joystick 0 is not a game controller\n");
printf("Exit\n");
SDL_Quit();
return -1;
}
gameController = SDL_GameControllerOpen(0);
if( ! gameController ) {
printf("No game controller found\n");
printf("Exit\n");
SDL_Quit();
}
printf("Opened Game Controller 0\n");
printf("Found a valid controller, named: %s\n", SDL_GameControllerName(gameController));
if( SDL_GameControllerGetButton(gameController, SDL_CONTROLLER_BUTTON_A) ) {
printf("Button press!\n"); // Never printed
}
// Exit
SDL_GameControllerClose(gameController);
SDL_Quit();
return 0;
}
Here is the output I get:
1 joysticks found
Opened Game Controller 0
Found a valid controller, named: PS4 Controller
What am I doing wrong?