Input timing

Started by
5 comments, last by stylin 18 years, 8 months ago
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.
If you insist on saying "DUH", try to make a post that makes a little more sense
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 messagecase 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.
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:
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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.
If you insist on saying "DUH", try to make a post that makes a little more sense
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.

---------------------------- By CommanderXXLCommander@bloomm.de----------------------------
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).
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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:
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid

This topic is closed to new replies.

Advertisement