From aa43dc9575e6000fa84ad6f342b26958b22e6d7e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 26 May 2024 00:00:44 -0500 Subject: [PATCH] [jasper/world/studio] Simplify CollisionMapView with MapTileHighlighter --- .../worldobjectseteditor/collisionmapview.cpp | 105 +----------------- .../worldobjectseteditor/collisionmapview.hpp | 13 +-- 2 files changed, 8 insertions(+), 110 deletions(-) diff --git a/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.cpp b/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.cpp index 6b4ad32..6fed321 100644 --- a/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.cpp +++ b/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.cpp @@ -11,56 +11,19 @@ CollisionMap mapIdx(auto x, auto y) noexcept { + static_cast(x); } -const glutils::ProgramSource CollisionView::s_programSrc = { - .shaderParams = { - { - .len = 2, - .name = ox::String("vPosition"), - }, - { - .len = 1, - .name = ox::String("vSelection"), - }, - }, - .vertShader = ox::sfmt(R"( - {} - in vec2 vPosition; - in float vSelection; - out float fSelection; - void main() { - gl_Position = vec4(vPosition, 0.0, 1.0); - fSelection = vSelection; - })", ncore::gl::GlslVersion), - .fragShader = ox::sfmt(R"( - {} - in float fSelection; - out vec4 outColor; - void main() { - outColor = vec4(0.0, 0.7, 1.0, 0.4) * fSelection; - })", ncore::gl::GlslVersion), -}; - CollisionView::CollisionView(studio::StudioContext &sctx): m_nctx(ncore::init(sctx.tctx, { .glInstallDrawer = false, .glSpriteCount = 0, }).unwrapThrow()), m_frameBuffer(glutils::generateFrameBuffer(240 * s_scale, 160 * s_scale)) { - m_bufferSet.vao = glutils::generateVertexArrayObject(); - m_bufferSet.vbo = glutils::generateBuffer(); - m_bufferSet.ebo = glutils::generateBuffer(); - glBindVertexArray(m_bufferSet.vao); - sendVbo(m_bufferSet); - sendEbo(m_bufferSet); - m_shader = glutils::buildShaderProgram(s_programSrc).unwrapThrow(); - glBindVertexArray(0); } ox::Error CollisionView::setup( ox::FileAddress const&tsAddr, ox::FileAddress const&palAddr, int const w, - int h, + int const h, uint_t tile, CollisionMap colMap) noexcept { m_subsheetTilesWidth = w; @@ -69,6 +32,7 @@ ox::Error CollisionView::setup( ncore::setBgStatus(*m_nctx, 0, true); oxReturnError(ncore::loadBgTileSheet(*m_nctx, 0, tsAddr)); oxReturnError(ncore::loadBgPalette(*m_nctx, 0, palAddr)); + oxReturnError(m_highlighter.setup({w / 2, h / 2})); buildGlBuffers(colMap); return {}; } @@ -76,17 +40,7 @@ ox::Error CollisionView::setup( void CollisionView::draw() noexcept { glutils::FrameBufferBind const frameBufferBind(m_frameBuffer); ncore::gl::draw(*m_nctx, s_scale); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glUseProgram(m_shader); - glBindVertexArray(m_bufferSet.vao); - sendVbo(m_bufferSet); - sendEbo(m_bufferSet); - auto const elmCnt = static_cast(m_bufferSet.elements.size()); - glDrawElements(GL_TRIANGLES, elmCnt, GL_UNSIGNED_INT, nullptr); - glBindVertexArray(0); - glUseProgram(0); - glDisable(GL_BLEND); + m_highlighter.draw(); } int CollisionView::scale() const noexcept { @@ -97,34 +51,6 @@ glutils::FrameBuffer const&CollisionView::framebuffer() const noexcept { return m_frameBuffer; } -void CollisionView::setPixelBufferObject( - unsigned vertexRow, - float x, float y, - bool const selected, - float *vbo, - GLuint *ebo) const noexcept { - auto constexpr xmod = static_cast(ncore::TileWidth) / 240.f * 4; - auto constexpr ymod = static_cast(ncore::TileHeight) / 160.f * 4; - x *= xmod; - y *= -ymod; - x -= 1.0f; - y += 1.0f - ymod; - auto const selection = 1.f * static_cast(selected); - // don't worry, these memcpys gets optimized to something much more ideal - std::array const vertices{ - x, y, selection, // bottom left - x + xmod, y, selection, // bottom right - x + xmod, y + ymod, selection, // top right - x, y + ymod, selection, // top left - }; - memcpy(vbo, vertices.data(), sizeof(vertices)); - ox::Array const elms{ - vertexRow + 0, vertexRow + 1, vertexRow + 2, - vertexRow + 2, vertexRow + 3, vertexRow + 0, - }; - memcpy(ebo, elms.data(), sizeof(elms)); -} - bool CollisionView::click(ox::Vec2 const&pos, CollisionMap &colMap) noexcept { auto const inMap = colMap; auto const x = static_cast(pos.x * static_cast(m_subsheetTilesWidth)) / 2; @@ -138,36 +64,15 @@ bool CollisionView::click(ox::Vec2 const&pos, CollisionMap &colMap) noexcept { } void CollisionView::buildGlBuffers(CollisionMap colMap) noexcept { - auto const vboLength = static_cast(s_programSrc.rowLen) * 4; - auto constexpr eboLength = 6; - auto const tileCnt = - static_cast(m_subsheetTilesWidth) * static_cast(m_subsheetTilesHeight); - m_bufferSet.vertices.resize(tileCnt * vboLength); - m_bufferSet.elements.resize(tileCnt * eboLength); - size_t i = 0; auto tile = m_sheetTileStart; for (auto y = 0; y < m_subsheetTilesHeight; ++y) { for (auto x = 0; x < m_subsheetTilesWidth; ++x) { ncore::setBgTile(*m_nctx, 0, x, y, tile); - auto const vbo = &m_bufferSet.vertices[i * vboLength]; - auto const ebo = &m_bufferSet.elements[i * eboLength]; - setPixelBufferObject( - static_cast(i * 4), - static_cast(x), - static_cast(y), - (colMap >> mapIdx(x, y)) & 1, - vbo, - ebo); - ++i; + auto const collidable = (colMap >> mapIdx(x, y)) & 1; + std::ignore = m_highlighter.setTileHighlight({x, y}, collidable); ++tile; } } - glUseProgram(m_shader); - glBindVertexArray(m_bufferSet.vao); - sendVbo(m_bufferSet); - sendEbo(m_bufferSet); - glBindVertexArray(0); - glUseProgram(0); } } diff --git a/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.hpp b/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.hpp index 9f7e8a4..29632de 100644 --- a/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.hpp +++ b/src/jasper/modules/world/src/studio/worldobjectseteditor/collisionmapview.hpp @@ -11,21 +11,21 @@ #include +#include "../maptilehighlighter.hpp" + namespace jasper::world { namespace ncore = nostalgia::core; class CollisionView { private: - static const glutils::ProgramSource s_programSrc; static constexpr int s_scale = 5; ncore::ContextUPtr m_nctx; glutils::FrameBuffer m_frameBuffer; - glutils::GLProgram m_shader; - glutils::BufferSet m_bufferSet; uint_t m_sheetTileStart{}; int m_subsheetTilesWidth{}; int m_subsheetTilesHeight{}; + MapTileHighlighter m_highlighter; public: CollisionView(studio::StudioContext &sctx); @@ -46,13 +46,6 @@ class CollisionView { [[nodiscard]] glutils::FrameBuffer const&framebuffer() const noexcept; - void setPixelBufferObject( - unsigned vertexRow, - float x, float y, - bool selected, - float *vbo, - GLuint *ebo) const noexcept; - /** * @return true if colMap changes */