diff --git a/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp b/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp index 1eaa080c..cf7998c7 100644 --- a/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp +++ b/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp @@ -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( diff --git a/src/nostalgia/modules/core/src/tilesheet.cpp b/src/nostalgia/modules/core/src/tilesheet.cpp index 9f622ad4..18b62d04 100644 --- a/src/nostalgia/modules/core/src/tilesheet.cpp +++ b/src/nostalgia/modules/core/src/tilesheet.cpp @@ -191,27 +191,28 @@ ox::Result 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 ¤tIdx = 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(