Jump to content

  • Log In with Google      Sign In   
  • Create Account


[DX8] GetDeviceState for joystick


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Sh@dow   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 August 2011 - 04:12 PM

Hi everyone,
I'm using DirectInput to read joystick state. Especially i need button states. Here is the code.

BOOL CALLBACK
enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
    HRESULT result;
    result = di->CreateDevice(instance->guidInstance, &joystick, NULL);
    if (FAILED(result)) { 
        return DIENUM_STOP;
    }
    return DIENUM_CONTINUE;
}

bool EnumerateDevices()
{
if (FAILED(result = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
                               	IID_IDirectInput8, (VOID**)&di, NULL))) 
	{
		return false;
	}
	if (FAILED(result = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback,
                                NULL, DIEDFL_ATTACHEDONLY))) {
    return false;
	}
}

//////////////////MAIN CODE////////////////////
if (joystick == NULL) {
    OutputDebugString(L"JOYSTIC NOT FOUND\n");
    return false;
}

DIDEVCAPS capabilities;

if (FAILED(result = joystick->SetDataFormat(&c_dfDIJoystick))) 
{
	OutputDebugString(L"ERROR\n");
    return;
}

if (FAILED(result = joystick->SetCooperativeLevel(dlg_handle, DISCL_NONEXCLUSIVE |
                                              DISCL_BACKGROUND))) 
{
	OutputDebugString(L"ERROR\n");
    return;
}
capabilities.dwSize = sizeof(DIDEVCAPS);
if (FAILED(result = joystick->GetCapabilities(&capabilities))) 
{
	OutputDebugString(L"ERROR\n");
    return;
}
// this structure holds the data for the property changes
DIPROPRANGE joy_axis_range;
// first set x axis tp -1024 to 1024
joy_axis_range.lMin = -1024;
joy_axis_range.lMax = 1024;

joy_axis_range.diph.dwSize   	= sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj  = DIJOFS_X;
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
joystick->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);

// now y-axis
joy_axis_range.lMin = -1024;
joy_axis_range.lMax = 1024;
joy_axis_range.diph.dwSize   	= sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj        = DIJOFS_Y;
joy_axis_range.diph.dwHow        = DIPH_BYOFFSET;
joystick->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
    
if (joystick == NULL) {
   	OutputDebugString(L"Joystic not found\n");
    }
	result = joystick->Acquire();
	if (FAILED(result = joystick->GetDeviceState(sizeof(DIJOYSTATE), &js))) {
        OutputDebugString(L"The device should have been acquired during the Poll()\n");
		return; 
    }

    joystick->Unacquire();
	joystick->Release();

In EnumDevice i see that the joystic is found. In GetCapabilities function i see the correct parameters of the joystick. When I say Poll the result is DI_NOEFFECT. Sho i removed Poll method. Acquire returns no errors. But after i say GetDeviceState the rgbButtons are zeros even if i press buttons.
The program is dialog based. In CooperativeLevel i pass the handle of the dialog. Where is the problem? Why buttons are not readed?
Thanks,

Sponsor:

#2 Sh@dow   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 August 2011 - 12:59 AM

I found that the problem is in delay between Acquire and GetDeviceState. When no delay is used the buttons are not read. When delay is 50ms or more the button state is returned correctly. I think hardcoded delay value is not a good idea. Cause it can vary from driver to driver. How could i select a proper one?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS