Skip to content

Commit 4b76009

Browse files
committed
REVIEWED: Window scaling with HighDPI on macOS #5059
1 parent 6d562e5 commit 4b76009

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

examples/core/core_highdpi_testbed.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ int main(void)
4747
mousePos = GetMousePosition();
4848
currentMonitor = GetCurrentMonitor();
4949
scaleDpi = GetWindowScaleDPI();
50+
51+
if (IsKeyPressed(KEY_SPACE)) ToggleBorderlessWindowed();
5052
//----------------------------------------------------------------------------------
5153

5254
// Draw

src/platforms/rcore_desktop_glfw.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ void ToggleBorderlessWindowed(void)
246246

247247
if (mode != NULL)
248248
{
249-
if (!FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE))
249+
if (!FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE))
250250
{
251251
// Store screen position and size
252252
// NOTE: If it was on fullscreen, screen position was already stored, so skip setting it here
@@ -286,13 +286,15 @@ void ToggleBorderlessWindowed(void)
286286
glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_TRUE);
287287
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
288288

289+
#if !defined(__APPLE__)
289290
// Make sure to restore size to HighDPI
290291
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
291292
{
292293
Vector2 scaleDpi = GetWindowScaleDPI();
293294
CORE.Window.previousScreen.width *= scaleDpi.x;
294295
CORE.Window.previousScreen.height *= scaleDpi.y;
295296
}
297+
#endif
296298

297299
// Return previous screen size and position
298300
// NOTE: The order matters here, it must set size first, then set position, otherwise the screen will be positioned incorrectly
@@ -1443,22 +1445,14 @@ int InitPlatform(void)
14431445
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT)) glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); // Transparent framebuffer
14441446
else glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_FALSE); // Opaque framebuffer
14451447

1446-
// HACK: Most of this was written before GLFW_SCALE_FRAMEBUFFER existed and
1447-
// was enabled by default. Disabling it gets back the old behavior. A
1448-
// complete fix will require removing a lot of CORE.Window.render manipulation code
1449-
// NOTE: This currently doesn't work on macOS(see #5185), so we skip it there
1450-
// when FLAG_WINDOW_HIGHDPI is *unset*
1451-
#if !defined(__APPLE__)
1452-
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
1453-
#endif
1454-
14551448
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
14561449
{
14571450
#if defined(__APPLE__)
14581451
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
14591452
#endif
14601453
// Resize window content area based on the monitor content scale
1461-
// NOTE: This hint only has an effect on platforms where screen coordinates and pixels always map 1:1 such as Windows and X11
1454+
// NOTE: This hint only has an effect on platforms where screen coordinates and
1455+
// pixels always map 1:1 such as Windows and X11
14621456
// On platforms like macOS the resolution of the framebuffer is changed independently of the window size
14631457
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); // Scale content area based on the monitor content scale where window is placed on
14641458
#if defined(__APPLE__)
@@ -1674,7 +1668,6 @@ int InitPlatform(void)
16741668
int fbWidth = CORE.Window.screen.width;
16751669
int fbHeight = CORE.Window.screen.height;
16761670

1677-
#if !defined(__APPLE__)
16781671
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
16791672
{
16801673
// NOTE: On APPLE platforms system should manage window/input scaling and also framebuffer scaling
@@ -1683,11 +1676,11 @@ int InitPlatform(void)
16831676

16841677
// Screen scaling matrix is required in case desired screen area is different from display area
16851678
CORE.Window.screenScale = MatrixScale((float)fbWidth/CORE.Window.screen.width, (float)fbHeight/CORE.Window.screen.height, 1.0f);
1686-
1679+
#if !defined(__APPLE__)
16871680
// Mouse input scaling for the new screen size
16881681
SetMouseScale((float)CORE.Window.screen.width/fbWidth, (float)CORE.Window.screen.height/fbHeight);
1682+
#endif
16891683
}
1690-
#endif
16911684

16921685
CORE.Window.render.width = fbWidth;
16931686
CORE.Window.render.height = fbHeight;
@@ -1834,11 +1827,14 @@ static void ErrorCallback(int error, const char *description)
18341827
static void WindowSizeCallback(GLFWwindow *window, int width, int height)
18351828
{
18361829
// Nothing to do for now on window resize...
1830+
//TRACELOG(LOG_INFO, "GLFW3: Window size callback called [%i,%i]", width, height);
18371831
}
18381832

18391833
// GLFW3: Framebuffer size change callback, runs when framebuffer is resized
18401834
static void FramebufferSizeCallback(GLFWwindow *window, int width, int height)
18411835
{
1836+
//TRACELOG(LOG_INFO, "GLFW3: Window framebuffer size callback called [%i,%i]", width, height);
1837+
18421838
// WARNING: On window minimization, callback is called,
18431839
// but we don't want to change internal screen values, it breaks things
18441840
if ((width == 0) || (height == 0)) return;
@@ -1880,6 +1876,8 @@ static void WindowPosCallback(GLFWwindow *window, int x, int y)
18801876
// GLFW3: Window content scale callback, runs on monitor content scale change detected
18811877
static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley)
18821878
{
1879+
TRACELOG(LOG_INFO, "GLFW3: Window content scale changed, scale: [%.2f,%.2f]", scalex, scaley);
1880+
18831881
float fbWidth = (float)CORE.Window.screen.width*scalex;
18841882
float fbHeight = (float)CORE.Window.screen.height*scaley;
18851883

src/rcore.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3815,14 +3815,7 @@ void SetupViewport(int width, int height)
38153815
CORE.Window.render.height = height;
38163816

38173817
// Set viewport width and height
3818-
// NOTE: We consider render size (scaled) and offset in case black bars are required and
3819-
// render area does not match full display area (this situation is only applicable on fullscreen mode)
3820-
#if defined(__APPLE__)
3821-
Vector2 scale = GetWindowScaleDPI();
3822-
rlViewport(CORE.Window.renderOffset.x/2*scale.x, CORE.Window.renderOffset.y/2*scale.y, (CORE.Window.render.width)*scale.x, (CORE.Window.render.height)*scale.y);
3823-
#else
38243818
rlViewport(CORE.Window.renderOffset.x/2, CORE.Window.renderOffset.y/2, CORE.Window.render.width, CORE.Window.render.height);
3825-
#endif
38263819

38273820
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
38283821
rlLoadIdentity(); // Reset current matrix (projection)

0 commit comments

Comments
 (0)