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

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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));
HR(mKeyboard->SetDataFormat(&c_dfDIKeyboard));
HR(mKeyboard->SetCooperativeLevel(gd3dApp->getMainWnd(),
keyboardCoopFlags));
HR(mKeyboard->Acquire());

// Now use mDInput to set up the mouse

HR(mDInput->CreateDevice(GUID_SysMouse, &mMouse, 0));
HR(mMouse->SetDataFormat(&c_dfDIMouse2));
HR(mMouse->SetCooperativeLevel(gd3dApp->getMainWnd(),
mouseCoopFlags));
HR(mMouse->Acquire());

// Now we use mDInput to set up the joypad


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

// 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
}


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++)
{
{
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 on other sites

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628697
• Total Posts
2984273

• 20
• 9
• 13
• 13
• 11