Sign in to follow this  

Can't read joystick output

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

Hello devs, I have been able to acquire my directinput joystick. however, I keep getting 0 for my x axis. any ideas? sorry, i wasn't able to post in the directX forum for some reason.
HRESULT UpdateInputState(HWND hwnd)
{
	HRESULT hr;
	
	if (NULL == g_pDevice)
		return S_OK;
	
	hr = g_pDevice->Poll();		//Poll the device to read the current state
	if( FAILED( hr ) )
    {
        // DInput is telling us that the input stream has been
        // interrupted. We aren't tracking any state between polls, so
        // we don't have any special reset that needs to be done. We
        // just re-acquire and try again.
        hr = g_pDevice->Acquire();
        while( hr == DIERR_INPUTLOST )
            hr = g_pDevice->Acquire();

        // hr may be DIERR_OTHERAPPHASPRIO or other errors.  This
        // may occur when the app is minimized or in the process of 
        // switching, so just try again later 
        return S_OK;
    }

    // Get the input's device state
	if( FAILED( hr = g_pDevice->GetDeviceState( sizeof( DIJOYSTATE2 ), &js ) ) ){
      
		return hr; // The device should have been acquired during the Poll()
	}

	return S_OK;
	
}


void Steer(void)
{	
     wheelX+=js.lX;     //pass this to RenderScene();

}


[Edited by - bobby_nguyen on November 17, 2008 11:52:32 PM]

Share this post


Link to post
Share on other sites
I dont know to much about DirectInput but I do have it working in my 3D engine. I followed a tutorial somewhere or referenced a book. The one difference I can see between your implementation and mine is that I do not call Poll each time.



if (FAILED(m_pJoystick->GetDeviceState(sizeof(DIJOYSTATE), (LPVOID)&m_dxJoystickState)))
{
while (DIERR_INPUTLOST == m_pJoystick->Acquire());
return &m_decadeJoystickState;
}

m_decadeJoystickState.XAxis = (short)m_dxJoystickState.lX * -1;
m_decadeJoystickState.YAxis = (short)m_dxJoystickState.lY * -1;

for (int l_iButton = 0; l_iButton < 32; l_iButton++)
{
if (m_dxJoystickState.rgbButtons[l_iButton] > 32)
m_decadeJoystickState.ButtonPressed[l_iButton] = true;
else
m_decadeJoystickState.ButtonPressed[l_iButton] = false;
}



I call Poll once at the end of the Init function, (after calling Acquire).

Share this post


Link to post
Share on other sites
thanks. However, after making changes as you suggested, I am still getting 0s from the joystick.

Maybe I am not understanding what Poll() does specifically. I should be calling Poll, then GetDeviceState right? How often do I need to Poll/GetDeviceState?

Share this post


Link to post
Share on other sites
Hi, ive only used DI on a basic level for joystick input because of the lack of tutorials (that i can see)

but im pretty sure you need to create a DIPROPRANGE structure and initialize it
when the joystick has analog controls.

in the structure you can set the max range that that the joystick analogs can reach for example if you set the max range to 1000 extreme left/top or right/bottom (not entirely sure how it works) would be 1000 and the analog when centered would be 0

Hope that helps
~Reegan

Share this post


Link to post
Share on other sites
Hi

There probbaly are some issues with my implementation but if it will help you I shall post the code here.


#define JOYSTICK_PRESSED 255

struct sJoystickState
{
short XAxis;
short YAxis;
bool ButtonPressed[36];
};

class CJoystick
{
//Private Member Variables
private:

LPDIRECTINPUTDEVICE8 m_pJoystick; //Pointer to DirectInput Joystick
DIJOYSTATE m_dxJoystickState; //DirectX State of Joystick
sJoystickState m_decadeJoystickState; //Decade State of Joystick
char m_pszJoystickName[260]; //Name of the Joystick


//Public Member Variables
public:

CJoystick();
~CJoystick();

bool Initialise(HWND p_hWnd, LPDIRECTINPUT8 p_pDirectInput, GUID p_guidDevice, char* p_pszDeviceName);
char* get_JoystickName();
sJoystickState* get_State();
};

CJoystick::CJoystick()
{
ZeroMemory(&m_decadeJoystickState, sizeof(sJoystickState));
m_pJoystick = NULL;
}

CJoystick::~CJoystick()
{
if (NULL != m_pJoystick)
{
m_pJoystick->Unacquire();
m_pJoystick->Release();
}
}

bool CJoystick::Initialise(HWND p_hWnd, LPDIRECTINPUT8 p_pDirectInput, GUID p_guidDevice, char* p_pszDeviceName)
{
//Save the Device Name
strcpy_s(m_pszJoystickName, 260, p_pszDeviceName);

//Create the Joystick Device
LPDIRECTINPUTDEVICE8 l_pCurrentInputDevice = NULL;
p_pDirectInput->CreateDevice(p_guidDevice, &l_pCurrentInputDevice, NULL);
if (FAILED(l_pCurrentInputDevice->QueryInterface(IID_IDirectInputDevice8, (void**)&m_pJoystick)))
{
return false;
}
SAFE_RELEASE(l_pCurrentInputDevice);

//Set cooperative level
if (FAILED(m_pJoystick->SetCooperativeLevel(GetDesktopWindow(), DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
return false;
}
if (FAILED(m_pJoystick->SetDataFormat(&c_dfDIJoystick)))
{
return false;
}

//Set range and dead zones
//X Range
DIPROPRANGE l_JoystickRange;
l_JoystickRange.lMin = -JOYSTICK_PRESSED;
l_JoystickRange.lMax = JOYSTICK_PRESSED;
l_JoystickRange.diph.dwSize = sizeof(DIPROPRANGE);
l_JoystickRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
l_JoystickRange.diph.dwObj = DIJOFS_X;
l_JoystickRange.diph.dwHow = DIPH_BYOFFSET;
m_pJoystick->SetProperty(DIPROP_RANGE, &l_JoystickRange.diph);

//Y Range
l_JoystickRange.lMin = -JOYSTICK_PRESSED;
l_JoystickRange.lMax = JOYSTICK_PRESSED;
l_JoystickRange.diph.dwSize = sizeof(DIPROPRANGE);
l_JoystickRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
l_JoystickRange.diph.dwObj = DIJOFS_Y;
l_JoystickRange.diph.dwHow = DIPH_BYOFFSET;
m_pJoystick->SetProperty(DIPROP_RANGE, &l_JoystickRange.diph);

//Acquire the Joystick
if (FAILED(m_pJoystick->Acquire()))
{
return false;
}

//Poll the Joystick
if (FAILED(m_pJoystick->Poll()))
{
return false;
}

return true;
}

char* CJoystick::get_JoystickName()
{
return m_pszJoystickName;
}

sJoystickState* CJoystick::get_State()
{
if (NULL != m_pJoystick)
{
if (FAILED(m_pJoystick->GetDeviceState(sizeof(DIJOYSTATE), (LPVOID)&m_dxJoystickState)))
{
while (DIERR_INPUTLOST == m_pJoystick->Acquire());
return &m_decadeJoystickState;
}

m_decadeJoystickState.XAxis = (short)m_dxJoystickState.lX * -1;
m_decadeJoystickState.YAxis = (short)m_dxJoystickState.lY * -1;

for (int l_iButton = 0; l_iButton < 32; l_iButton++)
{
if (m_dxJoystickState.rgbButtons[l_iButton] > 32)
m_decadeJoystickState.ButtonPressed[l_iButton] = true;
else
m_decadeJoystickState.ButtonPressed[l_iButton] = false;
}

switch (m_dxJoystickState.rgdwPOV[0])
{
case -1:
{
m_decadeJoystickState.ButtonPressed[32] = false; // UP
m_decadeJoystickState.ButtonPressed[33] = false; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = false; // DOWN
m_decadeJoystickState.ButtonPressed[35] = false; // LEFT
break;
}
case 0:
{
m_decadeJoystickState.ButtonPressed[32] = true; // UP
m_decadeJoystickState.ButtonPressed[33] = false; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = false; // DOWN
m_decadeJoystickState.ButtonPressed[35] = false; // LEFT
break;
}
case 4500:
{
m_decadeJoystickState.ButtonPressed[32] = true; // UP
m_decadeJoystickState.ButtonPressed[33] = true; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = false; // DOWN
m_decadeJoystickState.ButtonPressed[35] = false; // LEFT
break;
}
case 9000:
{
m_decadeJoystickState.ButtonPressed[32] = false; // UP
m_decadeJoystickState.ButtonPressed[33] = true; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = false; // DOWN
m_decadeJoystickState.ButtonPressed[35] = false; // LEFT
break;
}
case 9000+4500:
{
m_decadeJoystickState.ButtonPressed[32] = false; // UP
m_decadeJoystickState.ButtonPressed[33] = true; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = true; // DOWN
m_decadeJoystickState.ButtonPressed[35] = false; // LEFT
break;
}
case 18000:
{
m_decadeJoystickState.ButtonPressed[32] = false; // UP
m_decadeJoystickState.ButtonPressed[33] = false; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = true; // DOWN
m_decadeJoystickState.ButtonPressed[35] = false; // LEFT
break;
}
case 18000+4500:
{
m_decadeJoystickState.ButtonPressed[32] = false; // UP
m_decadeJoystickState.ButtonPressed[33] = false; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = true; // DOWN
m_decadeJoystickState.ButtonPressed[35] = true; // LEFT
break;
}
case 18000+9000:
{
m_decadeJoystickState.ButtonPressed[32] = false; // UP
m_decadeJoystickState.ButtonPressed[33] = false; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = false; // DOWN
m_decadeJoystickState.ButtonPressed[35] = true; // LEFT
break;
}
case 18000+9000+4500:
{
m_decadeJoystickState.ButtonPressed[32] = true; // UP
m_decadeJoystickState.ButtonPressed[33] = false; // RIGHT
m_decadeJoystickState.ButtonPressed[34] = false; // DOWN
m_decadeJoystickState.ButtonPressed[35] = true; // LEFT
break;
}
}
}
else
{
ZeroMemory(&m_decadeJoystickState, sizeof(sJoystickState));
}

return &m_decadeJoystickState;
}



As suggested by Reegan, filling the DIPROPRANGE tells DirectInput to return a value between Min and Max for each Axis of the Joystick. (Here I set it to -255, 255).

Ciarán

Share this post


Link to post
Share on other sites

This topic is 3313 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this