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
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));