remove Viewport class

This commit is contained in:
MihailRis 2025-04-02 17:35:17 +03:00
parent 105561ad77
commit 9843a1fc27
17 changed files with 60 additions and 115 deletions

View File

@ -211,7 +211,7 @@ void Engine::updateFrontend() {
double delta = time.getDelta();
updateHotkeys();
audio::update(delta);
gui->act(delta, Viewport(window->getSize()));
gui->act(delta, window->getSize());
screen->update(delta);
gui->postAct();
}
@ -229,7 +229,6 @@ void Engine::nextFrame() {
void Engine::renderFrame() {
screen->draw(time.getDelta());
Viewport viewport(window->getSize());
DrawContext ctx(nullptr, *window, nullptr);
gui->draw(ctx, *assets);
}

View File

@ -591,9 +591,7 @@ void Hud::setDebug(bool flag) {
}
void Hud::draw(const DrawContext& ctx){
const Viewport& viewport = ctx.getViewport();
const uint width = viewport.getWidth();
const uint height = viewport.getHeight();
const auto& viewport = ctx.getViewport();
bool is_menu_open = menu.hasOpenPage();
darkOverlay->setVisible(is_menu_open);
@ -601,8 +599,8 @@ void Hud::draw(const DrawContext& ctx){
updateElementsPosition(viewport);
uicamera->setFov(height);
uicamera->setAspectRatio(viewport.getRatio());
uicamera->setFov(viewport.y);
uicamera->setAspectRatio(viewport.x / static_cast<float>(viewport.y));
auto batch = ctx.getBatch2D();
batch->begin();
@ -620,28 +618,28 @@ void Hud::draw(const DrawContext& ctx){
int chsizex = texture != nullptr ? texture->getWidth() : 16;
int chsizey = texture != nullptr ? texture->getHeight() : 16;
batch->rect(
(width-chsizex)/2, (height-chsizey)/2,
chsizex, chsizey, 0,0, 1,1, 1,1,1,1
(viewport.x - chsizex) / 2, (viewport.y - chsizey) / 2,
chsizex, chsizey, 0, 0, 1, 1, 1, 1, 1, 1
);
}
}
void Hud::updateElementsPosition(const Viewport& viewport) {
const uint width = viewport.getWidth();
const uint height = viewport.getHeight();
void Hud::updateElementsPosition(const glm::uvec2& viewport) {
if (inventoryOpen) {
float caWidth = inventoryView && showContentPanel
? contentAccess->getSize().x
: 0.0f;
contentAccessPanel->setPos(glm::vec2(width-caWidth, 0));
contentAccessPanel->setPos(glm::vec2(viewport.x - caWidth, 0));
glm::vec2 invSize = inventoryView ? inventoryView->getSize() : glm::vec2();
if (secondUI == nullptr) {
if (inventoryView) {
inventoryView->setPos(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-invSize.y/2
glm::min(
viewport.x / 2 - invSize.x / 2,
viewport.x - caWidth - 10 - invSize.x
),
viewport.y / 2 - invSize.y / 2
));
}
} else {
@ -651,17 +649,21 @@ void Hud::updateElementsPosition(const Viewport& viewport) {
float totalHeight = invSize.y + secondUISize.y + interval;
if (inventoryView) {
inventoryView->setPos(glm::vec2(
glm::min(width/2-invwidth/2, width-caWidth-10-invwidth),
height/2+totalHeight/2-invSize.y
glm::min(
viewport.x / 2 - invwidth / 2,
viewport.x - caWidth - 10 - invwidth
),
viewport.y / 2 + totalHeight / 2 - invSize.y
));
}
if (secondUI->getPositionFunc() == nullptr) {
secondUI->setPos(glm::vec2(
glm::min(
width / 2.f - invwidth / 2.f,
width - caWidth - (inventoryView ? 10 : 0) - invwidth
viewport.x / 2.f - invwidth / 2.f,
viewport.x - caWidth - (inventoryView ? 10 : 0) -
invwidth
),
height / 2.f - totalHeight / 2.f
viewport.y / 2.f - totalHeight / 2.f
));
}
}
@ -669,7 +671,7 @@ void Hud::updateElementsPosition(const Viewport& viewport) {
if (exchangeSlot != nullptr) {
exchangeSlot->setPos(input.getCursor().pos);
}
hotbarView->setPos(glm::vec2(width/2, height-65));
hotbarView->setPos(glm::vec2(viewport.x / 2, viewport.y - 65));
hotbarView->setSelected(player.getChosenSlot());
}

View File

@ -18,7 +18,6 @@ class Inventory;
class LevelFrontend;
class UiDocument;
class DrawContext;
class Viewport;
class ImageData;
class Input;
@ -131,7 +130,7 @@ class Hud : public util::ObjectsKeeper {
std::shared_ptr<gui::InventoryView> createHotbar();
void processInput(bool visible);
void updateElementsPosition(const Viewport& viewport);
void updateElementsPosition(const glm::uvec2& viewport);
void updateHotbarControl();
void cleanup();

View File

@ -13,7 +13,6 @@
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/ImageData.hpp"
#include "graphics/core/PostProcessing.hpp"
#include "graphics/core/Viewport.hpp"
#include "graphics/render/Decorator.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/ui/GUI.hpp"

View File

@ -29,7 +29,7 @@ DrawContext::DrawContext(
Batch2D* g2d
) : window(window),
parent(parent),
viewport({window.getSize()}),
viewport(window.getSize()),
g2d(g2d),
flushable(g2d)
{}
@ -55,11 +55,7 @@ DrawContext::~DrawContext() {
}
}
glViewport(
0, 0,
parent->viewport.getWidth(),
parent->viewport.getHeight()
);
glViewport(0, 0, parent->viewport.x, parent->viewport.y);
if (depthMask != parent->depthMask) {
glDepthMask(parent->depthMask);
@ -80,7 +76,7 @@ DrawContext::~DrawContext() {
}
}
const Viewport& DrawContext::getViewport() const {
const glm::uvec2& DrawContext::getViewport() const {
return viewport;
}
@ -99,13 +95,9 @@ DrawContext DrawContext::sub(Flushable* flushable) const {
return ctx;
}
void DrawContext::setViewport(const Viewport& viewport) {
void DrawContext::setViewport(const glm::uvec2& viewport) {
this->viewport = viewport;
glViewport(
0, 0,
viewport.getWidth(),
viewport.getHeight()
);
glViewport(0, 0, viewport.x, viewport.y);
}
void DrawContext::setFramebuffer(Framebuffer* fbo) {

View File

@ -1,7 +1,9 @@
#pragma once
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>
#include "commons.hpp"
#include "Viewport.hpp"
#include "typedefs.hpp"
class Window;
@ -11,7 +13,7 @@ class Framebuffer;
class DrawContext {
Window& window;
const DrawContext* parent;
Viewport viewport;
glm::uvec2 viewport;
Batch2D* g2d;
Flushable* flushable = nullptr;
Framebuffer* fbo = nullptr;
@ -31,10 +33,10 @@ public:
Batch2D* getBatch2D() const;
const Viewport& getViewport() const;
const glm::uvec2& getViewport() const;
DrawContext sub(Flushable* flushable=nullptr) const;
void setViewport(const Viewport& viewport);
void setViewport(const glm::uvec2& viewport);
void setFramebuffer(Framebuffer* fbo);
void setDepthMask(bool flag);
void setDepthTest(bool flag);

View File

@ -3,7 +3,6 @@
#include "Shader.hpp"
#include "Texture.hpp"
#include "Framebuffer.hpp"
#include "Viewport.hpp"
#include "DrawContext.hpp"
#include <stdexcept>
@ -23,9 +22,9 @@ PostProcessing::~PostProcessing() = default;
void PostProcessing::use(DrawContext& context) {
const auto& vp = context.getViewport();
if (fbo) {
fbo->resize(vp.getWidth(), vp.getHeight());
fbo->resize(vp.x, vp.y);
} else {
fbo = std::make_unique<Framebuffer>(vp.getWidth(), vp.getHeight());
fbo = std::make_unique<Framebuffer>(vp.x, vp.y);
}
context.setFramebuffer(fbo.get());
}
@ -37,7 +36,7 @@ void PostProcessing::render(const DrawContext& context, Shader* screenShader) {
const auto& viewport = context.getViewport();
screenShader->use();
screenShader->uniform2i("u_screenSize", viewport.size());
screenShader->uniform2i("u_screenSize", viewport);
fbo->getTexture()->bind();
quadMesh->draw();
}

View File

@ -1,16 +0,0 @@
#include "Viewport.hpp"
Viewport::Viewport(uint width, uint height)
: width(width), height(height) {
}
Viewport::Viewport(const glm::ivec2& size) : width(size.x), height(size.y) {
}
uint Viewport::getWidth() const {
return width;
}
uint Viewport::getHeight() const {
return height;
}

View File

@ -1,24 +0,0 @@
#pragma once
#include <glm/glm.hpp>
#include "typedefs.hpp"
class Viewport {
uint width;
uint height;
public:
Viewport(uint width, uint height);
Viewport(const glm::ivec2& size);
virtual uint getWidth() const;
virtual uint getHeight() const;
glm::ivec2 size() const {
return glm::ivec2(width, height);
}
float getRatio() const {
return width / static_cast<float>(height);
}
};

View File

@ -13,7 +13,6 @@
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/core/Texture.hpp"
#include "graphics/core/Viewport.hpp"
#include "graphics/commons/Model.hpp"
#include <glm/ext.hpp>
@ -127,7 +126,7 @@ std::unique_ptr<Atlas> BlocksPreview::build(
glm::vec3(0, 1, 0)));
AtlasBuilder builder;
ctx.setViewport(Viewport(iconSize, iconSize));
ctx.setViewport({iconSize, iconSize});
display::setBgColor(glm::vec4(0.0f));
fbo.bind();

View File

@ -64,8 +64,6 @@ void GuidesRenderer::renderDebugLines(
) {
DrawContext ctx = pctx.sub(&batch);
const auto& viewport = ctx.getViewport();
uint displayWidth = viewport.getWidth();
uint displayHeight = viewport.getHeight();
ctx.setDepthTest(true);
@ -91,15 +89,15 @@ void GuidesRenderer::renderDebugLines(
}
float length = 40.f;
glm::vec3 tsl(displayWidth / 2, displayHeight / 2, 0.f);
glm::vec3 tsl(viewport.x / 2, viewport.y / 2, 0.f);
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
linesShader.uniformMatrix(
"u_projview",
glm::ortho(
0.f,
static_cast<float>(displayWidth),
static_cast<float>(viewport.x),
0.f,
static_cast<float>(displayHeight),
static_cast<float>(viewport.y),
-length,
length
) * model *

View File

@ -101,11 +101,9 @@ void Skybox::draw(
float daytime,
float fog)
{
const Viewport& viewport = pctx.getViewport();
int width = viewport.getWidth();
int height = viewport.getHeight();
const glm::uvec2& viewport = pctx.getViewport();
drawBackground(camera, assets, width, height);
drawBackground(camera, assets, viewport.x, viewport.y);
DrawContext ctx = pctx.sub();
ctx.setBlendMode(BlendMode::addition);
@ -145,7 +143,7 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
ctx.setDepthMask(false);
ctx.setDepthTest(false);
ctx.setFramebuffer(fbo.get());
ctx.setViewport(Viewport(size, size));
ctx.setViewport({size, size});
auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture());
assert(cubemap != nullptr);

View File

@ -85,14 +85,14 @@ void TextsRenderer::renderNote(
}
pos /= projpos.w;
pos.z = 0;
xvec = {2.0f / viewport.getWidth() * scale, 0, 0};
yvec = {0, 2.0f / viewport.getHeight() * scale, 0};
xvec = {2.0f / viewport.x * scale, 0, 0};
yvec = {0, 2.0f / viewport.y * scale, 0};
} else {
auto matrix = camera.getProjView();
auto screenPos = matrix * glm::vec4(pos, 1.0f);
xvec = glm::vec3(2.0f / viewport.getWidth() * scale, 0, 0);
yvec = glm::vec3(0, 2.0f / viewport.getHeight() * scale, 0);
xvec = glm::vec3(2.0f / viewport.x * scale, 0, 0);
yvec = glm::vec3(0, 2.0f / viewport.y * scale, 0);
pos = screenPos / screenPos.w;
}

View File

@ -339,7 +339,7 @@ void WorldRenderer::draw(
auto world = level.getWorld();
const auto& vp = pctx.getViewport();
camera.setAspectRatio(vp.getRatio());
camera.setAspectRatio(vp.x / static_cast<float>(vp.y));
const auto& settings = engine.getSettings();
const auto& worldInfo = world->getInfo();

View File

@ -197,8 +197,8 @@ void GUI::actFocused() {
}
}
void GUI::act(float delta, const Viewport& vp) {
container->setSize(vp.size());
void GUI::act(float delta, const glm::uvec2& vp) {
container->setSize(vp);
container->act(delta);
auto prevfocus = focus;
@ -233,7 +233,6 @@ void GUI::draw(const DrawContext& pctx, const Assets& assets) {
auto ctx = pctx.sub(batch2D.get());
auto& viewport = ctx.getViewport();
glm::vec2 wsize = viewport.size();
auto& page = menu->getCurrent();
if (page.panel) {
@ -243,9 +242,9 @@ void GUI::draw(const DrawContext& pctx, const Assets& assets) {
panel->cropToContent();
}
}
menu->setPos((wsize - menu->getSize()) / 2.0f);
uicamera->setFov(wsize.y);
uicamera->setAspectRatio(viewport.getRatio());
menu->setPos((glm::vec2(viewport) - menu->getSize()) / 2.0f);
uicamera->setFov(viewport.y);
uicamera->setAspectRatio(viewport.x / static_cast<float>(viewport.y));
auto uishader = assets.get<Shader>("ui");
uishader->use();
@ -277,11 +276,11 @@ void GUI::draw(const DrawContext& pctx, const Assets& assets) {
batch2D->untexture();
auto node = hover->getParent();
while (node) {
auto pos = node->calcPos();
auto parentPos = node->calcPos();
auto size = node->getSize();
batch2D->setColor(0, 255, 255);
batch2D->lineRect(pos.x, pos.y, size.x-1, size.y-1);
batch2D->lineRect(parentPos.x, parentPos.y, size.x-1, size.y-1);
node = node->getParent();
}

View File

@ -9,7 +9,6 @@
#include <glm/glm.hpp>
#include <unordered_map>
class Viewport;
class DrawContext;
class Assets;
class Camera;
@ -106,7 +105,7 @@ namespace gui {
/// @brief Main input handling and logic update method
/// @param delta delta time
/// @param viewport window size
void act(float delta, const Viewport& viewport);
void act(float delta, const glm::uvec2& viewport);
/// @brief Draw all visible elements on main container
/// @param pctx parent graphics context

View File

@ -237,8 +237,8 @@ void Label::draw(const DrawContext& pctx, const Assets& assets) {
textYOffset = pos.y-calcPos().y;
totalLineHeight = lineHeight;
auto& viewport = pctx.getViewport();
glm::vec4 bounds {0, 0, viewport.getWidth(), viewport.getHeight()};
const auto& viewport = pctx.getViewport();
glm::vec4 bounds {0, 0, viewport.x, viewport.y};
if (parent) {
auto ppos = parent->calcPos();
auto psize = parent->getSize();