Skip to content

Commit

Permalink
reduce the garbage produced every update
Browse files Browse the repository at this point in the history
cache ControllerButton.values() and ControllerAxis.values() to prevent creating garbage
also axis values are no longer boxed into Float objects every update
  • Loading branch information
klianc09 committed Jun 24, 2023
1 parent 94636b5 commit 6c8eed0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.badlogic.gdx.controllers.desktop.support;

public class FloatWrapper {
public float value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class JamepadController implements Controller {
private static final IntMap<ControllerButton> CODE_TO_BUTTON = new IntMap<>(ControllerButton.values().length);
private static final IntMap<ControllerAxis> CODE_TO_AXIS = new IntMap<>(ControllerAxis.values().length);
private static final Logger logger = new Logger(JamepadController.class.getSimpleName());
private static final ControllerButton[] CONTROLLER_BUTTON_VALUES = ControllerButton.values();
private static final ControllerAxis[] CONTROLLER_AXIS_VALUES = ControllerAxis.values();

static {
for (ControllerButton button : ControllerButton.values()) {
Expand All @@ -31,7 +33,7 @@ public class JamepadController implements Controller {

private final CompositeControllerListener compositeControllerListener = new CompositeControllerListener();
private final IntMap<Boolean> buttonState = new IntMap<>();
private final IntMap<Float> axisState = new IntMap<>();
private final IntMap<FloatWrapper> axisState = new IntMap<>();
private final String uuid;
private final String name;
private ControllerIndex controllerIndex;
Expand Down Expand Up @@ -128,22 +130,23 @@ private ControllerAxis toAxis(int axisCode) {
}

private void updateAxisState() {
for (ControllerAxis axis : ControllerAxis.values()) {
for (ControllerAxis axis : CONTROLLER_AXIS_VALUES) {
int id = axis.ordinal();

float value = getAxis(id);
if (value != axisState.get(id)) {
FloatWrapper axisValueWrapper = axisState.get(id);
if (value != axisValueWrapper.value) {
if (logger.getLevel() == Logger.DEBUG) {
logger.debug("Axis [" + id + " - " + toAxis(id) + "] moved [" + value + "]");
}
compositeControllerListener.axisMoved(this, id, value);
}
axisState.put(id, value);
axisValueWrapper.value = value;
}
}

private void updateButtonsState() {
for (ControllerButton button : ControllerButton.values()) {
for (ControllerButton button : CONTROLLER_BUTTON_VALUES) {
int id = button.ordinal();

boolean pressed = getButton(id);
Expand All @@ -164,7 +167,7 @@ private void updateButtonsState() {

private void initializeState() {
for (ControllerAxis axis : ControllerAxis.values()) {
axisState.put(axis.ordinal(), 0.0f);
axisState.put(axis.ordinal(), new FloatWrapper());
}

for (ControllerButton button : ControllerButton.values()) {
Expand Down

0 comments on commit 6c8eed0

Please sign in to comment.