Message Queue Woes

Started by
5 comments, last by Telastyn 18 years, 10 months ago
I've run into a bit of an issue with message queues. My game has a primitive means of handling input: if the window receives a WM_KEYDOWN, it sets the corresponding key in an array to true. If it receives a WM_KEYUP, it sets it to false. If I hold "up", jiggle the camera around with the mouse, and release it, if enough message have built up in the queue it continues to "hold" up for a few moments then stops as the WM_KEYUP gets processed. Which is, obviously, no good.
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) f_done = true;
else { TranslateMessage(&msg); DispatchMessage(&msg); }
} 

if(f_active)
{
(some game code)
}
This is how I handle things. I tried putting in a "while" instead of the "if" to process every message right away, but the game didn't update the screen until I'd let go of the mouse or the keyboard. Which is even worse. :D Any suggestions? I'm thinking of putting the queue in a thread of its own but I'm not sure I want to do that just yet and mess around with threads if I don't have to.. Seems like the problem's more of a design issue that needs to be sorted out than something that can just be tossed into the multithreading void and forgotten about until it comes back and bites me in the ass a few months down the road. ;)
Advertisement
You definetively want to use while there. Right now you get only one message processed, then run through your whole game loop which may take quite some time.

You'll probably notice when you move your window about for some seconds.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Quote:Original post by Endurion
You definetively want to use while there. Right now you get only one message processed, then run through your whole game loop which may take quite some time.

You'll probably notice when you move your window about for some seconds.


Problem with that is that when I move the camera around (using the mouse) it doesn't update the display until I release it. Which is pretty bad.

Quote:Original post by RuneLancer
I tried putting in a "while" instead of the "if" to process every message right away, but the game didn't update the screen until I'd let go of the mouse or the keyboard. Which is even worse. :D
I would suggest using the function GetAsyncKeyState...here is an example of an implementation


#define KEY_DOWN(VK_KEY) ( (::GetAsyncKeyState( (VK_KEY) ) & 0x8000) > 0)

or the good ol' c++ way

bool IsKeyDown(int nVirtualKey)
{
return ( (::GetAsyncKeyState(nVirtualKey) & 0x8000) > 0 )
}


now the problem with this, is it doesn't take what window your trying to get input from so you can add one more minor addition. I'm assuming a global HWND for the window from which you want to get input


bool IsKeyDown(int nVirtualKey)
{
bool retVal = false;
if( g_hWnd == GetFocus() )
{
retVal = (::GetAsyncKeyState(nVirtualKey) & 0x8000) > 0
}

return retVal;
}


Look up the Virtual Key Codes on MSDN

hth
moe.ron
I would suggest using the function GetAsyncKeyState...here is an example of an implementation

#define KEY_DOWN(VK_KEY) ( (::GetAsyncKeyState( (VK_KEY) ) & 0x8000) > 0)//or the good ol' c++ waybool IsKeyDown(int nVirtualKey){    return ( (::GetAsyncKeyState(nVirtualKey) & 0x8000) > 0 )}//now the problem with this, is it doesn't take what window your trying to get //input from so you can add one more minor addition. I'm assuming a global HWND //for the window from which you want to get inputbool IsKeyDown(int nVirtualKey){    bool retVal = false;    if( g_hWnd == GetFocus() )    {         retVal = (::GetAsyncKeyState(nVirtualKey) & 0x8000) > 0     }        return retVal;}


Look up the Virtual Key Codes on MSDN

[EDIT]
Could an OP please remove my post as an AP? sorry about that, thought I was logged in!
[EDIT]

hth
moe.ron
moe.ron
Thanks; I plan on using DirectInput to manage input later on, though for the time being it'll probably be a better means of handling the keyboard. :)

That still doesn't solve my problem though. As it currently is, my message queue only processes one message per frame, and the queue builds up considerably over time. But sticking a while in there means I don't update my game states while the user moves the camera with the mouse, which is just as bad (only, in a different way :) )

Any suggestions/pointers?
I'm not sure this will help, as I've never had queue problems [keep the main loop shorter!], but you could always do "in between". Don't use the full while loop, but set a configurable limit:
int limit=4;int x;x=0;while (message_exists && x<limit){       handle_message();      ++x;}


that way you can handle a number of messages, so the queue doesn't fill up quite so fast, but not so many that it bogs down the display. Not sure it will help too much, but it might improve things enough.

This topic is closed to new replies.

Advertisement