From d0206c35fb22b976af2d281217710b23204aaea4 Mon Sep 17 00:00:00 2001 From: lizzie Date: Tue, 21 Oct 2025 21:39:09 +0200 Subject: [qt] fix aspect ratio enum mismatch for framebuffer (#2792) the framebuffer: ```c++ enum class AspectRatio { Default, R4_3, R21_9, R16_10, StretchToWindow, }; ``` the actual enum ```c++ ENUM(AspectRatio, R16_9, R4_3, R21_9, R16_10, Stretch); ``` If someone were to add a new setting it would likely cause catastrophe. 1280/720 = 16/9 Signed-off-by: lizzie Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2792 Reviewed-by: MaranBr Reviewed-by: crueter Co-authored-by: lizzie Co-committed-by: lizzie --- src/core/frontend/framebuffer_layout.cpp | 79 +++++++++++++------------------- 1 file changed, 32 insertions(+), 47 deletions(-) (limited to 'src/core/frontend/framebuffer_layout.cpp') diff --git a/src/core/frontend/framebuffer_layout.cpp b/src/core/frontend/framebuffer_layout.cpp index 63e17f9ceb..cd7b5b3046 100644 --- a/src/core/frontend/framebuffer_layout.cpp +++ b/src/core/frontend/framebuffer_layout.cpp @@ -14,69 +14,54 @@ namespace Layout { // Finds the largest size subrectangle contained in window area that is confined to the aspect ratio -template -static Common::Rectangle MaxRectangle(Common::Rectangle window_area, - float screen_aspect_ratio) { - const float scale = (std::min)(static_cast(window_area.GetWidth()), - static_cast(window_area.GetHeight()) / screen_aspect_ratio); - return Common::Rectangle{0, 0, static_cast(std::round(scale)), - static_cast(std::round(scale * screen_aspect_ratio))}; +static Common::Rectangle MaxRectangle(Common::Rectangle window_area, float screen_aspect_ratio) noexcept { + const float scale = (std::min)(float(window_area.GetWidth()), float(window_area.GetHeight()) / screen_aspect_ratio); + return Common::Rectangle{0, 0, u32(std::round(scale)), u32(std::round(scale * screen_aspect_ratio))}; } -FramebufferLayout DefaultFrameLayout(u32 width, u32 height) { - ASSERT(width > 0); - ASSERT(height > 0); +/// @brief Factory method for constructing a default FramebufferLayout +/// @param width Window framebuffer width in pixels +/// @param height Window framebuffer height in pixels +/// @return Newly created FramebufferLayout object with default screen regions initialized +FramebufferLayout DefaultFrameLayout(u32 width, u32 height) noexcept { + ASSERT(width > 0 && height > 0); + auto const window_aspect_ratio = float(height) / float(width); + auto const emulation_aspect_ratio = EmulationAspectRatio(Settings::values.aspect_ratio.GetValue(), window_aspect_ratio); + Common::Rectangle const screen_window_area{0, 0, width, height}; + auto screen = MaxRectangle(screen_window_area, emulation_aspect_ratio); + if (window_aspect_ratio < emulation_aspect_ratio) { + screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2); + } else { + screen = screen.TranslateY((height - screen.GetHeight()) / 2); + } // The drawing code needs at least somewhat valid values for both screens // so just calculate them both even if the other isn't showing. - FramebufferLayout res{ + return FramebufferLayout{ .width = width, .height = height, - .screen = {}, + .screen = screen, .is_srgb = false, }; - - const float window_aspect_ratio = static_cast(height) / static_cast(width); - const float emulation_aspect_ratio = EmulationAspectRatio( - static_cast(Settings::values.aspect_ratio.GetValue()), window_aspect_ratio); - - const Common::Rectangle screen_window_area{0, 0, width, height}; - Common::Rectangle screen = MaxRectangle(screen_window_area, emulation_aspect_ratio); - - if (window_aspect_ratio < emulation_aspect_ratio) { - screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2); - } else { - screen = screen.TranslateY((height - screen.GetHeight()) / 2); - } - - res.screen = screen; - return res; -} - -FramebufferLayout FrameLayoutFromResolutionScale(f32 res_scale) { - const bool is_docked = Settings::IsDockedMode(); - const u32 screen_width = is_docked ? ScreenDocked::Width : ScreenUndocked::Width; - const u32 screen_height = is_docked ? ScreenDocked::Height : ScreenUndocked::Height; - - const u32 width = static_cast(static_cast(screen_width) * res_scale); - const u32 height = static_cast(static_cast(screen_height) * res_scale); - - return DefaultFrameLayout(width, height); } -float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio) { +/// @brief Convenience method to determine emulation aspect ratio +/// @param aspect Represents the index of aspect ratio stored in Settings::values.aspect_ratio +/// @param window_aspect_ratio Current window aspect ratio +/// @return Emulation render window aspect ratio +float EmulationAspectRatio(Settings::AspectRatio aspect, float window_aspect_ratio) noexcept { switch (aspect) { - case AspectRatio::Default: - return static_cast(ScreenUndocked::Height) / ScreenUndocked::Width; - case AspectRatio::R4_3: + case Settings::AspectRatio::R16_9: + return 9.0f / 16.0f; + case Settings::AspectRatio::R4_3: return 3.0f / 4.0f; - case AspectRatio::R21_9: + case Settings::AspectRatio::R21_9: return 9.0f / 21.0f; - case AspectRatio::R16_10: + case Settings::AspectRatio::R16_10: return 10.0f / 16.0f; - case AspectRatio::StretchToWindow: + case Settings::AspectRatio::Stretch: return window_aspect_ratio; default: - return static_cast(ScreenUndocked::Height) / ScreenUndocked::Width; + return float(ScreenUndocked::Height) / ScreenUndocked::Width; } } -- cgit v1.2.3