Created
September 25, 2013 15:21
-
-
Save urkle/6701236 to your computer and use it in GitHub Desktop.
SDL2 Joystick hot-plugging example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAX_CONTROLLERS 4 | |
class JoypadController { | |
public: | |
JoypadController() : m_is_connected(false), m_gamepad(0), m_instance_id(-1), m_haptic(0) {} | |
int JoypadController::processEvent(const SDL_Event& event); | |
private: | |
SDL_GameController *m_gamepad; | |
SDL_Haptic *m_haptic; | |
SDL_JoystickID m_instance_id; | |
bool m_is_connected; | |
static JoypadController m_controllers[MAX_CONTROLLERS]; | |
static int GetControllerIndex(SDL_JoystickID instance); | |
void Open(int device); | |
void Close(); | |
} | |
// Opens the joystick controller | |
void JoypadController::Open(int device) | |
{ | |
m_gamepad = SDL_GameControllerOpen(device); | |
SDL_Joystick *j = SDL_GameControllerGetJoystick(m_gamepad); | |
m_instance_id = SDL_JoystickInstanceID(j); | |
is_connected = true; | |
if (SDL_JoystickIsHaptic(j)) { | |
m_haptic = SDL_HapticOpenFromJoystick(j); | |
printf("Haptic Effects: %d\n", SDL_HapticNumEffects(m_haptic)); | |
printf("Haptic Query: %x\n", SDL_HapticQuery(m_haptic)); | |
if (SDL_HapticRumbleSupported(m_haptic)) { | |
if (SDL_HapticRumbleInit(m_haptic) != 0) { | |
printf("Haptic Rumble Init: %s\n", SDL_GetError()); | |
SDL_HapticClose(m_haptic); | |
m_haptic = 0; | |
} | |
} else { | |
SDL_HapticClose(m_haptic); | |
m_haptic = 0; | |
} | |
} | |
} | |
void JoypadController::Close() | |
{ | |
if (is_connected) { | |
is_connected = false; | |
if (haptic) { | |
SDL_HapticClose(haptic); | |
haptic = 0; | |
} | |
SDL_GameControllerClose(gamepad); | |
gamepad = 0; | |
} | |
} | |
int JoypadController::GetControllerIndex(SDL_JoystickID instance) | |
{ | |
for (int i = 0; i < MAX_CONTROLLERS; ++i) | |
{ | |
if (ms_controllers[i].m_is_connected && ms_controllers[i].m_instance_id == instance) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
int JoypadController::processEvent(const SDL_Event& event) | |
{ | |
switch (event.type) { | |
case SDL_CONTROLLERAXISMOTION: { | |
// handle axis motion | |
break; | |
} | |
case SDL_CONTROLLERBUTTONDOWN: | |
case SDL_CONTROLLERBUTTONUP: { | |
// handle button up/down | |
break; | |
} | |
case SDL_CONTROLLERDEVICEADDED: { | |
if (event.cdevice.which < MAX_CONTROLLERS ) { | |
JoypadController& jc = s_controllers[event.cdevice.which]; | |
jc.Open(event.cdevice.which); | |
} | |
break; | |
} | |
case SDL_CONTROLLERDEVICEREMOVED: { | |
int cIndex = GetControllerIndex(event.cdevice.which); | |
if (cIndex < 0) return 0; // unknown controller? | |
JoypadController& jc = ms_controllers[cIndex]; | |
jc.Close(); | |
break; | |
} | |
} | |
return 0; | |
} |
Thanks, that helped me a lot with this:
https://github.com/slajerek/MTEngineSDL/blob/master/src/Engine/Core/GAM_GamePads.cpp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, this post was the only helpful discussion of these two events on the whole internet! (SDL_CONTROLLERDEVICEADDED and SDL_CONTROLLERDEVICEREMOVED)