Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ForceFeedback()
- {
- // set up direct input
- DirectInput di = new DirectInput();
- Device wheel = null;
- // find wheel
- foreach (DeviceInstance instance in di.GetDevices(
- DeviceClass.GameControl,
- DeviceEnumerationFlags.AttachedOnly | DeviceEnumerationFlags.ForceFeedback))
- {
- wheel = new Joystick(di, instance.InstanceGuid);
- }
- // set cooperative level
- wheel.SetCooperativeLevel(GetWindowHandle(),
- CooperativeLevel.Exclusive | CooperativeLevel.Background);
- // set axis mode to absolute
- wheel.Properties.AxisMode = DeviceAxisMode.Absolute;
- // set buffer size
- wheel.Properties.BufferSize = 128;
- // acquire wheel for capturing
- wheel.Acquire();
- //Configure axes
- int[] axis = null;
- foreach (DeviceObjectInstance doi in wheel.GetObjects())
- {
- // set axes ranges
- if (((int)doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
- {
- wheel.Properties.Range =
- new InputRange(-5000, 5000);
- }
- int[] temp;
- // get info about first two ff axes on device
- if (doi.ObjectType.CompareTo(ToGuid((int)DeviceObjectTypeFlags.ForceFeedbackActuator)) != 0)
- {
- if (axis != null)
- {
- temp = new int[axis.Length + 1];
- axis.CopyTo(temp, 0);
- axis = temp;
- }
- else
- {
- axis = new int[1];
- }
- // store the offset of each axis.
- axis[axis.Length - 1] = doi.Offset;
- if (axis.Length == 2)
- {
- break;
- }
- }
- }
- // see if wheel supports ConstantForce and set it
- Effect e;
- foreach (EffectInfo ei in wheel.GetEffects(EffectType.All))
- {
- if ((ei.Type & EffectType.ConstantForce) != 0)
- {
- // fill in some generic values for the effect
- EffectParameters param = new EffectParameters();
- param.Directions = new int[axis.Length];
- param.Axes = new int[axis.Length];
- // -> param.conditionStruct ?
- // -> param.effectType ?
- param.Duration = int.MaxValue;
- param.Gain = 10000;
- param.SamplePeriod = 0;
- param.Flags = EffectFlags.ObjectOffsets | EffectFlags.Cartesian;
- param.Axes = axis;
- e = new Effect(wheel, wheel.Information.ForceFeedbackDriverGuid, param);
- e.Start();
- }
- }
- }
- /// <summary>
- /// Convert int to guid. To be used in forcefeedback function
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- private static Guid ToGuid(int value)
- {
- byte[] bytes = new byte[16];
- BitConverter.GetBytes(value).CopyTo(bytes, 0);
- return new Guid(bytes);
- }
- // function using native dll to get active window handle (for sharpdx cooperation levels)
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- private static extern System.IntPtr GetActiveWindow();
- public static System.IntPtr GetWindowHandle()
- {
- return GetActiveWindow();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement