From 849d50be8edbc84a49fc421543b3f9852cf5956e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 8 Jan 2025 21:34:01 -0600 Subject: [PATCH] [nostalgia/core] Make getTileIdx return an Optional --- .../core/include/nostalgia/core/tilesheet.hpp | 8 +++++++- src/nostalgia/modules/core/src/tilesheet.cpp | 12 +++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp b/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp index bbb7aca7..7561e196 100644 --- a/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp +++ b/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp @@ -212,6 +212,10 @@ struct TileSheetV4 { pixels(std::move(pPixels)) { } + /** + * + * @return the dimensional size of the SubSheet (e.g. width * height) + */ [[nodiscard]] constexpr std::size_t size() const noexcept { return static_cast(columns) * static_cast(rows); @@ -273,7 +277,7 @@ size_t getTileCnt(TileSheet const&ts) noexcept; TileSheet::SubSheet const*getSubsheet(TileSheet const&ts, SubSheetId id) noexcept; [[nodiscard]] -size_t getTileIdx(TileSheet const&ts, SubSheetId id) noexcept; +ox::Optional getTileIdx(TileSheet const&ts, SubSheetId id) noexcept; [[nodiscard]] uint8_t getPixel4Bpp(TileSheet::SubSheet const&ss, std::size_t idx) noexcept; @@ -392,6 +396,8 @@ ox::Result getIdFor(TileSheet const&ts, ox::StringViewCR path) noexc ox::Result getTileOffset(TileSheet const&ts, ox::StringViewCR pNamePath) noexcept; +ox::Result getTileOffset(TileSheet const&ts, SubSheetId pId) noexcept; + ox::Result getNameFor(TileSheet::SubSheet const&ss, SubSheetId pId) noexcept; ox::Result getNameFor(TileSheet const&ts, SubSheetId pId) noexcept; diff --git a/src/nostalgia/modules/core/src/tilesheet.cpp b/src/nostalgia/modules/core/src/tilesheet.cpp index 6a113935..d82ac5f3 100644 --- a/src/nostalgia/modules/core/src/tilesheet.cpp +++ b/src/nostalgia/modules/core/src/tilesheet.cpp @@ -66,10 +66,10 @@ static ox::Optional getPixelIdx( return ox::Optional{}; } -size_t getTileIdx(TileSheet const&ts, SubSheetId const id) noexcept { +ox::Optional getTileIdx(TileSheet const&ts, SubSheetId const id) noexcept { size_t idx{}; auto const out = getPixelIdx(ts.subsheet, id, idx, ts.bpp); - return out.or_value(0) / PixelsPerTile; + return out ? ox::Optional{ox::in_place, *out / PixelsPerTile} : out; } uint8_t getPixel4Bpp(TileSheet::SubSheet const&ss, std::size_t idx) noexcept { @@ -355,12 +355,12 @@ ox::Result getIdFor(TileSheet const&ts, ox::StringViewCR path) noexc /** * Gets the offset in tiles of the desired subsheet. */ -static ox::Result getTileOffset( +static ox::Result getTileOffset( TileSheet::SubSheet const&ss, ox::SpanView const&pNamePath, int8_t pBpp, std::size_t pIt = 0, - unsigned pCurrentTotal = 0) noexcept { + uint32_t pCurrentTotal = 0) noexcept { // pIt == pNamePath.size() - 1 && if (ss.name != pNamePath[pIt]) { return ox::Error(2, "Wrong branch"); @@ -374,12 +374,14 @@ static ox::Result getTileOffset( if (!err) { return offset; } + // Possible bug? Shoud this be usinga a recursive version of + // pixelCnt will count pixels in subsheets of sub as well. pCurrentTotal += pixelCnt(sub, pBpp) / PixelsPerTile; } return ox::Error(1, "SubSheet not found"); } -ox::Result getTileOffset(TileSheet const&ts, ox::StringViewCR pNamePath) noexcept { +ox::Result getTileOffset(TileSheet const&ts, ox::StringViewCR pNamePath) noexcept { return core::getTileOffset(ts.subsheet, ox::split<8>(pNamePath, '.'), ts.bpp); }