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
+5 -13
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) {
+6 -4
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);
+3 -4
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();
}
-16
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;
}
-24
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);
}
};
+1 -2
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();
+3 -5
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 *
+3 -5
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);
+4 -4
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;
}
+1 -1
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();
+7 -8
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();
}
+1 -2
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
+2 -2
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();