2018-01-13 22:22:39 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2016-05-03 08:07:17 +02:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-01-12 04:24:09 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
2016-05-03 08:07:17 +02:00
|
|
|
#include "common/assert.h"
|
2017-05-28 01:10:25 +02:00
|
|
|
#include "core/frontend/framebuffer_layout.h"
|
2018-08-31 08:16:16 +02:00
|
|
|
#include "core/settings.h"
|
2016-05-03 08:07:17 +02:00
|
|
|
|
|
|
|
namespace Layout {
|
|
|
|
|
2016-11-05 08:47:05 +01:00
|
|
|
// Finds the largest size subrectangle contained in window area that is confined to the aspect ratio
|
|
|
|
template <class T>
|
2019-02-27 04:47:49 +01:00
|
|
|
static Common::Rectangle<T> MaxRectangle(Common::Rectangle<T> window_area,
|
|
|
|
float screen_aspect_ratio) {
|
2016-11-05 08:47:05 +01:00
|
|
|
float scale = std::min(static_cast<float>(window_area.GetWidth()),
|
|
|
|
window_area.GetHeight() / screen_aspect_ratio);
|
2019-02-27 04:47:49 +01:00
|
|
|
return Common::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)),
|
|
|
|
static_cast<T>(std::round(scale * screen_aspect_ratio))};
|
2016-05-03 08:07:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 08:14:24 +02:00
|
|
|
FramebufferLayout DefaultFrameLayout(u32 width, u32 height) {
|
2016-05-03 08:07:17 +02:00
|
|
|
ASSERT(width > 0);
|
|
|
|
ASSERT(height > 0);
|
2016-11-05 08:47:05 +01:00
|
|
|
// The drawing code needs at least somewhat valid values for both screens
|
|
|
|
// so just calculate them both even if the other isn't showing.
|
2020-04-15 21:59:23 +02:00
|
|
|
FramebufferLayout res{width, height, false, {}};
|
2016-11-05 08:47:05 +01:00
|
|
|
|
2020-02-14 06:06:26 +01:00
|
|
|
const float window_aspect_ratio = static_cast<float>(height) / width;
|
2020-02-14 20:39:04 +01:00
|
|
|
const float emulation_aspect_ratio = EmulationAspectRatio(
|
|
|
|
static_cast<AspectRatio>(Settings::values.aspect_ratio), window_aspect_ratio);
|
2017-08-25 23:53:07 +02:00
|
|
|
|
2019-05-29 08:14:24 +02:00
|
|
|
const Common::Rectangle<u32> screen_window_area{0, 0, width, height};
|
|
|
|
Common::Rectangle<u32> screen = MaxRectangle(screen_window_area, emulation_aspect_ratio);
|
2017-08-25 23:53:07 +02:00
|
|
|
|
|
|
|
if (window_aspect_ratio < emulation_aspect_ratio) {
|
2018-01-10 04:36:07 +01:00
|
|
|
screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2);
|
2017-08-25 23:53:07 +02:00
|
|
|
} else {
|
2018-01-10 04:36:07 +01:00
|
|
|
screen = screen.TranslateY((height - screen.GetHeight()) / 2);
|
2017-08-25 23:53:07 +02:00
|
|
|
}
|
2019-05-29 08:14:24 +02:00
|
|
|
|
2018-01-10 04:36:07 +01:00
|
|
|
res.screen = screen;
|
2017-08-25 23:53:07 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:14:24 +02:00
|
|
|
FramebufferLayout FrameLayoutFromResolutionScale(u32 res_scale) {
|
|
|
|
u32 width, height;
|
2018-08-31 08:16:16 +02:00
|
|
|
|
|
|
|
if (Settings::values.use_docked_mode) {
|
2020-03-11 18:02:30 +01:00
|
|
|
width = ScreenDocked::Width * res_scale;
|
|
|
|
height = ScreenDocked::Height * res_scale;
|
2018-08-31 08:16:16 +02:00
|
|
|
} else {
|
|
|
|
width = ScreenUndocked::Width * res_scale;
|
|
|
|
height = ScreenUndocked::Height * res_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefaultFrameLayout(width, height);
|
|
|
|
}
|
|
|
|
|
2020-02-14 20:39:04 +01:00
|
|
|
float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio) {
|
2020-02-14 06:06:26 +01:00
|
|
|
switch (aspect) {
|
2020-02-14 20:39:04 +01:00
|
|
|
case AspectRatio::Default:
|
2020-02-14 06:06:26 +01:00
|
|
|
return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
|
2020-02-14 20:39:04 +01:00
|
|
|
case AspectRatio::R4_3:
|
|
|
|
return 3.0f / 4.0f;
|
|
|
|
case AspectRatio::R21_9:
|
2020-02-14 06:06:26 +01:00
|
|
|
return 9.0f / 21.0f;
|
2020-02-14 20:39:04 +01:00
|
|
|
case AspectRatio::StretchToWindow:
|
2020-02-14 06:06:26 +01:00
|
|
|
return window_aspect_ratio;
|
|
|
|
default:
|
|
|
|
return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 23:53:07 +02:00
|
|
|
} // namespace Layout
|