Skip to content

Instantly share code, notes, and snippets.

@connorjclark
Created April 9, 2022 21:45
Show Gist options
  • Save connorjclark/0b7268acd6bfa324c4db38dde7928110 to your computer and use it in GitHub Desktop.
Save connorjclark/0b7268acd6bfa324c4db38dde7928110 to your computer and use it in GitHub Desktop.
joystick sdl example
// emcc example.c -o example.html -g -s ASYNCIFY=1 -s PTHREAD_POOL_SIZE=2 -s USE_SDL=2 -s EXIT_RUNTIME=1
// clang example.c -o example -lsdl2 -g -I"$(dirname $(which emcc))/cache/sysroot/include/SDL2" && ./example
#include <stdio.h>
#include <SDL.h>
void print_joys()
{
int num_joy, i;
num_joy = SDL_NumJoysticks();
printf("%d joysticks found\n", num_joy);
for (i = 0; i < num_joy; i++)
{
SDL_Joystick *joystick = SDL_JoystickOpen(i);
printf("%s\n", SDL_JoystickName(joystick));
}
}
int main()
{
if (SDL_Init(SDL_INIT_EVERYTHING - SDL_INIT_HAPTIC) < 0)
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
print_joys();
SDL_Event event;
while (SDL_WaitEvent(&event) >= 0)
{
switch (event.type)
{
case SDL_JOYDEVICEADDED:
printf("added!\n");
print_joys();
break;
case SDL_JOYDEVICEREMOVED:
printf("removed!\n");
print_joys();
break;
case SDL_KEYDOWN:
return 0;
break;
case SDL_QUIT:
return 0;
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment