@@ -63,8 +63,10 @@ namespace vibration {
6363 if (thrVibration == NULL ) {
6464 quitVibrationThread = false ;
6565
66- for (int k = 0 ; k < MAX_EFFECTS; k++)
66+ for (int k = 0 ; k < MAX_EFFECTS; k++) {
6767 VibEffects[k].isActive = FALSE ;
68+ VibEffects[k].dwEffectId = -1 ;
69+ }
6870
6971 thrVibration.reset (new std::thread (VibrationController::VibrationThreadEntryPoint));
7072 }
@@ -171,47 +173,93 @@ namespace vibration {
171173 void VibrationController::StartEffect (DWORD dwEffectID, LPCDIEFFECT peff)
172174 {
173175 mtxSync.lock ();
176+
177+ int idx = -1 ;
178+ // Reusing the same idx if effect was already created
174179 for (int k = 0 ; k < MAX_EFFECTS; k++) {
175- if (VibEffects[k].isActive && k < MAX_EFFECTS - 1 )
176- continue ;
180+ if (VibEffects[k].dwEffectId == dwEffectID) {
181+ idx = k;
182+ break ;
183+ }
184+ }
177185
178- // Calculating intensity
179- byte forceX = 0xfe ;
180- byte forceY = 0xfe ;
186+ // Find a non-active idx
187+ if (idx < 0 ) {
188+ for (int k = 0 ; k < MAX_EFFECTS; k++) {
189+ if (!VibEffects[k].isActive || k == MAX_EFFECTS - 1 ) {
190+ idx = k;
191+ break ;
192+ }
193+ }
194+ }
195+
196+ // Calculating intensity
197+ byte forceX = 0xfe ;
198+ byte forceY = 0xfe ;
199+
200+ byte magnitude = 0xfe ;
201+ if (peff->cbTypeSpecificParams == 4 ) {
202+ LPDICONSTANTFORCE effParams = (LPDICONSTANTFORCE)peff->lpvTypeSpecificParams ;
203+ magnitude = (byte)(round ((((double )effParams->lMagnitude ) / 10000.0 ) * 254.0 ));
204+ }
181205
206+ if (peff->cAxes == 1 ) {
207+ // If direction is negative, then it is a forceX
208+ // Otherwise it is a forceY
209+ LONG direction = peff->rglDirection [0 ];
210+ static byte lastForceX = 0 ;
211+ static byte lastForceY = 0 ;
212+
213+ forceX = lastForceX;
214+ forceY = lastForceY;
215+
216+ if (direction == -1 ) {
217+ // forceX = lastForceX = (byte)(round((((double)peff->dwGain) / 10000.0) * 254.0));
218+ forceX = lastForceX = magnitude;
219+ }
220+ else if (direction == 1 ) {
221+ // forceY = lastForceY = (byte)(round((((double)peff->dwGain) / 10000.0) * 254.0));
222+ forceY = lastForceY = magnitude;
223+ }
224+
225+ }
226+ else {
182227 if (peff->cAxes >= 1 ) {
183228 LONG fx = peff->rglDirection [0 ];
184- if (fx <= 1 ) fx = peff->dwGain ;
229+ // if (fx <= 1) fx = peff->dwGain;
185230
186- forceX = forceY = (byte)abs (round ((((double )fx) / 10000.0 ) * 254.0 ));
231+ if (fx > 0 )
232+ forceX = forceY = magnitude;
233+ else
234+ forceX = forceY = 0 ;
187235 }
188236
189237 if (peff->cAxes >= 2 ) {
190238 LONG fy = peff->rglDirection [1 ];
191- if (fy <= 1 ) fy = peff->dwGain ;
239+ // if (fy <= 1) fy = peff->dwGain;
192240
193- forceY = (byte)abs (round ((((double )fy) / 10000.0 ) * 254.0 ));
241+ if (fy > 0 )
242+ forceY = magnitude;
243+ else
244+ forceY = 0 ;
194245 }
246+ }
195247
196248
197- DWORD frame = GetTickCount ();
198-
199- VibEffects[k].forceX = forceX;
200- VibEffects[k].forceY = forceY;
249+ DWORD frame = GetTickCount ();
201250
202- VibEffects[k].dwEffectId = dwEffectID;
203- VibEffects[k].dwStartFrame = frame + (peff->dwStartDelay / 1000 );
204- VibEffects[k].dwStopFrame =
205- peff->dwDuration == INFINITE ? INFINITE :
206- VibEffects[k].dwStartFrame + (peff->dwDuration / 1000 );
207- VibEffects[k].isActive = TRUE ;
208- VibEffects[k].started = FALSE ;
251+ VibEffects[idx].forceX = forceX;
252+ VibEffects[idx].forceY = forceY;
209253
210- break ;
211- }
254+ VibEffects[idx].dwEffectId = dwEffectID;
255+ VibEffects[idx].dwStartFrame = frame + (peff->dwStartDelay / 1000 );
256+ VibEffects[idx].dwStopFrame =
257+ peff->dwDuration == INFINITE ? INFINITE :
258+ VibEffects[idx].dwStartFrame + (peff->dwDuration / 1000 );
259+ VibEffects[idx].isActive = TRUE ;
260+ VibEffects[idx].started = FALSE ;
212261
213262 mtxSync.unlock ();
214-
215263 StartVibrationThread ();
216264 }
217265
0 commit comments