From e4c3866017efff918c714928b76e76282081b7d8 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 29 May 2024 02:16:07 -0500 Subject: [PATCH] [nostalgia/core] Fix subsheet resize not to read garbage --- src/nostalgia/modules/core/src/tilesheet.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/nostalgia/modules/core/src/tilesheet.cpp b/src/nostalgia/modules/core/src/tilesheet.cpp index bd2aa99d..9f622ad4 100644 --- a/src/nostalgia/modules/core/src/tilesheet.cpp +++ b/src/nostalgia/modules/core/src/tilesheet.cpp @@ -134,16 +134,20 @@ void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, ox::Point const&pt, uint8_t } static ox::Error setPixelCount(ox::Vector &pixels, int8_t pBpp, std::size_t cnt) noexcept { + size_t sz{}; switch (pBpp) { case 4: - pixels.resize(cnt / 2); - return OxError(0); + sz = cnt / 2; + break; case 8: - pixels.resize(cnt); - return OxError(0); + sz = cnt; + break; default: return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount"); } + pixels.reserve(sz); + pixels.resize(sz); + return {}; } ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) noexcept { @@ -158,8 +162,8 @@ unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept { ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const&sz) noexcept { ox::Vector out; oxReturnError(setPixelCount(out, pBpp, static_cast(sz.width * sz.height) * PixelsPerTile)); - auto const w = sz.width * TileWidth; - auto const h = sz.height * TileHeight; + auto const w = ss.columns * TileWidth; + auto const h = ss.rows * TileHeight; for (auto x = 0; x < w; ++x) { for (auto y = 0; y < h; ++y) { auto const palIdx = getPixel(ss, pBpp, {x, y});