From 45495697463707c0fe61cfca16d4a4ce4fdba3da Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 6 Jun 2023 00:09:49 -0500 Subject: [PATCH] [nostalgia/core/studio] Fix pixel line grid scaling on zoom --- .../core/studio/tilesheeteditor-imgui.cpp | 2 +- .../core/studio/tilesheeteditorview.cpp | 4 +++- .../core/studio/tilesheetpixelgrid.cpp | 20 ++++++++++++++----- .../core/studio/tilesheetpixelgrid.hpp | 4 +++- src/nostalgia/core/studio/tilesheetpixels.cpp | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp index 6ac691de..d0415a68 100644 --- a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp @@ -16,7 +16,7 @@ template ox::Error toPngFile(const ox::String &path, const TileSheet::SubSheet &s, const Palette &pal, int8_t bpp) noexcept { ox::Vector pixels; s.readPixelsTo(&pixels, bpp); - const unsigned rows = s.rows == -1 ? pixels.size() / PixelsPerTile : static_cast(s.rows); + const unsigned rows = s.rows == -1 ? static_cast(pixels.size()) / PixelsPerTile : static_cast(s.rows); const unsigned cols = s.columns == -1 ? 1 : static_cast(s.columns); const auto width = cols * TileWidth; const auto height = rows * TileHeight; diff --git a/src/nostalgia/core/studio/tilesheeteditorview.cpp b/src/nostalgia/core/studio/tilesheeteditorview.cpp index d1950b40..12cfc92f 100644 --- a/src/nostalgia/core/studio/tilesheeteditorview.cpp +++ b/src/nostalgia/core/studio/tilesheeteditorview.cpp @@ -11,7 +11,8 @@ namespace nostalgia::core { -TileSheetEditorView::TileSheetEditorView(turbine::Context *ctx, ox::CRStringView path): m_model(ctx, path), m_pixelsDrawer(&m_model) { +TileSheetEditorView::TileSheetEditorView(turbine::Context *ctx, ox::CRStringView path): + m_model(ctx, path), m_pixelsDrawer(&m_model) { // build shaders oxThrowError(m_pixelsDrawer.buildShader()); oxThrowError(m_pixelGridDrawer.buildShader()); @@ -102,6 +103,7 @@ ox::Error TileSheetEditorView::markUpdated() noexcept { void TileSheetEditorView::ackUpdate() noexcept { m_updated = false; m_pixelsDrawer.update(m_viewSize); + m_pixelGridDrawer.update(m_viewSize, *m_model.activeSubSheet()); m_model.ackUpdate(); } diff --git a/src/nostalgia/core/studio/tilesheetpixelgrid.cpp b/src/nostalgia/core/studio/tilesheetpixelgrid.cpp index 7a40715b..14d81d0d 100644 --- a/src/nostalgia/core/studio/tilesheetpixelgrid.cpp +++ b/src/nostalgia/core/studio/tilesheetpixelgrid.cpp @@ -2,7 +2,10 @@ * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. */ +#include + #include + #include "tilesheetpixelgrid.hpp" namespace nostalgia::core { @@ -54,19 +57,26 @@ void TileSheetGrid::initBufferSet(const ox::Vec2 &paneSize, const TileSheet::Sub reinterpret_cast(4 * sizeof(float))); } +void TileSheetGrid::update(ox::Vec2 const&paneSize, TileSheet::SubSheet const&subsheet) noexcept { + glBindVertexArray(m_bufferSet.vao); + setBufferObjects(paneSize, subsheet); + glutils::sendVbo(m_bufferSet); + glutils::sendEbo(m_bufferSet); +} + void TileSheetGrid::setBufferObject(ox::Point pt1, ox::Point pt2, Color32 c, float *vbo, const ox::Vec2 &pixSize) noexcept { const auto x1 = static_cast(pt1.x) * pixSize.x - 1.f; const auto y1 = 1.f - static_cast(pt1.y) * pixSize.y; const auto x2 = static_cast(pt2.x) * pixSize.x - 1.f; const auto y2 = 1.f - static_cast(pt2.y) * pixSize.y; // don't worry, this memcpy gets optimized to something much more ideal - const float vertices[VertexVboLength] = {x1, y1, x2, y2, redf(c), greenf(c), bluef(c)}; - memcpy(vbo, vertices, sizeof(vertices)); + const ox::Array vertices = {x1, y1, x2, y2, redf(c), greenf(c), bluef(c)}; + memcpy(vbo, vertices.data(), sizeof(vertices)); } void TileSheetGrid::setBufferObjects(const ox::Vec2 &paneSize, const TileSheet::SubSheet &subsheet) noexcept { const auto pixSize = pixelSize(paneSize); - const auto set = [&](unsigned i, ox::Point pt1, ox::Point pt2, Color32 c) { + const auto set = [&](std::size_t i, ox::Point pt1, ox::Point pt2, Color32 c) { const auto vbo = &m_bufferSet.vertices[i * VertexVboLength]; setBufferObject(pt1, pt2, c, vbo, pixSize); }; @@ -75,9 +85,9 @@ void TileSheetGrid::setBufferObjects(const ox::Vec2 &paneSize, const TileSheet:: const auto height = subsheet.rows * TileHeight; const auto tileCnt = static_cast(subsheet.columns + subsheet.rows); const auto pixelCnt = static_cast(width + height); - m_bufferSet.vertices.resize((tileCnt + pixelCnt + 4) * VertexVboLength); + m_bufferSet.vertices.resize(static_cast(tileCnt + pixelCnt + 4) * VertexVboLength); // set buffer - auto i = 0ull; + std::size_t i = 0; // pixel outlines constexpr auto pixOutlineColor = color32(0.4431f, 0.4901f, 0.4941f); for (auto x = 0; x < subsheet.columns * TileWidth + 1; ++x) { diff --git a/src/nostalgia/core/studio/tilesheetpixelgrid.hpp b/src/nostalgia/core/studio/tilesheetpixelgrid.hpp index 5601458c..9649d0fb 100644 --- a/src/nostalgia/core/studio/tilesheetpixelgrid.hpp +++ b/src/nostalgia/core/studio/tilesheetpixelgrid.hpp @@ -68,7 +68,9 @@ class TileSheetGrid { void draw(bool update, const ox::Vec2 &scroll) noexcept; - void initBufferSet(const ox::Vec2 &paneSize, const TileSheet::SubSheet &subsheet) noexcept; + void initBufferSet(const ox::Vec2 &paneSize, TileSheet::SubSheet const&subsheet) noexcept; + + void update(ox::Vec2 const&paneSize, TileSheet::SubSheet const&subsheet) noexcept; private: static void setBufferObject(ox::Point pt1, ox::Point pt2, Color32 c, float *vbo, const ox::Vec2 &pixSize) noexcept; diff --git a/src/nostalgia/core/studio/tilesheetpixels.cpp b/src/nostalgia/core/studio/tilesheetpixels.cpp index 2e69c8dc..c0a0a9fd 100644 --- a/src/nostalgia/core/studio/tilesheetpixels.cpp +++ b/src/nostalgia/core/studio/tilesheetpixels.cpp @@ -121,7 +121,7 @@ void TileSheetPixels::setBufferObjects(const ox::Vec2 &paneSize) noexcept { const auto b = (blue16(color) + 31) / 2; color = color16(r, g, b); } - setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo); + setPixelBufferObject(paneSize, static_cast(i * VertexVboRows), fx, fy, color, vbo, ebo); }); }