Skip to main content
GameDev.net gamedev.net
🔒 Locked

FRUSTRATION!!! Windows Msg Router Woes

Started by Premonition Apr 25, 2004 at 12:57 PM 3 replies 500+ views
Original Post
Premonition
Premonition
I am attempting (keyword) to write the message router in the windows wrapper tutorial. The code compiles correctly but the messages are not being routed correctly... when alt-F4 or use the system menu to close the program it still runs. Here''s the code:
// Window''s main loop
LRESULT CWND::MainLoop()
{
  bool done = false;
  
  // Main message loop
  while(!done)
  {
    PeekMessage(&m_msg, m_hwnd, 0, 0, PM_REMOVE);
    
    // If  WM_QUIT
    if(m_msg.message == WM_QUIT)
    {
      done = true;
    }
    else
    {
      // Translate & dispatch to even quene
      TranslateMessage(&m_msg);
      DispatchMessage(&m_msg);
      
      // Main game logic cycle
      m_GameCycle();
    }
  }
  
  return m_msg.wParam;
}



// Class message router: this function is static and will rout messages
// to the correct class instance
LRESULT CALLBACK CWND::MessageRouter(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  CWND *p_wnd = 0;

  if(msg == WM_NCCREATE)
  {
    // retrieve Window instance from window creation data and associate
    LPVOID p_temp = ((LPCREATESTRUCT)lParam)->lpCreateParams;
    p_wnd = reinterpret_cast(p_temp);
        SetWindowLong(hwnd, GWL_USERDATA, reinterpret_cast(p_wnd));

	  // save window handle
    if(p_wnd)
      p_wnd->SetHWND(hwnd);
  }
  else
  {
    // retrieve associated Window instance
    p_wnd = reinterpret_cast(GetWindowLong(hwnd, GWL_USERDATA));
  }
  
  if(p_wnd)
  {
    t_messageIterator i;
    i = p_wnd->GetMessageHandler(msg);
    if(i == NULL)
        { MessageBox(NULL, "Message not recognized!", "ERROR", MB_OK); }
    else
        { return (i->second)(p_wnd, hwnd, wParam, lParam); }
  }
    
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
 
Any suggestions?
smitty1276
smitty1276
I had that problem before and realized later that I wasn''t "quitting" properly. All of the messages were handled.
bcome
bcome
I had the same problem too, it has something to do about giving the window handle to the GetMessage/PeekMessage (I forget the details, I'll see if I can find the post.)

replace:
PeekMessage(&m_msg, m_hwnd    , 0, 0, PM_REMOVE);  


with:
PeekMessage(&m_msg, NULL    , 0, 0, PM_REMOVE);  


edit: can't find that post...

[edited by - bcome on April 25, 2004 8:05:06 PM]
<!-- ................................ -->u r what u bcomeMy (XHTML
Premonition
Premonition
That''s not it... the code is failing because the class object never passes this test:



if(p_wnd)
{
t_messageIterator i;

// retrieve the associated message handler
i = p_wnd->GetMessageHandler(msg);
return (i->second)(p_wnd, hwnd, wParam, lParam);
}



It moves to the default WndProc because the window that is retrieved by GetWindowLong() dosen''t exist because


if(p_wnd)
{
...
}


FAILS!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.