Skip to content

Commit 520c8cf

Browse files
Simplified GetWindowScaleDPI() so it does not fetch the wrong DPI scale some times (Windows looks at center of window while the old raylib code looked on upper left corner of window, now it just uses the glfw function to fetch the window's current scaling). Also introduced a callback to update the CORE.Window.screenScaling when the content scaling updates, previously scaling did not work correctly on systems with multiple monitors that have different DPI scaling. (#3701)
1 parent b7141d5 commit 520c8cf

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

src/platforms/rcore_desktop.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ static void WindowIconifyCallback(GLFWwindow *window, int iconified);
123123
static void WindowMaximizeCallback(GLFWwindow* window, int maximized); // GLFW3 Window Maximize Callback, runs when window is maximized
124124
static void WindowFocusCallback(GLFWwindow *window, int focused); // GLFW3 WindowFocus Callback, runs when window get/lose focus
125125
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths); // GLFW3 Window Drop Callback, runs when drop files into window
126+
static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley); // GLFW3 Window Content Scale Callback, runs when window changes scale
126127

127128
// Input callbacks events
128129
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); // GLFW3 Keyboard Callback, runs on key pressed
@@ -941,31 +942,8 @@ Vector2 GetWindowPosition(void)
941942
// Get window scale DPI factor for current monitor
942943
Vector2 GetWindowScaleDPI(void)
943944
{
944-
float xdpi = 1.0;
945-
float ydpi = 1.0;
946-
Vector2 scale = { 1.0f, 1.0f };
947-
Vector2 windowPos = GetWindowPosition();
948-
949-
int monitorCount = 0;
950-
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
951-
952-
// Check window monitor
953-
for (int i = 0; i < monitorCount; i++)
954-
{
955-
glfwGetMonitorContentScale(monitors[i], &xdpi, &ydpi);
956-
957-
int xpos, ypos, width, height;
958-
glfwGetMonitorWorkarea(monitors[i], &xpos, &ypos, &width, &height);
959-
960-
if ((windowPos.x >= xpos) && (windowPos.x < xpos + width) &&
961-
(windowPos.y >= ypos) && (windowPos.y < ypos + height))
962-
{
963-
scale.x = xdpi;
964-
scale.y = ydpi;
965-
break;
966-
}
967-
}
968-
945+
Vector2 scale = {0};
946+
glfwGetWindowContentScale(platform.handle, &scale.x, &scale.y);
969947
return scale;
970948
}
971949

@@ -1553,6 +1531,11 @@ int InitPlatform(void)
15531531
glfwSetWindowFocusCallback(platform.handle, WindowFocusCallback);
15541532
glfwSetDropCallback(platform.handle, WindowDropCallback);
15551533

1534+
if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
1535+
{
1536+
glfwSetWindowContentScaleCallback(platform.handle, WindowContentScaleCallback);
1537+
}
1538+
15561539
// Set input callback events
15571540
glfwSetKeyCallback(platform.handle, KeyCallback);
15581541
glfwSetCharCallback(platform.handle, CharCallback);
@@ -1638,6 +1621,11 @@ static void WindowSizeCallback(GLFWwindow *window, int width, int height)
16381621
// NOTE: Postprocessing texture is not scaled to new size
16391622
}
16401623

1624+
static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley)
1625+
{
1626+
CORE.Window.screenScale = MatrixScale(scalex, scaley, 1.0f);
1627+
}
1628+
16411629
// GLFW3 WindowIconify Callback, runs when window is minimized/restored
16421630
static void WindowIconifyCallback(GLFWwindow *window, int iconified)
16431631
{

src/platforms/rcore_web.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ void ClosePlatform(void); // Close platform
110110
static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
111111

112112
// Window callbacks events
113-
static void WindowSizeCallback(GLFWwindow *window, int width, int height); // GLFW3 WindowSize Callback, runs when window is resized
114-
static void WindowIconifyCallback(GLFWwindow *window, int iconified); // GLFW3 WindowIconify Callback, runs when window is minimized/restored
115-
//static void WindowMaximizeCallback(GLFWwindow *window, int maximized); // GLFW3 Window Maximize Callback, runs when window is maximized
116-
static void WindowFocusCallback(GLFWwindow *window, int focused); // GLFW3 WindowFocus Callback, runs when window get/lose focus
117-
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths); // GLFW3 Window Drop Callback, runs when drop files into window
113+
static void WindowSizeCallback(GLFWwindow *window, int width, int height); // GLFW3 WindowSize Callback, runs when window is resized
114+
static void WindowIconifyCallback(GLFWwindow *window, int iconified); // GLFW3 WindowIconify Callback, runs when window is minimized/restored
115+
//static void WindowMaximizeCallback(GLFWwindow *window, int maximized); // GLFW3 Window Maximize Callback, runs when window is maximized
116+
static void WindowFocusCallback(GLFWwindow *window, int focused); // GLFW3 WindowFocus Callback, runs when window get/lose focus
117+
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths); // GLFW3 Window Drop Callback, runs when drop files into window
118+
static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley); // GLFW3 Window Content Scale Callback, runs when window changes scale
118119

119120
// Input callbacks events
120121
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); // GLFW3 Keyboard Callback, runs on key pressed
@@ -1190,6 +1191,11 @@ int InitPlatform(void)
11901191
glfwSetWindowFocusCallback(platform.handle, WindowFocusCallback);
11911192
glfwSetDropCallback(platform.handle, WindowDropCallback);
11921193

1194+
if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
1195+
{
1196+
glfwSetWindowContentScaleCallback(platform.handle, WindowContentScaleCallback);
1197+
}
1198+
11931199
// Set input callback events
11941200
glfwSetKeyCallback(platform.handle, KeyCallback);
11951201
glfwSetCharCallback(platform.handle, CharCallback);
@@ -1327,6 +1333,11 @@ static void WindowSizeCallback(GLFWwindow *window, int width, int height)
13271333
// NOTE: Postprocessing texture is not scaled to new size
13281334
}
13291335

1336+
static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley)
1337+
{
1338+
CORE.Window.screenScale = MatrixScale(scalex, scaley, 1.0f);
1339+
}
1340+
13301341
// GLFW3 WindowIconify Callback, runs when window is minimized/restored
13311342
static void WindowIconifyCallback(GLFWwindow *window, int iconified)
13321343
{

0 commit comments

Comments
 (0)