From 1dff26d895ce0400d23a6db51f3ca0cb92d4d459 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 31 Jan 2024 23:16:41 -0600 Subject: [PATCH] [nostalgia/core] Add getTileCnt and fix getTileIdx --- src/nostalgia/modules/core/src/tilesheet.cpp | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/nostalgia/modules/core/src/tilesheet.cpp b/src/nostalgia/modules/core/src/tilesheet.cpp index 67b2c2e0..58ebc156 100644 --- a/src/nostalgia/modules/core/src/tilesheet.cpp +++ b/src/nostalgia/modules/core/src/tilesheet.cpp @@ -14,6 +14,7 @@ std::size_t idx(TileSheet::SubSheet const&ss, ox::Point const&pt) noexcept { return ptToIdx(pt, ss.columns); } +[[nodiscard]] static TileSheet::SubSheet const*getSubsheet(TileSheet::SubSheet const&ss, SubSheetId const id) noexcept { if (ss.id == id) { return &ss; @@ -26,6 +27,24 @@ static TileSheet::SubSheet const*getSubsheet(TileSheet::SubSheet const&ss, SubSh return {}; } +[[nodiscard]] +static size_t getTileCnt(TileSheet::SubSheet const&ss, int const bpp) noexcept { + if (ss.subsheets.empty()) { + auto const bytesPerTile = bpp == 4 ? 32u : 64u; + return ss.pixels.size() / bytesPerTile; + } else { + size_t out{}; + for (auto const&child : ss.subsheets) { + out += getTileCnt(child, bpp); + } + return out; + } +} + +size_t getTileCnt(TileSheet const&ts) noexcept { + return getTileCnt(ts.subsheet, ts.bpp); +} + TileSheet::SubSheet const*getSubsheet(TileSheet const&ts, SubSheetId const id) noexcept { return getSubsheet(ts.subsheet, id); } @@ -42,7 +61,7 @@ static ox::Optional getPixelIdx( if (auto out = getPixelIdx(child, id, idx, bpp)) { return out; } - idx += pixelCnt(ss, bpp); + idx += pixelCnt(child, bpp); } return {}; }