Skip to content

Commit 6dbf61c

Browse files
authored
Merge pull request #38 from klianc09/master
Reduce garbage produced every update
2 parents 21b7c13 + 8072235 commit 6dbf61c

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

gdx-controllers-desktop/src/main/java/com/badlogic/gdx/controllers/desktop/support/JamepadController.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.badlogic.gdx.controllers.ControllerListener;
55
import com.badlogic.gdx.controllers.ControllerMapping;
66
import com.badlogic.gdx.controllers.ControllerPowerLevel;
7+
import com.badlogic.gdx.utils.IntFloatMap;
78
import com.badlogic.gdx.utils.IntMap;
89
import com.badlogic.gdx.utils.Logger;
910
import com.badlogic.gdx.utils.TimeUtils;
@@ -18,20 +19,23 @@ public class JamepadController implements Controller {
1819
private static final IntMap<ControllerButton> CODE_TO_BUTTON = new IntMap<>(ControllerButton.values().length);
1920
private static final IntMap<ControllerAxis> CODE_TO_AXIS = new IntMap<>(ControllerAxis.values().length);
2021
private static final Logger logger = new Logger(JamepadController.class.getSimpleName());
22+
// ControllerButton.values() and ControllerAxis.values() is cached once, to avoid producing garbage every frame
23+
private static final ControllerButton[] CONTROLLER_BUTTON_VALUES = ControllerButton.values();
24+
private static final ControllerAxis[] CONTROLLER_AXIS_VALUES = ControllerAxis.values();
2125

2226
static {
23-
for (ControllerButton button : ControllerButton.values()) {
27+
for (ControllerButton button : CONTROLLER_BUTTON_VALUES) {
2428
CODE_TO_BUTTON.put(button.ordinal(), button);
2529
}
2630

27-
for (ControllerAxis axis : ControllerAxis.values()) {
31+
for (ControllerAxis axis : CONTROLLER_AXIS_VALUES) {
2832
CODE_TO_AXIS.put(axis.ordinal(), axis);
2933
}
3034
}
3135

3236
private final CompositeControllerListener compositeControllerListener = new CompositeControllerListener();
3337
private final IntMap<Boolean> buttonState = new IntMap<>();
34-
private final IntMap<Float> axisState = new IntMap<>();
38+
private final IntFloatMap axisState = new IntFloatMap();
3539
private final String uuid;
3640
private final String name;
3741
private ControllerIndex controllerIndex;
@@ -128,11 +132,11 @@ private ControllerAxis toAxis(int axisCode) {
128132
}
129133

130134
private void updateAxisState() {
131-
for (ControllerAxis axis : ControllerAxis.values()) {
135+
for (ControllerAxis axis : CONTROLLER_AXIS_VALUES) {
132136
int id = axis.ordinal();
133137

134138
float value = getAxis(id);
135-
if (value != axisState.get(id)) {
139+
if (value != axisState.get(id, 0)) {
136140
if (logger.getLevel() == Logger.DEBUG) {
137141
logger.debug("Axis [" + id + " - " + toAxis(id) + "] moved [" + value + "]");
138142
}
@@ -143,7 +147,7 @@ private void updateAxisState() {
143147
}
144148

145149
private void updateButtonsState() {
146-
for (ControllerButton button : ControllerButton.values()) {
150+
for (ControllerButton button : CONTROLLER_BUTTON_VALUES) {
147151
int id = button.ordinal();
148152

149153
boolean pressed = getButton(id);
@@ -163,11 +167,11 @@ private void updateButtonsState() {
163167
}
164168

165169
private void initializeState() {
166-
for (ControllerAxis axis : ControllerAxis.values()) {
167-
axisState.put(axis.ordinal(), 0.0f);
170+
for (ControllerAxis axis : CONTROLLER_AXIS_VALUES) {
171+
axisState.put(axis.ordinal(), 0);
168172
}
169173

170-
for (ControllerButton button : ControllerButton.values()) {
174+
for (ControllerButton button : CONTROLLER_BUTTON_VALUES) {
171175
buttonState.put(button.ordinal(), false);
172176
}
173177
}

0 commit comments

Comments
 (0)