diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 48c30af3..2471597c 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -198,6 +198,19 @@ struct TileSheet { setPixel(bpp, idx, palIdx); } + constexpr auto setPixelCount(int8_t bpp, std::size_t cnt) noexcept { + switch (bpp) { + case 4: + pixels.resize(cnt * 2); + return OxError(0); + case 8: + pixels.resize(cnt); + return OxError(0); + default: + return OxError(1, "Invalid bpp 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 diff --git a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp index 4601a0ea..d950e9f1 100644 --- a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp @@ -102,7 +102,6 @@ void TileSheetEditorImGui::draw(core::Context*) noexcept { } ImGui::EndChild(); m_subsheetEditor.draw(); - ImGui::ShowDemoWindow(); } void TileSheetEditorImGui::drawSubsheetSelector(TileSheet::SubSheet *subsheet, TileSheet::SubSheetIdx *path) noexcept { @@ -241,10 +240,11 @@ ox::Error TileSheetEditorImGui::markUnsavedChanges(int) noexcept { void TileSheetEditorImGui::SubSheetEditor::draw() noexcept { constexpr auto modalFlags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; constexpr auto popupName = "Edit SubSheet"; - if (m_show) { - ImGui::OpenPopup(popupName); - ImGui::SetNextWindowSize(ImVec2(235, 125)); + if (!m_show) { + return; } + ImGui::OpenPopup(popupName); + ImGui::SetNextWindowSize(ImVec2(235, 125)); if (ImGui::BeginPopupModal(popupName, &m_show, modalFlags)) { ImGui::InputText("Name", m_name.data(), m_name.cap()); if (m_cols != -1) { diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.hpp b/src/nostalgia/core/studio/tilesheeteditormodel.hpp index 97b9305d..6568cc52 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.hpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.hpp @@ -206,7 +206,7 @@ struct UpdateSubSheetCommand: public studio::UndoCommand { sheet.name = m_newName; sheet.columns = m_newCols; sheet.rows = m_newRows; - sheet.pixels.resize(static_cast(m_newCols * m_newRows * PixelsPerTile)); + oxLogError(sheet.setPixelCount(m_img->bpp, static_cast(m_newCols * m_newRows))); } void undo() noexcept final { diff --git a/src/nostalgia/core/studio/tilesheetpixelgrid.cpp b/src/nostalgia/core/studio/tilesheetpixelgrid.cpp index b5342bfe..10b05999 100644 --- a/src/nostalgia/core/studio/tilesheetpixelgrid.cpp +++ b/src/nostalgia/core/studio/tilesheetpixelgrid.cpp @@ -36,7 +36,7 @@ void TileSheetGrid::initBufferSet(const geo::Vec2 &paneSize, const TileSheet::Su glBindVertexArray(m_bufferSet.vao); // vbo m_bufferSet.vbo = glutils::generateBuffer(); - setBufferObjects(paneSize, subsheet, &m_bufferSet); + setBufferObjects(paneSize, subsheet); glutils::sendVbo(m_bufferSet); // vbo layout const auto pt1Attr = static_cast(glGetAttribLocation(m_shader, "vPt1")); @@ -62,10 +62,10 @@ void TileSheetGrid::setBufferObject(geo::Point pt1, geo::Point pt2, Color32 c, f memcpy(vbo, vertices, sizeof(vertices)); } -void TileSheetGrid::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet, glutils::BufferSet *bg) noexcept { +void TileSheetGrid::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet) noexcept { const auto pixSize = pixelSize(paneSize); - const auto set = [&bg, &pixSize](unsigned i, geo::Point pt1, geo::Point pt2, Color32 c) { - const auto vbo = &bg->vertices[i * VertexVboLength]; + const auto set = [&](unsigned i, geo::Point pt1, geo::Point pt2, Color32 c) { + const auto vbo = &m_bufferSet.vertices[i * VertexVboLength]; setBufferObject(pt1, pt2, c, vbo, pixSize); }; // set buffer length diff --git a/src/nostalgia/core/studio/tilesheetpixelgrid.hpp b/src/nostalgia/core/studio/tilesheetpixelgrid.hpp index f3c37c4a..87c5a80c 100644 --- a/src/nostalgia/core/studio/tilesheetpixelgrid.hpp +++ b/src/nostalgia/core/studio/tilesheetpixelgrid.hpp @@ -74,7 +74,7 @@ class TileSheetGrid { private: static void setBufferObject(geo::Point pt1, geo::Point pt2, Color32 c, float *vbo, const geo::Vec2 &pixSize) noexcept; - void setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet, glutils::BufferSet *bg) noexcept; + void setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet) noexcept; [[nodiscard]] geo::Vec2 pixelSize(const geo::Vec2 &paneSize) const noexcept; diff --git a/src/nostalgia/core/studio/tilesheetpixels.cpp b/src/nostalgia/core/studio/tilesheetpixels.cpp index c49b4d3b..5e035ced 100644 --- a/src/nostalgia/core/studio/tilesheetpixels.cpp +++ b/src/nostalgia/core/studio/tilesheetpixels.cpp @@ -36,7 +36,7 @@ void TileSheetPixels::initBufferSet(const geo::Vec2 &paneSize, const TileSheet & // vbo & ebo m_bufferSet.vbo = glutils::generateBuffer(); m_bufferSet.ebo = glutils::generateBuffer(); - setBufferObjects(paneSize, img, subSheet, pal, &m_bufferSet); + setBufferObjects(paneSize, img, subSheet, pal); glutils::sendVbo(m_bufferSet); glutils::sendEbo(m_bufferSet); // vbo layout @@ -78,22 +78,23 @@ void TileSheetPixels::setPixelBufferObject(const geo::Vec2 &paneSize, unsigned v memcpy(ebo, elms, sizeof(elms)); } -void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal, glutils::BufferSet *bg) noexcept { - const auto setPixel = [this, &paneSize, &bg, &subSheet, &pal](std::size_t i, uint8_t p) { +void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal) noexcept { + const auto setPixel = [&](std::size_t i, uint8_t p) { const auto color = pal.colors[p]; const auto pt = idxToPt(static_cast(i), subSheet.columns); const auto fx = static_cast(pt.x); const auto fy = static_cast(pt.y); - const auto vbo = &bg->vertices[i * VertexVboLength]; - const auto ebo = &bg->elements[i * VertexEboLength]; + const auto vbo = &m_bufferSet.vertices[i * VertexVboLength]; + const auto ebo = &m_bufferSet.elements[i * VertexEboLength]; setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo); }; // set buffer lengths const auto width = subSheet.columns * TileWidth; const auto height = subSheet.rows * TileHeight; - const auto tiles = static_cast(width * height); - m_bufferSet.vertices.resize(tiles * VertexVboLength); - m_bufferSet.elements.resize(tiles * VertexEboLength); + oxDebugf("rows: {}", subSheet.rows); + const auto pixels = static_cast(width * height); + m_bufferSet.vertices.resize(pixels * VertexVboLength); + m_bufferSet.elements.resize(pixels * VertexEboLength); // set pixels if (img.bpp == 4) { for (std::size_t i = 0; i < subSheet.pixels.size(); ++i) { diff --git a/src/nostalgia/core/studio/tilesheetpixels.hpp b/src/nostalgia/core/studio/tilesheetpixels.hpp index 185d5fca..4e776093 100644 --- a/src/nostalgia/core/studio/tilesheetpixels.hpp +++ b/src/nostalgia/core/studio/tilesheetpixels.hpp @@ -56,7 +56,7 @@ class TileSheetPixels { private: void setPixelBufferObject(const geo::Vec2 &paneS, unsigned vertexRow, float x, float y, Color16 color, float *vbo, GLuint *ebo) const noexcept; - void setBufferObjects(const geo::Vec2 &paneS, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal, glutils::BufferSet *bg) noexcept; + void setBufferObjects(const geo::Vec2 &paneS, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal) noexcept; };