forked from Clones/Controlify
🐛 Fix crashing with some joysticks
This commit is contained in:
@ -12,10 +12,7 @@ import org.lwjgl.glfw.GLFW;
|
|||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class JoystickState implements ControllerState {
|
public class JoystickState implements ControllerState {
|
||||||
@ -73,32 +70,32 @@ public class JoystickState implements ControllerState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static JoystickState fromJoystick(JoystickController<?> joystick, int joystickId) {
|
public static JoystickState fromJoystick(JoystickController<?> joystick, int joystickId) {
|
||||||
FloatBuffer axesBuffer = GLFW.glfwGetJoystickAxes(joystickId);
|
Optional<FloatBuffer> axesBuffer = Optional.ofNullable(GLFW.glfwGetJoystickAxes(joystickId));
|
||||||
float[] inAxes = new float[axesBuffer.limit()];
|
float[] inAxes = new float[axesBuffer.map(FloatBuffer::limit).orElse(0)];
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (axesBuffer.hasRemaining()) {
|
while (axesBuffer.isPresent() && axesBuffer.get().hasRemaining()) {
|
||||||
inAxes[i] = axesBuffer.get();
|
inAxes[i] = axesBuffer.get().get();
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer buttonBuffer = GLFW.glfwGetJoystickButtons(joystickId);
|
Optional<ByteBuffer> buttonBuffer = Optional.ofNullable(GLFW.glfwGetJoystickButtons(joystickId));
|
||||||
boolean[] inButtons = new boolean[buttonBuffer.limit()];
|
boolean[] inButtons = new boolean[axesBuffer.map(FloatBuffer::limit).orElse(0)];
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (buttonBuffer.hasRemaining()) {
|
while (buttonBuffer.isPresent() && buttonBuffer.get().hasRemaining()) {
|
||||||
inButtons[i] = buttonBuffer.get() == GLFW.GLFW_PRESS;
|
inButtons[i] = buttonBuffer.get().get() == GLFW.GLFW_PRESS;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer hatBuffer = GLFW.glfwGetJoystickHats(joystickId);
|
Optional<ByteBuffer> hatBuffer = Optional.ofNullable(GLFW.glfwGetJoystickHats(joystickId));
|
||||||
HatState[] inHats = new HatState[hatBuffer.limit()];
|
HatState[] inHats = new HatState[hatBuffer.map(ByteBuffer::limit).orElse(0)];
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (hatBuffer.hasRemaining()) {
|
while (hatBuffer.isPresent() && hatBuffer.get().hasRemaining()) {
|
||||||
var state = switch (hatBuffer.get()) {
|
var state = switch (hatBuffer.get().get()) {
|
||||||
case GLFW.GLFW_HAT_CENTERED -> HatState.CENTERED;
|
case GLFW.GLFW_HAT_CENTERED -> HatState.CENTERED;
|
||||||
case GLFW.GLFW_HAT_UP -> HatState.UP;
|
case GLFW.GLFW_HAT_UP -> HatState.UP;
|
||||||
case GLFW.GLFW_HAT_RIGHT -> HatState.RIGHT;
|
case GLFW.GLFW_HAT_RIGHT -> HatState.RIGHT;
|
||||||
|
Reference in New Issue
Block a user