Creating an Effect

Create an effect object by using the IDirectInputDevice8::CreateEffect method, as in the following code example where g_lpdid points to an instance of the interface. This example creates a very simple effect that pulls the joystick away from the user at full force for half a second.

HRESULT  hr;
LPDIRECTINPUTEFFECT lpdiEffect;  // receives pointer to created effect
DIEFFECT diEffect;               // parameters for created effect
DWORD    dwAxes[2] = { DIJOFS_X, DIJOFS_Y };
LONG     lDirection[2] = { 18000, 0 };
DICONSTANTFORCE diConstantForce; 

diConstantForce.lMagnitude = DI_FFNOMINALMAX;   // Full force
diEffect.dwSize          = sizeof(DIEFFECT); 
diEffect.dwFlags         = DIEFF_POLAR | DIEFF_OBJECTOFFSETS; 
diEffect.dwDuration      = (DWORD)(0.5 * DI_SECONDS);
diEffect.dwSamplePeriod  = 0;                 // = default 
diEffect.dwGain          = DI_FFNOMINALMAX;   // No scaling
diEffect.dwTriggerButton = DIEB_NOTRIGGER;    // Not a button response
diEffect.dwTriggerRepeatInterval = 0;         // Not applicable
diEffect.cAxes                   = 2; 
diEffect.rgdwAxes                = &dwAxes[0]; 
diEffect.rglDirection            = &lDirection[0]; 
diEffect.lpEnvelope              = NULL; 
diEffect.cbTypeSpecificParams    = sizeof(DICONSTANTFORCE);
diEffect.lpvTypeSpecificParams   = &diConstantForce;  

hr = g_lpdid->CreateEffect(GUID_ConstantForce,
                           &d iEffect,
                           &lpdiEffect,
						   NULL);

In the method call, the first parameter identifies the supported effect with which the created effect is to be associated. The example uses one of the predefined globally unique identifier (GUID)s found in Dinput.h. If you use a predefined GUID, the call fails if the device does not support the effect.

The second parameter sets the parameters as specified in the DIEFFECT structure.

The third parameter receives a pointer to the effect object if the call is successful.

The DIEFF_POLAR flag specifies the type of coordinates used for the direction of the force. (See Effect Direction.) It is combined with DIEFF_OBJECTOFFSETS, which indicates that any buttons or axes used in other members are identified by their offsets within the DIDATAFORMAT structure for the device. The alternative is to use the DIEFF_OBJECTIDS flag, signifying that buttons and axes are identified by the dwType member of the DIDEVICEOBJECTINSTANCE structure returned for the object when it was enumerated with the IDirectInputDevice8::EnumObjects method.

For more information about the members of the DIEFFECT structure, see Effect Direction.