Skip to content

Commit 17962ee

Browse files
author
Sascha Brandt
committed
allow manual setting for GUI screen size (for DPI scaling)
1 parent 119baec commit 17962ee

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

Base/Draw.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct DrawContext {
122122
uint32_t meshOffset = 0;
123123
Geometry::Vec2i position,screenSize;
124124
Geometry::Rect_i scissor;
125+
Geometry::Vec2 scale;
125126

126127
RenderingContext* rc;
127128
Util::Reference<Shader> shader;
@@ -149,6 +150,7 @@ struct DrawContext {
149150
uint32_t meshOffset = 0;
150151
Geometry::Vec2i position,screenSize;
151152
Geometry::Rect_i scissor;
153+
Geometry::Vec2 scale;
152154

153155
GLuint shaderProg = 0;
154156
GLuint vertexBuffer = 0;
@@ -379,7 +381,7 @@ static bool init() {
379381
#ifdef GUI_BACKEND_RENDERING
380382

381383
//! (static)
382-
void Draw::beginDrawing(Rendering::RenderingContext& rc, const Geometry::Vec2i & screenSize) {
384+
void Draw::beginDrawing(Rendering::RenderingContext& rc, const Geometry::Vec2i & screenSize, const Geometry::Vec2 & renderScale) {
383385
ctxt.rc = &rc;
384386

385387
static bool initialized = false;
@@ -391,6 +393,7 @@ void Draw::beginDrawing(Rendering::RenderingContext& rc, const Geometry::Vec2i &
391393
ctxt.position = Geometry::Vec2(0,0);
392394
ctxt.activeTexture = nullptr;
393395
ctxt.screenSize = screenSize;
396+
ctxt.scale = renderScale;
394397
ctxt.meshOffset = 0;
395398

396399
rc.pushAndSetDepthBuffer(DepthBufferParameters(false, false, Comparison::ALWAYS));
@@ -401,7 +404,7 @@ void Draw::beginDrawing(Rendering::RenderingContext& rc, const Geometry::Vec2i &
401404
rc.pushScissor();
402405
resetScissor();
403406
rc.pushAndSetShader(ctxt.shader.get());
404-
ctxt.shader->setUniform(rc, Uniform(UNIFORM_SCREEN_SCALE, Geometry::Vec2(2.0f/screenSize.getWidth(),-2.0f/screenSize.getHeight())));
407+
ctxt.shader->setUniform(rc, Uniform(UNIFORM_SCREEN_SCALE, Geometry::Vec2(2.0f/screenSize.getWidth(),-2.0f/screenSize.getHeight()) * ctxt.scale));
405408

406409
GET_GL_ERROR();
407410
}
@@ -507,9 +510,11 @@ void Draw::flush() {
507510
#ifdef GUI_BACKEND_RENDERING
508511
BlendingParameters blending(BlendingParameters::SRC_ALPHA, BlendingParameters::ONE_MINUS_SRC_ALPHA);
509512
ctxt.mesh->openVertexData().markAsChanged();
513+
float yOffset = static_cast<float>(ctxt.screenSize.y()) * ctxt.scale.y() - static_cast<float>(ctxt.screenSize.getHeight());
510514
for(const auto& cmd : ctxt.commands) {
511515
ctxt.shader->setUniform(*ctxt.rc, {UNIFORM_POS_OFFSET, cmd.offset});
512-
ctxt.rc->setScissor(ScissorParameters(cmd.scissor));
516+
Geometry::Rect scissor(cmd.scissor.getX() * ctxt.scale.x(), cmd.scissor.getY() * ctxt.scale.y() - yOffset, cmd.scissor.getWidth() * ctxt.scale.x(), cmd.scissor.getHeight() * ctxt.scale.y());
517+
ctxt.rc->setScissor(ScissorParameters(Geometry::Rect_i(scissor)));
513518
if(cmd.blending)
514519
blending.enable();
515520
else

Base/Draw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Draw {
2929
public:
3030
// general
3131
#ifdef GUI_BACKEND_RENDERING
32-
GUIAPI static void beginDrawing(Rendering::RenderingContext& rc, const Geometry::Vec2i & screenSize);
32+
GUIAPI static void beginDrawing(Rendering::RenderingContext& rc, const Geometry::Vec2i & screenSize, const Geometry::Vec2 & renderScale={1.0f,1.0f});
3333
GUIAPI static Rendering::RenderingContext& getRenderingContext();
3434
#else // GUI_BACKEND_RENDERING
3535
GUIAPI static void beginDrawing(const Geometry::Vec2i & screenSize);

GUI_Manager.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ Rect GUI_Manager::getScreenRect()const{
293293
return globalContainer->getLocalRect();
294294
}
295295

296+
void GUI_Manager::setScreenSize(const Geometry::Vec2& size) {
297+
globalContainer->setSize(size);
298+
}
299+
296300
// ------------------------------------------------------------------------
297301
// Event handling & Listener
298302
bool GUI_Manager::isCtrlPressed() const {
@@ -458,8 +462,9 @@ void GUI_Manager::display()
458462

459463
#ifdef GUI_BACKEND_RENDERING
460464
Geometry::Rect_i viewport = rc.getViewport();
461-
globalContainer->setSize(static_cast<float>(viewport.getWidth()), static_cast<float>(viewport.getHeight()));
462-
Draw::beginDrawing(rc, Geometry::Vec2i(viewport.getWidth(),viewport.getHeight()));
465+
//globalContainer->setSize(static_cast<float>(viewport.getWidth()), static_cast<float>(viewport.getHeight()));
466+
Geometry::Vec2 renderScale(static_cast<float>(viewport.getWidth()) / globalContainer->getWidth(), static_cast<float>(viewport.getHeight()) / globalContainer->getHeight());
467+
Draw::beginDrawing(rc, Geometry::Vec2i(viewport.getWidth(),viewport.getHeight()), renderScale);
463468
#else // GUI_BACKEND_RENDERING
464469
Geometry::Rect_i viewport = Draw::queryViewport();
465470
globalContainer->setSize(static_cast<float>(viewport.getWidth()), static_cast<float>(viewport.getHeight()));

GUI_Manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class GUI_Manager {
105105
GUIAPI void display();
106106
#endif // GUI_BACKEND_RENDERING
107107
GUIAPI Geometry::Rect getScreenRect()const;
108+
GUIAPI void setScreenSize(const Geometry::Vec2& size);
108109

109110
//! Associate a window (e.g. X11 or SDL) to the GUI manager
110111
void setWindow(Util::UI::Window * newWindow) {

0 commit comments

Comments
 (0)