Skip to content

Commit

Permalink
libpinmame: fix incorrect index for manually handled mechs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsm174 committed Nov 23, 2023
1 parent 0a716bb commit e4f1a21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
48 changes: 27 additions & 21 deletions src/libpinmame/libpinmame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ static void* _p_userData = nullptr;
static int _displaysInit;
static UINT8 _displayData[PINMAME_MAX_DISPLAYS][DMD_MAXX * DMD_MAXY];

static int _mechInit[MECH_MAXMECH / 2];
static PinmameMechInfo _mechInfo[MECH_MAXMECH / 2];
static int _mechInit[MECH_MAXMECH];
static PinmameMechInfo _mechInfo[MECH_MAXMECH];

static PinmameAudioInfo _audioInfo;
static float _audioData[PINMAME_ACCUMULATOR_SAMPLES * 2];
Expand Down Expand Up @@ -452,32 +452,40 @@ extern "C" void libpinmame_log_error(const char* format, ...) {
******************************************************/

extern "C" void libpinmame_update_mech(const int mechNo, mech_tMechData* p_mechData) {
int index = mechNo - ((g_fHandleMechanics == 0) ? (MECH_MAXMECH / 2) : 0);

int speed = p_mechData->speed / p_mechData->ret;

if (_mechInit[index]) {
if (_mechInfo[index].pos != p_mechData->pos || _mechInfo[index].speed != speed) {
_mechInfo[index].pos = p_mechData->pos;
_mechInfo[index].speed = speed;
if (_mechInit[mechNo]) {
if (_mechInfo[mechNo].pos != p_mechData->pos || _mechInfo[mechNo].speed != speed) {
_mechInfo[mechNo].pos = p_mechData->pos;
_mechInfo[mechNo].speed = speed;

if (_p_Config->cb_OnMechUpdated) {
(*(_p_Config->cb_OnMechUpdated))(index, &_mechInfo[index], _p_userData);
if (g_fHandleMechanics == 0) {
(*(_p_Config->cb_OnMechUpdated))(mechNo - (MECH_MAXMECH / 2) + 1, &_mechInfo[mechNo], _p_userData);
}
else {
(*(_p_Config->cb_OnMechUpdated))(mechNo, &_mechInfo[mechNo], _p_userData);
}
}
}
}
else {
_mechInit[index] = 1;
_mechInit[mechNo] = 1;

_mechInfo[index].type = p_mechData->type;
_mechInfo[index].length = p_mechData->length;
_mechInfo[index].steps = p_mechData->steps;
_mechInfo[mechNo].type = p_mechData->type;
_mechInfo[mechNo].length = p_mechData->length;
_mechInfo[mechNo].steps = p_mechData->steps;

_mechInfo[index].pos = p_mechData->pos;
_mechInfo[index].speed = speed;
_mechInfo[mechNo].pos = p_mechData->pos;
_mechInfo[mechNo].speed = speed;

if (_p_Config->cb_OnMechAvailable) {
(*(_p_Config->cb_OnMechAvailable))(index, &_mechInfo[index], _p_userData);
if (g_fHandleMechanics == 0) {
(*(_p_Config->cb_OnMechAvailable))(mechNo - (MECH_MAXMECH / 2) + 1, &_mechInfo[mechNo], _p_userData);
}
else {
(*(_p_Config->cb_OnMechAvailable))(mechNo, &_mechInfo[mechNo], _p_userData);
}
}
}
}
Expand Down Expand Up @@ -978,7 +986,7 @@ LIBPINMAME_API int PinmameGetChangedLEDs(const uint64_t mask, const uint64_t mas
******************************************************/

LIBPINMAME_API int PinmameGetMaxMechs() {
return MECH_MAXMECH / 2;
return (MECH_MAXMECH / 2);
}

/******************************************************
Expand All @@ -998,7 +1006,7 @@ LIBPINMAME_API PINMAME_STATUS PinmameSetMech(const int mechNo, const PinmameMech
return MECH_HANDLE_MECHANICS;
}

if (mechNo >= MECH_MAXMECH / 2) {
if (mechNo < 1 || mechNo > (MECH_MAXMECH / 2)) {
return MECH_NO_INVALID;
}

Expand Down Expand Up @@ -1026,9 +1034,7 @@ LIBPINMAME_API PINMAME_STATUS PinmameSetMech(const int mechNo, const PinmameMech
}
}

_mechInit[mechNo] = 0;

mech_add(mechNo + (MECH_MAXMECH / 2), &mechInitData);
mech_add((MECH_MAXMECH / 2) + mechNo - 1, &mechInitData);

return OK;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libpinmame/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void CALLBACK OnStateUpdated(int state, const void* p_userData) {
mechConfig.sw[0].startPos = 0;
mechConfig.sw[0].endPos = 5;

PinmameSetMech(0, &mechConfig);
PinmameSetMech(1, &mechConfig);
}
}

Expand Down

0 comments on commit e4f1a21

Please sign in to comment.