diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 2471597c..3c62092c 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -180,6 +180,22 @@ struct TileSheet { return getPixel(bpp, idx); } + constexpr auto walkPixels(int8_t bpp, auto callback) const noexcept { + if (bpp == 4) { + for (std::size_t i = 0; i < pixels.size(); ++i) { + const auto colorIdx1 = pixels[i] & 0xF; + const auto colorIdx2 = pixels[i] >> 4; + callback(i * 2 + 0, colorIdx1); + callback(i * 2 + 1, colorIdx2); + } + } else { + for (std::size_t i = 0; i < pixels.size(); ++i) { + const auto p = pixels[i]; + callback(i, p); + } + } + } + constexpr void setPixel(int8_t bpp, uint64_t idx, uint8_t palIdx) noexcept { auto &pixel = this->pixels[idx / 2]; if (bpp == 4) { diff --git a/src/nostalgia/core/studio/tilesheetpixels.cpp b/src/nostalgia/core/studio/tilesheetpixels.cpp index 5e035ced..16aba39a 100644 --- a/src/nostalgia/core/studio/tilesheetpixels.cpp +++ b/src/nostalgia/core/studio/tilesheetpixels.cpp @@ -79,15 +79,6 @@ void TileSheetPixels::setPixelBufferObject(const geo::Vec2 &paneSize, unsigned v } void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal) noexcept { - const auto setPixel = [&](std::size_t i, uint8_t p) { - const auto color = pal.colors[p]; - const auto pt = idxToPt(static_cast(i), subSheet.columns); - const auto fx = static_cast(pt.x); - const auto fy = static_cast(pt.y); - const auto vbo = &m_bufferSet.vertices[i * VertexVboLength]; - const auto ebo = &m_bufferSet.elements[i * VertexEboLength]; - setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo); - }; // set buffer lengths const auto width = subSheet.columns * TileWidth; const auto height = subSheet.rows * TileHeight; @@ -96,19 +87,15 @@ void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileShee m_bufferSet.vertices.resize(pixels * VertexVboLength); m_bufferSet.elements.resize(pixels * VertexEboLength); // set pixels - if (img.bpp == 4) { - for (std::size_t i = 0; i < subSheet.pixels.size(); ++i) { - const auto colorIdx1 = subSheet.pixels[i] & 0xF; - const auto colorIdx2 = subSheet.pixels[i] >> 4; - setPixel(i * 2 + 0, colorIdx1); - setPixel(i * 2 + 1, colorIdx2); - } - } else { - for (std::size_t i = 0; i < subSheet.pixels.size(); ++i) { - const auto p = subSheet.pixels[i]; - setPixel(i, p); - } - } + subSheet.walkPixels(img.bpp, [&](std::size_t i, uint8_t p) { + const auto color = pal.colors[p]; + const auto pt = idxToPt(static_cast(i), subSheet.columns); + const auto fx = static_cast(pt.x); + const auto fy = static_cast(pt.y); + const auto vbo = &m_bufferSet.vertices[i * VertexVboLength]; + const auto ebo = &m_bufferSet.elements[i * VertexEboLength]; + setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo); + }); } }