]> Devoid-pointer.net GitWeb - FFBChecker.git/commitdiff
Implement periodic effects via SDL2
authorMichal Malý <madcatxster@devoid-pointer.net>
Sat, 1 Aug 2015 11:25:05 +0000 (13:25 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Sat, 1 Aug 2015 11:25:05 +0000 (13:25 +0200)
CMakeLists.txt
sdl2ffbconditioneffect.h
sdl2ffbeffect.cpp
sdl2ffbeffectfactory.cpp
sdl2ffbeffectfactory.h
sdl2ffbperiodiceffect.cpp [new file with mode: 0644]
sdl2ffbperiodiceffect.h [new file with mode: 0644]

index a18ce447f211a0d110ef56a0b206c3000b98fe74..964f67132f61f26dbd036123db1f886a0982b3c0 100644 (file)
@@ -59,6 +59,7 @@ if (SDL2_FOUND)
         sdl2ffbeffect.cpp
         sdl2ffbconstanteffect.cpp
         sdl2ffbconditioneffect.cpp
+        sdl2ffbperiodiceffect.cpp
         sdl2deviceprober.cpp)
 endif()
 
index ed2345f293562da142394bbd0bc43fb8e9018319..178abc8da364f72a21420ac5bc7eacfc43a6ca5f 100644 (file)
@@ -11,9 +11,10 @@ public:
   virtual SDL_HapticEffect* createFFstruct();
   inline const std::shared_ptr<FFBEffectParameters> parameters() const { return m_params; }
   bool setParameters(std::shared_ptr<FFBEffectParameters> params);
-  bool setParameters(std::shared_ptr<FFBConditionEffectParameters> params);
 
 private:
+  bool setParameters(std::shared_ptr<FFBConditionEffectParameters> params);
+
   std::shared_ptr<FFBConditionEffectParameters> m_params;
 };
 
index cc407a98b9402a9de770e085ae77b103a0956e0b..2e22b921ee7b6be81af68fd0048fc1aac6670d24 100644 (file)
@@ -65,8 +65,8 @@ bool SDL2FFBEffect::checkGenericParameters(const std::shared_ptr<FFBEffectParame
   if (!GlobalSettings::GS()->doSanityChecks)
     return true;
 
-  if (!checkBoundsInclusive(params->direction, 0, 35999)) {
-    QMessageBox::warning(nullptr, CAPTION, "Direction must be within <0; 35999)");
+  if (!checkBoundsInclusive(params->direction, 0, 36000)) {
+    QMessageBox::warning(nullptr, CAPTION, "Direction must be within <0; 36000)");
     return false;
   }
 
index 19fc4b289ec86dcd8e9223b6b066b4076051c18f..c7197005b54bcc6031c429f930c09e1568f885c0 100644 (file)
@@ -9,6 +9,8 @@ std::shared_ptr<FFBEffect> SDL2FFBEffectFactory::createEffect(FFBEffectTypes typ
       return std::shared_ptr<SDL2FFBEffect>(new SDL2FFBConditionEffect());
     case FFBEffectTypes::CONSTANT:
       return std::shared_ptr<SDL2FFBEffect>(new SDL2FFBConstantEffect());
+    case FFBEffectTypes::PERIODIC:
+      return std::shared_ptr<SDL2FFBEffect>(new SDL2FFBPeriodicEffect());
     default:
       return std::shared_ptr<FFBEffect>(new FFBNullEffect());
   }
index 14453e4aabb7db43cea838d4e910b5bb8c3fbb2b..998742bd1c0e1318dd99df9cbeceb8eb95843b72 100644 (file)
@@ -5,6 +5,7 @@
 #include "ffbnulleffect.h"
 #include "sdl2ffbconstanteffect.h"
 #include "sdl2ffbconditioneffect.h"
+#include "sdl2ffbperiodiceffect.h"
 
 class SDL2FFBEffectFactory
 {
diff --git a/sdl2ffbperiodiceffect.cpp b/sdl2ffbperiodiceffect.cpp
new file mode 100644 (file)
index 0000000..5559c3a
--- /dev/null
@@ -0,0 +1,101 @@
+#include "sdl2ffbperiodiceffect.h"
+#include "globalsettings.h"
+#include <QtWidgets/QMessageBox>
+
+SDL2FFBPeriodicEffect::SDL2FFBPeriodicEffect() :
+  SDL2FFBEffect(FFBEffectTypes::PERIODIC)
+{}
+
+SDL_HapticEffect* SDL2FFBPeriodicEffect::createFFstruct()
+{
+  SDL_HapticEffect* effect = SDL2FFBEffect::createFFstruct();
+
+  if (effect == nullptr)
+    return nullptr;
+
+  switch (m_params->waveform) {
+    case PeriodicWaveforms::SINE:
+      effect->type = SDL_HAPTIC_SINE;
+      break;
+    case PeriodicWaveforms::TRIANGLE:
+      effect->type = SDL_HAPTIC_TRIANGLE;
+      break;
+    case PeriodicWaveforms::SAW_UP:
+      effect->type = SDL_HAPTIC_SAWTOOTHUP;
+      break;
+    case PeriodicWaveforms::SAW_DOWN:
+      effect->type = SDL_HAPTIC_SAWTOOTHDOWN;
+    default:
+      delete effect;
+      return nullptr;
+  }
+
+  effect->periodic.direction.type = SDL_HAPTIC_POLAR;
+  effect->periodic.direction.dir[0] = m_params->direction;
+
+  if (m_params->replayLength == 0)
+    effect->periodic.length = SDL_HAPTIC_INFINITY;
+  else
+    effect->periodic.length = m_params->replayLength;
+
+  effect->periodic.delay = m_params->replayDelay;
+
+  effect->periodic.period = m_params->period;
+  effect->periodic.magnitude = m_params->magnitude;
+  effect->periodic.offset = m_params->offset;
+  effect->periodic.phase = m_params->phase;
+
+  return effect;
+}
+
+bool SDL2FFBPeriodicEffect::setParameters(const std::shared_ptr<FFBEffectParameters> params)
+{
+  try {
+    const std::shared_ptr<FFBPeriodicEffectParameters> iParams = std::dynamic_pointer_cast<FFBPeriodicEffectParameters>(params);
+    return setParameters(iParams);
+  } catch (std::bad_cast& ) {
+    return false;
+  }
+}
+
+bool SDL2FFBPeriodicEffect::setParameters(const std::shared_ptr<FFBPeriodicEffectParameters> params)
+{
+  if (!GlobalSettings::GS()->doSanityChecks)
+    return true;
+
+  if (!checkGenericParameters(params))
+    return false;
+
+  if (!checkEnvelopeParameters(params->attackLength, params->attackLevel, params->fadeLength, params->fadeLevel))
+    return false;
+
+  if (!checkBoundsInclusive(params->period, 0, 36000)) {
+    QMessageBox::warning(nullptr, CAPTION, "Period must be within <0; 36000>");
+    return false;
+  }
+
+  if (!checkBoundsInclusive(params->magnitude, -0x7FFF, 0x7FFF)) {
+    QMessageBox::warning(nullptr, CAPTION, "Magnitude must be within <-32767; 32767>");
+    return false;
+  }
+
+  if (!checkBoundsInclusive(params->offset, -0x7FFF, 0x7FFF)) {
+    QMessageBox::warning(nullptr, CAPTION, "Offset must be within <-32767; 32767>");
+    return false;
+  }
+
+  if (!checkBoundsInclusive(params->phase, 0, 36000)) {
+    QMessageBox::warning(nullptr, CAPTION, "Phase must be withing <0; 36000>");
+    return false;
+  }
+
+  if (params->waveform == PeriodicWaveforms::NONE ||
+      params->waveform == PeriodicWaveforms::SQUARE) {
+    QMessageBox::warning(nullptr, CAPTION, "Unsupported waveform");
+    return false;
+  }
+
+  m_params = params;
+  return true;
+}
+
diff --git a/sdl2ffbperiodiceffect.h b/sdl2ffbperiodiceffect.h
new file mode 100644 (file)
index 0000000..aa15774
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef SDL2FFBPERIODIEFFECT_H
+#define SDL2FFBPERIODIEFFECT_H
+
+#include "sdl2ffbeffect.h"
+#include "ffbperiodiceffectparameters.h"
+
+class SDL2FFBPeriodicEffect : public SDL2FFBEffect
+{
+public:
+  SDL2FFBPeriodicEffect();
+  SDL_HapticEffect* createFFstruct();
+  inline const std::shared_ptr<FFBEffectParameters> parameters() const { return m_params; }
+  bool setParameters(const std::shared_ptr<FFBEffectParameters> params);
+
+private:
+  bool setParameters(const std::shared_ptr<FFBPeriodicEffectParameters> params);
+
+  std::shared_ptr<FFBPeriodicEffectParameters> m_params;
+};
+
+#endif // SDL2FFBEPERIODIFFECT_H