minor refactor

This commit is contained in:
MihailRis 2024-03-06 11:13:33 +03:00
parent e3297a418e
commit a8aa0550c9
4 changed files with 17 additions and 12 deletions

View File

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

View File

@ -107,6 +107,7 @@ class Hud {
std::shared_ptr<InventoryView> createContentAccess();
std::shared_ptr<InventoryView> createHotbar();
void updateElementsPosition(const Viewport& viewport);
void cleanup();
public:
Hud(Engine* engine, LevelFrontend* frontend, Player* player);

View File

@ -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<Texture>(pixels, 1, 1, GL_RGBA);
_texture = nullptr;
}
Batch2D::~Batch2D(){
delete blank;
delete[] buffer;
}

View File

@ -15,10 +15,9 @@ class Batch2D {
float* buffer;
size_t capacity;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Texture> blank;
size_t index;
glm::vec4 color;
Texture* blank;
Texture* _texture;
void vertex(