[nostalgia/studio] Add export option to tilesheet editor

This commit is contained in:
2022-05-25 21:21:28 -05:00
parent 2448bdcc82
commit 0adfaa7901
6 changed files with 103 additions and 31 deletions
+45 -21
View File
@@ -32,13 +32,13 @@ enum class TileSheetSpace {
struct NostalgiaPalette {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.NostalgiaPalette";
static constexpr auto TypeVersion = 1;
ox::Vector<Color16> colors;
ox::Vector<Color16> colors = {};
};
struct Palette {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.Palette";
static constexpr auto TypeVersion = 1;
ox::Vector<Color16> colors;
ox::Vector<Color16> colors = {};
};
// Predecessor to TileSheet, kept for backward compatibility
@@ -51,7 +51,7 @@ struct NostalgiaGraphic {
int columns = 1;
ox::FileAddress defaultPalette;
Palette pal;
ox::Vector<uint8_t> pixels;
ox::Vector<uint8_t> pixels = {};
};
struct TileSheet {
@@ -64,7 +64,7 @@ struct TileSheet {
int columns = 1;
int rows = 1;
ox::Vector<SubSheet> subsheets;
ox::Vector<uint8_t> pixels;
ox::Vector<uint8_t> pixels = {};
constexpr SubSheet() noexcept = default;
constexpr SubSheet(const SubSheet &other) noexcept {
@@ -109,10 +109,34 @@ struct TileSheet {
return *this;
}
[[nodiscard]]
auto idx(const geo::Point &pt) const noexcept {
return ptToIdx(pt, columns);
}
/**
* Reads all pixels of this sheet or its children into the given pixel list
* @param pixels
*/
void readPixelsTo(ox::Vector<uint8_t> *pPixels, int8_t bpp) const noexcept {
if (subsheets.size()) {
for (auto &s: subsheets) {
s.readPixelsTo(pPixels);
}
} else {
if (bpp == 4) {
for (auto p: this->pixels) {
pPixels->emplace_back(p & 0b1111);
pPixels->emplace_back(p >> 4);
}
} else {
for (auto p: this->pixels) {
pPixels->emplace_back(p);
}
}
}
}
/**
* Reads all pixels of this sheet or its children into the given pixel list
* @param pixels
@@ -158,8 +182,8 @@ struct TileSheet {
}
[[nodiscard]]
constexpr auto getPixel(int8_t bpp, std::size_t idx) const noexcept {
if (bpp == 4) {
constexpr auto getPixel(int8_t pBpp, std::size_t idx) const noexcept {
if (pBpp == 4) {
return getPixel4Bpp(idx);
} else {
return getPixel8Bpp(idx);
@@ -179,13 +203,13 @@ struct TileSheet {
}
[[nodiscard]]
constexpr auto getPixel(int8_t bpp, const geo::Point &pt) const noexcept {
constexpr auto getPixel(int8_t pBpp, const geo::Point &pt) const noexcept {
const auto idx = ptToIdx(pt, columns);
return getPixel(bpp, idx);
return getPixel(pBpp, idx);
}
constexpr auto walkPixels(int8_t bpp, auto callback) const noexcept {
if (bpp == 4) {
constexpr auto walkPixels(int8_t pBpp, auto callback) const noexcept {
if (pBpp == 4) {
for (std::size_t i = 0; i < pixels.size(); ++i) {
const auto colorIdx1 = pixels[i] & 0xF;
const auto colorIdx2 = pixels[i] >> 4;
@@ -200,9 +224,9 @@ struct TileSheet {
}
}
constexpr void setPixel(int8_t bpp, uint64_t idx, uint8_t palIdx) noexcept {
constexpr void setPixel(int8_t pBpp, uint64_t idx, uint8_t palIdx) noexcept {
auto &pixel = this->pixels[idx / 2];
if (bpp == 4) {
if (pBpp == 4) {
if (idx & 1) {
pixel = (pixel & 0b0000'1111) | (palIdx << 4);
} else {
@@ -213,13 +237,13 @@ struct TileSheet {
}
}
constexpr void setPixel(int8_t bpp, const geo::Point &pt, uint8_t palIdx) noexcept {
constexpr void setPixel(int8_t pBpp, const geo::Point &pt, uint8_t palIdx) noexcept {
const auto idx = ptToIdx(pt, columns);
setPixel(bpp, idx, palIdx);
setPixel(pBpp, idx, palIdx);
}
constexpr auto setPixelCount(int8_t bpp, std::size_t cnt) noexcept {
switch (bpp) {
constexpr auto setPixelCount(int8_t pBpp, std::size_t cnt) noexcept {
switch (pBpp) {
case 4:
pixels.resize(cnt / 2);
return OxError(0);
@@ -227,17 +251,17 @@ struct TileSheet {
pixels.resize(cnt);
return OxError(0);
default:
return OxError(1, "Invalid bpp used for TileSheet::SubSheet::setPixelCount");
return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount");
}
}
/**
* Gets a count of the pixels in this sheet, and not that of its children.
* @param bpp bits per pixel, need for knowing how to count the pixels
* @param pBpp bits per pixel, need for knowing how to count the pixels
* @return a count of the pixels in this sheet
*/
constexpr auto pixelCnt(int8_t bpp) const noexcept {
return bpp == 4 ? pixels.size() * 2 : pixels.size();
constexpr auto pixelCnt(int8_t pBpp) const noexcept {
return pBpp == 4 ? pixels.size() * 2 : pixels.size();
}
};
@@ -386,7 +410,7 @@ struct CompactTileSheet {
static constexpr auto TypeVersion = 1;
int8_t bpp = 0;
ox::FileAddress defaultPalette;
ox::Vector<uint8_t> pixels;
ox::Vector<uint8_t> pixels = {};
};
oxModelBegin(NostalgiaPalette)