Jump to content
  • Advertisement
Sign in to follow this  
moose517

directinput questions

This topic is 3799 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

i am working on a connect4 game in C++ with directx as you might guess from the title of this thread. But now i am to the point that i want the player to get to take a turn, i want them to press the numbers 1 through 7 to correspond with the columns of the board. However, when i press a number it just fills the whole column up like its reading the keyboard many times instead of just once. Here is the code relating to the input from the user.
	if (gDInput->keyDown(DIK_1))
	{
		// check for empty spot
		if (mBoard[0][0] == 0)
		{
			// empty space found, now find lowest and go there
			for (int i = NUM_ROWS; i > -1; i--)
			{
				if (mBoard[0] == 0)
				{
					mBoard[0] = mPlayer;
				}
			}
		}
	}



So my question to all of you is how do i get it to read the input once and then move on to the computers move and then back without filling up the whole column? And here is the Keydown function
bool DirectInput::keyDown(char key)
{
	return (mKeyboardState[key] & 0x80) != 0;
}


[Edited by - moose517 on June 15, 2008 8:51:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
Looks like you are using DirectInput. From your description of the problem you are using Immediate Mode. DirectInput also has a Buffered Mode of input where you receive one key each time you ask for it. My suggestion would be to pull the first key pressed then set a variable marking who's turn it is and then go through the game loop again. It would then detect that it is the computer's turn and you can ignore input. At the end of the computers turn set the variable back to say it is the player's turn and repeat.

Share this post


Link to post
Share on other sites
ok, so based on what you said i looked up the buffered mode at MSDN and am now completely confused, i'm starting to hate this project, i thought it would be easier to make a connect 4 game than anything else. I wanted to try the mouse but that didn't work out so well either, the directx wrapper classes that i am using just don't have the stuff that i seem to need to do this. By the way while i'm here, i am drawing my game board but want to have it scale to fix the entire client area of the windos, do you know how to accomplish this?

Share this post


Link to post
Share on other sites
You could have a boolean flag that indicates if the user has released the key. For example, you would first ask the user for the key. Once you recieve the keydown message, you set the boolean flag to false, thus meaning that the key is pressed. Then, once the key is released you set the flag to true.

You then only process the input when the boolean flag is true.

Pseudo code:

CheckKeyPress()
{
if(key1 is down)
{
keyNumber = 1
bIsReleased = false
}
}

CheckKeyRelease()
{
if(key1 is up)
bIsReleased = true
}

in the game loop, you would have:

CheckKeyPress()
CheckKeyRelease()
if(bIsReleased)
{
// Do your processing of the key number here
ProcessNumber()
keyNumber = -1 // To clear the key press
}

Share this post


Link to post
Share on other sites
hmmm, ok, I'll cut the directinput out and just use the windows message loop, means i'll have to create some kind of function because the message loop is contained in a wrapper class and i would need to get whether key is pressed.

Share this post


Link to post
Share on other sites
Quote:
Original post by moose517
hmmm, ok, I'll cut the directinput out and just use the windows message loop, means i'll have to create some kind of function because the message loop is contained in a wrapper class and i would need to get whether key is pressed.
There's several ways to wrap your window proc up in a class, and that class could have a pointer to an "input state" class (Or it could contain it). That input state could just be an array of bool's, where each bool indicates if a key is up or down. For proper key repeat, you'd want to send "key down" events every time you get a WM_KEYDOWN (Since the key will already be down when you get the second WM_KEYDOWN). Alternatively, you could use an array of ints instead, where each int is the repeat count, but that'd require keeping track of the previous repeat count when you go to check if the key has been auto-repeated again.

Share this post


Link to post
Share on other sites
ok, i know that i am being stubborn about this but i still want to try the directinput method even though you say not to. Well the function keyDown has (mKeyboardState[key] & 0x80) != 0. i dont know what 0x80 does to the statement to know if they needs changed or what. all i know is that its supposed to tell me if the key is pressed but i want a function to be able to use if for when the key is released. i have an idea using this but if it doesn't work then i'll go to the winproc

Share this post


Link to post
Share on other sites
Quote:
Original post by moose517
ok, i know that i am being stubborn about this but i still want to try the directinput method even though you say not to. Well the function keyDown has (mKeyboardState[key] & 0x80) != 0. i dont know what 0x80 does to the statement to know if they needs changed or what. all i know is that its supposed to tell me if the key is pressed but i want a function to be able to use if for when the key is released. i have an idea using this but if it doesn't work then i'll go to the winproc


The WM_KEYDOWN/WM_KEYUP message approach is still recommended.

But, if you want to do this via direct input, you could keep track of the state of the keyboard from the previous frame. Then, you have a key up if:

(mPreviousKeyboardState[key] & 0x80) && !(mCurrentKeyboardState[key] & 0x80)

Share this post


Link to post
Share on other sites
Quote:
Original post by moose517
Well the function keyDown has (mKeyboardState[key] & 0x80) != 0. i dont know what 0x80 does to the statement to know if they needs changed or what.
From the Documentation:
Quote:
For button input, only the low byte of dwData is significant. The high bit of the low byte is set if the button was pressed; it is clear if the button was released.
And obviously & 0x80 masks off all but the high bit in the low byte.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!