[nostalgia/core] Fix NostalgiaGraphic::setPixel

This commit is contained in:
Gary Talent 2022-02-16 02:11:27 -06:00
parent 627fb387e6
commit bb0592ade8

View File

@ -47,9 +47,8 @@ struct NostalgiaGraphic {
ox::Vector<uint8_t> pixels; ox::Vector<uint8_t> pixels;
[[nodiscard]] [[nodiscard]]
constexpr auto getPixel4Bpp(const geo::Point &pt) const noexcept { constexpr auto getPixel4Bpp(std::size_t idx) const noexcept {
oxAssert(bpp == 4, "NostalgiaGraphic::getPixel4Bpp: wrong bpp"); oxAssert(bpp == 4, "NostalgiaGraphic::getPixel4Bpp: wrong bpp");
const auto idx = ptToIdx(pt, this->columns);
if (idx & 1) { if (idx & 1) {
return this->pixels[idx / 2]; return this->pixels[idx / 2];
} else { } else {
@ -57,31 +56,51 @@ struct NostalgiaGraphic {
} }
} }
[[nodiscard]]
constexpr auto getPixel8Bpp(std::size_t idx) const noexcept {
oxAssert(bpp == 8, "NostalgiaGraphic::getPixel8Bpp: wrong bpp");
return this->pixels[idx];
}
[[nodiscard]]
constexpr auto getPixel(std::size_t idx) const noexcept {
if (this->bpp == 4) {
return getPixel4Bpp(idx);
} else {
return getPixel8Bpp(idx);
}
}
[[nodiscard]]
constexpr auto getPixel4Bpp(const geo::Point &pt) const noexcept {
oxAssert(bpp == 4, "NostalgiaGraphic::getPixel4Bpp: wrong bpp");
const auto idx = ptToIdx(pt, this->columns);
return getPixel4Bpp(idx);
}
[[nodiscard]] [[nodiscard]]
constexpr auto getPixel8Bpp(const geo::Point &pt) const noexcept { constexpr auto getPixel8Bpp(const geo::Point &pt) const noexcept {
oxAssert(bpp == 8, "NostalgiaGraphic::getPixel8Bpp: wrong bpp"); oxAssert(bpp == 8, "NostalgiaGraphic::getPixel8Bpp: wrong bpp");
const auto idx = ptToIdx(pt, this->columns); const auto idx = ptToIdx(pt, this->columns);
return this->pixels[idx]; return getPixel8Bpp(idx);
} }
[[nodiscard]] [[nodiscard]]
constexpr auto getPixel(const geo::Point &pt) const noexcept { constexpr auto getPixel(const geo::Point &pt) const noexcept {
if (this->bpp == 4) { const auto idx = ptToIdx(pt, this->columns);
return getPixel4Bpp(pt); return getPixel(idx);
} else {
return getPixel8Bpp(pt);
}
} }
constexpr void setPixel(uint64_t idx, uint8_t palIdx) noexcept { constexpr void setPixel(uint64_t idx, uint8_t palIdx) noexcept {
auto &pixel = this->pixels[idx / 2];
if (bpp == 4) { if (bpp == 4) {
if (idx & 1) { if (idx & 1) {
pixels[idx / 2] &= 0b0000'1111 | (palIdx << 4); pixel = (pixel & 0b0000'1111) | (palIdx << 4);
} else { } else {
pixels[idx / 2] &= 0b1111'0000 | (palIdx); pixel = (pixel & 0b1111'0000) | (palIdx);
} }
} else { } else {
this->pixels[idx] = palIdx; pixel = palIdx;
} }
} }