diff --git a/src/video.cpp b/src/video.cpp index 530e283284fe..bcae73ec35ce 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -406,23 +406,25 @@ bool CVideo::window_has_flags(uint32_t flags) const return (window->get_flags() & flags) != 0; } -std::pair CVideo::get_dpi_scale_factor() const +std::pair CVideo::get_dpi() const { - std::pair result{1.0f, 1.0f}; - - if(!window) { - return result; - } - float hdpi, vdpi; - int returncode = SDL_GetDisplayDPI(window->get_display_index(), nullptr, &hdpi, &vdpi); - - if (returncode == 0) { - result.first = hdpi / MAGIC_DPI_SCALE_NUMBER; - result.second = vdpi / MAGIC_DPI_SCALE_NUMBER; + if(window && SDL_GetDisplayDPI(window->get_display_index(), nullptr, &hdpi, &vdpi) == 0) { + return { hdpi, vdpi }; } + // SDL doesn't know the screen dpi, there's a configuration issue, or we + // don't have a window yet. + return { 0.0f, 0.0f }; +} - return result; +std::pair CVideo::get_dpi_scale_factor() const +{ + auto dpi = get_dpi(); + if(dpi.first != 0.0f && dpi.second != 0.0f) { + return { dpi.first / MAGIC_DPI_SCALE_NUMBER, dpi.second / MAGIC_DPI_SCALE_NUMBER }; + } + // Assume a scale factor of 1.0 if the screen dpi is currently unknown. + return { 1.0f, 1.0f }; } std::vector CVideo::get_available_resolutions(const bool include_current) diff --git a/src/video.hpp b/src/video.hpp index a06bbff405a5..fb82bf8156fb 100644 --- a/src/video.hpp +++ b/src/video.hpp @@ -79,6 +79,11 @@ class CVideo /** Returns a pointer to the underlying SDL window. */ sdl::window* get_window(); + bool has_window() + { + return get_window() != nullptr; + } + private: enum MODE_EVENT { TO_RES, TO_FULLSCREEN, TO_WINDOWED, TO_MAXIMIZED_WINDOW }; @@ -127,6 +132,9 @@ class CVideo /** Returns the window renderer height in pixels or in screen coordinates. */ int get_height(bool as_pixels = true) const; + /** The current game screen dpi. */ + std::pair get_dpi() const; + /** The current scale factor on High-DPI screens. */ std::pair get_dpi_scale_factor() const;