Sign in to follow this  

DirectInput help needed

Recommended Posts

Hi you guys, I've spent weeks on this project and can't get my force feedback to work in my c++ program. Can anyone take a look at a portion of my directX code and see what i am doing wrong?
LPDIRECTINPUT8 din;    // the pointer to our DirectInput interface
LPDIRECTINPUTDEVICE8 dinjoystick;	//the pointer to the joystick device
DWORD      dwAxes[2] = {DIJOFS_X, DIJOFS_Y};
LONG       lDirection[2] = {0, 0};
DIPERIODIC diPeriodic;      // type-specific parameters
DIENVELOPE diEnvelope;      // envelope
DIEFFECT   diEffect;        // general parameters
LPDIRECTINPUTEFFECT  g_lpdiEffect;  // global effect object

// function prototypes
void initDInput(HINSTANCE hInstance, HWND hWnd);    // sets up and initializes DirectInput
void cleanDInput(void);    // closes DirectInput and releases memory

void initDInput(HINSTANCE hInstance, HWND hWnd){	//this is the function that initializes DirectInput
	// create the DirectInput interface
    hr = DirectInput8Create(hInstance,    // the handle to the application
                       DIRECTINPUT_VERSION,    // the compatible version
                       IID_IDirectInput8,    // the DirectInput interface version
                       (void**)&din,    // the pointer to the interface
                       NULL);    // COM stuff, so we'll set it to NULL

      return;    // return to WinMain()

BOOL CALLBACK DIEnumDevicesProc(LPCDIDEVICEINSTANCE instance, LPVOID pvRef ){	//Joystick Functions
    HRESULT hr;

	GUID DeviceGuid = instance->guidInstance;
	//enumerating joystick devices
    hr = din->EnumDevices(DI8DEVCLASS_GAMECTRL,
		if (FAILED(hr))
		  // No force-feedback joystick available; take appropriate action. 

    //create game device
    hr = din->CreateDevice(instance->guidInstance, &dinjoystick, NULL);

		if (FAILED(hr)) { 

    // Stop enumeration. Note: we're just taking the first joystick we get.
    return DIENUM_STOP;

	// Set cooperative level.
    hr = dinjoystick->SetCooperativeLevel(NULL,
                                  DISCL_EXCLUSIVE | DISCL_FOREGROUND);
		if (FAILED(hr))
			return hr;

    // set game data format
    hr = dinjoystick->SetDataFormat(&c_dfDIJoystick);
    if (FAILED(hr))
        return hr;

	//turn off autocenter feature

    DIPropAutoCenter.diph.dwSize       = sizeof(DIPropAutoCenter);
    DIPropAutoCenter.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    DIPropAutoCenter.diph.dwObj        = 0;
    DIPropAutoCenter.diph.dwHow        = DIPH_DEVICE;
    DIPropAutoCenter.dwData            = DIPROPAUTOCENTER_OFF;

    hr = dinjoystick->SetProperty(DIPROP_AUTOCENTER, 
    if (FAILED(hr))
        // Handle the failure as appropriate



    if (dinjoystick == NULL) {
        return S_OK;

    // Poll the device to read the current state
    hr = dinjoystick->Poll(); 
    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 = dinjoystick->Acquire();
        while (hr == DIERR_INPUTLOST) {
            hr = dinjoystick->Acquire();

        // If we encounter a fatal error, return failure.
        if ((hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED)) {
            return E_FAIL;

        // If another application has control of this device, return successfully.
        // We'll just have to wait our turn to use the joystick.
        if (hr == DIERR_OTHERAPPHASPRIO) {
            return S_OK;

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

    return S_OK;

void createeffect(void){

	diPeriodic.dwMagnitude = DI_FFNOMINALMAX; 
	diPeriodic.lOffset = 0; 
	diPeriodic.dwPhase = 0; 
	diPeriodic.dwPeriod = (DWORD)(0.05 * DI_SECONDS); 
	diEnvelope.dwSize = sizeof(DIENVELOPE);
	diEnvelope.dwAttackLevel = 0; 
	diEnvelope.dwAttackTime = (DWORD)(0.5 * DI_SECONDS); 
	diEnvelope.dwFadeLevel = 0; 
	diEnvelope.dwFadeTime = (DWORD)(1.0 * DI_SECONDS); 
	diEffect.dwSize = sizeof(DIEFFECT); 
	diEffect.dwDuration = (DWORD)(2 * DI_SECONDS);

	diEffect.dwSamplePeriod = 0;               // = default 
	diEffect.dwGain = DI_FFNOMINALMAX;         // no scaling
	diEffect.dwTriggerButton = DIJOFS_BUTTON0;
	diEffect.dwTriggerRepeatInterval = 0;      
	diEffect.cAxes = 2; 
	diEffect.rgdwAxes = dwAxes; 
	diEffect.rglDirection = &lDirection[0]; 
	diEffect.lpEnvelope = &diEnvelope; 
	diEffect.cbTypeSpecificParams = sizeof(diPeriodic);
	diEffect.lpvTypeSpecificParams = &diPeriodic; 
	hr = dinjoystick->CreateEffect(
                     GUID_ConstantForce,     // GUID from enumeration
                     NULL,      // where the data is
                     &g_lpdiEffect,  // where to put interface pointer
                     NULL);          // no aggregation


void cleanDInput(void)	// this is the function that closes DirectInput
    dinjoystick->Unacquire();	//closing the joystick
	din->Release();    // close DirectInput before exiting


Share this post

Link to post
Share on other sites

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