Sign in to follow this  
RuneLancer

Message Queue Woes

Recommended Posts

RuneLancer    253
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. ;)

Share this post


Link to post
Share on other sites
Endurion    5411
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.

Share this post


Link to post
Share on other sites
RuneLancer    253
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
moeron    326
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

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

hth
moe.ron

Share this post


Link to post
Share on other sites
RuneLancer    253
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?

Share this post


Link to post
Share on other sites
Telastyn    3777
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this