[nostalgia/core] Fix validateSubSheetIdx

This commit is contained in:
Gary Talent 2024-05-31 23:58:21 -05:00
parent dcad44403e
commit 2180f7bf6f
2 changed files with 14 additions and 13 deletions

View File

@ -238,7 +238,7 @@ ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const&sz
* @return a valid version of idx
*/
[[nodiscard]]
TileSheet::SubSheetIdx validateSubSheetIdx(TileSheet const&ts, TileSheet::SubSheetIdx const&idx) noexcept;
TileSheet::SubSheetIdx validateSubSheetIdx(TileSheet const&ts, TileSheet::SubSheetIdx idx) noexcept;
[[nodiscard]]
TileSheet::SubSheet const&getSubSheet(

View File

@ -191,27 +191,28 @@ ox::Result<ox::StringView> getNameFor(TileSheet::SubSheet const&ss, SubSheetId p
TileSheet::SubSheetIdx validateSubSheetIdx(
TileSheet::SubSheetIdx const&pIdx,
TileSheet::SubSheetIdx &&pIdx,
std::size_t pIdxIt,
TileSheet::SubSheet const&pSubsheet) noexcept {
if (pIdxIt == pIdx.size()) {
return pIdx;
if (pIdxIt >= pIdx.size()) {
return std::move(pIdx);
}
const auto currentIdx = pIdx[pIdxIt];
auto &currentIdx = pIdx[pIdxIt];
if (pSubsheet.subsheets.size() <= currentIdx) {
auto out = pIdx;
if (!pSubsheet.subsheets.empty()) {
*out.back().value = pSubsheet.subsheets.size() - 1;
if (pSubsheet.subsheets.empty()) {
// currentIdx could not be repaired, remove
// this and all succeeding idxs and return
pIdx.resize(pIdxIt);
return std::move(pIdx);
} else {
out.pop_back();
currentIdx = pSubsheet.subsheets.size() - 1;
}
return out;
}
return validateSubSheetIdx(pIdx, pIdxIt + 1, pSubsheet.subsheets[pIdx[pIdxIt]]);
return validateSubSheetIdx(std::move(pIdx), pIdxIt + 1, pSubsheet.subsheets[currentIdx]);
}
TileSheet::SubSheetIdx validateSubSheetIdx(TileSheet const&ts, TileSheet::SubSheetIdx const&idx) noexcept {
return validateSubSheetIdx(idx, 0, ts.subsheet);
TileSheet::SubSheetIdx validateSubSheetIdx(TileSheet const&ts, TileSheet::SubSheetIdx idx) noexcept {
return validateSubSheetIdx(std::move(idx), 0, ts.subsheet);
}
const TileSheet::SubSheet &getSubSheet(