From 29fd9b8c4f88fe64115b1d84369cd6c7570a3026 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 30 Jul 2022 14:47:44 -0500 Subject: [PATCH] [nostalgia/core] Make invalid palette lookups return 0 instead of garbage memory --- src/nostalgia/core/gfx.hpp | 7 +++++++ src/nostalgia/core/studio/tilesheetpixels.cpp | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index dd082365..5c0f9359 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -39,6 +39,13 @@ struct Palette { static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.Palette"; static constexpr auto TypeVersion = 1; ox::Vector colors = {}; + [[nodiscard]] + constexpr Color16 color(auto idx) const noexcept { + if (idx < colors.size()) [[likely]] { + return colors[idx]; + } + return 0; + } }; // Predecessor to TileSheet, kept for backward compatibility diff --git a/src/nostalgia/core/studio/tilesheetpixels.cpp b/src/nostalgia/core/studio/tilesheetpixels.cpp index 9bc0675b..f6d759b5 100644 --- a/src/nostalgia/core/studio/tilesheetpixels.cpp +++ b/src/nostalgia/core/studio/tilesheetpixels.cpp @@ -93,7 +93,7 @@ void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize) noexcept { m_bufferSet.elements.resize(pixels * VertexEboLength); // set pixels subSheet->walkPixels(m_model->img().bpp, [&](std::size_t i, uint8_t p) { - auto color = pal->colors[p]; + auto color = pal->color(p); const auto pt = idxToPt(static_cast(i), subSheet->columns); const auto fx = static_cast(pt.x); const auto fy = static_cast(pt.y);