SDL: Apply updates immediately rather than going through UpdateOutput

We're going to remove UpdateOutput as it's redundant, and horribly
complicates the code.
This commit is contained in:
Jasper St. Pierre 2014-11-13 00:49:18 -08:00
parent e43ad58a3a
commit 1b3d0173f5
2 changed files with 86 additions and 146 deletions

View File

@ -121,40 +121,26 @@ Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsi
// constant effect // constant effect
if (supported_effects & SDL_HAPTIC_CONSTANT) if (supported_effects & SDL_HAPTIC_CONSTANT)
{ AddOutput(new ConstantEffect(m_haptic));
m_state_out.push_back(EffectIDState());
AddOutput(new ConstantEffect(m_state_out.back()));
}
// ramp effect // ramp effect
if (supported_effects & SDL_HAPTIC_RAMP) if (supported_effects & SDL_HAPTIC_RAMP)
{ AddOutput(new RampEffect(m_haptic));
m_state_out.push_back(EffectIDState());
AddOutput(new RampEffect(m_state_out.back()));
}
// sine effect // sine effect
if (supported_effects & SDL_HAPTIC_SINE) if (supported_effects & SDL_HAPTIC_SINE)
{ AddOutput(new SineEffect(m_haptic));
m_state_out.push_back(EffectIDState());
AddOutput(new SineEffect(m_state_out.back()));
}
#ifdef SDL_HAPTIC_SQUARE #ifdef SDL_HAPTIC_SQUARE
// square effect // square effect
if (supported_effects & SDL_HAPTIC_SQUARE) if (supported_effects & SDL_HAPTIC_SQUARE)
{ AddOutput(new SquareEffect(m_haptic));
m_state_out.push_back(EffectIDState());
AddOutput(new SquareEffect(m_state_out.back()));
}
#endif // defined(SDL_HAPTIC_SQUARE) #endif // defined(SDL_HAPTIC_SQUARE)
// triangle effect // triangle effect
if (supported_effects & SDL_HAPTIC_TRIANGLE) if (supported_effects & SDL_HAPTIC_TRIANGLE)
{ AddOutput(new TriangleEffect(m_haptic));
m_state_out.push_back(EffectIDState());
AddOutput(new TriangleEffect(m_state_out.back()));
}
} }
#endif #endif
@ -167,13 +153,6 @@ Joystick::~Joystick()
{ {
// stop/destroy all effects // stop/destroy all effects
SDL_HapticStopAll(m_haptic); SDL_HapticStopAll(m_haptic);
for (auto &i : m_state_out)
{
if (i.id != -1)
{
SDL_HapticDestroyEffect(m_haptic, i.id);
}
}
// close haptic first // close haptic first
SDL_HapticClose(m_haptic); SDL_HapticClose(m_haptic);
} }
@ -184,6 +163,26 @@ Joystick::~Joystick()
} }
#ifdef USE_SDL_HAPTIC #ifdef USE_SDL_HAPTIC
void Joystick::HapticEffect::Update()
{
if (m_id == -1 && m_effect.type > 0)
{
m_id = SDL_HapticNewEffect(m_haptic, &m_effect);
if (m_id > -1)
SDL_HapticRunEffect(m_haptic, m_id, 1);
}
else if (m_id > -1 && m_effect.type == 0)
{
SDL_HapticStopEffect(m_haptic, m_id);
SDL_HapticDestroyEffect(m_haptic, m_id);
m_id = -1;
}
else if (m_id > -1)
{
SDL_HapticUpdateEffect(m_haptic, m_id, &m_effect);
}
}
std::string Joystick::ConstantEffect::GetName() const std::string Joystick::ConstantEffect::GetName() const
{ {
return "Constant"; return "Constant";
@ -215,58 +214,51 @@ void Joystick::ConstantEffect::SetState(ControlState state)
{ {
if (state) if (state)
{ {
m_effect.effect.type = SDL_HAPTIC_CONSTANT; m_effect.type = SDL_HAPTIC_CONSTANT;
m_effect.effect.constant.length = SDL_HAPTIC_INFINITY; m_effect.constant.length = SDL_HAPTIC_INFINITY;
} }
else else
{ {
m_effect.effect.type = 0; m_effect.type = 0;
} }
const Sint16 old = m_effect.effect.constant.level; m_effect.constant.level = (Sint16)(state * 0x7FFF);
m_effect.effect.constant.level = (Sint16)(state * 0x7FFF); Update();
if (old != m_effect.effect.constant.level)
m_effect.changed = true;
} }
void Joystick::RampEffect::SetState(ControlState state) void Joystick::RampEffect::SetState(ControlState state)
{ {
if (state) if (state)
{ {
m_effect.effect.type = SDL_HAPTIC_RAMP; m_effect.type = SDL_HAPTIC_RAMP;
m_effect.effect.ramp.length = SDL_HAPTIC_INFINITY; m_effect.ramp.length = SDL_HAPTIC_INFINITY;
} }
else else
{ {
m_effect.effect.type = 0; m_effect.type = 0;
} }
const Sint16 old = m_effect.effect.ramp.start; m_effect.ramp.start = (Sint16)(state * 0x7FFF);
m_effect.effect.ramp.start = (Sint16)(state * 0x7FFF); Update();
if (old != m_effect.effect.ramp.start)
m_effect.changed = true;
} }
void Joystick::SineEffect::SetState(ControlState state) void Joystick::SineEffect::SetState(ControlState state)
{ {
if (state) if (state)
{ {
m_effect.effect.type = SDL_HAPTIC_SINE; m_effect.type = SDL_HAPTIC_SINE;
m_effect.effect.periodic.length = 250; m_effect.periodic.length = 250;
} }
else else
{ {
m_effect.effect.type = 0; m_effect.type = 0;
} }
const Sint16 old = m_effect.effect.periodic.magnitude; m_effect.periodic.period = 5;
m_effect.effect.periodic.period = 5; m_effect.periodic.magnitude = (Sint16)(state * 0x5000);
m_effect.effect.periodic.magnitude = (Sint16)(state * 0x5000); m_effect.periodic.attack_length = 0;
m_effect.effect.periodic.attack_length = 0; m_effect.periodic.fade_length = 500;
m_effect.effect.periodic.fade_length = 500; Update();
if (old != m_effect.effect.periodic.magnitude)
m_effect.changed = true;
} }
#ifdef SDL_HAPTIC_SQUARE #ifdef SDL_HAPTIC_SQUARE
@ -274,22 +266,19 @@ void Joystick::SquareEffect::SetState(ControlState state)
{ {
if (state) if (state)
{ {
m_effect.effect.type = SDL_HAPTIC_SQUARE; m_effect.type = SDL_HAPTIC_SQUARE;
m_effect.effect.periodic.length = 250; m_effect.periodic.length = 250;
} }
else else
{ {
m_effect.effect.type = 0; m_effect.type = 0;
} }
const Sint16 old = m_effect.effect.periodic.magnitude; m_effect.periodic.period = 5;
m_effect.effect.periodic.period = 5; m_effect.periodic.magnitude = state * 0x5000;
m_effect.effect.periodic.magnitude = state * 0x5000; m_effect.periodic.attack_length = 0;
m_effect.effect.periodic.attack_length = 0; m_effect.periodic.fade_length = 100;
m_effect.effect.periodic.fade_length = 100; Update();
if (old != m_effect.effect.periodic.magnitude)
m_effect.changed = true;
} }
#endif // defined(SDL_HAPTIC_SQUARE) #endif // defined(SDL_HAPTIC_SQUARE)
@ -297,22 +286,19 @@ void Joystick::TriangleEffect::SetState(ControlState state)
{ {
if (state) if (state)
{ {
m_effect.effect.type = SDL_HAPTIC_TRIANGLE; m_effect.type = SDL_HAPTIC_TRIANGLE;
m_effect.effect.periodic.length = 250; m_effect.periodic.length = 250;
} }
else else
{ {
m_effect.effect.type = 0; m_effect.type = 0;
} }
const Sint16 old = m_effect.effect.periodic.magnitude; m_effect.periodic.period = 5;
m_effect.effect.periodic.period = 5; m_effect.periodic.magnitude = (Sint16)(state * 0x5000);
m_effect.effect.periodic.magnitude = (Sint16)(state * 0x5000); m_effect.periodic.attack_length = 0;
m_effect.effect.periodic.attack_length = 0; m_effect.periodic.fade_length = 100;
m_effect.effect.periodic.fade_length = 100; Update();
if (old != m_effect.effect.periodic.magnitude)
m_effect.changed = true;
} }
#endif #endif
@ -322,43 +308,6 @@ void Joystick::UpdateInput()
SDL_JoystickUpdate(); SDL_JoystickUpdate();
} }
void Joystick::UpdateOutput()
{
#ifdef USE_SDL_HAPTIC
for (auto &i : m_state_out)
{
if (i.changed) // if SetState was called on this output
{
if (-1 == i.id) // effect isn't currently uploaded
{
if (i.effect.type) // if outputstate is >0 this would be true
{
if ((i.id = SDL_HapticNewEffect(m_haptic, &i.effect)) > -1) // upload the effect
{
SDL_HapticRunEffect(m_haptic, i.id, 1); // run the effect
}
}
}
else // effect is already uploaded
{
if (i.effect.type) // if ouputstate >0
{
SDL_HapticUpdateEffect(m_haptic, i.id, &i.effect); // update the effect
}
else
{
SDL_HapticStopEffect(m_haptic, i.id); // else, stop and remove the effect
SDL_HapticDestroyEffect(m_haptic, i.id);
i.id = -1; // mark it as not uploaded
}
}
i.changed = false;
}
}
#endif
}
std::string Joystick::GetName() const std::string Joystick::GetName() const
{ {
return StripSpaces(GetJoystickName(m_sdl_index)); return StripSpaces(GetJoystickName(m_sdl_index));

View File

@ -33,17 +33,6 @@ class Joystick : public Core::Device
{ {
private: private:
#ifdef USE_SDL_HAPTIC
struct EffectIDState
{
EffectIDState() : effect(SDL_HapticEffect()), id(-1), changed(false) {}
SDL_HapticEffect effect;
int id;
bool changed;
};
#endif
class Button : public Core::Device::Input class Button : public Core::Device::Input
{ {
public: public:
@ -80,62 +69,65 @@ private:
}; };
#ifdef USE_SDL_HAPTIC #ifdef USE_SDL_HAPTIC
class ConstantEffect : public Output class HapticEffect : public Output
{ {
public: public:
std::string GetName() const override; HapticEffect(SDL_Haptic* haptic) : m_haptic(haptic), m_id(-1) {}
ConstantEffect(EffectIDState& effect) : m_effect(effect) {} ~HapticEffect() { m_effect.type = 0; Update(); }
void SetState(ControlState state) override;
private: protected:
EffectIDState& m_effect; void Update();
SDL_HapticEffect m_effect;
SDL_Haptic* m_haptic;
int m_id;
}; };
class RampEffect : public Output class ConstantEffect : public HapticEffect
{ {
public: public:
ConstantEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
std::string GetName() const override; std::string GetName() const override;
RampEffect(EffectIDState& effect) : m_effect(effect) {}
void SetState(ControlState state) override; void SetState(ControlState state) override;
private:
EffectIDState& m_effect;
}; };
class SineEffect : public Output class RampEffect : public HapticEffect
{ {
public: public:
RampEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
std::string GetName() const override;
void SetState(ControlState state) override;
};
class SineEffect : public HapticEffect
{
public:
SineEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
std::string GetName() const override; std::string GetName() const override;
SineEffect(EffectIDState& effect) : m_effect(effect) {}
void SetState(ControlState state) override; void SetState(ControlState state) override;
private:
EffectIDState& m_effect;
}; };
#ifdef SDL_HAPTIC_SQUARE #ifdef SDL_HAPTIC_SQUARE
class SquareEffect : public Output class SquareEffect : public HapticEffect
{ {
public: public:
SquareEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
std::string GetName() const; std::string GetName() const;
SquareEffect(EffectIDState& effect) : m_effect(effect) {}
void SetState(ControlState state); void SetState(ControlState state);
private:
EffectIDState& m_effect;
}; };
#endif // defined(SDL_HAPTIC_SQUARE) #endif // defined(SDL_HAPTIC_SQUARE)
class TriangleEffect : public Output class TriangleEffect : public HapticEffect
{ {
public: public:
TriangleEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
std::string GetName() const override; std::string GetName() const override;
TriangleEffect(EffectIDState& effect) : m_effect(effect) {}
void SetState(ControlState state) override; void SetState(ControlState state) override;
private:
EffectIDState& m_effect;
}; };
#endif #endif
public: public:
void UpdateInput() override; void UpdateInput() override;
void UpdateOutput() override;
Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsigned int index); Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsigned int index);
~Joystick(); ~Joystick();
@ -150,7 +142,6 @@ private:
const unsigned int m_index; const unsigned int m_index;
#ifdef USE_SDL_HAPTIC #ifdef USE_SDL_HAPTIC
std::list<EffectIDState> m_state_out;
SDL_Haptic* m_haptic; SDL_Haptic* m_haptic;
#endif #endif
}; };