ForceFeedback: Don't depend on the force_type_name index.

Instead use a for-each loop, compare GUIDs and save the name pointers.
This commit is contained in:
Jules Blok 2014-02-09 15:04:51 +09:00
parent c6d650c058
commit 992b91c082
2 changed files with 18 additions and 18 deletions

View File

@ -14,11 +14,13 @@ template class ForceFeedbackDevice::Force<DICONSTANTFORCE>;
template class ForceFeedbackDevice::Force<DIRAMPFORCE>; template class ForceFeedbackDevice::Force<DIRAMPFORCE>;
template class ForceFeedbackDevice::Force<DIPERIODIC>; template class ForceFeedbackDevice::Force<DIPERIODIC>;
static const struct typedef struct
{ {
GUID guid; GUID guid;
const char* name; const char* name;
} force_type_names[] = } ForceType;
static const ForceType force_type_names[] =
{ {
{GUID_ConstantForce, "Constant"}, // DICONSTANTFORCE {GUID_ConstantForce, "Constant"}, // DICONSTANTFORCE
{GUID_RampForce, "Ramp"}, // DIRAMPFORCE {GUID_RampForce, "Ramp"}, // DIRAMPFORCE
@ -79,17 +81,16 @@ bool ForceFeedbackDevice::InitForceFeedback(const LPDIRECTINPUTDEVICE8 device, i
//ZeroMemory(&env, sizeof(env)); //ZeroMemory(&env, sizeof(env));
//env.dwSize = sizeof(env); //env.dwSize = sizeof(env);
for (unsigned int f = 0; f < sizeof(force_type_names)/sizeof(*force_type_names); ++f) for (const ForceType& f : force_type_names)
{ {
// ugly if ladder if (f.guid == GUID_ConstantForce)
if (0 == f)
{ {
DICONSTANTFORCE diCF = {-10000}; DICONSTANTFORCE diCF = {-10000};
diCF.lMagnitude = DI_FFNOMINALMAX; diCF.lMagnitude = DI_FFNOMINALMAX;
eff.cbTypeSpecificParams = sizeof(DICONSTANTFORCE); eff.cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
eff.lpvTypeSpecificParams = &diCF; eff.lpvTypeSpecificParams = &diCF;
} }
else if (1 == f) else if (f.guid == GUID_RampForce)
{ {
eff.cbTypeSpecificParams = sizeof(DIRAMPFORCE); eff.cbTypeSpecificParams = sizeof(DIRAMPFORCE);
} }
@ -99,17 +100,16 @@ bool ForceFeedbackDevice::InitForceFeedback(const LPDIRECTINPUTDEVICE8 device, i
} }
LPDIRECTINPUTEFFECT pEffect; LPDIRECTINPUTEFFECT pEffect;
if (SUCCEEDED(device->CreateEffect(force_type_names[f].guid, &eff, &pEffect, NULL))) if (SUCCEEDED(device->CreateEffect(f.guid, &eff, &pEffect, NULL)))
{ {
m_state_out.push_back(EffectState(pEffect)); m_state_out.push_back(EffectState(pEffect));
// ugly if ladder again :/ if (f.guid == GUID_ConstantForce)
if (0 == f) AddOutput(new ForceConstant(f.name, m_state_out.back()));
AddOutput(new ForceConstant(f, m_state_out.back())); else if (f.guid == GUID_RampForce)
else if (1 == f) AddOutput(new ForceRamp(f.name, m_state_out.back()));
AddOutput(new ForceRamp(f, m_state_out.back()));
else else
AddOutput(new ForcePeriodic(f, m_state_out.back())); AddOutput(new ForcePeriodic(f.name, m_state_out.back()));
} }
} }
@ -214,8 +214,8 @@ void ForceFeedbackDevice::ForcePeriodic::SetState(const ControlState state)
} }
template <typename P> template <typename P>
ForceFeedbackDevice::Force<P>::Force(u8 index, EffectState& state) ForceFeedbackDevice::Force<P>::Force(const char* name, EffectState& state)
: m_index(index), m_state(state) : m_name(name), m_state(state)
{ {
memset(&params, 0, sizeof(params)); memset(&params, 0, sizeof(params));
} }
@ -223,7 +223,7 @@ ForceFeedbackDevice::Force<P>::Force(u8 index, EffectState& state)
template <typename P> template <typename P>
std::string ForceFeedbackDevice::Force<P>::GetName() const std::string ForceFeedbackDevice::Force<P>::GetName() const
{ {
return force_type_names[m_index].name; return m_name;
} }
} }

View File

@ -42,10 +42,10 @@ private:
{ {
public: public:
std::string GetName() const; std::string GetName() const;
Force(u8 index, EffectState& state); Force(const char* name, EffectState& state);
void SetState(ControlState state); void SetState(ControlState state);
private: private:
const u8 m_index; const char* m_name;
EffectState& m_state; EffectState& m_state;
P params; P params;
}; };