From 89584d2a68b88c239f37ecd39175eb51dd4a1fb1 Mon Sep 17 00:00:00 2001 From: Dayvid Albuquerque Date: Tue, 30 Dec 2025 22:51:04 -0300 Subject: [PATCH] support display scaling... --- game-window/src/window_glfw.cpp | 36 +++++++++++++++++++-------------- game-window/src/window_glfw.h | 6 ++++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/game-window/src/window_glfw.cpp b/game-window/src/window_glfw.cpp index 6680d80..91911ba 100644 --- a/game-window/src/window_glfw.cpp +++ b/game-window/src/window_glfw.cpp @@ -5,9 +5,10 @@ #include #include -#include #include +#include #include +#include #include "../../logger/include/log.h" #include "GLFW/glfw3.h" @@ -76,18 +77,16 @@ void GLFWGameWindow::setIcon(std::string const &iconPath) void GLFWGameWindow::setRelativeScale() { - int fx, fy; - glfwGetFramebufferSize(window, &fx, &fy); + float xs, ys; + glfwGetWindowContentScale(window, &xs, &ys); - int wx, wy; - glfwGetWindowSize(window, &wx, &wy); - - relativeScale = (int)floor(((fx / wx) + (fy / wy)) / 2); + xScale = xs; + yScale = ys; } -int GLFWGameWindow::getRelativeScale() const +std::pair GLFWGameWindow::getRelativeScale() const { - return relativeScale; + return std::make_pair(xScale, yScale); } void GLFWGameWindow::getWindowSize(int &width, int &height) const @@ -178,7 +177,7 @@ void GLFWGameWindow::swapBuffers() numFrames = 0; lastTimePerformance = currentTime; } - + if (limitFpsMode == LimitFPSMode::Limited) { while (glfwGetTime() < (lastTimeTargetFPS + 1.0 / targetFPS)) @@ -218,8 +217,13 @@ void GLFWGameWindow::_glfwCursorPosCallback(GLFWwindow *window, double x, if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) { +#ifdef __linux__ + double dx = x - user->lastMouseX; + double dy = y - user->lastMouseY; +#else double dx = (x - user->lastMouseX) * user->getRelativeScale(); double dy = (y - user->lastMouseY) * user->getRelativeScale(); +#endif user->onMouseRelativePosition(dx, dy); user->lastMouseX = x; @@ -227,8 +231,8 @@ void GLFWGameWindow::_glfwCursorPosCallback(GLFWwindow *window, double x, } else { - x *= user->getRelativeScale(); - y *= user->getRelativeScale(); + x *= user->xScale; + y *= user->yScale; user->onMousePosition(x, y); } @@ -241,8 +245,8 @@ void GLFWGameWindow::_glfwMouseButtonCallback(GLFWwindow *window, int button, double x, y; glfwGetCursorPos(window, &x, &y); - x *= user->getRelativeScale(); - y *= user->getRelativeScale(); + x *= user->xScale; + y *= user->yScale; user->onMouseButton(x, y, button + 1, action == GLFW_PRESS ? MouseButtonAction::PRESS @@ -368,7 +372,9 @@ void GLFWGameWindow::_glfwKeyCallback(GLFWwindow *window, int key, int scancode, void GLFWGameWindow::_glfwCharCallback(GLFWwindow *window, unsigned int ch) { GLFWGameWindow *user = (GLFWGameWindow *)glfwGetWindowUserPointer(window); + std::wstring_convert, char32_t> cvt; + user->onKeyboardText(cvt.to_bytes(ch)); } @@ -391,4 +397,4 @@ void GLFWGameWindow::_glfwWindowContentScaleCallback(GLFWwindow *window, { GLFWGameWindow *user = (GLFWGameWindow *)glfwGetWindowUserPointer(window); user->setRelativeScale(); -} \ No newline at end of file +} diff --git a/game-window/src/window_glfw.h b/game-window/src/window_glfw.h index 34729a0..e4fa2fe 100644 --- a/game-window/src/window_glfw.h +++ b/game-window/src/window_glfw.h @@ -3,6 +3,8 @@ #include #include +#include + class GLFWGameWindow : public GameWindow { private: @@ -13,7 +15,7 @@ class GLFWGameWindow : public GameWindow double lastMouseX = 0.0, lastMouseY = 0.0; int windowedX = -1, windowedY = -1; int windowedWidth = -1, windowedHeight = -1; - int relativeScale; + float xScale, yScale; bool focused = true; friend class GLFWJoystickManager; @@ -41,7 +43,7 @@ class GLFWGameWindow : public GameWindow void setIcon(std::string const &iconPath) override; - int getRelativeScale() const; + std::pair getRelativeScale() const; void setRelativeScale();