Jump to content
  • Advertisement
Sign in to follow this  

Can't get my joypad code to work in DirectInput...

This topic is 4294 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'm in the process of learning DirectX, and right now I'm doing some DirectInput. All I'm doing right now is just a little Hello World type thing to make some text appear when buttons are being pressed. My keyboard and mouse work fine, thankfully, but as the topic states, joypads are giving me a headache, and I can't work out why. The pad is in fact a Playstation 2->USB adaptor, and at first I thought it was just dodgy. The thing is, it works perfectly fine in joypad-supporting games, and everything works as it should in Control Panel -> Game Controllers -> Properties, with the icons that light up as buttons are being pressed and all that. It compiles with no errors at all, too. And if I start the program without the adaptor plugged in, it crashes because it can't find a joypad, so it has to be accepting it as a joypad, right? So, in the DirectInput constructor, I set up my keyboard, mouse and joypad:
	// Use mDInput to set up the keyboard

	HR(mDInput->CreateDevice(GUID_SysKeyboard, &mKeyboard, 0));

	// Now use mDInput to set up the mouse

	HR(mDInput->CreateDevice(GUID_SysMouse, &mMouse, 0));

	// Now we use mDInput to set up the joypad

	HR(mDInput->CreateDevice(GUID_Joystick, &mJoypad, 0));

The information for the devices go into the pointers mKeyboard, mMouse and mJoypad respectively. The cooperative level for all of them (being passed in a DWORD here) is set to Non-exclusive and Foreground. The joypad state to get buttons is of type DIJOYSTATE2. I've also tried DIJOYSTATE, but the same thing happens with both. Okay, so. After everything's been constructed, two functions are looped: updateScene() and drawScene(). All the code that's giving me the problem is in updateScene. The first thing it does is poll all the input devices. I've left out the Keyboard and Mouse parts of it here:
	// Poll the joypad

	hr = mJoypad->GetDeviceState(sizeof(DIJOYSTATE2), (void**) &mJoypadState);

	// This should take whatever the state of the joypad is and stick
	// it in mJoyState
	// If this is wrong I'll cry

	if( FAILED(hr) )
		// Joypad has been lost - so we'll zero out and attempt to acquire it
		ZeroMemory(&mJoypadState, sizeof(mJoypadState));
		hr = mJoypad->Acquire();

Then it does the check to see if any buttons are being held down on the controller:
	// The joypad state is of type DIJOYSTATE2 - so it has 128 buttons
	// and we should probably check them all, because the convertor is freaky
	// and I don't know what locations it maps the buttons to

	// I think Triangle is 'button 0' on this particular convertor, but I want
	// any convertor to work in the finished product, so that's irrelevant!

	// Also, the same thing happens if I set the state to DIJOYSTATE (the 
	// original)

	for (int n = 0 ; n < 128 ; n++)
		if( gDInput->joypadButtonDown( n ) )
			joybuttondown = true;
			// Without an else here to set joybuttondown to false when nothing
			// is being pressed, the message should stay on screen after even 1
			// framesworth of input

Easy enough = it goes through all 128 potentially-available buttons of DIJOYSTATE2 (I change that to 32 if I use DIJOYSTATE) and sets a boolean value to true if the joypadButtonDown() function returns true. Here is the code for joypadButtonDown:
bool DirectInput::joypadButtonDown(int button)
	return (mJoypadState.rgbButtons[button] & 0x80) != 0;
	// So here we're returning true if the button we're querying about
	// has the bits 0x80 set, as this happens if it is down


Afterwards, drawScene() is run, with the following code:
	if (joybuttondown == true)
		mFont->DrawTextA(0, _T("OMG WORKS"), -1, &formatRect, DT_CENTER | DT_VCENTER, 
						D3DCOLOR_XRGB(0, 0, 0));


But with the joypad, it just doesn't appear. I've gone through it step-by-step, holding the button down all the way through, but according to joypadButtonDown(), I'm not holding anything down at all. Why god why oh god why. :( The mouse and keyboard do work, perfectly, and the code is hardly different to the joypad's aside from the obvious. Can anyone see what I'm missing? I've had a friend look at this and he can't see anything wrong with it either, so I'm utterly baffled. Thanks in advance. [Edited by - Dache on October 18, 2006 7:35:13 PM]

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!