[nostalgia] Start on new TileSheetEditor

This commit is contained in:
2021-12-17 20:57:56 -06:00
parent ed074d07be
commit 775008a513
122 changed files with 651 additions and 2592 deletions
@@ -0,0 +1,90 @@
/*
* Copyright 2016 - 2021 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <nostalgia/core/consts.hpp>
#include "tilesheetpixelgrid.hpp"
namespace nostalgia::core {
ox::Error TileSheetGrid::buildShader() noexcept {
const auto pixelLineVshad = ox::sfmt(VShad, glutils::GlslVersion);
const auto pixelLineFshad = ox::sfmt(FShad, glutils::GlslVersion);
const auto pixelLineGshad = ox::sfmt(GShad, glutils::GlslVersion);
return glutils::buildShaderProgram(pixelLineVshad, pixelLineFshad, pixelLineGshad).moveTo(&m_shader);
}
void TileSheetGrid::draw(bool update) noexcept {
glUseProgram(m_shader);
glBindVertexArray(m_bufferSet.vao);
if (update) {
glutils::sendVbo(m_bufferSet);
}
glDrawElements(GL_POINTS, static_cast<GLsizei>(m_bufferSet.elements.size()), GL_UNSIGNED_INT, nullptr);
}
void TileSheetGrid::initBufferSet(const NostalgiaGraphic &img) noexcept {
// vao
m_bufferSet.vao = glutils::generateVertexArrayObject();
glBindVertexArray(m_bufferSet.vao);
// vbo & ebo
m_bufferSet.vbo = glutils::generateBuffer();
m_bufferSet.ebo = glutils::generateBuffer();
setBufferObjects(img, &m_bufferSet);
glutils::sendVbo(m_bufferSet);
glutils::sendEbo(m_bufferSet);
// vbo layout
const auto pt1Attr = static_cast<GLuint>(glGetAttribLocation(m_shader, "vPt1"));
glEnableVertexAttribArray(pt1Attr);
glVertexAttribPointer(pt1Attr, 2, GL_FLOAT, GL_FALSE, VertexVboRowLength * sizeof(float), nullptr);
const auto pt2Attr = static_cast<GLuint>(glGetAttribLocation(m_shader, "vPt2"));
glEnableVertexAttribArray(pt2Attr);
glVertexAttribPointer(pt2Attr, 2, GL_FLOAT, GL_FALSE, VertexVboRowLength * sizeof(float),
reinterpret_cast<void*>(2 * sizeof(float)));
}
void TileSheetGrid::setBufferObject(unsigned vertexRow, common::Point pt1, common::Point pt2, float *vbo, GLuint *ebo) noexcept {
const auto ps = pixelSize();
const auto x1 = static_cast<float>(pt1.x) * ps.x;
const auto y1 = static_cast<float>(pt1.y) * ps.y;
const auto x2 = static_cast<float>(pt2.x) * ps.x;
const auto y2 = static_cast<float>(pt2.y) * ps.y;
// don't worry, these memcpys gets optimized to something much more ideal
const float vertices[VertexVboLength] = {x1, y1, x2, y2};
memcpy(vbo, vertices, sizeof(vertices));
const GLuint elms[VertexEboLength] = {vertexRow};
memcpy(ebo, elms, sizeof(elms));
}
void TileSheetGrid::setBufferObjects(const NostalgiaGraphic &img, glutils::BufferSet *bg) noexcept {
const auto set = [bg](unsigned i, common::Point pt1, common::Point pt2) {
const auto vbo = &bg->vertices[i * VertexVboLength];
const auto ebo = &bg->elements[i * VertexEboLength];
setBufferObject(i * VertexVboRows, pt1, pt2, vbo, ebo);
};
// set buffer lengths
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);
// set buffers
auto i = 0ull;
for (auto x = 0; x < img.columns; ++x) {
set(0, {x, 0}, {x, img.rows});
++i;
}
for (auto y = 0; y < img.rows; ++y) {
set(0, {0, y}, {img.columns, y});
++i;
}
}
ImVec2 TileSheetGrid::pixelSize() noexcept {
const auto [sw, sh] = ImGui::GetContentRegionAvail();
constexpr float ymod = 0.35f / 10.0f;
const auto xmod = ymod * sh / sw;
return {xmod, ymod};
}
}