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:
parent
e43ad58a3a
commit
1b3d0173f5
|
@ -121,40 +121,26 @@ Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsi
|
|||
|
||||
// constant effect
|
||||
if (supported_effects & SDL_HAPTIC_CONSTANT)
|
||||
{
|
||||
m_state_out.push_back(EffectIDState());
|
||||
AddOutput(new ConstantEffect(m_state_out.back()));
|
||||
}
|
||||
AddOutput(new ConstantEffect(m_haptic));
|
||||
|
||||
// ramp effect
|
||||
if (supported_effects & SDL_HAPTIC_RAMP)
|
||||
{
|
||||
m_state_out.push_back(EffectIDState());
|
||||
AddOutput(new RampEffect(m_state_out.back()));
|
||||
}
|
||||
AddOutput(new RampEffect(m_haptic));
|
||||
|
||||
// sine effect
|
||||
if (supported_effects & SDL_HAPTIC_SINE)
|
||||
{
|
||||
m_state_out.push_back(EffectIDState());
|
||||
AddOutput(new SineEffect(m_state_out.back()));
|
||||
}
|
||||
AddOutput(new SineEffect(m_haptic));
|
||||
|
||||
#ifdef SDL_HAPTIC_SQUARE
|
||||
// square effect
|
||||
if (supported_effects & SDL_HAPTIC_SQUARE)
|
||||
{
|
||||
m_state_out.push_back(EffectIDState());
|
||||
AddOutput(new SquareEffect(m_state_out.back()));
|
||||
}
|
||||
AddOutput(new SquareEffect(m_haptic));
|
||||
|
||||
#endif // defined(SDL_HAPTIC_SQUARE)
|
||||
|
||||
// triangle effect
|
||||
if (supported_effects & SDL_HAPTIC_TRIANGLE)
|
||||
{
|
||||
m_state_out.push_back(EffectIDState());
|
||||
AddOutput(new TriangleEffect(m_state_out.back()));
|
||||
}
|
||||
AddOutput(new TriangleEffect(m_haptic));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -167,13 +153,6 @@ Joystick::~Joystick()
|
|||
{
|
||||
// stop/destroy all effects
|
||||
SDL_HapticStopAll(m_haptic);
|
||||
for (auto &i : m_state_out)
|
||||
{
|
||||
if (i.id != -1)
|
||||
{
|
||||
SDL_HapticDestroyEffect(m_haptic, i.id);
|
||||
}
|
||||
}
|
||||
// close haptic first
|
||||
SDL_HapticClose(m_haptic);
|
||||
}
|
||||
|
@ -184,6 +163,26 @@ Joystick::~Joystick()
|
|||
}
|
||||
|
||||
#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
|
||||
{
|
||||
return "Constant";
|
||||
|
@ -215,58 +214,51 @@ void Joystick::ConstantEffect::SetState(ControlState state)
|
|||
{
|
||||
if (state)
|
||||
{
|
||||
m_effect.effect.type = SDL_HAPTIC_CONSTANT;
|
||||
m_effect.effect.constant.length = SDL_HAPTIC_INFINITY;
|
||||
m_effect.type = SDL_HAPTIC_CONSTANT;
|
||||
m_effect.constant.length = SDL_HAPTIC_INFINITY;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_effect.effect.type = 0;
|
||||
m_effect.type = 0;
|
||||
}
|
||||
|
||||
const Sint16 old = m_effect.effect.constant.level;
|
||||
m_effect.effect.constant.level = (Sint16)(state * 0x7FFF);
|
||||
if (old != m_effect.effect.constant.level)
|
||||
m_effect.changed = true;
|
||||
m_effect.constant.level = (Sint16)(state * 0x7FFF);
|
||||
Update();
|
||||
}
|
||||
|
||||
void Joystick::RampEffect::SetState(ControlState state)
|
||||
{
|
||||
if (state)
|
||||
{
|
||||
m_effect.effect.type = SDL_HAPTIC_RAMP;
|
||||
m_effect.effect.ramp.length = SDL_HAPTIC_INFINITY;
|
||||
m_effect.type = SDL_HAPTIC_RAMP;
|
||||
m_effect.ramp.length = SDL_HAPTIC_INFINITY;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_effect.effect.type = 0;
|
||||
m_effect.type = 0;
|
||||
}
|
||||
|
||||
const Sint16 old = m_effect.effect.ramp.start;
|
||||
m_effect.effect.ramp.start = (Sint16)(state * 0x7FFF);
|
||||
if (old != m_effect.effect.ramp.start)
|
||||
m_effect.changed = true;
|
||||
m_effect.ramp.start = (Sint16)(state * 0x7FFF);
|
||||
Update();
|
||||
}
|
||||
|
||||
void Joystick::SineEffect::SetState(ControlState state)
|
||||
{
|
||||
if (state)
|
||||
{
|
||||
m_effect.effect.type = SDL_HAPTIC_SINE;
|
||||
m_effect.effect.periodic.length = 250;
|
||||
m_effect.type = SDL_HAPTIC_SINE;
|
||||
m_effect.periodic.length = 250;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_effect.effect.type = 0;
|
||||
m_effect.type = 0;
|
||||
}
|
||||
|
||||
const Sint16 old = m_effect.effect.periodic.magnitude;
|
||||
m_effect.effect.periodic.period = 5;
|
||||
m_effect.effect.periodic.magnitude = (Sint16)(state * 0x5000);
|
||||
m_effect.effect.periodic.attack_length = 0;
|
||||
m_effect.effect.periodic.fade_length = 500;
|
||||
|
||||
if (old != m_effect.effect.periodic.magnitude)
|
||||
m_effect.changed = true;
|
||||
m_effect.periodic.period = 5;
|
||||
m_effect.periodic.magnitude = (Sint16)(state * 0x5000);
|
||||
m_effect.periodic.attack_length = 0;
|
||||
m_effect.periodic.fade_length = 500;
|
||||
Update();
|
||||
}
|
||||
|
||||
#ifdef SDL_HAPTIC_SQUARE
|
||||
|
@ -274,22 +266,19 @@ void Joystick::SquareEffect::SetState(ControlState state)
|
|||
{
|
||||
if (state)
|
||||
{
|
||||
m_effect.effect.type = SDL_HAPTIC_SQUARE;
|
||||
m_effect.effect.periodic.length = 250;
|
||||
m_effect.type = SDL_HAPTIC_SQUARE;
|
||||
m_effect.periodic.length = 250;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_effect.effect.type = 0;
|
||||
m_effect.type = 0;
|
||||
}
|
||||
|
||||
const Sint16 old = m_effect.effect.periodic.magnitude;
|
||||
m_effect.effect.periodic.period = 5;
|
||||
m_effect.effect.periodic.magnitude = state * 0x5000;
|
||||
m_effect.effect.periodic.attack_length = 0;
|
||||
m_effect.effect.periodic.fade_length = 100;
|
||||
|
||||
if (old != m_effect.effect.periodic.magnitude)
|
||||
m_effect.changed = true;
|
||||
m_effect.periodic.period = 5;
|
||||
m_effect.periodic.magnitude = state * 0x5000;
|
||||
m_effect.periodic.attack_length = 0;
|
||||
m_effect.periodic.fade_length = 100;
|
||||
Update();
|
||||
}
|
||||
#endif // defined(SDL_HAPTIC_SQUARE)
|
||||
|
||||
|
@ -297,22 +286,19 @@ void Joystick::TriangleEffect::SetState(ControlState state)
|
|||
{
|
||||
if (state)
|
||||
{
|
||||
m_effect.effect.type = SDL_HAPTIC_TRIANGLE;
|
||||
m_effect.effect.periodic.length = 250;
|
||||
m_effect.type = SDL_HAPTIC_TRIANGLE;
|
||||
m_effect.periodic.length = 250;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_effect.effect.type = 0;
|
||||
m_effect.type = 0;
|
||||
}
|
||||
|
||||
const Sint16 old = m_effect.effect.periodic.magnitude;
|
||||
m_effect.effect.periodic.period = 5;
|
||||
m_effect.effect.periodic.magnitude = (Sint16)(state * 0x5000);
|
||||
m_effect.effect.periodic.attack_length = 0;
|
||||
m_effect.effect.periodic.fade_length = 100;
|
||||
|
||||
if (old != m_effect.effect.periodic.magnitude)
|
||||
m_effect.changed = true;
|
||||
m_effect.periodic.period = 5;
|
||||
m_effect.periodic.magnitude = (Sint16)(state * 0x5000);
|
||||
m_effect.periodic.attack_length = 0;
|
||||
m_effect.periodic.fade_length = 100;
|
||||
Update();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -322,43 +308,6 @@ void Joystick::UpdateInput()
|
|||
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
|
||||
{
|
||||
return StripSpaces(GetJoystickName(m_sdl_index));
|
||||
|
|
|
@ -33,17 +33,6 @@ class Joystick : public Core::Device
|
|||
{
|
||||
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
|
||||
{
|
||||
public:
|
||||
|
@ -80,62 +69,65 @@ private:
|
|||
};
|
||||
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
class ConstantEffect : public Output
|
||||
class HapticEffect : public Output
|
||||
{
|
||||
public:
|
||||
std::string GetName() const override;
|
||||
ConstantEffect(EffectIDState& effect) : m_effect(effect) {}
|
||||
void SetState(ControlState state) override;
|
||||
private:
|
||||
EffectIDState& m_effect;
|
||||
HapticEffect(SDL_Haptic* haptic) : m_haptic(haptic), m_id(-1) {}
|
||||
~HapticEffect() { m_effect.type = 0; Update(); }
|
||||
|
||||
protected:
|
||||
void Update();
|
||||
|
||||
SDL_HapticEffect m_effect;
|
||||
SDL_Haptic* m_haptic;
|
||||
int m_id;
|
||||
};
|
||||
|
||||
class RampEffect : public Output
|
||||
class ConstantEffect : public HapticEffect
|
||||
{
|
||||
public:
|
||||
ConstantEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
|
||||
std::string GetName() const override;
|
||||
RampEffect(EffectIDState& effect) : m_effect(effect) {}
|
||||
void SetState(ControlState state) override;
|
||||
private:
|
||||
EffectIDState& m_effect;
|
||||
};
|
||||
|
||||
class SineEffect : public Output
|
||||
class RampEffect : public HapticEffect
|
||||
{
|
||||
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;
|
||||
SineEffect(EffectIDState& effect) : m_effect(effect) {}
|
||||
void SetState(ControlState state) override;
|
||||
private:
|
||||
EffectIDState& m_effect;
|
||||
};
|
||||
|
||||
#ifdef SDL_HAPTIC_SQUARE
|
||||
class SquareEffect : public Output
|
||||
class SquareEffect : public HapticEffect
|
||||
{
|
||||
public:
|
||||
SquareEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
|
||||
std::string GetName() const;
|
||||
SquareEffect(EffectIDState& effect) : m_effect(effect) {}
|
||||
void SetState(ControlState state);
|
||||
private:
|
||||
EffectIDState& m_effect;
|
||||
};
|
||||
#endif // defined(SDL_HAPTIC_SQUARE)
|
||||
|
||||
class TriangleEffect : public Output
|
||||
class TriangleEffect : public HapticEffect
|
||||
{
|
||||
public:
|
||||
TriangleEffect(SDL_Haptic* haptic) : HapticEffect(haptic) {}
|
||||
std::string GetName() const override;
|
||||
TriangleEffect(EffectIDState& effect) : m_effect(effect) {}
|
||||
void SetState(ControlState state) override;
|
||||
private:
|
||||
EffectIDState& m_effect;
|
||||
};
|
||||
#endif
|
||||
|
||||
public:
|
||||
void UpdateInput() override;
|
||||
void UpdateOutput() override;
|
||||
|
||||
Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsigned int index);
|
||||
~Joystick();
|
||||
|
@ -150,7 +142,6 @@ private:
|
|||
const unsigned int m_index;
|
||||
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
std::list<EffectIDState> m_state_out;
|
||||
SDL_Haptic* m_haptic;
|
||||
#endif
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue