• Advertisement
Sign in to follow this  

Input timing

This topic is 4587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am making a tetris game in c++ and directx. The problem I am having is that when I read from the keyboard it moves my piece to fast. So I was thinking If the key on a keyboard is pressed and held down, call the function once every interval of time has elapsed. I am not to sure how to implement this though. Is this even a good idea? Should I put it in my main game file, so I can time it manually or should I put it in my input class and give each key I want an interval? Thanks for any suggestions you guys may have.

Share this post


Link to post
Share on other sites
Advertisement
Id reccomend using a "Timer".

A timer, is a simple tool that activates every so many seconds. Here is the function:


UINT SetTimer(HWND hWnd,
UINT nIDevent,
UINT nElapse,
TIMERPROC lpTimerFunc);









The parameters are:

hWnd: Your window handle
nIDevent: The ID of your timer.
nElapse: The delay in milliseconds before the timer should activate.
lpTimerFunc: The function to call to process the timer. set this to NULL to use default WndProc (This is what you should do).

You would create a timer like so:


//This timer will, number 8, activate every 2 seconds.
SetTimer(hwnd, 8, 2000, NULL)









When a timer activates, it sends a message to the processing function (which, in your case, should be your WndProc(), if you set lpTimerFunc to NULL). Add the following to your WndProc:


//recieved a WM_TIMER message
case WM_TIMER:
{
//if the wparam of the message, which contains the ID of the timer is 8, then do
//your work here.
if(wparam == 8)
{
//do your work you want to do here
}

}break;










Note you can replace the if statement with a switch if you want.

Finally, when you close your program, you must delete your timer, like so:

bool KillTimer(HWND hWnd,
UINT uIDEvent);








This function takes the HWND that the timer is set to, and the timers ID.

By using a timer, you can create delays between input processing of keys, giving the illusion of delayed movement.

If you need any further help, or find any mistakes in the code above, feel free to PM me.

Share this post


Link to post
Share on other sites
I would give your tetris block a velocity, and have it move based off that. Keyboard input changes velocity. How are you currently implementing the idle movement (piece is falling, no keys pressed)?

:stylin:

Share this post


Link to post
Share on other sites
i am currently moving it down a row, the row size is as big as the block 30x30. I just give it an extra row every interval which is currently two seconds. Every two seconds it moves it down a row, 30 points. if they push down it just moves it down a row. left moves it left a row, right moves it right a row.

Share this post


Link to post
Share on other sites
I guess you are not using Direct Input? So if you are using the WM_KEYDOWN and WM_KEYUP messages of windows the simplest methode would indeed be a timer. have a bool for each of the keys you need and set it to true in your WM_KEYDOWN handler, set it to false in your WM_KEYUP handler and move your parts based upon the bools in the WM_TIMER handler. If you implement more levels in your game you can simply modify the Timer interval to your new game speed. Tetris is perfectly fine with the normal windows timer, cause the intervals are big enough. For high speed game timing you should use other timing methods instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by CommanderXXL
I guess you are not using Direct Input? So if you are using the WM_KEYDOWN and WM_KEYUP messages of windows the simplest methode would indeed be a timer. have a bool for each of the keys you need and set it to true in your WM_KEYDOWN handler, set it to false in your WM_KEYUP handler and move your parts based upon the bools in the WM_TIMER handler. If you implement more levels in your game you can simply modify the Timer interval to your new game speed. Tetris is perfectly fine with the normal windows timer, cause the intervals are big enough. For high speed game timing you should use other timing methods instead.


What I did instead (and to much success) was to perform the movements as soon as I received each WM_KEYDOWN message. As a bonus, the windows auto-repeat feature kicked in if you held any button down long enough (just as you would probably expect to happen).

Share this post


Link to post
Share on other sites
Quote:
Original post by cptrnet
i am currently moving it down a row, the row size is as big as the block 30x30. I just give it an extra row every interval which is currently two seconds. Every two seconds it moves it down a row, 30 points. if they push down it just moves it down a row. left moves it left a row, right moves it right a row.

OK, I get what you're doing. With your current setup, delaying the player movement would be no different than what you're doing with your idle movement. Your block falls one unit, with no input, at a constant rate - (1 unit/2 sec). With input - either holding the arrow down, or continually pressing it - your block should fall one unit, at a constant rate - (1 unit/.2 sec, for example).

So no matter how much the player holds down the arrow key, your block should move the same number of times/second. Having a timer control this is easiest with your current layout. Making use of Windows repeat messages may not be what you need, since all you need to know is if the key is being held down or not. Also, this kind of setup is good because you can simply shorten the timer for later, faster rounds.

Stick to your first idea and place your timing code in your game logic - your input manager doesn't need to know how often to call MoveBlock(), it just needs to keep track of the keyboard. Hope this helps and good luck!

:stylin:

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement