1

我正在按照CodeProject的教程进行输入,并认为我会自己做一些。所以我尝试生成以下代码,但是当我按下方向键或其他按钮时,没有任何反应。但是当我按下开始和返回按钮时,它会注册。提前致谢。

CXBOXController班级

#ifndef _XBOX_CONTROLLER_H_
#define _XBOX_CONTROLLER_H_

// No MFC
#define WIN32_LEAN_AND_MEAN

// We need the Windows Header and the XInput Header
#include <windows.h>
#include <XInput.h>

// Now, the XInput Library
// NOTE: COMMENT THIS OUT IF YOU ARE NOT USING
// A COMPILER THAT SUPPORTS THIS METHOD OF LINKING LIBRARIES
#pragma comment(lib, "XInput.lib")

// XBOX Controller Class Definition
class CXBOXController
{
private:
XINPUT_STATE _controllerState;
int _controllerNum;
public:
CXBOXController(int playerNumber);
XINPUT_STATE GetState();
bool IsConnected();
void Vibrate(int leftVal = 0, int rightVal = 0);
};

#endif

CXBOXController::CXBOXController(int playerNumber)
{
// Set the Controller Number
_controllerNum = playerNumber - 1;
}

XINPUT_STATE CXBOXController::GetState()
{
// Zeroise the state
ZeroMemory(&_controllerState, sizeof(XINPUT_STATE));

// Get the state
XInputGetState(_controllerNum, &_controllerState);

return _controllerState;
}

bool CXBOXController::IsConnected()
{
// Zeroise the state
ZeroMemory(&_controllerState, sizeof(XINPUT_STATE));

// Get the state
DWORD Result = XInputGetState(_controllerNum, &_controllerState);

if(Result == ERROR_SUCCESS)
{
    return true;
}
else
{
    return false;
}
}

void CXBOXController::Vibrate(int leftVal, int rightVal)
{
// Create a Vibraton State
XINPUT_VIBRATION Vibration;

// Zeroise the Vibration
ZeroMemory(&Vibration, sizeof(XINPUT_VIBRATION));

// Set the Vibration Values
Vibration.wLeftMotorSpeed = leftVal;
Vibration.wRightMotorSpeed = rightVal;

// Vibrate the controller
XInputSetState(_controllerNum, &Vibration);
}

代码

                Player1 = new CXBOXController(1);

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
                {
                    std::cout << "Up";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
                {
                    std::cout << "Down";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
                                    {
                    std::cout << "Right";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
                {
                    std::cout << "Left";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B)
                {
                    std::cout << "Right-Click";
                }

                if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
                {
                    std::cout << "Click";
                }
4

1 回答 1

1

我在我的游戏引擎中使用以下代码;区分上升键和下降键的技巧是测量游戏手柄前后的状态:

void Nitro::GamePadState::GetState(u32 playerIndex)
{
    XINPUT_STATE xinputState;

    DWORD result = XInputGetState(playerIndex, &xinputState);

    if (result == ERROR_SUCCESS)
    {
        Triggers.Left = xinputState.Gamepad.bLeftTrigger;
        Triggers.Right = xinputState.Gamepad.bRightTrigger;
        ThumbSticks.Left = short2 (xinputState.Gamepad.sThumbLX, xinputState.Gamepad.sThumbLY);
        ThumbSticks.Right = short2 (xinputState.Gamepad.sThumbRX, xinputState.Gamepad.sThumbRY);
        mButtonStates.Update(xinputState.Gamepad.wButtons);
    }
}

class NitroAPI ButtonState
{
private:
    u32 mButtonStates;      // Current buttons' state
    u32 mPrevButtonStates;  // Previous buttons' state
    u32 mButtonDowns;       // 1 = button pressed this frame
    u32 mButtonUps;         // 1 = button released this frame

public:
    ButtonState();
    void Update(u32 newState);

    bool IsButtonDown(Buttons button) const;
    bool IsButtonFalling(Buttons button) const;
    bool IsButtonUp(Buttons button) const;
    bool IsButtonRising(Buttons button) const;
};

重要的方法是 Update 和 IsButton*。I XOR 前一个和当前按钮状态以区分发生了什么变化:

void Nitro::ButtonState::Update(u32 newState)
{
    mPrevButtonStates = mButtonStates;
    mButtonStates = newState;

    u32 buttonChanges = mButtonStates ^ mPrevButtonStates;
    mButtonDowns = buttonChanges & mButtonStates;
    mButtonUps = buttonChanges & (~mButtonStates);
}

然后简单的比较一下,看看哪个按钮变了:

bool Nitro::ButtonState::IsButtonFalling(Buttons button) const
{
    return ((mButtonStates & button) != 0) && ((mPrevButtonStates & button) == 0);
}

bool Nitro::ButtonState::IsButtonRising(Buttons button) const
{
    return ((mButtonStates & button) == 0) && ((mPrevButtonStates & button) != 0);
}

bool Nitro::ButtonState::IsButtonDown(Buttons button) const
{
    return ((mButtonStates & button) != 0);
}

bool Nitro::ButtonState::IsButtonUp(Buttons button) const
{
    return ((mButtonStates & button) == 0);
}

在您的情况下,您可以通过将代码放在 while 循环中来缓解问题,或者使用类似于我发布的内容来跟踪瞬态:

while (true)
{
    DWORD Result = XInputGetState(_controllerNum, &_controllerState);
    if (result == ERROR_SUCCESS)
    {
        if(_controllerState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
        // ...
    }
}
于 2012-07-08T10:43:36.643 回答