Skip to main content
gamedev.net
🔒 Locked

DInput questions

Started by vincent_valentine Jul 2, 2004 at 9:29 AM 8 replies 600+ views
Original Post
vincent_valentine
vincent_valentine
I'm a newbie start-from-scratch, and at the moment i'm trying to write my Tetris clone with DirectX. There's a DInput problem that troubles me, and i really need some help: - When the player move the brick (right or left) i want the brick to move just once even when the player push and hold the "move right" key. (Normally when I push and hold the button, the brick keeps slipping away like i'm tapping the key) Sounds stupid, but i can't find any tutorial on DInput. So can someone help me out? Damn, I must learn to write the KISS way...
:: Try not. Do. Or do not. There is no try. :: (Yoda)http://80.100.152.172/nguyenkhanhduy
illadel4life
illadel4life
msdn.microsoft.com Sorta strating from scratch and that site helps. ALso in the SDK has a lot of examples, and the source code to boot. Also you might want to look into the action mapper. Trust, once u get that setup your'll know exactly which key that you want to know has been pressed not just any key. GL
JoeyBlow2
JoeyBlow2
You should read the DirectInput section of the SDK docs.

But for a quick lesson, (I'm not going to give you code, I'm only going to explain a few things you can use when reading the SDK docs)

There are 2 modes to DirectInput.

Buffered and Unbuffered.

Buffered = When the state of the key changes, it is placed into the buffer. This is commonly KEY_DOWN, and KEY_UP events.

Unbuffered = Every single time the function is called, it gives you the state of the keyboard (in a 256 char array) of UP/DOWN.

You can use both modes simotaniously if you want to.

With tetris, I'd assume you would want Buffered. This will make reading the docs easier, since you won't have to read about unbuffered.

The Lion King
The Lion King
If you are talking about how to initialize and do several other stuff then check the documentation.

As far as the problem you mentioned above is concerned ... try to cath the value when the KEY IS RELEASED (UP) not when the KEY IS PRESSED (DOWN) ...

that would do the trick for you ;)

Cheers!
vincent_valentine
vincent_valentine
Actually, i was meant to ask another thing. What if the player doesn't release the key? I want the game to automatically release the key after it has been pressed, so that the brick on the screen won't move forever but only for a step.

Could I send a message (key pressed or released code) to Dinput?
:: Try not. Do. Or do not. There is no try. :: (Yoda)http://80.100.152.172/nguyenkhanhduy
Alvarny
Alvarny
One way to do this is to have a variable that holds the status of keys. Lets say we are doing left key. Intial state of the left key is unpressed. The user presses the key. You check the status and it says unpressed. Ok do whatever you do when the user presses the key and set the status to pressed. Then each time you check and the key is still presses you can do nothing. Once the key is released set the state back to released and you are ready to go again.
The Lion King
The Lion King
You can use exactly what Alvarny says or you can add timer to what he suggests. Just keep track with the help of another variable ... ;)
vincent_valentine
vincent_valentine
Hey I did like you guys said, like this:


if (KeyDown(keystate, DIK_RIGHT))
{
if (!g_bkp)
{
posx+=size;
g_bkp=true;
}
}
if (KeyUp(keystate, DIK_RIGHT))
g_bkp=false;


Where g_bkp is a boolean (keypressed)

But do i need to manage a new boolean variable for each movement key? Let's say I have directional key: up down left right. So then I need g_b_rightkp, g_b_leftkp ... so on...
Or must I store an array of boolean?
:: Try not. Do. Or do not. There is no try. :: (Yoda)http://80.100.152.172/nguyenkhanhduy
The Lion King
The Lion King
If used wisely ... a single boolean would do the trick for you.

And just for the record ... if you want to implement that if the key is pressed and hold by the user and you want to meve the tile or piece one step in lets say one second (or less or more) then you can use timer.

Topic Locked

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

Sign in to reply to this topic.