[nostalgia/core/studio] Fix pixel line grid scaling on zoom

This commit is contained in:
Gary Talent 2023-06-06 00:09:49 -05:00
parent aff3b04fe2
commit 4549569746
5 changed files with 23 additions and 9 deletions

View File

@ -16,7 +16,7 @@ template<bool alpha = false>
ox::Error toPngFile(const ox::String &path, const TileSheet::SubSheet &s, const Palette &pal, int8_t bpp) noexcept {
ox::Vector<uint8_t> pixels;
s.readPixelsTo(&pixels, bpp);
const unsigned rows = s.rows == -1 ? pixels.size() / PixelsPerTile : static_cast<unsigned>(s.rows);
const unsigned rows = s.rows == -1 ? static_cast<unsigned>(pixels.size()) / PixelsPerTile : static_cast<unsigned>(s.rows);
const unsigned cols = s.columns == -1 ? 1 : static_cast<unsigned>(s.columns);
const auto width = cols * TileWidth;
const auto height = rows * TileHeight;

View File

@ -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();
}

View File

@ -2,7 +2,10 @@
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <ox/claw/write.hpp>
#include <nostalgia/core/consts.hpp>
#include "tilesheetpixelgrid.hpp"
namespace nostalgia::core {
@ -54,19 +57,26 @@ void TileSheetGrid::initBufferSet(const ox::Vec2 &paneSize, const TileSheet::Sub
reinterpret_cast<void*>(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<float>(pt1.x) * pixSize.x - 1.f;
const auto y1 = 1.f - static_cast<float>(pt1.y) * pixSize.y;
const auto x2 = static_cast<float>(pt2.x) * pixSize.x - 1.f;
const auto y2 = 1.f - static_cast<float>(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<float, VertexVboLength> 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<unsigned>(subsheet.columns + subsheet.rows);
const auto pixelCnt = static_cast<unsigned>(width + height);
m_bufferSet.vertices.resize((tileCnt + pixelCnt + 4) * VertexVboLength);
m_bufferSet.vertices.resize(static_cast<std::size_t>(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) {

View File

@ -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;

View File

@ -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<unsigned>(i * VertexVboRows), fx, fy, color, vbo, ebo);
});
}