From a8aa0550c9e8e670583e56b707b8b4456af2698d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 6 Mar 2024 11:13:33 +0300 Subject: [PATCH] minor refactor --- src/frontend/hud.cpp | 22 ++++++++++++++-------- src/frontend/hud.h | 1 + src/graphics/Batch2D.cpp | 3 +-- src/graphics/Batch2D.h | 3 +-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 48847ea1..94772963 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -478,6 +478,8 @@ void Hud::draw(const GfxContext& ctx){ const uint width = viewport.getWidth(); const uint height = viewport.getHeight(); + updateElementsPosition(viewport); + uicamera->setFov(height); auto batch = ctx.getBatch2D(); @@ -486,12 +488,9 @@ void Hud::draw(const GfxContext& ctx){ Shader* uishader = assets->getShader("ui"); uishader->use(); uishader->uniformMatrix("u_projview", uicamera->getProjView()); - - hotbarView->setPos(glm::vec2(width/2, height-65)); - hotbarView->setSelected(player->getChosenSlot()); // Crosshair - if (!pause && Events::_cursor_locked && !player->debug) { + if (!pause && !inventoryOpen && !player->debug) { GfxContext chctx = ctx.sub(); chctx.blendMode(blendmode::inversion); auto texture = assets->getTexture("gui/crosshair"); @@ -504,7 +503,13 @@ void Hud::draw(const GfxContext& ctx){ ); batch->flush(); } + batch->flush(); +} +void Hud::updateElementsPosition(const Viewport& viewport) { + const uint width = viewport.getWidth(); + const uint height = viewport.getHeight(); + if (inventoryOpen) { float caWidth = inventoryView ? contentAccess->getSize().x : 0.0f; contentAccessPanel->setPos(glm::vec2(width-caWidth, 0)); @@ -518,10 +523,10 @@ void Hud::draw(const GfxContext& ctx){ )); } } else { - glm::vec2 blockInvSize = secondUI->getSize(); - float invwidth = glm::max(invSize.x, blockInvSize.x); + glm::vec2 secondUISize = secondUI->getSize(); + float invwidth = glm::max(invSize.x, secondUISize.x); int interval = invSize.y > 0.0 ? 5 : 0; - float totalHeight = invSize.y + blockInvSize.y + interval; + float totalHeight = invSize.y + secondUISize.y + interval; if (inventoryView) { inventoryView->setPos(glm::vec2( glm::min(width/2-invwidth/2, width-caWidth-10-invwidth), @@ -535,7 +540,8 @@ void Hud::draw(const GfxContext& ctx){ } } grabbedItemView->setPos(glm::vec2(Events::cursor)); - batch->flush(); + hotbarView->setPos(glm::vec2(width/2, height-65)); + hotbarView->setSelected(player->getChosenSlot()); } bool Hud::isInventoryOpen() const { diff --git a/src/frontend/hud.h b/src/frontend/hud.h index 1cae1c5c..b3bc6c83 100644 --- a/src/frontend/hud.h +++ b/src/frontend/hud.h @@ -107,6 +107,7 @@ class Hud { std::shared_ptr createContentAccess(); std::shared_ptr createHotbar(); + void updateElementsPosition(const Viewport& viewport); void cleanup(); public: Hud(Engine* engine, LevelFrontend* frontend, Player* player); diff --git a/src/graphics/Batch2D.cpp b/src/graphics/Batch2D.cpp index ce9fe3ac..d576a387 100644 --- a/src/graphics/Batch2D.cpp +++ b/src/graphics/Batch2D.cpp @@ -19,12 +19,11 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){ ubyte pixels[] = { 0xFF, 0xFF, 0xFF, 0xFF }; - blank = new Texture(pixels, 1, 1, GL_RGBA); + blank = std::make_unique(pixels, 1, 1, GL_RGBA); _texture = nullptr; } Batch2D::~Batch2D(){ - delete blank; delete[] buffer; } diff --git a/src/graphics/Batch2D.h b/src/graphics/Batch2D.h index 2c6e0e2a..7f83e258 100644 --- a/src/graphics/Batch2D.h +++ b/src/graphics/Batch2D.h @@ -15,10 +15,9 @@ class Batch2D { float* buffer; size_t capacity; std::unique_ptr mesh; + std::unique_ptr blank; size_t index; glm::vec4 color; - - Texture* blank; Texture* _texture; void vertex(