[nostalgia/core/studio] Cleanup and decouple main TileSheetEditor drawing from ImGui

This commit is contained in:
2022-02-13 02:25:14 -06:00
parent 779dd1fd05
commit 7370b23c9d
10 changed files with 123 additions and 128 deletions
+10 -11
View File
@@ -18,7 +18,7 @@ ox::Error TileSheetPixels::buildShader() noexcept {
return glutils::buildShaderProgram(Vshad, Fshad).moveTo(&m_shader);
}
void TileSheetPixels::draw(bool update, const ImVec2 &scroll) noexcept {
void TileSheetPixels::draw(bool update, const common::Vec2 &scroll) noexcept {
glUseProgram(m_shader);
glBindVertexArray(m_bufferSet.vao);
if (update) {
@@ -29,14 +29,14 @@ void TileSheetPixels::draw(bool update, const ImVec2 &scroll) noexcept {
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_bufferSet.elements.size()), GL_UNSIGNED_INT, nullptr);
}
void TileSheetPixels::initBufferSet(const NostalgiaGraphic &img, const NostalgiaPalette &pal) noexcept {
void TileSheetPixels::initBufferSet(const common::Vec2 &paneSize, const NostalgiaGraphic &img, const NostalgiaPalette &pal) 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, pal, &m_bufferSet);
setBufferObjects(paneSize, img, pal, &m_bufferSet);
glutils::sendVbo(m_bufferSet);
glutils::sendEbo(m_bufferSet);
// vbo layout
@@ -49,20 +49,19 @@ void TileSheetPixels::initBufferSet(const NostalgiaGraphic &img, const Nostalgia
reinterpret_cast<void*>(2 * sizeof(float)));
}
ImVec2 TileSheetPixels::pixelSize(const ImVec2 &paneSize) const noexcept {
common::Vec2 TileSheetPixels::pixelSize(const common::Vec2 &paneSize) const noexcept {
const auto [sw, sh] = paneSize;
constexpr float ymod = 0.35f / 10.0f;
const auto xmod = ymod * sh / sw;
return {xmod * m_pixelSizeMod, ymod * m_pixelSizeMod};
}
void TileSheetPixels::setPixelBufferObject(unsigned vertexRow, float x, float y, Color16 color, float *vbo, GLuint *ebo) noexcept {
const auto [xmod, ymod] = pixelSize();
void TileSheetPixels::setPixelBufferObject(const common::Vec2 &paneSize, unsigned vertexRow, float x, float y, Color16 color, float *vbo, GLuint *ebo) noexcept {
const auto [xmod, ymod] = pixelSize(paneSize);
x *= xmod;
y *= -ymod;
x -= 1.0f;
y += 1.0f - ymod;
//std::cout << x << ", " << y << ", " << (x + xmod) << ", " << (y + ymod) << '\n';
const auto r = redf(color), g = greenf(color), b = bluef(color);
// don't worry, these memcpys gets optimized to something much more ideal
const float vertices[VertexVboLength] = {
@@ -79,15 +78,15 @@ void TileSheetPixels::setPixelBufferObject(unsigned vertexRow, float x, float y,
memcpy(ebo, elms, sizeof(elms));
}
void TileSheetPixels::setBufferObjects(const NostalgiaGraphic &img, const NostalgiaPalette &pal, glutils::BufferSet *bg) noexcept {
const auto setPixel = [this, bg, img, pal](std::size_t i, uint8_t p) {
void TileSheetPixels::setBufferObjects(const common::Vec2 &paneSize, const NostalgiaGraphic &img, const NostalgiaPalette &pal, glutils::BufferSet *bg) noexcept {
const auto setPixel = [this, paneSize, bg, img, pal](std::size_t i, uint8_t p) {
const auto color = pal.colors[p];
const auto pt = idxToPt(i, img.columns);
const auto pt = idxToPt(static_cast<int>(i), img.columns);
const auto fx = static_cast<float>(pt.x);
const auto fy = static_cast<float>(pt.y);
const auto vbo = &bg->vertices[i * VertexVboLength];
const auto ebo = &bg->elements[i * VertexEboLength];
setPixelBufferObject(i * VertexVboRows, fx, fy, color, vbo, ebo);
setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo);
};
// set buffer lengths
const auto width = img.columns * TileWidth;