gpt4 book ai didi

c++ - 为什么 Xbox Controller 没有响应?

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:32 25 4
gpt4 key购买 nike

我正在学习 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";
}

最佳答案

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

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)
// ...
}
}

关于c++ - 为什么 Xbox Controller 没有响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com