gpt4 book ai didi

c++ - 我应该使用 DirectInput 还是 Windows 消息循环?

转载 作者:IT老高 更新时间:2023-10-28 22:35:51 27 4
gpt4 key购买 nike

我正在开发 C++ DirectX 2D 游戏,我需要键盘和鼠标输入。
维基百科说:

Microsoft recommends that new applications make use of the Windows message loop for keyboard and mouse input instead of DirectInput

那我应该怎么用呢?
我有一个 GameScreen 类负责绘图和更新(游戏逻辑),我在 Windows 消息循环中调用 Draw 和 Update 方法。

谢谢

最佳答案

由于您几乎必须运行消息泵才能拥有一个窗口,因此您也可以使用该泵来处理键盘和鼠标输入。是否将键盘事件交给子窗口完全取决于您的泵,如果您愿意,可以在泵中处理它们。

您的典型消息泵如下所示:

while (GetMessage(&msg, NULL, 0, 0))
{
if (WM_QUIT == msg.message)
break;
TranslateMessage(&msg); // post a WM_CHAR message if this is a WM_KEYDOWN
DispatchMessage(&msg); // this sends messages to the msg.hwnd
}

对于游戏,您的泵可能看起来更像这样

while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD))
{
bool fHandled = false;
if (msg.message >= WM_MOUSEFIRST && msg.message <= WM_MOUSELAST)
fHandled = MyHandleMouseEvent(&msg);
else if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST)
fHandled = MyHandleKeyEvent(&msg);
else if (WM_QUIT == msg.message)
break;

if ( ! fHandled)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
// if there are no more messages to handle right now, do some
// game slice processing.
//
}
}

当然,您的实际泵可能会比这更复杂,可能使用 MsgWaitForMultipleObjects 以便即使没有消息要处理,您也可以定期唤醒,但在有消息时立即唤醒.

关于c++ - 我应该使用 DirectInput 还是 Windows 消息循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2165230/

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