Skip to content

Commit

Permalink
video: Expose dpi and window status getters
Browse files Browse the repository at this point in the history
These are currently used directly in CVideo::video_settings_report(),
which we want to remove from this class and implement externally.

This commit also reworks get_dpi_scale_factor() to be implemented in
terms of get_dpi().
  • Loading branch information
irydacea committed Jun 16, 2020
1 parent 59e6728 commit 2f1e2dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/video.cpp
Expand Up @@ -406,23 +406,25 @@ bool CVideo::window_has_flags(uint32_t flags) const
return (window->get_flags() & flags) != 0;
}

std::pair<float, float> CVideo::get_dpi_scale_factor() const
std::pair<float, float> CVideo::get_dpi() const
{
std::pair<float, float> 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<float, float> 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<point> CVideo::get_available_resolutions(const bool include_current)
Expand Down
8 changes: 8 additions & 0 deletions src/video.hpp
Expand Up @@ -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 };

Expand Down Expand Up @@ -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<float, float> get_dpi() const;

/** The current scale factor on High-DPI screens. */
std::pair<float, float> get_dpi_scale_factor() const;

Expand Down

0 comments on commit 2f1e2dd

Please sign in to comment.