我应该使用DirectInput还是Windows消息循环? [英] Should I use DirectInput or Windows message loop?

查看:476
本文介绍了我应该使用DirectInput还是Windows消息循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++ DirectX 2D游戏,我需要键盘和鼠标输入。

维基百科说:


Microsoft建议新应用程序使用Windows消息循环来处理键盘和鼠标输入,而不是DirectInput


使用它?

我有一个GameScreen类,它负责绘图和更新(游戏逻辑),我在窗口消息循环中调用Draw和Update方法。



感谢

解决方案

泵为了有一个窗口,你可以使用那个泵来处理键盘和鼠标输入以及。这完全取决于您的泵是否将键盘事件传送到子窗口,如果您愿意,您可以在泵中处理它们。



您的典型消息泵看起来像这样:

  while(GetMessage(& msg,NULL,0,0))
{
if (WM_QUIT = msg.message)
break;
TranslateMessage(& msg); // post一个WM_CHAR消息,如果这是一个WM_KEYDOWN
DispatchMessage(& msg); // this messages 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
{
//如果现在没有更多的消息要处理,请做一些
//游戏切片处理。
//
}
}

泵可能甚至比这更复杂,可能与 MsgWaitForMultipleObjects ,使您可以定期醒来,即使没有消息要处理,但立即有消息时。 / p>

I'm working on a C++ DirectX 2D game and I need keyboard and mouse input.
Wikipedia says:

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

So how should I use it?
I have a GameScreen class whice take care of the drawing and the updating(game logic), I call the Draw and the Update methods inside a windows message loop.

Thanks

解决方案

Since you pretty much have to run a message pump in order to have a window, you might as well use that pump to handle keyboard and mouse input as well. It's entirely up to your pump whether you hand keyboard events on to a child window, you can handle them in the pump if you prefer.

Your typical message pump looks like this:

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
}

For a game, your pump might look more like this

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

Of course, your actual pump will likely be even more complex than that, possibly with a MsgWaitForMultipleObjects so that you can wake periodically even if there a no messages to process, but immediatelly when there are messages.

这篇关于我应该使用DirectInput还是Windows消息循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆