From 11fbf260455faa054072df7bafb7065f1fce25ef Mon Sep 17 00:00:00 2001 From: Albert Liu <45282415+ggrtk@users.noreply.github.com> Date: Sat, 14 Nov 2020 17:23:22 -0800 Subject: [PATCH] HostDisplay: Use floats internally for draw rectangle calculation --- src/core/host_display.cpp | 49 +++++++++++++++++++++------------------ src/core/host_display.h | 4 ++-- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/core/host_display.cpp b/src/core/host_display.cpp index 91fc8650c..bac43529f 100644 --- a/src/core/host_display.cpp +++ b/src/core/host_display.cpp @@ -135,9 +135,10 @@ void HostDisplay::ClearSoftwareCursor() m_cursor_texture_scale = 1.0f; } -void HostDisplay::CalculateDrawRect(s32 window_width, s32 window_height, s32* out_left, s32* out_top, s32* out_width, - s32* out_height, s32* out_left_padding, s32* out_top_padding, float* out_scale, - float* out_x_scale, bool apply_aspect_ratio /* = true */) const +void HostDisplay::CalculateDrawRect(s32 window_width, s32 window_height, float* out_left, float* out_top, + float* out_width, float* out_height, float* out_left_padding, + float* out_top_padding, float* out_scale, float* out_x_scale, + bool apply_aspect_ratio /* = true */) const { const float x_scale = apply_aspect_ratio ? @@ -166,24 +167,25 @@ void HostDisplay::CalculateDrawRect(s32 window_width, s32 window_height, s32* ou if (out_left_padding) { if (m_display_integer_scaling) - *out_left_padding = std::max((window_width - static_cast(display_width * scale)) / 2, 0); + *out_left_padding = std::max((static_cast(window_width) - display_width * scale) / 2.0f, 0.0f); else - *out_left_padding = 0; + *out_left_padding = 0.0f; } if (out_top_padding) { switch (m_display_alignment) { case Alignment::LeftOrTop: - *out_top_padding = 0; + *out_top_padding = 0.0f; break; case Alignment::Center: - *out_top_padding = std::max((window_height - static_cast(display_height * scale)) / 2, 0); + *out_top_padding = + std::max((static_cast(window_height) - (display_height * scale)) / 2.0f, 0.0f); break; case Alignment::RightOrBottom: - *out_top_padding = std::max(window_height - static_cast(display_height * scale), 0); + *out_top_padding = std::max(static_cast(window_height) - (display_height * scale), 0.0f); break; } } @@ -200,15 +202,16 @@ void HostDisplay::CalculateDrawRect(s32 window_width, s32 window_height, s32* ou switch (m_display_alignment) { case Alignment::LeftOrTop: - *out_left_padding = 0; + *out_left_padding = 0.0f; break; case Alignment::Center: - *out_left_padding = std::max((window_width - static_cast(display_width * scale)) / 2, 0); + *out_left_padding = + std::max((static_cast(window_width) - (display_width * scale)) / 2.0f, 0.0f); break; case Alignment::RightOrBottom: - *out_left_padding = std::max(window_width - static_cast(display_width * scale), 0); + *out_left_padding = std::max(static_cast(window_width) - (display_width * scale), 0.0f); break; } } @@ -216,16 +219,16 @@ void HostDisplay::CalculateDrawRect(s32 window_width, s32 window_height, s32* ou if (out_top_padding) { if (m_display_integer_scaling) - *out_top_padding = std::max((window_height - static_cast(display_height * scale)) / 2, 0); + *out_top_padding = std::max((static_cast(window_height) - (display_height * scale)) / 2.0f, 0.0f); else - *out_top_padding = 0; + *out_top_padding = 0.0f; } } - *out_width = static_cast(active_width * scale); - *out_height = static_cast(active_height * scale); - *out_left = static_cast(active_left * scale); - *out_top = static_cast(active_top * scale); + *out_width = active_width * scale; + *out_height = active_height * scale; + *out_left = active_left * scale; + *out_top = active_top * scale; if (out_scale) *out_scale = scale; } @@ -233,10 +236,12 @@ void HostDisplay::CalculateDrawRect(s32 window_width, s32 window_height, s32* ou std::tuple HostDisplay::CalculateDrawRect(s32 window_width, s32 window_height, s32 top_margin, bool apply_aspect_ratio /* = true */) const { - s32 left, top, width, height, left_padding, top_padding; + float left, top, width, height, left_padding, top_padding; CalculateDrawRect(window_width, window_height - top_margin, &left, &top, &width, &height, &left_padding, &top_padding, nullptr, nullptr, apply_aspect_ratio); - return std::make_tuple(left + left_padding, top + top_padding + top_margin, width, height); + + return std::make_tuple(static_cast(left + left_padding), static_cast(top + top_padding) + top_margin, + static_cast(width), static_cast(height)); } std::tuple HostDisplay::CalculateSoftwareCursorDrawRect() const @@ -262,14 +267,14 @@ std::tuple HostDisplay::ConvertWindowCoordinatesToDisplayCoordinat s32 window_width, s32 window_height, s32 top_margin) const { - s32 left, top, width, height, left_padding, top_padding; + float left, top, width, height, left_padding, top_padding; float scale, x_scale; CalculateDrawRect(window_width, window_height - top_margin, &left, &top, &width, &height, &left_padding, &top_padding, &scale, &x_scale); // convert coordinates to active display region, then to full display region - const float scaled_display_x = static_cast(window_x - (left_padding)); - const float scaled_display_y = static_cast(window_y - (top_padding + top_margin)); + const float scaled_display_x = static_cast(window_x) - left_padding; + const float scaled_display_y = static_cast(window_y) - top_padding + static_cast(top_margin); // scale back to internal resolution const float display_x = scaled_display_x / scale / x_scale; diff --git a/src/core/host_display.h b/src/core/host_display.h index fa7989766..c4024ca5c 100644 --- a/src/core/host_display.h +++ b/src/core/host_display.h @@ -213,8 +213,8 @@ protected: ALWAYS_INLINE bool HasSoftwareCursor() const { return static_cast(m_cursor_texture); } ALWAYS_INLINE bool HasDisplayTexture() const { return (m_display_texture_handle != nullptr); } - void CalculateDrawRect(s32 window_width, s32 window_height, s32* out_left, s32* out_top, s32* out_width, - s32* out_height, s32* out_left_padding, s32* out_top_padding, float* out_scale, + void CalculateDrawRect(s32 window_width, s32 window_height, float* out_left, float* out_top, float* out_width, + float* out_height, float* out_left_padding, float* out_top_padding, float* out_scale, float* out_x_scale, bool apply_aspect_ratio = true) const; std::tuple CalculateSoftwareCursorDrawRect() const;