Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SDL正在发送错误的控制器索引_C++_C_Sdl - Fatal编程技术网

C++ SDL正在发送错误的控制器索引

C++ SDL正在发送错误的控制器索引,c++,c,sdl,C++,C,Sdl,我正在创建一个简单的游戏,并使用SDL2处理来自控制器的输入。问题是SDL没有为控制器提供正确的索引,这导致我的游戏崩溃 /receive the controller index given by SDL int cIdx = evt.cdevice.which; switch (evt.type) { case SDL_CONTROLLERAXISMOTION: { //......... break; } case SDL_

我正在创建一个简单的游戏,并使用SDL2处理来自控制器的输入。问题是SDL没有为控制器提供正确的索引,这导致我的游戏崩溃

/receive the controller index given by SDL
int cIdx = evt.cdevice.which;

switch (evt.type)
{
    case SDL_CONTROLLERAXISMOTION:
    {
        //.........
        break;
    }
    case SDL_CONTROLLERBUTTONUP:
    {

        InputButton sender = InputButton(evt.cbutton.button);

        controllerStates[cIdx]->SetButtonState(sender, false);
        break;
    }
    case SDL_CONTROLLERBUTTONDOWN:
    {
        if (evt.cbutton.button != SDL_CONTROLLER_BUTTON_INVALID)
        {
            InputButton sender = InputButton(evt.cbutton.button);

            controllerStates[cIdx]->SetButtonState(sender, true);
        }
        break;
    }
    case SDL_CONTROLLERDEVICEADDED:
    {
        if (SDL_IsGameController(cIdx))
        {
            SDL_GameController * controller = SDL_GameControllerOpen(cIdx);

            AddController(controller);
        }
        break;
    }
    case SDL_CONTROLLERDEVICEREMOVED:
    {
        RemoveController(controllers[(cIdx)]);
        break;
    }
}

当用户第一次添加控制器时,此功能正常工作,SDL将0作为SDL_CONTROLLERDEVICEADDED事件中的索引发送给我,对于其他事件也发送0。 问题是,如果用户尝试断开并重新连接控制器,SDL将0作为SDL_CONTROLLERDEVICEADDED事件中的索引发送,并将1作为导致游戏崩溃的其他事件的索引发送

/receive the controller index given by SDL
int cIdx = evt.cdevice.which;

switch (evt.type)
{
    case SDL_CONTROLLERAXISMOTION:
    {
        //.........
        break;
    }
    case SDL_CONTROLLERBUTTONUP:
    {

        InputButton sender = InputButton(evt.cbutton.button);

        controllerStates[cIdx]->SetButtonState(sender, false);
        break;
    }
    case SDL_CONTROLLERBUTTONDOWN:
    {
        if (evt.cbutton.button != SDL_CONTROLLER_BUTTON_INVALID)
        {
            InputButton sender = InputButton(evt.cbutton.button);

            controllerStates[cIdx]->SetButtonState(sender, true);
        }
        break;
    }
    case SDL_CONTROLLERDEVICEADDED:
    {
        if (SDL_IsGameController(cIdx))
        {
            SDL_GameController * controller = SDL_GameControllerOpen(cIdx);

            AddController(controller);
        }
        break;
    }
    case SDL_CONTROLLERDEVICEREMOVED:
    {
        RemoveController(controllers[(cIdx)]);
        break;
    }
我还可以简单地检查索引是否可以避免崩溃,但这将是无用的,因为所有控制器事件都将被忽略

任何帮助都将不胜感激

谢谢

根据SDL,SDL_GameControllerOpen上使用的索引不是在未来事件中识别控制器的索引。因此,您需要使用操纵杆id

    switch (evt.type)
    {
        case SDL_CONTROLLERAXISMOTION:
        {
            //...
            break;
        }
        case SDL_CONTROLLERBUTTONUP:
        {
            //Get the joystick id from the controller index 
            //....
            break;
        }
        case SDL_CONTROLLERBUTTONDOWN:
        {
            //Get the joystick id from the controller index 
            //....
            break;
        }
        case SDL_CONTROLLERDEVICEADDED:
        {
            if (SDL_IsGameController(cIdx))
            {
                SDL_GameController * controller = SDL_GameControllerOpen(cIdx);
                SDL_Joystick* j      = SDL_GameControllerGetJoystick(controller);
                SDL_JoystickID joyId = SDL_JoystickInstanceID(j);

                //Save the joystick id to used in the future events
                AddController(controller);
            }
            break;
        }
        case SDL_CONTROLLERDEVICEREMOVED:
        {
             //Get the joystick id from the controller index 

            break;
        }
   }
根据SDL,SDL_GameControllerOpen上使用的索引不是在未来事件中识别控制器的索引。因此,您需要使用操纵杆id

    switch (evt.type)
    {
        case SDL_CONTROLLERAXISMOTION:
        {
            //...
            break;
        }
        case SDL_CONTROLLERBUTTONUP:
        {
            //Get the joystick id from the controller index 
            //....
            break;
        }
        case SDL_CONTROLLERBUTTONDOWN:
        {
            //Get the joystick id from the controller index 
            //....
            break;
        }
        case SDL_CONTROLLERDEVICEADDED:
        {
            if (SDL_IsGameController(cIdx))
            {
                SDL_GameController * controller = SDL_GameControllerOpen(cIdx);
                SDL_Joystick* j      = SDL_GameControllerGetJoystick(controller);
                SDL_JoystickID joyId = SDL_JoystickInstanceID(j);

                //Save the joystick id to used in the future events
                AddController(controller);
            }
            break;
        }
        case SDL_CONTROLLERDEVICEREMOVED:
        {
             //Get the joystick id from the controller index 

            break;
        }
   }