From 807223566fe20b8c9a00177a5a3d194f4f70028e Mon Sep 17 00:00:00 2001 From: Anton Klinger Date: Wed, 13 Dec 2023 11:01:21 +0100 Subject: [PATCH] add comment to explain why these constants were introduced also use the constants instead of ControllerButton.values() consistently throughout the file --- .../controllers/desktop/support/JamepadController.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gdx-controllers-desktop/src/main/java/com/badlogic/gdx/controllers/desktop/support/JamepadController.java b/gdx-controllers-desktop/src/main/java/com/badlogic/gdx/controllers/desktop/support/JamepadController.java index e8401db..d354467 100644 --- a/gdx-controllers-desktop/src/main/java/com/badlogic/gdx/controllers/desktop/support/JamepadController.java +++ b/gdx-controllers-desktop/src/main/java/com/badlogic/gdx/controllers/desktop/support/JamepadController.java @@ -19,15 +19,16 @@ public class JamepadController implements Controller { private static final IntMap CODE_TO_BUTTON = new IntMap<>(ControllerButton.values().length); private static final IntMap CODE_TO_AXIS = new IntMap<>(ControllerAxis.values().length); private static final Logger logger = new Logger(JamepadController.class.getSimpleName()); + // ControllerButton.values() and ControllerAxis.values() is cached once, to avoid producing garbage every frame private static final ControllerButton[] CONTROLLER_BUTTON_VALUES = ControllerButton.values(); private static final ControllerAxis[] CONTROLLER_AXIS_VALUES = ControllerAxis.values(); static { - for (ControllerButton button : ControllerButton.values()) { + for (ControllerButton button : CONTROLLER_BUTTON_VALUES) { CODE_TO_BUTTON.put(button.ordinal(), button); } - for (ControllerAxis axis : ControllerAxis.values()) { + for (ControllerAxis axis : CONTROLLER_AXIS_VALUES) { CODE_TO_AXIS.put(axis.ordinal(), axis); } } @@ -166,11 +167,11 @@ private void updateButtonsState() { } private void initializeState() { - for (ControllerAxis axis : ControllerAxis.values()) { + for (ControllerAxis axis : CONTROLLER_AXIS_VALUES) { axisState.put(axis.ordinal(), 0); } - for (ControllerButton button : ControllerButton.values()) { + for (ControllerButton button : CONTROLLER_BUTTON_VALUES) { buttonState.put(button.ordinal(), false); } }