Skip to content

Commit

Permalink
SDL: Add axis event deduplication
Browse files Browse the repository at this point in the history
We now do this in the backedns instead of centrally since on some backends this is more efficient.
  • Loading branch information
hrydgard committed Nov 16, 2023
1 parent 931fdfb commit 4be1706
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 21 additions & 8 deletions SDL/SDLJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,28 @@ void SDLJoystick::ProcessInput(const SDL_Event &event){
}
break;
case SDL_CONTROLLERAXISMOTION:
AxisInput axis;
// TODO: Can we really cast axis events like that? Do they match?
axis.axisId = (InputAxis)event.caxis.axis;
axis.value = event.caxis.value / 32767.0f;
if (axis.value > 1.0f) axis.value = 1.0f;
if (axis.value < -1.0f) axis.value = -1.0f;
axis.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.caxis.which);
NativeAxis(&axis, 1);
{
InputDeviceID deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.caxis.which);
// TODO: Can we really cast axis IDs like that? Do they match?
InputAxis axisId = (InputAxis)event.caxis.axis;
float value = event.caxis.value * (1.f / 32767.f);
if (value > 1.0f) value = 1.0f;
if (value < -1.0f) value = -1.0f;
// Filter duplicate axis values.
auto key = std::pair<InputDeviceID, InputAxis>(deviceId, axisId);
auto iter = prevAxisValue_.find(key);
if (iter == prevAxisValue_.end()) {
prevAxisValue_[key] = value;
} else if (iter->second != value) {
iter->second = value;
AxisInput axis;
axis.axisId = axisId;
axis.value = value;
axis.deviceId = deviceId;
NativeAxis(&axis, 1);
} // else ignore event.
break;
}
case SDL_CONTROLLERDEVICEREMOVED:
// for removal events, "which" is the instance ID for SDL_CONTROLLERDEVICEREMOVED
for (auto it = controllers.begin(); it != controllers.end(); ++it) {
Expand Down
4 changes: 4 additions & 0 deletions SDL/SDLJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class SDLJoystick{
void setUpControllers();
InputKeyCode getKeycodeForButton(SDL_GameControllerButton button);
int getDeviceIndex(int instanceId);

bool registeredAsEventHandler;
std::vector<SDL_GameController *> controllers;
std::map<int, int> controllerDeviceMap;

// Deduplicate axis events. Pair is device, axis.
std::map<std::pair<InputDeviceID, InputAxis>, float> prevAxisValue_;
};

0 comments on commit 4be1706

Please sign in to comment.