Skip to content

Instantly share code, notes, and snippets.

@duganchen
Created November 25, 2019 14:42
Show Gist options
  • Save duganchen/51911f49587d96855bd33e24aaedf942 to your computer and use it in GitHub Desktop.
Save duganchen/51911f49587d96855bd33e24aaedf942 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
int main()
{
SDL_GameController *controller;
SDL_Event e;
int quit;
if (SDL_Init(SDL_INIT_GAMECONTROLLER) != 0)
{
printf("SDL_Init Error: %s\n", SDL_GetError());
return 1;
}
if (SDL_NumJoysticks() == 0)
{
printf("No joysticks attached\n");
return 1;
}
if (!SDL_IsGameController(0))
{
printf("Joystick 0 is not a game controller\n");
return 1;
}
controller = SDL_GameControllerOpen(0);
if (!controller)
{
printf("Unable to open game controller\n");
return 1;
}
printf("Game controller: %s\n", SDL_GameControllerName(controller));
quit = 0;
while (!quit)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_CONTROLLERBUTTONDOWN)
{
quit = 1;
switch (e.cbutton.button)
{
case SDL_CONTROLLER_BUTTON_INVALID:
printf("INVALID\n");
break;
case SDL_CONTROLLER_BUTTON_A:
printf("A\n");
break;
case SDL_CONTROLLER_BUTTON_B:
printf("B\n");
break;
case SDL_CONTROLLER_BUTTON_X:
printf("X\n");
break;
case SDL_CONTROLLER_BUTTON_Y:
printf("Y\n");
break;
case SDL_CONTROLLER_BUTTON_BACK:
printf("BACK\n");
break;
case SDL_CONTROLLER_BUTTON_GUIDE:
printf("GUIDE\n");
break;
case SDL_CONTROLLER_BUTTON_START:
printf("START\n");
break;
case SDL_CONTROLLER_BUTTON_LEFTSTICK:
printf("LEFTSTICK\n");
break;
case SDL_CONTROLLER_BUTTON_RIGHTSTICK:
printf("RIGHTSTICK\n");
break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
printf("LEFTSHOULDER\n");
break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
printf("RIGHTSHOULDER\n");
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
printf("DPAD_UP\n");
break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
printf("DPAD_DOWN\n");
break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
printf("DPAD_LEFT\n");
break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
printf("DPAD_RIGHT\n");
break;
case SDL_CONTROLLER_BUTTON_MAX:
printf("MAX\n");
break;
default:
break;
}
printf("%d\n", e.cbutton.button);
}
}
}
SDL_GameControllerClose(controller);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment