-
Input Problems
I'm trying to accept input for movement using the Windows event handler. I use keys EDSF for up/down/left/right. When you hold a key down, it moves fine for the first few frames, but then it gets a bit jumpy and scrolls slower. I'm not sure what is doing this but I'm thinking its something with the key buffer getting full or something...?
The attachment is a program demonstrating the problem I'm having. E/D/S/F=up/down/left/right and esc to exit. Notice if you tap the key repeatedly it moves fine, but when you hold it down it blogs down.
Thanks!
//napKIN
-
No offense, but now how are we going to be able to tell what the problem is without any source code. Looking at it though I would say it runs fine holding it down and repetive pressing. I would make the movement a little more smooth, but post up the source and I'll take a look at it.
-
Code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC;
static HDC hDC;
switch(message)
{
case WM_CHAR:
{
switch (toupper(wParam))
{
case 'E':
{
player1.y+=player1.speed;
return 0;
}
case 'F':
{
player1.x+=player1.speed;
return 0;
}
case 'D':
{
player1.y-=player1.speed;
return 0;
}
case 'S':
{
player1.x-=player1.speed;
return 0;
}
default:
break;
}
}
break;
default:
break;
}
return (DefWindowProc(hwnd, message, wParam, lParam));
}
There is the movement message handline. I'm not sure what else would be relevant to include. I scrolled NE across the map without input, and it scrolled fine, so I know that it is something to do with the input :\
//napKIN
edit: The code didn't format correctly...I deleted all the whitespace in it, hopefully it will work now. Oh yeah and the camera is focused on (player1.x, player1.y), both coords are floats.
-
Well windows messages are pretty slow as far as performance concerns go. I would look into something like GetAsyncKeyState to handle your input. Make a function that checks input every frame and updates accordingly and do NOT handle it in your msg handler. Here is the info on the function.
http://msdn.microsoft.com/library/de...nckeystate.asp
-
Thanks *alot*! It works perfectly, just what I needed. (Good thing you responded cause I was starting to get extremely annoyed)
//napKIN
-
Just a couple things, instead of "switch(toupper(wParam))" it should be either switch((int)wParam) or just switch(wParam). It will always be a capital anyways. **oops, only for WM_KEYDOWN and WM_KEYUP, didn't notice you were using WM_CHAR
Also, if you want to use windows messages, that's fine too. This will cut out the jerky movement:
Code:
(global variable or something)
bool e, f, d, s;
(windows procedure)
switch(msg)
{
case WM_KEYDOWN:
switch((int)wParam)
{
case 'E':
e = true;
break;
case 'F':
f = true;
break;
(etc.)
}
return true;
case WM_KEYUP:
switch((int)wParam)
{
case 'E':
e = false;
break;
(etc.)
}
return true;
}
Then you can check the state of the variables each frame, in the same way that you would use GetAsyncKeyState(). On the other hand, if you go with GetAsyncKeyState, you probably want to make macro or inline function to wrap around it like KEY_DOWN() or something, since GetAsync... is pretty long to type.