Skip to content

Instantly share code, notes, and snippets.

@fabiocolacio
Created December 14, 2017 23:57
Show Gist options
  • Save fabiocolacio/6af2ef76a706443adb210d23bd036d04 to your computer and use it in GitHub Desktop.
Save fabiocolacio/6af2ef76a706443adb210d23bd036d04 to your computer and use it in GitHub Desktop.
Beginner-friendly demonstration using SDL2 to read from a joystick.
#include <SDL2/SDL.h>
// This program opens a joystick and tells you
// when a button is pressed or an axis is moved.
// This demsontrates how to read from the joystick
// using an event-based system. In another example
// I will show how to poll the state of each button.
int main() {
// Initialize the joystick subsystem for SDL2
int joysticks = SDL_Init(SDL_INIT_JOYSTICK);
// If there was an error setting up the joystick subsystem, quit.
if (joysticks < 0) {
printf("Unable to initialize the joystick subsystem.\n");
return -1;
}
// Check how many joysticks are connected.
joysticks = SDL_NumJoysticks();
printf("There are %d joysticks connected.\n", joysticks);
// If there are joysticks connected, open one up for reading
if (joysticks > 0) {
if (SDL_JoystickOpen(0) == NULL) {
printf("There was an error reading from the joystick.\n");
return -1;
}
}
// If there are no joysticks connected, exit the program.
else {
printf("There are no joysticks connected. Exiting...\n");
return -1;
}
int quit = 0;
SDL_Event event;
// An infinite loop that keeps going until we set
// the quit variable to a non-zero value. We
// put this loop so that we can keep on listening to
// the joystick until we are done with it.
while (!quit) {
// The event variable stores a list of events.
// The inner loop keeps reading the events
// one-by-one until there are no events left
// to read. SDL_PollEvent(&event) just checks
// if any new events have happend, and stores them
// inside of the event variable.
while (SDL_PollEvent(&event) != 0) {
// A switch statement conditionally runs different
// code depending on the value it is given.
// Here we check the type of event that happened,
// and do something different depending on what type of
// event it was.
switch (event.type) {
case SDL_QUIT:
quit = 1;
break;
case SDL_JOYAXISMOTION:
printf("The value of axis %d was changed to %d.\n", event.jaxis.axis, event.jaxis.value);
break;
case SDL_JOYHATMOTION:
printf("The hat with index %d was moved to position %d.\n", event.jhat.hat, event.jhat.value);
break;
case SDL_JOYBUTTONDOWN:
printf("The button with index %d was pressed.\n", event.jbutton.button);
break;
case SDL_JOYBUTTONUP:
printf("The button with index %d was released.\n", event.jbutton.button);
break;
case SDL_JOYDEVICEADDED:
printf("A Joystick was connected and assigned index %d.\n", event.jdevice.which);
break;
case SDL_JOYDEVICEREMOVED:
printf("The Joystick with index %d was removed.\n", event.jdevice.which);
break;
}
}
}
// Free up any resources that SDL allocated.
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment