From 6a2954f82b7d316cd1c1436661c9e6bad09421c3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 26 Dec 2023 17:28:41 -0600 Subject: [PATCH] [nostalgia/core] Remove id from TileSheetV3::Subsheet, add TileSheetV4 --- .../core/include/nostalgia/core/tilesheet.hpp | 67 ++++++++++++++++--- .../modules/core/src/keel/keelmodule.cpp | 14 ++-- .../modules/core/src/keel/typeconv.cpp | 28 ++++++++ .../modules/core/src/keel/typeconv.hpp | 14 +++- .../tilesheeteditor/tilesheeteditorview.cpp | 2 +- .../tilesheeteditor/tilesheetpixels.cpp | 10 +-- .../tilesheeteditor/tilesheetpixels.hpp | 4 +- src/nostalgia/modules/core/src/tilesheet.cpp | 13 ++++ 8 files changed, 125 insertions(+), 27 deletions(-) diff --git a/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp b/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp index 47b214bf..f3e7be45 100644 --- a/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp +++ b/src/nostalgia/modules/core/include/nostalgia/core/tilesheet.hpp @@ -61,7 +61,7 @@ struct TileSheetV2 { using SubSheetId = int32_t; -struct TileSheet { +struct TileSheetV3 { using SubSheetIdx = ox::Vector; struct SubSheet { @@ -73,6 +73,36 @@ struct TileSheet { int rows = 0; ox::Vector subsheets; ox::Vector pixels; + constexpr SubSheet() noexcept = default; + SubSheet( + SubSheetId pId, + ox::CRStringView pName, + int pColumns, + int pRows, + int bpp) noexcept; + }; + + static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.TileSheet"; + static constexpr auto TypeVersion = 3; + int8_t bpp = 4; + SubSheetId idIt = 0; + ox::FileAddress defaultPalette; + SubSheet subsheet{0, "Root", 1, 1, bpp}; + +}; + +struct TileSheet { + using SubSheetIdx = ox::Vector; + + struct SubSheet { + static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.TileSheet.SubSheet"; + static constexpr auto TypeVersion = 4; + SubSheetId id = 0; + ox::String name; + int columns = 0; + int rows = 0; + ox::Vector subsheets; + ox::Vector pixels; constexpr SubSheet() noexcept = default; constexpr SubSheet(SubSheet const&other) noexcept = default; @@ -189,7 +219,7 @@ struct TileSheet { }; static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.TileSheet"; - static constexpr auto TypeVersion = 3; + static constexpr auto TypeVersion = 4; int8_t bpp = 4; SubSheetId idIt = 0; ox::FileAddress defaultPalette; @@ -274,11 +304,11 @@ struct TileSheet { }; -using TileSheetV3 = TileSheet; +using TileSheetV4 = TileSheet; struct CompactTileSheet { static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.CompactTileSheet"; - static constexpr auto TypeVersion = 1; + static constexpr auto TypeVersion = 2; int8_t bpp = 0; ox::FileAddress defaultPalette; ox::Vector pixels = {}; @@ -294,9 +324,9 @@ oxModelBegin(TileSheetV1) oxModelEnd() oxModelBegin(TileSheetV2::SubSheet) - oxModelField(name); - oxModelField(rows); - oxModelField(columns); + oxModelField(name) + oxModelField(rows) + oxModelField(columns) oxModelField(subsheets) oxModelField(pixels) oxModelEnd() @@ -308,10 +338,9 @@ oxModelBegin(TileSheetV2) oxModelEnd() oxModelBegin(TileSheetV3::SubSheet) - oxModelField(id); - oxModelField(name); - oxModelField(rows); - oxModelField(columns); + oxModelField(name) + oxModelField(rows) + oxModelField(columns) oxModelField(subsheets) oxModelField(pixels) oxModelEnd() @@ -323,6 +352,22 @@ oxModelBegin(TileSheetV3) oxModelField(subsheet) oxModelEnd() +oxModelBegin(TileSheetV4::SubSheet) + oxModelField(id) + oxModelField(name) + oxModelField(rows) + oxModelField(columns) + oxModelField(subsheets) + oxModelField(pixels) +oxModelEnd() + +oxModelBegin(TileSheetV4) + oxModelField(bpp) + oxModelField(idIt) + oxModelField(defaultPalette) + oxModelField(subsheet) +oxModelEnd() + oxModelBegin(CompactTileSheet) oxModelField(bpp) oxModelField(defaultPalette) diff --git a/src/nostalgia/modules/core/src/keel/keelmodule.cpp b/src/nostalgia/modules/core/src/keel/keelmodule.cpp index 57dd9036..cf4fb0c0 100644 --- a/src/nostalgia/modules/core/src/keel/keelmodule.cpp +++ b/src/nostalgia/modules/core/src/keel/keelmodule.cpp @@ -18,7 +18,8 @@ class KeelModule: public keel::Module { private: NostalgiaPaletteToPaletteConverter m_nostalgiaPaletteToPaletteConverter; TileSheetV1ToTileSheetV2Converter m_tileSheetV1ToTileSheetV2Converter; - TileSheetV2ToTileSheetV3Converter m_tileSheetV2ToTileSheetConverter; + TileSheetV2ToTileSheetV3Converter m_tileSheetV2ToTileSheetV3Converter; + TileSheetV3ToTileSheetV4Converter m_tileSheetV3ToTileSheetV4Converter; TileSheetToCompactTileSheetConverter m_tileSheetToCompactTileSheetConverter; public: @@ -32,7 +33,8 @@ class KeelModule: public keel::Module { return { keel::generateTypeDesc, keel::generateTypeDesc, - keel::generateTypeDesc, + keel::generateTypeDesc, + keel::generateTypeDesc, keel::generateTypeDesc, keel::generateTypeDesc, }; @@ -43,7 +45,8 @@ class KeelModule: public keel::Module { return { &m_nostalgiaPaletteToPaletteConverter, &m_tileSheetV1ToTileSheetV2Converter, - &m_tileSheetV2ToTileSheetConverter, + &m_tileSheetV2ToTileSheetV3Converter, + &m_tileSheetV3ToTileSheetV4Converter, &m_tileSheetToCompactTileSheetConverter, }; } @@ -57,8 +60,9 @@ class KeelModule: public keel::Module { const auto typeId = ox::buildTypeId( hdr.clawHdr.typeName, hdr.clawHdr.typeVersion, hdr.clawHdr.typeParams); if (typeId == ox::buildTypeId() || - typeId == ox::buildTypeId() || - typeId == ox::buildTypeId()) { + typeId == ox::buildTypeId() || + typeId == ox::buildTypeId() || + typeId == ox::buildTypeId()) { oxReturnError(keel::convertBuffToBuff( ctx, buff, ox::ClawFormat::Metal).moveTo(buff)); } diff --git a/src/nostalgia/modules/core/src/keel/typeconv.cpp b/src/nostalgia/modules/core/src/keel/typeconv.cpp index 7a3662ea..e7d1eb39 100644 --- a/src/nostalgia/modules/core/src/keel/typeconv.cpp +++ b/src/nostalgia/modules/core/src/keel/typeconv.cpp @@ -53,6 +53,34 @@ ox::Error TileSheetV2ToTileSheetV3Converter::convert( return {}; } + +void TileSheetV3ToTileSheetV4Converter::convertSubsheet( + TileSheetV3::SubSheet &src, + TileSheetV4::SubSheet &dst, + SubSheetId &idIt) noexcept { + dst.id = idIt; + dst.name = std::move(src.name); + dst.columns = src.columns; + dst.rows = src.rows; + dst.pixels = std::move(src.pixels); + ++idIt; + dst.subsheets.resize(src.subsheets.size()); + for (auto i = 0u; i < src.subsheets.size(); ++i) { + convertSubsheet(src.subsheets[i], dst.subsheets[i], idIt); + } +} + +ox::Error TileSheetV3ToTileSheetV4Converter::convert( + keel::Context&, + TileSheetV3 &src, + TileSheetV4 &dst) const noexcept { + dst.bpp = src.bpp; + dst.idIt = src.idIt; + dst.defaultPalette = std::move(src.defaultPalette); + convertSubsheet(src.subsheet, dst.subsheet, dst.idIt); + return {}; +} + ox::Error TileSheetToCompactTileSheetConverter::convert( keel::Context&, TileSheet &src, diff --git a/src/nostalgia/modules/core/src/keel/typeconv.hpp b/src/nostalgia/modules/core/src/keel/typeconv.hpp index 012ff7fe..d47e25fc 100644 --- a/src/nostalgia/modules/core/src/keel/typeconv.hpp +++ b/src/nostalgia/modules/core/src/keel/typeconv.hpp @@ -25,11 +25,19 @@ class TileSheetV1ToTileSheetV2Converter: public keel::Converter { + static void convertSubsheet( + TileSheetV2::SubSheet &src, + TileSheetV3::SubSheet &dst, + SubSheetId &idIt) noexcept; + ox::Error convert(keel::Context&, TileSheetV2 &src, TileSheetV3 &dst) const noexcept final; +}; + +class TileSheetV3ToTileSheetV4Converter: public keel::Converter { static void convertSubsheet( - TileSheetV2::SubSheet &src, - TileSheetV3::SubSheet &dst, + TileSheetV3::SubSheet &src, + TileSheetV4::SubSheet &dst, SubSheetId &idIt) noexcept; - ox::Error convert(keel::Context&, TileSheetV2 &src, TileSheetV3 &dst) const noexcept final; + ox::Error convert(keel::Context&, TileSheetV3 &src, TileSheetV4 &dst) const noexcept final; }; class TileSheetToCompactTileSheetConverter: public keel::Converter { diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp index f1117c3d..5e6abb40 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp @@ -13,7 +13,7 @@ namespace nostalgia::core { TileSheetEditorView::TileSheetEditorView(turbine::Context &ctx, ox::StringView path, studio::UndoStack &undoStack): m_model(ctx, path, undoStack), - m_pixelsDrawer(&m_model) { + m_pixelsDrawer(m_model) { // build shaders oxThrowError(m_pixelsDrawer.buildShader()); oxThrowError(m_pixelGridDrawer.buildShader()); diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.cpp index bf471fdd..358929a5 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.cpp @@ -9,7 +9,7 @@ namespace nostalgia::core { -TileSheetPixels::TileSheetPixels(TileSheetEditorModel *model) noexcept: m_model(model) { +TileSheetPixels::TileSheetPixels(TileSheetEditorModel &model) noexcept: m_model(model) { } void TileSheetPixels::setPixelSizeMod(float sm) noexcept { @@ -94,15 +94,15 @@ void TileSheetPixels::setPixelBufferObject( void TileSheetPixels::setBufferObjects(ox::Vec2 const&paneSize) noexcept { // set buffer lengths - const auto subSheet = m_model->activeSubSheet(); - const auto pal = m_model->pal(); + const auto subSheet = m_model.activeSubSheet(); + const auto pal = m_model.pal(); const auto width = subSheet->columns * TileWidth; const auto height = subSheet->rows * TileHeight; const auto pixels = static_cast(width * height); m_bufferSet.vertices.resize(pixels * VertexVboLength); m_bufferSet.elements.resize(pixels * VertexEboLength); // set pixels - subSheet->walkPixels(m_model->img().bpp, [&](std::size_t i, uint8_t p) { + subSheet->walkPixels(m_model.img().bpp, [&](std::size_t i, uint8_t p) { auto color = pal->color(p); const auto pt = idxToPt(static_cast(i), subSheet->columns); const auto fx = static_cast(pt.x); @@ -115,7 +115,7 @@ void TileSheetPixels::setBufferObjects(ox::Vec2 const&paneSize) noexcept { if (i * VertexEboLength + VertexEboLength > m_bufferSet.elements.size()) { return; } - if (m_model->pixelSelected(i)) { + if (m_model.pixelSelected(i)) { const auto r = red16(color) / 2; const auto g = (green16(color) + 20) / 2; const auto b = (blue16(color) + 31) / 2; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.hpp index 80aa8ae5..7725462f 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheetpixels.hpp @@ -43,10 +43,10 @@ class TileSheetPixels { float m_pixelSizeMod = 1; glutils::GLProgram m_shader; glutils::BufferSet m_bufferSet; - const class TileSheetEditorModel *m_model = nullptr; + const class TileSheetEditorModel &m_model; public: - explicit TileSheetPixels(class TileSheetEditorModel *model) noexcept; + explicit TileSheetPixels(class TileSheetEditorModel &model) noexcept; void setPixelSizeMod(float sm) noexcept; diff --git a/src/nostalgia/modules/core/src/tilesheet.cpp b/src/nostalgia/modules/core/src/tilesheet.cpp index 0ec205a8..ead35332 100644 --- a/src/nostalgia/modules/core/src/tilesheet.cpp +++ b/src/nostalgia/modules/core/src/tilesheet.cpp @@ -10,6 +10,19 @@ namespace nostalgia::core { +TileSheetV3::SubSheet::SubSheet( + SubSheetId pId, + ox::CRStringView pName, + int pColumns, + int pRows, + int bpp) noexcept: + id(pId), + name(pName), + columns(pColumns), + rows(pRows), + pixels(static_cast(columns * rows * PixelsPerTile) / (bpp == 4 ? 2u : 1u)) { +} + TileSheet::SubSheet::SubSheet(SubSheet &&other) noexcept: id (other.id), name (std::move(other.name)),