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

keypress speed issues...

Started by becklighter Aug 17, 2011 at 12:29 AM 3 replies 2.4k views
Original Post
becklighter
becklighter
I'm currently working on my first game with genuine DirectX enabled graphics. My Tetris type game is coming along well, but I've encountered a problem that I can't seem to correct. Button presses appear to occur extremely fast. I use the space bar to rotate the falling shape, but when I press it, I get a whirling blur as it draws out every different rotation in succession until I let off the key. The same problem occurs when I press down, or left and right. It makes it difficult to line up the pieces correctly. Does anyone have a clue what I might be doing wrong?

Here are what I believe to be the most pertinent pieces of code:
[source]
...
#define KeyDown(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KeyUp(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
...

...
MSG msg;
while(TRUE){
DWORD start = GetTickCount();
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
if(msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

RenderFrame();

#pragma region Move Block
if(KeyDown(VK_SPACE) && !fall.Move(0, 0, 1))
;// Play beep type sound uh.. uh..
if(KeyDown(VK_RIGHT) && !fall.Move(0, 1, 0))
;// Play beep type sound uh.. uh..
if(KeyDown(VK_LEFT) && !fall.Move(0, -1, 0))
;// Play beep type sound uh.. uh..
if(KeyDown(VK_DOWN) && !fall.Move(1, 0, 0))
if(fall.SetPiece())
SendMessage(hWnd, WM_DESTROY, 0, 0);
#pragma endregion

if(KeyDown(VK_ESCAPE))
SendMessage(hWnd, WM_DESTROY, 0, 0);

while((start - GetTickCount()) < 25);
}
...
[/source]
beatlefan
beatlefan
It's because it's executing that code hundreds, if not, thousands of times per second.

For something like this you would want to use the key down messages.
becklighter
becklighter

It's because it's executing that code hundreds, if not, thousands of times per second.

For something like this you would want to use the key down messages.


Thank you for your reply, but, isn't that what I'm doing? Am I missing something?
beatlefan
beatlefan

Thank you for your reply, but, isn't that what I'm doing? Am I missing something?


What your doing is checking if the key is down. You want to check for key down events, which have a built in delay. So where you have your


if (msg.message == WM_QUIT)


you will want to add a


else if(msg.message == WM_KEYDOWN)


and if that's true check which key is down and move your tetris pieces.
becklighter
becklighter

[quote name='becklighter' timestamp='1313542464' post='4850096']
Thank you for your reply, but, isn't that what I'm doing? Am I missing something?


What your doing is checking if the key is down. You want to check for key down events, which have a built in delay. So where you have your


if (msg.message == WM_QUIT)


you will want to add a


else if(msg.message == WM_KEYDOWN)


and if that's true check which key is down and move your tetris pieces.
[/quote]

Thank you, that is exactly what I needed.

Topic Locked

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

Sign in to reply to this topic.