gpt4 book ai didi

C++ Directx鼠标输入在失去焦点后表现得很奇怪

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

您好,在失去窗口焦点(alt+tab/windows 键)后,我的鼠标输入出现了一个小问题。

焦点回到游戏窗口后,鼠标(偏航) Action 缓慢且没有反应(出于某种原因,俯仰工作正常)。有时它会移动,有时它根本不动。

部分代码如下:

bool Game::initDirectInput(HINSTANCE hInstance)
{
hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInput, NULL);

hr = directInput->CreateDevice(GUID_SysKeyboard, &DIKeyboard, NULL);

hr = directInput->CreateDevice(GUID_SysMouse, &DIMouse, NULL);

hr = DIKeyboard->SetDataFormat(&c_dfDIKeyboard);
hr = DIKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

hr = DIMouse->SetDataFormat(&c_dfDIMouse2);
hr = DIMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE | DISCL_NOWINKEY);

return true;
}

void Game::detectInput(double delta)
{
DIMOUSESTATE2 mouseCurrState;

BYTE keyboardState[256];

DIKeyboard->Acquire();
DIMouse->Acquire();

DIMouse->GetDeviceState(sizeof(DIMOUSESTATE2), &mouseCurrState);

DIKeyboard->GetDeviceState(sizeof(keyboardState),(LPVOID)&keyboardState);

camera.detectInput(delta, &keyboardState[0], &mouseCurrState);
}

void Camera::detectInput(double delta, BYTE *keyboardState, DIMOUSESTATE2 *mouseCurrState){

float speed = 16.0f * delta;


...


if((mouseCurrState->lX != mouseLastState.lX) || (mouseCurrState->lY != mouseLastState.lY))
{
camYaw += mouseCurrState->lX * 0.002f;

camPitch += mouseCurrState->lY * 0.002f;

if(camPitch > 0.85f)
camPitch = 0.85f;
if(camPitch < -0.85f)
camPitch = -0.85f;

mouseLastState = *mouseCurrState;
}

update();

return;
}

void Camera::update(){
camRotationMatrix = XMMatrixRotationRollPitchYaw(camPitch, camYaw, 0);
camTarget = XMVector3TransformCoord(DefaultForward, camRotationMatrix );
camTarget = XMVector3Normalize(camTarget);

XMMATRIX RotateYTempMatrix;
RotateYTempMatrix = XMMatrixRotationY(camYaw);

camRight = XMVector3TransformCoord(DefaultRight, RotateYTempMatrix);
camForward = XMVector3TransformCoord(DefaultForward, RotateYTempMatrix);
camUp = XMVector3Cross(camForward, camRight);

camPosition += moveLeftRight*camRight;
camPosition += moveBackForward*camForward;
camPosition += moveUpDown*DefaultUp;

moveLeftRight = 0.0f;
moveBackForward = 0.0f;
moveUpDown = 0.0f;

camTarget = camPosition + camTarget;

camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );
}

对于那些想知道的人,我刚刚开始使用本教程学习 c++ 和 directx:http://www.braynzarsoft.net/index.php?p=DX11Lessons(虽然我有相当多的使用 OpenGL + java 的经验。)

最佳答案

我通过添加一个简单的 if 语句解决了这个问题:

if(mouseLastState.lX > -10000 && mouseLastState.lX < 10000){
camYaw += mouseLastState.lX * 0.002f;
}

问题是,当窗口重新获得焦点时,它会返回一个如此高的值,以至于偏航不再有效。

关于C++ Directx鼠标输入在失去焦点后表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24168743/

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