我正在CodeProject点击教程并提供输入,并认为我会自己做一些。所以我试图生成以下代码,但是当我按下D-pad或其他按钮时没有任何反应。但是当我按下“开始”和“返回”按钮时,它会注册。提前谢谢。
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";
}
答案 0 :(得分: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 *。我对前一个和当前按钮状态进行异或,以区分变化的内容:
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)
// ...
}
}