[ Pobierz całość w formacie PDF ]
.It also passes in the address of a global effect identifier, guidEffect,which will eventually contain the enumerated effect.The flag DIEFT_CONSTANTFORCE isused to indicate that only constant force effects are to be enumerated.The work of obtaining a global effect identifier is left up to the enumeration callbackfunction, EnumEffectProc().Listing 18.2 contains the code for this function.LISTING 18.2 The EnumEffectProc() Callback Function That Is Called toEnumerate Force Feedback Effects1: BOOL CALLBACK EnumEffectProc(LPCDIEFFECTINFO pei, LPVOID pv)2: {3: GUID* pguidEffect = NULL;4:5: if(pv)6: {7: // Set the global effect identifier8: pguidEffect = (GUID*)pv;9: *pguidEffect = pei->guid;10:11: // Stop enumerating12: return DIENUM_STOP;13: }14:15: // Keep enumerating16: return DIENUM_CONTINUE;17: }25 1634xCH18 11/13/99 11:12 AM Page 367Getting Through to the User Force Feedback 367The EnumEffectProc() callback function in Listing 18.2 presents a fairly simplisticapproach to enumerating effects because it grabs the first effect and quits.You could addadditional logic to dig into the DIEFFECTINFO structure pointed to by the pei parameterand determine more about each enumerated effect.For now, let s assume that the firstenumerated constant force effect is acceptable.That way we can move on to creating theactual effect object.Creating Force Feedback EffectsAfter you ve determined that a particular force feedback effect is available, you re readyto create the actual effect.Force feedback effects are relatively complex to allow for lotsof flexibility.For this reason, there are a variety of different data structures that you mustinitialize in order to create an effect.Following are the structures involved in creating aneffect:An array of axes for the effectAn array of direction values for the effectA type-specific structure such as DICONSTANTFORCE, DIRAMPFORCE, DIPERIODIC, orDICONDITIONA DIENVELOPE structure for defining the effect s envelope (optional)18A DIEFFECT structure that pulls together the other data structuresThe first two data structures are very closely related because the number of axes of aneffect determines the number of dimensions of the direction.For example, an effect act-ing along the X and Y axes will require a direction that consists of X and Y components,assuming that you re expressing the direction in terms of Cartesian coordinates.The realpoint I m getting at is that the number of elements in the direction array must match thenumber of elements in the axes array, even if you don t end up using all the elements.Why would you not use all the elements?The answer has to do with the fact that you can express the direction of an effect in oneof three ways:Cartesian coordinatesPolar coordinatesSpherical coordinatesCartesian coordinates consist of simple XYZ components.However, because they arebeing used solely for establishing a direction, the magnitudes of the values don t reallymatter.For example, the following direction arrays all refer to a two-dimensional effectoriginating in the up, or north, direction:LONG lDirection1[2] = { 0, -1 };LONG lDirection2[2] = { 0, -4 };LONG lDirection3[2] = { 0, -27 };25 1634xCH18 11/13/99 11:12 AM Page 368368 Hour 18Keep in mind that the Y-axis increases down, which means that a negative Y directionindicates an upward direction.Although Cartesian coordinates are perfectly acceptable for describing effect directions, Iprefer polar coordinates.Polar coordinates involve a single value that indicates a direc-tional angle measured clockwise from the up (north) direction, which lies at 0 degrees.So, a two-dimensional direction originating from the east direction would have the fol-lowing polar coordinates:LONG lDirection[2] = { 90 * DI_DEGREES, 0 };Notice that the DI_DEGREES constant is used to properly convert the degrees for use withDirectInput, which expects degrees to be entered in hundredths of degrees.TheDI_DEGREES constant makes the code a little easier to read.You might also be curiousabout the second array element, 0.If you recall, I said that the number of elements in thedirection array must match the number of elements in the axes array.Because the firstelement is all that matters when dealing with polar coordinates, you can just set the sec-ond element to 0.Your other option in establishing effect direction is to use spherical coordinates.Spherical coordinates are really only useful for describing three-dimensional directions,which as of yet aren t supported in any force feedback devices.So, let s skip the detailsof them and move on.Now that I ve hopefully convinced you to use polar coordinates to define directions, let ssee some code for both the axes and direction arrays together.The following code estab-lishes a two-dimensional effect acting from the northwest direction:DWORD dwAxes[2] = { DIJOFS_X, DIJOFS_Y };LONG lDirection[2] = { 315 * DI_DEGREES, 0 };The next step in creating an effect is to fill out a type-specific structure with details aboutthe effect.In the case of a constant force effect, this involves filling out a DICONSTANT-FORCE structure.Fortunately, the DICONSTANTFORCE structure contains only one member,lMagnitude, which specifies the magnitude of the force.Following is code to set themagnitude of a constant force to the maximum allowable force:DICONSTANTFORCE diConstantForce;diConstantForce.lMagnitude = DI_FFNOMINALMAX;The magnitude of constant force effects ranges from -10000 to +10000.TheDI_FFNOMINALMAX predefined constant is set to +10000.A negative forcemagnitude acts in the opposite direction of the force, whereas a magnitudeof 0 results in no force.25 1634xCH18 11/13/99 11:12 AM Page 369Getting Through to the User Force Feedback 369The last structure to set up for an effect is the DIEFFECT structure, which pulls togetherall the other effect data.This structure is where you indicate the type of coordinates usedto specify the direction of the effect (DIEFF_CARTESIAN, DIEFF_POLAR, or DIEFF_SPHERI-CAL).You also set the duration of the effect, in microseconds, along with the gain of theeffect, if any.Additionally, you can set the effect so that it is triggered automatically offone of the joystick fire buttons.Following is sample code to fill out a DIEFFECT structurefor a simple constant force effect:DIEFFECT diEffect;diEffect.dwSize = sizeof(DIEFFECT);diEffect.dwFlags = DIEFF_POLAR | DIEFF_OBJECTOFFSETS;diEffect.dwDuration = 4 * DI_SECONDS;diEffect.dwSamplePeriod = 0;diEffect.dwGain = DI_FFNOMINALMAX;diEffect.dwTriggerButton = DIEB_NOTRIGGER;diEffect.dwTriggerRepeatInterval = 0;diEffect.cAxes = 2;diEffect.rgdwAxes = dwAxes;diEffect.rglDirection = lDirection;diEffect.lpEnvelope = NULL;diEffect
[ Pobierz całość w formacie PDF ]