diff --git a/src/sdltiles.cpp b/src/sdltiles.cpp index f46c97b081f8c..60f00d61c5101 100644 --- a/src/sdltiles.cpp +++ b/src/sdltiles.cpp @@ -3624,6 +3624,42 @@ int projected_window_height() return get_option( "TERMINAL_Y" ) * fontheight; } +// Measures scaling factor for high-dpi displays +static std::pair get_display_scale( int display_index ) +{ +#if SDL_VERSION_ATLEAST(2,26,0) + // NOLINTNEXTLINE(cata-combine-locals-into-point) + int x = SDL_WINDOWPOS_CENTERED_DISPLAY( display_index ); + // NOLINTNEXTLINE(cata-combine-locals-into-point) + int y = SDL_WINDOWPOS_CENTERED_DISPLAY( display_index ); + + SDL_Window *w = SDL_CreateWindow( + "probe", + x, y, + 16, 16, + SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI + ); + if( !w ) { + return std::make_pair( 1.0f, 1.0f ); + } + + int lw = 0; + int lh = 0; + SDL_GetWindowSize( w, &lw, &lh ); + int pw; + int ph; + SDL_GetWindowSizeInPixels( w, &pw, &ph ); + SDL_DestroyWindow( w ); + + float scale_w = lw ? static_cast( pw ) / static_cast( lw ) : 1.0f; + float scale_h = lh ? static_cast( ph ) / static_cast( lh ) : 1.0f; + return std::make_pair( scale_w, scale_h ); +#else + ( void )display_index; // avoid unused parameter lint + return std::make_pair( 1.0f, 1.0f ); +#endif +} + static void init_term_size_and_scaling_factor() { scaling_factor = 1; @@ -3658,7 +3694,11 @@ static void init_term_size_and_scaling_factor() SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_MAXIMIZED ) ); +#if SDL_VERSION_ATLEAST(2,26,0) + SDL_GetWindowSizeInPixels( test_window.get(), &max_width, &max_height ); +#else SDL_GetWindowSize( test_window.get(), &max_width, &max_height ); +#endif // If the video subsystem isn't reset the test window messes things up later test_window.reset(); @@ -3667,8 +3707,9 @@ static void init_term_size_and_scaling_factor() } else { // For fullscreen or window borderless maximum size is the display size - max_width = current_display.w; - max_height = current_display.h; + auto [ dpi_scale_w, dpi_scale_h ] = get_display_scale( current_display_id ); + max_width = dpi_scale_w * current_display.w; + max_height = dpi_scale_h * current_display.h; } } else { dbg( D_WARNING ) << "Failed to get current Display Mode, assuming infinite display size.";