[nostalgia/core] Upgrade TileSheet format to support subsheets and add conversion system

This commit is contained in:
2022-02-17 04:27:23 -06:00
parent 3c44c86e91
commit 7ac7909510
11 changed files with 290 additions and 36 deletions
@@ -47,7 +47,7 @@ class TileSheetEditorImGui: public studio::Editor {
void draw(core::Context*) noexcept override;
virtual studio::UndoStack *undoStack() noexcept override;
studio::UndoStack *undoStack() noexcept final;
protected:
void saveItem() override;
@@ -40,8 +40,8 @@ void TileSheetEditor::draw() noexcept {
void TileSheetEditor::scrollV(const geo::Vec2 &paneSz, float wheel, bool zoomMod) noexcept {
const auto pixelSize = m_pixelsDrawer.pixelSize(paneSz);
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(img().columns) * TileWidth,
pixelSize.y * static_cast<float>(img().rows) * TileHeight);
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(img().columns()) * TileWidth,
pixelSize.y * static_cast<float>(img().rows()) * TileHeight);
if (zoomMod) {
m_pixelSizeMod = ox::clamp(m_pixelSizeMod + wheel * 0.02f, 0.55f, 2.f);
m_pixelsDrawer.setPixelSizeMod(m_pixelSizeMod);
@@ -57,8 +57,8 @@ void TileSheetEditor::scrollV(const geo::Vec2 &paneSz, float wheel, bool zoomMod
void TileSheetEditor::scrollH(const geo::Vec2 &paneSz, float wheelh) noexcept {
const auto pixelSize = m_pixelsDrawer.pixelSize(paneSz);
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(img().columns) * TileWidth,
pixelSize.y * static_cast<float>(img().rows) * TileHeight);
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(img().columns()) * TileWidth,
pixelSize.y * static_cast<float>(img().rows()) * TileHeight);
m_scrollOffset.x += wheelh * 0.1f;
m_scrollOffset.x = ox::clamp(m_scrollOffset.x, -(sheetSize.x / 2), 0.f);
}
@@ -25,9 +25,9 @@ void TileSheetEditorModel::paste() {
void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept {
if (m_ongoingDrawCommand) {
m_updated = m_ongoingDrawCommand->append(ptToIdx(pt, m_img.columns));
m_updated = m_ongoingDrawCommand->append(ptToIdx(pt, m_img.columns()));
} else {
const auto idx = ptToIdx(pt, m_img.columns);
const auto idx = ptToIdx(pt, m_img.columns());
if (m_img.getPixel(idx) != palIdx) {
pushCommand(new DrawCommand(&m_updated, &m_img, idx, palIdx));
}
@@ -48,7 +48,7 @@ void TileSheetEditorModel::ackUpdate() noexcept {
void TileSheetEditorModel::getFillPixels(bool *pixels, const geo::Point &pt, int oldColor) const noexcept {
const auto tileIdx = [this](const geo::Point &pt) noexcept {
return ptToIdx(pt, img().columns) / PixelsPerTile;
return ptToIdx(pt, img().columns()) / PixelsPerTile;
};
// get points
const auto leftPt = pt + geo::Point(-1, 0);
@@ -56,11 +56,11 @@ void TileSheetEditorModel::getFillPixels(bool *pixels, const geo::Point &pt, int
const auto topPt = pt + geo::Point(0, -1);
const auto bottomPt = pt + geo::Point(0, 1);
// calculate indices
const auto idx = ptToIdx(pt, m_img.columns);
const auto leftIdx = ptToIdx(leftPt, m_img.columns);
const auto rightIdx = ptToIdx(rightPt, m_img.columns);
const auto topIdx = ptToIdx(topPt, m_img.columns);
const auto bottomIdx = ptToIdx(bottomPt, m_img.columns);
const auto idx = ptToIdx(pt, m_img.columns());
const auto leftIdx = ptToIdx(leftPt, m_img.columns());
const auto rightIdx = ptToIdx(rightPt, m_img.columns());
const auto topIdx = ptToIdx(topPt, m_img.columns());
const auto bottomIdx = ptToIdx(bottomPt, m_img.columns());
const auto tile = tileIdx(pt);
// mark pixels to update
pixels[idx % PixelsPerTile] = true;
@@ -69,31 +69,31 @@ void TileSheetGrid::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet
setBufferObject(pt1, pt2, c, vbo, pixSize);
};
// set buffer length
const auto width = img.columns * TileWidth;
const auto height = img.rows * TileHeight;
const auto width = img.columns() * TileWidth;
const auto height = img.rows() * TileHeight;
const auto pixelCnt = static_cast<unsigned>(width * height);
const auto tileCnt = static_cast<unsigned>(img.columns * img.rows);
const auto tileCnt = static_cast<unsigned>(img.columns() * img.rows());
m_bufferSet.vertices.resize((tileCnt + pixelCnt) * VertexVboLength);
// set buffer
auto i = 0ull;
// pixel outlines
constexpr auto pixOutlineColor = color32(0.4431f, 0.4901f, 0.4941f);
for (auto x = 0; x < img.columns * TileWidth + 1; ++x) {
set(i, {x, 0}, {x, img.rows * TileHeight}, pixOutlineColor);
for (auto x = 0; x < img.columns() * TileWidth + 1; ++x) {
set(i, {x, 0}, {x, img.rows() * TileHeight}, pixOutlineColor);
++i;
}
for (auto y = 0; y < img.rows * TileHeight + 1; ++y) {
set(i, {0, y}, {img.columns * TileWidth, y}, pixOutlineColor);
for (auto y = 0; y < img.rows() * TileHeight + 1; ++y) {
set(i, {0, y}, {img.columns() * TileWidth, y}, pixOutlineColor);
++i;
}
// tile outlines
constexpr auto tileOutlineColor = color32(0.f, 0.f, 0.f);
for (auto x = 0; x < img.columns * TileWidth + 1; x += TileWidth) {
set(i, {x, 0}, {x, img.rows * TileHeight}, tileOutlineColor);
for (auto x = 0; x < img.columns() * TileWidth + 1; x += TileWidth) {
set(i, {x, 0}, {x, img.rows() * TileHeight}, tileOutlineColor);
++i;
}
for (auto y = 0; y < img.rows * TileHeight + 1; y += TileHeight) {
set(i, {0, y}, {img.columns * TileWidth, y}, tileOutlineColor);
for (auto y = 0; y < img.rows() * TileHeight + 1; y += TileHeight) {
set(i, {0, y}, {img.columns() * TileWidth, y}, tileOutlineColor);
++i;
}
}
@@ -81,7 +81,7 @@ void TileSheetPixels::setPixelBufferObject(const geo::Vec2 &paneSize, unsigned v
void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet &img, const Palette &pal, glutils::BufferSet *bg) noexcept {
const auto setPixel = [this, paneSize, bg, img, pal](std::size_t i, uint8_t p) {
const auto color = pal.colors[p];
const auto pt = idxToPt(static_cast<int>(i), img.columns);
const auto pt = idxToPt(static_cast<int>(i), img.columns());
const auto fx = static_cast<float>(pt.x);
const auto fy = static_cast<float>(pt.y);
const auto vbo = &bg->vertices[i * VertexVboLength];
@@ -89,8 +89,8 @@ void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileShee
setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo);
};
// set buffer lengths
const auto width = img.columns * TileWidth;
const auto height = img.rows * TileHeight;
const auto width = img.columns() * TileWidth;
const auto height = img.rows() * TileHeight;
const auto tiles = static_cast<unsigned>(width * height);
m_bufferSet.vertices.resize(tiles * VertexVboLength);
m_bufferSet.elements.resize(tiles * VertexEboLength);