Joysticks - How can I get more than one working
devo_au

Hey,

I'm making a game that has 2 or more players using gamepads.
I can't find an event

ALLEGRO_EVENT ev; 
if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN)
{}

that allows me to determine
what gamepad the button presses are occurring from.

If I use states

//im using all the init stuff correctly, 
//this is just a snippit to explain what im up to
ALLEGRO_JOYSTICK_STATE joyState1;
al_get_joystick_state(joy1, &joyState1);
cout << joyState1.button << "\n";

I get the same output from all the main buttons (ie. 00E9A5D8)
but for some reason it can read select and the triggers.

Hope someone can help - Thanks Dev

RPG Hacker

Have you iterated over all joysticks Allegro returns to you? Maybe it's polling some data from a weird device. Or maybe it's a driver issue of some sorts.

As for using ALLEGRO_EVENTs, this page has some more information for you:
Manual
Basically, once you detect a ALLEGRO_EVENT_JOYSTICK_AXIS, ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN or ALLEGRO_EVENT_JOYSTICK_BUTTON_UP event, you can check joystick.id to get the ID of the joystick. I'm not sure if ALLEGRO_EVENT already has a joystick field right away or if you need to cast it to another type, but I'd expect that it just uses a union internally and that it already has the joystick field, so you can just check that.

Or in other words: ev.joystick.id

Edgar Reynaldo

Yes, RPG Hacker is correct. Use ev.joystick.id to get the joystick id. And with joystick button events, use ev.joystick.button to get the button pressed.

As for al_get_joystick_state, you're not using it correctly. joystick_state.button is an array, not a bool value indicating whether a button was pressed. You need to access the specific button that you are asking for first :

for (int i = 0 ; i < al_get_num_joysticks() ; ++i) {
   ALLEGRO_JOYSTICK* joy = al_get_joystick(i);
   ALLEGRO_JOYSTICK_STATE joy_state;
   al_get_joystick_state(joy , &joy_state);
   for (int j = 0 ; j < al_get_joystick_num_buttons ; ++j) {
      if (joy_state.button[j]) {
         printf("Joystick number %d has button %d pressed.\n" , i , j);
      }
   }
}

devo_au

Awesome thank you for your replies..

RPG - I didn't know about ev.joystick.id. Iterating them. Hmmm...
Edgar - Ah! that hex number is an address of a array.

Thanks for that loop Edgar. So thats what RPG meant.
Plugged it in - problem solved. Just have to hook it
up to my players.

Cheers guys. You have helped me heaps.. Dev

Thread #616346. Printed from Allegro.cc