Skip to content

Commit 4cb4980

Browse files
committed
Snap output window to framebuffer pixel grid
1 parent c35574e commit 4cb4980

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

platforms/shared/desktop/gui.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ static void main_window(void)
454454

455455
int w = (int)ImGui::GetIO().DisplaySize.x;
456456
int h = (int)ImGui::GetIO().DisplaySize.y - (application_show_menu ? gui_main_menu_height : 0);
457+
ImGuiIO& io = ImGui::GetIO();
458+
float fb_scale_x = io.DisplayFramebufferScale.x > 0.0f ? io.DisplayFramebufferScale.x : 1.0f;
459+
float fb_scale_y = io.DisplayFramebufferScale.y > 0.0f ? io.DisplayFramebufferScale.y : 1.0f;
460+
bool fractional_fb_scale = (fabsf(fb_scale_x - roundf(fb_scale_x)) > 0.001f) || (fabsf(fb_scale_y - roundf(fb_scale_y)) > 0.001f);
457461

458462
int selected_ratio = config_debug.debug ? 0 : config_video.ratio;
459463
float ratio = 0;
@@ -510,8 +514,22 @@ static void main_window(void)
510514
{
511515
case 0:
512516
{
513-
int factor_w = w / w_corrected;
514-
int factor_h = h / h_corrected;
517+
int factor_w;
518+
int factor_h;
519+
520+
if (fractional_fb_scale)
521+
{
522+
int available_w_fb = (int)floorf((float)w * fb_scale_x);
523+
int available_h_fb = (int)floorf((float)h * fb_scale_y);
524+
factor_w = available_w_fb / w_corrected;
525+
factor_h = available_h_fb / h_corrected;
526+
}
527+
else
528+
{
529+
factor_w = w / w_corrected;
530+
factor_h = h / h_corrected;
531+
}
532+
515533
scale_multiplier = (factor_w < factor_h) ? factor_w : factor_h;
516534
break;
517535
}
@@ -534,15 +552,23 @@ static void main_window(void)
534552
}
535553
}
536554

537-
float window_width = (float)(w_corrected * scale_multiplier);
538-
float window_height = (float)(h_corrected * scale_multiplier);
555+
float window_width = 0.0f;
556+
float window_height = 0.0f;
539557

540-
ImGuiIO& io = ImGui::GetIO();
541-
float fb_scale_x = io.DisplayFramebufferScale.x > 0.0f ? io.DisplayFramebufferScale.x : 1.0f;
542-
float fb_scale_y = io.DisplayFramebufferScale.y > 0.0f ? io.DisplayFramebufferScale.y : 1.0f;
543-
544-
window_width = roundf(window_width * fb_scale_x) / fb_scale_x;
545-
window_height = roundf(window_height * fb_scale_y) / fb_scale_y;
558+
if (!config_debug.debug && fractional_fb_scale && ((config_video.scale == 0) || (config_video.scale == 1)))
559+
{
560+
int window_width_fb = w_corrected * scale_multiplier;
561+
int window_height_fb = h_corrected * scale_multiplier;
562+
window_width = (float)window_width_fb / fb_scale_x;
563+
window_height = (float)window_height_fb / fb_scale_y;
564+
}
565+
else
566+
{
567+
window_width = (float)(w_corrected * scale_multiplier);
568+
window_height = (float)(h_corrected * scale_multiplier);
569+
window_width = roundf(window_width * fb_scale_x) / fb_scale_x;
570+
window_height = roundf(window_height * fb_scale_y) / fb_scale_y;
571+
}
546572

547573
gui_main_window_width = (int)roundf(window_width);
548574
gui_main_window_height = (int)roundf(window_height);

0 commit comments

Comments
 (0)