[CLOSED] glfwGetJoystickButtons IndexOutOfBoundsException

Started by Zellcore, May 20, 2016, 13:51:47

Previous topic - Next topic

Zellcore

I am using a Xbox One controller. So I followed the Input handling guide https://github.com/LWJGL/lwjgl3-wiki/wiki/2.6.3-Input-handling-with-GLFW#joystick-input to learn about it. Now when I want to use D-Pad Right, D-Pad Down or D-Pad Left I get an IndexOutOfBoundsException.
When I use limits() it returns 14 how it should. So in the ByteBuffer index 11,12 and 13 are unreachable using lwjgl 3.0.0b.
I donÃ,´t know what to do because everything else works fine ! Is it a GLFW issue or do I miss something?

> Exception in thread "Game Thread" java.lang.IndexOutOfBoundsException
   at java.nio.Buffer.checkIndex(Buffer.java:546)
   at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:685)
   at engine.input.XboxController.DPad_Down_Button_Pressed(XboxController.java:68)
   at example.Pong.loop(Pong.java:49)
   at example.Pong.update(Pong.java:34)
   at example.Pong.run(Pong.java:118)
   at java.lang.Thread.run(Thread.java:745)

One more. ShouldnÃ,´t the Buffer be bigger? Like 16 entries for the left and right trigger?
And one fix for the guide. It would be nice to start counting from 0. Because later there is the sentence "On index 8 and 9. It does only return 1 when you press down the stick, not moving it around." which is correct when you start by zero. But it is a little bit confusing if you look at the table above.

Hope you can help me.

Kai

QuoteWhen I use limits() it returns 14 how it should. So in the ByteBuffer index 11,12 and 13 are unreachable using lwjgl 3.0.0b.
This sentence I don't quite understand. When limit() returns 14 then indexes 11, 12 and 13 are available/reachable/defined and accessing them should not result in an exception.
What index do you get an IndexOutOfBoundsException with, exactly?

Generally, LWJGL just returns a ByteBuffer that has the size reported by GLFW. If GLFW's glfwGetJoystickButtons() reports 14, then the returned ByteBuffer of LWJGL's glfwGetJoystickButtons() will have a capacity/limit/size of 14.

abcdef

at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:685)


Shouldn't you use getByte() instead of getInt()?

Zellcore

Quote from: Kai on May 20, 2016, 14:19:10
QuoteWhen I use limits() it returns 14 how it should. So in the ByteBuffer index 11,12 and 13 are unreachable using lwjgl 3.0.0b.
This sentence I don't quite understand. When limit() returns 14 then indexes 11, 12 and 13 are available/reachable/defined and accessing them should not result in an exception.
What index do you get an IndexOutOfBoundsException with, exactly?

Generally, LWJGL just returns a ByteBuffer that has the size reported by GLFW. If GLFW's glfwGetJoystickButtons() reports 14, then the returned ByteBuffer of LWJGL's glfwGetJoystickButtons() will have a capacity/limit/size of 14.
I had the same problem understanding it. I tried to explain that when I used
public boolean DPad_Right_Button_Pressed() {
        return bbuf.get(11) == 1;
    }

I get the Exception.  Where bbuf is the ByteBuffer I get using glfwGetJoystickButtons(). The same for bbuf.get(12) [D-Pad Down] and bbuf.get(13)[D-Pad Left]. So the Buffer has a size of 14 but throws the Exception using an index greater 10.



Quote from: abcdef on May 20, 2016, 15:46:18
at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:685)


Shouldn't you use getByte() instead of getInt()?
Well fixing the problem was way more easier. Just use get(int index). Finish. There is no getByte(). I guess I missed it when I went trough the methods. Awkward.....

But whats more interesting now is, what is the difference between get(), getByte(), getInt() that it came to that Exception?

And after reading the guide again I saw that LT and RT are in glfwGetJoystickAxes() which should be obvious  ;D

Thanks for your help.

Kai

Ah, sorry, I did not really see from your stacktrace above, that you were using getInt(). You cannot use getInt(), because the data in the ByteBuffer are just bytes.
getInt() will read 4 bytes and decode them as 32-bit signed integer. But this is not how GLFW encodes the data into the ByteBuffer. They are bytes. So you _must_ use ByteBuffer.get().

abcdef

You are correct, there is no getByte(). Just wanted to make it clear that you wanted a method that returned a byte rather than an int