Search code examples
c++directxdirect3ddirectinput

Should I use DirectInput or Windows message loop?


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


Solution

  • 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.