RobertBeckebans / RBDOOM-3-BFG

Doom 3 BFG Edition source port with updated DX12 / Vulkan renderer and modern game engine features
https://www.moddb.com/mods/rbdoom-3-bfg
GNU General Public License v3.0
1.47k stars 253 forks source link

Controller/Joystick input added twice to SDL joystick_polls and causes assert #942

Open SRSaunders opened 4 days ago

SRSaunders commented 4 days ago

When testing controller/joytick input using SDL, there is something strange that adds controller button input events twice in sysEvent_t Sys_GetEvent() and causes an assert later inside idUsercmdGenLocal::Joystick():

case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
    static int controllerButtonRemap[][2] =
    {
        {K_JOY1, J_ACTION1},
        {K_JOY2, J_ACTION2},
        {K_JOY3, J_ACTION3},
        {K_JOY4, J_ACTION4},
        {K_JOY9, J_ACTION9},
        {K_JOY11, J_ACTION11},
        {K_JOY10, J_ACTION10},
        {K_JOY7, J_ACTION7},
        {K_JOY8, J_ACTION8},
        {K_JOY5, J_ACTION5},
        {K_JOY6, J_ACTION6},
        {K_JOY_DPAD_UP, J_DPAD_UP},
        {K_JOY_DPAD_DOWN, J_DPAD_DOWN},
        {K_JOY_DPAD_LEFT, J_DPAD_LEFT},
        {K_JOY_DPAD_RIGHT, J_DPAD_RIGHT},
    };
    joystick_polls.Append( joystick_poll_t( controllerButtonRemap[ev.cbutton.button][1], ev.cbutton.state == SDL_PRESSED ? 1 : 0 ) );

    res.evType = SE_KEY;
    res.evValue = controllerButtonRemap[ev.cbutton.button][0];
    res.evValue2 = ev.cbutton.state == SDL_PRESSED ? 1 : 0;

--> joystick_polls.Append( joystick_poll_t( res.evValue, res.evValue2 ) );
    return res;

The first call to joystick_polls.Append() properly adds a mapped joystick J_* value for the joystick buttons. The second call to joystick_polls.Append() incorrectly adds a mapped key K_* value (>= 256) which is out of bounds for idUsercmdGenLocal::Joystick() and causes an assert in that code. Commenting out the above line results in correct behaviour in my testing.

Why is this second call to joystick_polls.Append() in the code? Is there some reason for this that I am missing, or can we comment out or remove this line?

RobertBeckebans commented 4 days ago

I haven't tested the gamepad code for a very long time on Linux. I'll have a look at it next week. IIRC most button mappings were wrong compared to the handling on Windows.

SRSaunders commented 4 days ago

Using a PS4 controller attached via USB, I have tested using Win10+DS4Windows, Manjaro Linux, and macOS Ventura. With my change above (i.e. remove highlighted second call to joystick_polls.Append() in sysEvent_t Sys_GetEvent()), the mappings and behaviour on Linux and macOS (i.e. SDL2) are almost identical to Windows. Seems to work fine.

I don't have a modern Xbox controller to test with, so I can't confirm for that input device.