SDL joystick handling with multiple sticks

29423feee2c8904e56009913425351b3
0
3DModelerMan 101 Oct 19, 2008 at 20:47

I’ve been reading the SDL tutorials and I was wondering how I would be able to handle multiple axis events, like if a controller had a left analog stick and a right analog stick, would the right analog stick’s left/right (X/Y)
axis, be “event.jaxis.axis == 5”?.
Thanks:happy: .

7 Replies

Please log in or register to post a reply.

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Oct 19, 2008 at 23:32

You use SDL_JoystickNumAxes to get the number of axes available on the device, and then call SDL_JoystickGetAxis to get the position of the axis you want.

EDIT: It sounds like you are using SDL_JoyAxisEvent. In that case, the axis field does indeed contain the index of the axis in which motion was detected.

29423feee2c8904e56009913425351b3
0
3DModelerMan 101 Oct 20, 2008 at 14:05

Thanks, do you know how I would be able to detect if the button was pressed at the same time as the stick?, just do
“ case SDL_JOYAXISMOTION && SDL_JOYBUTTONDOWN: “
right?.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Oct 20, 2008 at 16:06

I think you want a single & there, not a double &&.

29423feee2c8904e56009913425351b3
0
3DModelerMan 101 Oct 20, 2008 at 17:41

Oh, what’s the difference?, is a single & C where a double and is C++?.
I use C++ so it should work fine. But if does’nt I’ll try a single one.
Thanks:happy: .

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Oct 20, 2008 at 17:55

Actually, never mind what I said. The SDL_Event structure is a discriminated union, so you’ll never get more than one event type at the same time. If you want to test that the user pushes the button and moves the stick in a certain time frame, you’ll have to do it yourself.

(By the way, it has nothing to do with C vs C++. Single & is a bitwise op, double && is a logical op. In this case, I thought you were looking at a bitfield, so the bitwise op would be the appropriate one to use.)

29423feee2c8904e56009913425351b3
0
3DModelerMan 101 Oct 21, 2008 at 00:06

Oh, thanks for the help, I’ll have a bool variable, that is set to true when a button is pressed and then reset, I’ll also try something similar for the analog.

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Oct 21, 2008 at 14:19

Right, you will have to track the state of the buttons as you receive button up/down events and then check the stored button state when you receive axis events. Alternatively, you can turn-off joystick events and poll the state of the joystick as needed.