Advertisement
Guest User

Untitled

a guest
Jul 11th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. private void ForceFeedback()
  2. {
  3. // set up direct input
  4. DirectInput di = new DirectInput();
  5. Device wheel = null;
  6.  
  7. // find wheel
  8. foreach (DeviceInstance instance in di.GetDevices(
  9. DeviceClass.GameControl,
  10. DeviceEnumerationFlags.AttachedOnly | DeviceEnumerationFlags.ForceFeedback))
  11. {
  12. wheel = new Joystick(di, instance.InstanceGuid);
  13. }
  14.  
  15. // set cooperative level
  16. wheel.SetCooperativeLevel(GetWindowHandle(),
  17. CooperativeLevel.Exclusive | CooperativeLevel.Background);
  18.  
  19. // set axis mode to absolute
  20. wheel.Properties.AxisMode = DeviceAxisMode.Absolute;
  21.  
  22. // set buffer size
  23. wheel.Properties.BufferSize = 128;
  24.  
  25. // acquire wheel for capturing
  26. wheel.Acquire();
  27.  
  28. //Configure axes
  29. int[] axis = null;
  30. foreach (DeviceObjectInstance doi in wheel.GetObjects())
  31. {
  32. // set axes ranges
  33. if (((int)doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
  34. {
  35. wheel.Properties.Range =
  36. new InputRange(-5000, 5000);
  37. }
  38.  
  39. int[] temp;
  40.  
  41. // get info about first two ff axes on device
  42. if (doi.ObjectType.CompareTo(ToGuid((int)DeviceObjectTypeFlags.ForceFeedbackActuator)) != 0)
  43. {
  44. if (axis != null)
  45. {
  46. temp = new int[axis.Length + 1];
  47. axis.CopyTo(temp, 0);
  48. axis = temp;
  49. }
  50. else
  51. {
  52. axis = new int[1];
  53. }
  54.  
  55. // store the offset of each axis.
  56. axis[axis.Length - 1] = doi.Offset;
  57. if (axis.Length == 2)
  58. {
  59. break;
  60. }
  61. }
  62. }
  63.  
  64. // see if wheel supports ConstantForce and set it
  65. Effect e;
  66.  
  67. foreach (EffectInfo ei in wheel.GetEffects(EffectType.All))
  68. {
  69. if ((ei.Type & EffectType.ConstantForce) != 0)
  70. {
  71. // fill in some generic values for the effect
  72. EffectParameters param = new EffectParameters();
  73. param.Directions = new int[axis.Length];
  74. param.Axes = new int[axis.Length];
  75. // -> param.conditionStruct ?
  76. // -> param.effectType ?
  77. param.Duration = int.MaxValue;
  78. param.Gain = 10000;
  79. param.SamplePeriod = 0;
  80. param.Flags = EffectFlags.ObjectOffsets | EffectFlags.Cartesian;
  81. param.Axes = axis;
  82.  
  83. e = new Effect(wheel, wheel.Information.ForceFeedbackDriverGuid, param);
  84. e.Start();
  85. }
  86. }
  87. }
  88.  
  89. /// <summary>
  90. /// Convert int to guid. To be used in forcefeedback function
  91. /// </summary>
  92. /// <param name="value"></param>
  93. /// <returns></returns>
  94. private static Guid ToGuid(int value)
  95. {
  96. byte[] bytes = new byte[16];
  97. BitConverter.GetBytes(value).CopyTo(bytes, 0);
  98. return new Guid(bytes);
  99. }
  100.  
  101. // function using native dll to get active window handle (for sharpdx cooperation levels)
  102. [System.Runtime.InteropServices.DllImport("user32.dll")]
  103. private static extern System.IntPtr GetActiveWindow();
  104.  
  105. public static System.IntPtr GetWindowHandle()
  106. {
  107. return GetActiveWindow();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement