Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
Listing 8-4: Changing effect parameters at run time
    var

       .

       .

       .

      {tracks the direction in degrees}

      EffectDir: Integer;

    implementation

       .

       .

       .

    procedure TfrmJoyTestBed.FormActivate(Sender: TObject);

    var

       .

       .

       .

      AxisArray: array[0..1] of DWORD;

      DirArray: array[0..1] of longint;

      EffectInfo: TDIEffect;

      PeriodicInfo: TDIPeriodic;

    begin

       .

       .

       .

      {this effect plays on the X and Y axis}

      AxisArray[0] := DIJOFS_X;

      AxisArray[1] := DIJOFS_Y;

      {initialize the direction}

      EffectDir := 1;

      DirArray[0] := EffectDir*DI_DEGREES;

      DirArray[1] := 0;

      {initialize a periodic effect}

      PeriodicInfo.dwMagnitude := DI_FFNOMINALMAX;

      PeriodicInfo.lOffset := 0;

      PeriodicInfo.dwPhase := 0;

      PeriodicInfo.dwPeriod := DI_SECONDS;;

      {initialize the global effect parameters}

      EffectInfo.dwSize := SizeOf(TDIEffect);

      EffectInfo.dwFlags := DIEFF_OBJECTOFFSETS or DIEFF_POLAR;

      EffectInfo.dwDuration := INFINITE;              // infinite duration

      EffectInfo.dwSamplePeriod := 0;

      EffectInfo.dwGain := DI_FFNOMINALMAX;

      EffectInfo.dwTriggerButton := DIEB_NOTRIGGER;   // no trigger

      EffectInfo.dwTriggerRepeatInterval := 0;

      EffectInfo.cAxes := 2;

      EffectInfo.rgdwAxes := @AxisArray;

      EffectInfo.rglDirection := @DirArray;

      EffectInfo.lpEnvelope := nil;

      EffectInfo.cbTypeSpecificParams := SizeOf(TDIPeriodic);

      EffectInfo.lpvTypeSpecificParams := @PeriodicInfo;

      {create a sine wave periodic effect}

      FDirectInputDevice.CreateEffect(GUID_sine, @EffectInfo, PeriodicEffect, nil);

      {start the effect, suppressing all other effects on the device}

      PeriodicEffect.Start(1, DIES_SOLO);

      {enable the timer that changes the effect direction}

      tmrDirChanger.Enabled := TRUE;

    end;

    procedure TfrmJoyTestBed.tmrDirChangerTimer(Sender: TObject);

    var

      DirArray: array[0..1] of longint;

      EffectInfo: TDIEffect;

    begin

      {increase the effect direction angle}

      Inc(EffectDir, 2);

      {roll the angle over nicely}

      if EffectDir > 360 then

        EffectDir := 1;

      {display the new angle}

      lblAngle.Caption := Effect Angle: +IntToStr(EffectDir);

      {indicate the new direction}

      DirArray[0] := EffectDir*DI_DEGREES;

      DirArray[1] := 0;

      {set up the effect parameters}

      FillChar(EffectInfo, SizeOf(TDIEffect), 0);

      EffectInfo.dwFlags := DIEFF_POLAR;

      EffectInfo.dwSize := SizeOf(TDIEffect);

      EffectInfo.rglDirection := @DirArray;

      EffectInfo.cAxes := 2;

      {dynamically change the parameters, indicating that the effect

       should not restart. if the device doesn’t support this, the change

       will fail, but the effect will continue to play}

      PeriodicEffect.SetParameters(EffectInfo, DIEP_DIRECTION or DIEP_NORESTART);

    end;
### Envelopes The basic effect types give the developer a wide range of possibilities for creating tactile feedback. Indeed, using custom forces or modifying a force at run time gives us even greater control over the “feel” of the feedback. We can further customize an effect by using an envelope. An envelope is a method by which a force can be shaped, and is very similar in concept to envelopes in musical terms. Specifically, an envelope describes the starting magnitude for an effect and the amount of time it takes for the magnitude to go from this initial starting point to the magnitude defined by the type-specific parameters of the specific effect. This is known as the attack. Similarly, the envelope also describes the final magnitude of the effect and the amount of time it takes for the magnitude to go from the effect-defined magnitude to this final level. This is known as the fade. These times take place during the entire duration of the effect, so the sum of both times should not exceed this duration. Any remaining time indicates the sustain, and determines how much time the effect is played at the defined magnitude. A visual illustration of an envelope is shown in Figure 8-7.
| envelope: A series of values that shape a force at the beginning and end of playback.
Figure 8-7: An envelope shaping an effect
| Note: Envelopes do not apply to condition effects.
Envelopes are defined by a TDIEnvelope structure. A pointer to this structure is placed in the lpEnvelope member of the TDIEffect structure when an effect is defined. The TDIEnvelope structure is defined as:
    TDIEnvelope = packed record

      dwSize : DWORD;                   // the size of the structure in bytes

      dwAttackLevel : DWORD;            // the initial magnitude

      dwAttackTime : DWORD;             // the attack duration

      dwFadeLevel : DWORD;              // the ending magnitude

      dwFadeTime : DWORD;               // the fade duration

    end;
As usual with so many DirectX structures, the dwSize member must be set to SizeOf(TDIEnvelope). The dwAttackLevel member defines the starting magnitude for the effect. Actually, this defines a percentage, in hundredths of a percent, of the maximum available force in the direction of the effect, and thus can be a value between 0 and 10,000. The dwFadeLevel member is very similar, except that it defines the percentage of maximum available force at the end of effect playback. Both dwAttackTime and dwFadeTime define the duration of the attack and fade in microseconds.
| Note: Neither the attack nor fade levels need to be less than the sustain level. If they are defined greater than the sustain level, the effect will start at a greater level than the sustain and will fall down to it (or vice versa in the case of fade level).
The following example demonstrates applying an envelope to a sine wave periodic force.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.