Skip to content

Instantly share code, notes, and snippets.

@Ilgrim
Forked from adituv/main.cpp
Created May 28, 2023 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ilgrim/e9a3142bbefb085051aa8d552f2c711b to your computer and use it in GitHub Desktop.
Save Ilgrim/e9a3142bbefb085051aa8d552f2c711b to your computer and use it in GitHub Desktop.
SDL Gamepad Input Stuff
#include <cstdio>
#include <string>
#include <vector>
#include <SDL.h>
void logJoystickEvent(SDL_Event e);
std::string showHatState(Uint8 hat);
int main(int argc, char* argv[]) {
// Detect first gamepad on which a key is pressed, then log its events.
bool quit = false;
bool deviceFound = false;
SDL_Event e;
int num_joysticks = 0;
SDL_Joystick* activeJoystick = nullptr;
SDL_JoystickID activeJoystickID;
auto joysticks = std::vector<SDL_Joystick*>();
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
printf("SDL initialization failed. Error: %s\n", SDL_GetError());
}
num_joysticks = SDL_NumJoysticks();
for (int i = 0; i < num_joysticks; i++) {
auto joystick = SDL_JoystickOpen(i);
if (joystick == nullptr) {
printf("Warning: unable to open game controller %d! SDL Error: %s\n", i, SDL_GetError());
}
else {
joysticks.push_back(joystick);
}
}
while (!deviceFound && !quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_JOYDEVICEADDED) {
auto joystick = SDL_JoystickOpen(e.jdevice.which);
if (joystick == nullptr) {
printf("Warning: unable to open game controller %d! SDL Error: %s\n", e.jdevice.which, SDL_GetError());
}
else {
joysticks.push_back(joystick);
}
}
else if (e.type == SDL_JOYDEVICEREMOVED) {
auto joystick = SDL_JoystickFromInstanceID(e.jdevice.which);
SDL_JoystickClose(joystick);
}
else if (e.type == SDL_JOYBUTTONDOWN) {
activeJoystick = SDL_JoystickFromInstanceID(e.jbutton.which);
deviceFound = true;
logJoystickEvent(e);
}
}
}
// Stop listening to all other joysticks;
activeJoystickID = SDL_JoystickInstanceID(activeJoystick);
auto it = joysticks.begin();
while (it != joysticks.end()) {
if (SDL_JoystickInstanceID(*it) != activeJoystickID) {
it = joysticks.erase(it);
}
else {
it++;
}
}
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_JOYBUTTONUP) {
logJoystickEvent(e);
if (e.jbutton.button == 7) { // start on xbox 360 controller
quit = true;
}
}
else if (e.type == SDL_JOYBUTTONDOWN
|| e.type == SDL_JOYAXISMOTION
|| e.type == SDL_JOYHATMOTION) {
logJoystickEvent(e);
}
else if (e.type == SDL_JOYDEVICEREMOVED) {
if (e.jdevice.which == activeJoystickID) {
printf("Error: active joystick removed.\n");
quit = true;
}
}
}
}
SDL_JoystickClose(activeJoystick);
SDL_Quit();
return 0;
}
void logJoystickEvent(SDL_Event e) {
Uint32 timestamp = 0;
std::string eventType;
int index;
std::string otherData;
switch (e.type) {
case SDL_JOYBUTTONUP:
timestamp = e.jbutton.timestamp;
eventType = "RELEASE";
index = e.jbutton.button;
otherData = "";
break;
case SDL_JOYBUTTONDOWN:
timestamp = e.jbutton.timestamp;
eventType = "PRESS";
index = e.jbutton.button;
otherData = "";
break;
case SDL_JOYHATMOTION:
timestamp = e.jhat.timestamp;
eventType = "POV";
index = e.jhat.hat;
otherData = showHatState(e.jhat.value);
break;
case SDL_JOYAXISMOTION:
timestamp = e.jaxis.timestamp;
eventType = "STICK";
index = e.jaxis.axis;
otherData = std::to_string(e.jaxis.value);
break;
}
printf("%u\t%s\t%u\t%s\n", timestamp, eventType.c_str(), index, otherData.c_str());
}
std::string showHatState(Uint8 hat) {
switch (hat) {
case SDL_HAT_LEFTUP:
return "LU";
break;
case SDL_HAT_UP:
return "XU";
break;
case SDL_HAT_RIGHTUP:
return "RU";
break;
case SDL_HAT_LEFT:
return "LX";
break;
case SDL_HAT_CENTERED:
return "XX";
break;
case SDL_HAT_RIGHT:
return "RX";
break;
case SDL_HAT_LEFTDOWN:
return "LD";
break;
case SDL_HAT_DOWN:
return "XD";
break;
case SDL_HAT_RIGHTDOWN:
return "RD";
break;
default:
return "??";
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment