DirectInput - Keyboard TOO responsive

Socrates877

Newcomer
Joined
Oct 27, 2003
Messages
3
Right now I'm using directinput to get input from the keyboard. I'm designing my own version of tetris to learn directx 9 (c#). I get lots of keyboard input. I mean, it's like, really responsive.

For example, I have one key mapped to a function that rotates a block as it's falling down the screen. I barely tap the key and the block must rotate about 10 times (and it should only rotate it 1/4). And jesus help you if you hold the button down. The amount of air coming off the block-turned-windmill will blow you back to your neighbors house.

Ok, so question is, how can I make the block only move once each time the user presses the button? I essentially want to create a keyup event.
 
And jesus help you if you hold the button down. The amount of air coming off the block-turned-windmill will blow you back to your neighbors house.

LOL! :)

It doesn't sound like a keyboard input problem, but more like a frames per second problem. You need to rig your blocks so they only turn so much per second (like 4 times a second). This is done using a high performance timer.

You may find this thread useful;
http://www.xtremedotnettalk.com/showthread.php?s=&threadid=75420

Skip over the first 5 posts or so, and scroll down where people start talking about the Query Performance Timer.

Also read this to put things into perspective;
http://www.gamedev.net/reference/articles/article753.asp

Hope that helps. If not, check out the GameUtil class library w/ Demo that I'll be releasing later tonight/tomorrow.. it may help to actually see something that uses the Query Performance Timer.
 
still curious though

I read about animating by incrementing values each frame or by using a timer. Using a timer seemed better since if frame rate ever drops, the animations wouldn't get all out of whack. Instead, my engine uses either Environment.TickCount (like in the sdk samples) or a class I built around making windows api calls using the performance timers. Most of the time I don't need the accuracy of the performance timer so I use the tickcount. This way I can let the computer go balls to wall on rendering, and, if there is a glitch, the game won't get out of sync.

So without controlling the framerate, I'm still wondering how to control the keyboard. I'll continue working on it and post if I find a good way. I better take a look at the sdk again.

Thanks
 
I wasn't talking about limiting the framerate for the game loop. You can limit the movement of your blocks via the timer.

I uploaded my GameUtil class library, which has a demo in it. In the demo, you can walk a character around.. which his movement is based off of a timer. So he will move at the same rate no matter what.

This is what the characters movement basically boils down to;
float movementAmount = ticksPerSecond * Character.MovementPerSecond;
// Move char in direction based on movementAmount.

Instead of movementAmount, you can use something like turnCounter, incrementing it based on the ticks per second;

C#:
// Constants
public const float TurnsPerSecond = 6;

// Declarations
private float _turnCounter = 0.0F;

// Somewhere in a turn method.
_turnCounter += Block.TurnsPerSecond * ticksPerSecond;

if (_turnCounter >= 1) {
   // Turn block based on direction.

   _turnCounter = 0.0F;
}

I hope that helps.
 
Back
Top