Joystick lag (Windows)
Andrew Gillett

I'm testing Allegro 5 joystick code using three controllers. With two of them, there are frequently random delays in input changes being recognised – delays of up to about a second. The other works flawlessly all the time. I have tested them with Windows 10's "set up USB game controllers" , and they all respond immediately with no lag.

Here's the relevant part of my code:

ALLEGRO_EVENT e;
while (al_get_next_event(eventQueue, &e))
{
switch (e.type)
{
case ALLEGRO_EVENT_JOYSTICK_AXIS:
for(auto& a : actors)
if (e.joystick.id == a->joy)
{
ALLEGRO_JOYSTICK_STATE js;
al_get_joystick_state(a->joy, &js);
a->vel.x = js.stick[0].axis[0] * 100;
a->vel.y = js.stick[0].axis[1] * 100;
}
break;
}
}

Edgar Reynaldo

What does the rest of your event loop look like?

Andrew Gillett

Like this, with the code above inside Advance.

while(true)
{
float deltaTime = m_timingManager->GetDeltaTime();
Advance(deltaTime);
Render();
al_flip_display();
}

relpatseht

So, you're loping forever as long as there are new joystick events, then rendering. As long as the joystick is held down, you're going to get new events. Maybe just look for 1 event of each type.

Andrew Gillett

This isn't a case of getting stuck in the event loop, because when this lag problem occurs, the rest of the game carries on running normally. I can also see that if I output the number of events per frame, events only occur when the stick position has changed.

Edgar Reynaldo

The joystick state is not guaranteed to be current. If an event has occurred since the state was asked for, then it is no longer current. Use the event fields in the ALLEGRO_EVENT_JOYSTICK_AXIS event.

Andrew Gillett

Code now looks like this, but the lag is still there (always with the same two controllers, and not the other).

case ALLEGRO_EVENT_JOYSTICK_AXIS:
for(auto& a : actors)
if (e.joystick.id == a->joy)
{
switch (e.joystick.axis)
{
case 0: a->vel.x = e.joystick.pos * 100; break;
case 1: a->vel.y = e.joystick.pos * 100; break;
}

The problem does not occur with Allegro 4.

Dizzy Egg

I'm a bit high and all that, and not really a master when it comes to allegro5 events, but it's not a mutex/re-entry thing with the auto for loop is it? Do things improve if you do this?

ALLEGRO_JOYSTICK_STATE js;
al_get_joystick_state(actor[e.joystick.id].joy, &js);
actor[e.joystick.id].vel.x = js.stick[0].axis[0] * 100;
actor[e.joystick.id].vel.y = js.stick[0].axis[1] * 100;
}

or somehing similar....etc

Edgar Reynaldo

Please post the specific type of joysticks that you are using. Brand, model, etc...

Also, are you seeing delays in axis reporting or button presses or what?

Andrew Gillett

The lag occurs with both axis movement and button presses.

The only controller which operates with no lag 100% of the time is this:
https://www.amazon.co.uk/gp/product/B07BJ273SC/
It's a clone of the Xbox 360 controller and uses the same driver. In the Game Controllers control panel, it is listed as "Controller (Inno GamePad..)". In my test game, the name is reported as "XInput Joystick".

The second controller, which does have the random lag, is this:
https://www.amazon.co.uk/gp/product/B01HTBX9AK/all
The brand name shown on Amazon is CSL. It is listed in Device Manager as "USB Input Device". In the game controllers control panel, and in my test game, it is listed as "USB Joystick".

The third controller, which also has the lag, is:
https://www.amazon.co.uk/gp/product/B00VPMT68E/
The brand name is Kabalo. As with the device above, the names in Device Manager and Game Controllers/test game are "USB Input Device" and "USB Joystick". The controller has an 'ANALOG' button - with this disabled, analog stick input is received as digital input. The state of the ANALOG button makes no difference to the lag issue.

The last controller is where things get even weirder. It's a genuine PS4 controller, connected via USB and operating via DS4Windows. In my test game, this is recognised as two separate controllers, named "XInput Joystick" and "Wireless Controller". The left analog stick controls two players. The former does not exhibit the lag issue, although does have an issue with the player position creeping a small amount when the stick is supposedly centred. The latter does exhibit the lag issue, but does not have the same issue when the stick is centred. In the Game Controllers control panel, it is listed under both "Controller (Xbox 360 for Windows)" and "Wireless Controller".

It's interesting to note that the two controllers which don't exhibit the lag issue are both reported by Allegro as being named "XInput Joystick". The second and third controllers appear to only support DirectInput, so I was going to suggest that it's an issue with DirectInput. Regarding the PS4 controller, I wondered if the it was being recognised both as an XInput and DirectInput controller. But it also shows up twice in a separate game controller tester application which only supports XInput controllers. And finally, in an Allegro 4 game, the PS4 controller is also recognised as two controllers. As in my Allegro 5 test app, the left stick controls two players once – the only difference being that each player recognises a different bus and as being button 1.

Edgar Reynaldo

I suggest filing a bug report on the issue tracker on github here :

https://github.com/liballeg/allegro5/issues

I can't really help much. I have a joystick I can try with allegro and see what it does.

Can you post code for a simple program that I can compile and test that exhibits the issue described?

beoran is our local joystick guru wonder if he's hanging about somewhere.

Try ex_joystick_events and see if it works for those sticks.

Andrew Gillett

The lag issue also happens with both of the joystick example programs. I will file a report on Github.

The issue with the PS4 controller being recognised as two controllers has been resolved by closing the DS4Windows application.

UPDATE: The problem does not occur with my two other PCs. All PCs are on Windows 10.

UPDATE: Issue opened here: https://github.com/liballeg/allegro5/issues/1026
I did some investigation and found some possible leads.

Thread #617750. Printed from Allegro.cc