Skip to content

[rcore][glfw] SetWindowSize(), SetWindowMinSize(), SetWindowMaxSize(): use logical units with FLAG_WINDOW_HIGHDPI - #6363

Merged
raysan5 merged 1 commit into
raysan5:masterfrom
jqntn:fix-setwindowsize-highdpi
Nov 9, 2026
Merged

raysan5 merged 1 commit into
raysan5:masterfrom
jqntn:fix-setwindowsize-highdpi

Conversation

@jqntn

@jqntn jqntn commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #6349

With FLAG_WINDOW_HIGHDPI on Windows and X11, SetWindowSize() passes the requested size straight to GLFW, so it's treated as physical pixels, while InitWindow() takes logical units. It also stores that size in CORE.Window.screen, which holds logical pixels. FramebufferSizeCallback() corrects it afterwards, but GLFW only calls it when the framebuffer size actually changes. So a same-size SetWindowSize() leaves GetScreenWidth()/GetScreenHeight() in physical pixels until the next resize.

This PR makes SetWindowSize() take logical units, like InitWindow(). The size is multiplied by the DPI scale before being passed to GLFW, using the same calculation and platform check as InitPlatform(), so CORE.Window.screen always holds the logical size. SetWindowMinSize() and SetWindowMaxSize() get the same treatment. macOS and Wayland are left as they are, since the GLFW window size is already logical there. Nothing changes without FLAG_WINDOW_HIGHDPI.

Testing (FLAG_WINDOW_HIGHDPI | FLAG_WINDOW_RESIZABLE):

Step Windows 150%, before after X11 200%, before after
InitWindow(800, 450) 800x450 (1200x675) 800x450 (1200x675) 800x450 (1600x900) 800x450 (1600x900)
SetWindowSize(800, 450) 533x300 (800x450) 800x450 (1200x675) 400x225 (800x450) 800x450 (1600x900)
SetWindowSize(500, 300) 333x200 (500x300) 500x300 (750x450) 250x150 (500x300) 500x300 (1000x600)
SetWindowSize(500, 300) again 500x300 (500x300) 500x300 (750x450) 500x300 (500x300) 500x300 (1000x600)

Values are GetScreenWidth()xGetScreenHeight() (GetRenderWidth()xGetRenderHeight()).

SetWindowMinSize(600, 350) and SetWindowMaxSize(1000, 600) now limit the window to 900x525 and 1500x900 at 150% (600x350 and 1000x600 before). On X11, the size hints are 1200x700 and 2000x1200 at 200%.

  • Windows 11, 150% display scale, MSVC
  • Linux X11, Xvfb with Xft.dpi: 192 (scale 2.0), bundled GLFW

Without FLAG_WINDOW_HIGHDPI the output is identical before and after, and there are no new warnings with GCC (-Wall -Wextra) or MSVC. I haven't tested macOS or Wayland.

Notes:

  • This also fixes ToggleBorderlessWindowed() after a same-size SetWindowSize(). ToggleBorderlessWindowed() saves CORE.Window.screen when entering borderless mode, then multiplies it by the DPI scale on exit to get the window size. Since the saved size was in physical pixels, the restored window was too large. In the test above, toggling twice changed the window from 500x300 to 750x450 on Windows and 1000x600 on X11. With this PR, it stays at 750x450 and 1000x600.
  • SetWindowMinSize() and SetWindowMaxSize() apply the DPI scale once, when they're called, and GLFW doesn't rescale the limits when the monitor scale changes. So on Windows, a 600x350 minimum set at 150% stays 900x525 after the window moves to a 100% monitor. Before this PR, the limits weren't scaled at all. X11 isn't affected because GLFW uses one global scale read at startup, and on macOS and Wayland the limits aren't scaled since GLFW already works in logical units there. This could be handled in another PR.
  • The SDL3 backend has the same problem (SDL 3.2.10, same X11 setup), along with a few other HighDPI issues: InitWindow(800, 450) gives a 400x225 screen where GLFW gives 800x450, and its resize handler truncates the logical size where GLFW rounds it since [rcore][glfw] Round HighDPI window sizes instead of truncating #6346. Happy to fix those in a separate PR if you want.
  • This uses the runtime check from InitPlatform() rather than #if !defined(_GLFW_WAYLAND). CMake builds only pass _GLFW_WAYLAND to GLFW's own sources, not to rcore.c, and when both X11 and Wayland are enabled, only a runtime check knows which one is in use. With the #if form, the conversion would run on Wayland in CMake builds and be skipped on X11 in Makefile/Zig builds with both backends. Passing _GLFW_WAYLAND to rcore.c in CMake, like [cmake][X11] Add x11 libraries for raylib as glfw make private #5625 did for _GLFW_X11, could be another PR as well.

@jqntn
jqntn force-pushed the fix-setwindowsize-highdpi branch from 5b90377 to 0b71486 Compare November 3, 2026 22:47
@jqntn
jqntn marked this pull request as ready for review November 3, 2026 23:06
@Ne0nWinds

Ne0nWinds commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

@raysan5 Is SetWindowSize() supposed to take physical or logical units? If it takes physical units, this PR is correct.

If it's supposed to take logical DPI units, we'd have to multiply (rather than divide) by the DPI scale and pass that into glfwSetWindowSize. Example:

if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI) && (glfwGetPlatform() != GLFW_PLATFORM_WAYLAND))
{
    Vector2 scaleDpi = GetWindowScaleDPI();
    width = (int)roundf((float)width*scaleDpi.x);
    height = (int)roundf((float)height*scaleDpi.y);
    glfwSetWindowSize(platform.handle, width, height);
}
else
{
    glfwSetWindowSize(platform.handle, width, height);
}

My PR (#6352) uses logical DPI units, so if SetWindowSize() is supposed to take physical units, I'll have to make some changes.

@raysan5

raysan5 commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

@Ne0nWinds Definitely it should take logical units, aligned with InitWindow(), physical units are calculated and managed internally if HighDPI-awareness is required by user.

@Ne0nWinds

Copy link
Copy Markdown
Contributor

@jqntn I think your PR does fix the specific issue in the bug. However, SetWindowSize is supposed to take logical units, rather than physical ones.

If you pass in a width of 500 to SetWindowSize on a high-dpi display with 1.5× scaling, GetRenderWidth() should return 750.

Here's a simple test:

SetConfigFlags(FLAG_WINDOW_HIGHDPI | FLAG_WINDOW_RESIZABLE);
InitWindow(800, 450, "example");
SetWindowSize(800, 450); // the window size shouldn't change

I think SetWindowMinSize and SetWindowMaxSize also have the same issue, so once you figure out a fix for SetWindowSize, you could probably apply it to those two functions as well.

@jqntn
jqntn force-pushed the fix-setwindowsize-highdpi branch from 0b71486 to 0f458e5 Compare November 8, 2026 21:21
@jqntn jqntn changed the title [rcore][glfw] SetWindowSize(): set logical screen size with FLAG_WINDOW_HIGHDPI [rcore][glfw] SetWindowSize(), SetWindowMinSize(), SetWindowMaxSize(): use logical units with FLAG_WINDOW_HIGHDPI Nov 8, 2026
@jqntn

jqntn commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Ne0nWinds and @raysan5 for the guidance, I think now we should be on the same page

@raysan5 raysan5 added the windowing Issues about the window system label Nov 9, 2026
@raysan5
raysan5 merged commit 73100d0 into raysan5:master Nov 9, 2026
@raysan5

raysan5 commented Nov 9, 2026

Copy link
Copy Markdown
Owner

@jqntn thanks for the review.

The SDL3 backend has the same problem (SDL 3.2.10, same X11 setup), along with a few other HighDPI issues: InitWindow(800, 450) gives a 400x225 screen where GLFW gives 800x450, and its resize handler truncates the logical size where GLFW rounds it since #6346. Happy to fix those in a separate PR if you want.

Feel free to review it, all backends should be consistent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

windowing Issues about the window system

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[rcore][glfw] Same-size SetWindowSize() leaves GetScreenWidth()/GetScreenHeight() at physical pixels (FLAG_WINDOW_HIGHDPI)

3 participants