framebuffer_layout.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include "common/assert.h"
  6. #include "core/frontend/framebuffer_layout.h"
  7. #include "core/settings.h"
  8. namespace Layout {
  9. // Finds the largest size subrectangle contained in window area that is confined to the aspect ratio
  10. template <class T>
  11. static MathUtil::Rectangle<T> maxRectangle(MathUtil::Rectangle<T> window_area,
  12. float screen_aspect_ratio) {
  13. float scale = std::min(static_cast<float>(window_area.GetWidth()),
  14. window_area.GetHeight() / screen_aspect_ratio);
  15. return MathUtil::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)),
  16. static_cast<T>(std::round(scale * screen_aspect_ratio))};
  17. }
  18. FramebufferLayout DefaultFrameLayout(unsigned width, unsigned height) {
  19. ASSERT(width > 0);
  20. ASSERT(height > 0);
  21. // The drawing code needs at least somewhat valid values for both screens
  22. // so just calculate them both even if the other isn't showing.
  23. FramebufferLayout res{width, height};
  24. const float emulation_aspect_ratio{static_cast<float>(ScreenUndocked::Height) /
  25. ScreenUndocked::Width};
  26. MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height};
  27. MathUtil::Rectangle<unsigned> screen = maxRectangle(screen_window_area, emulation_aspect_ratio);
  28. float window_aspect_ratio = static_cast<float>(height) / width;
  29. if (window_aspect_ratio < emulation_aspect_ratio) {
  30. screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2);
  31. } else {
  32. screen = screen.TranslateY((height - screen.GetHeight()) / 2);
  33. }
  34. res.screen = screen;
  35. return res;
  36. }
  37. FramebufferLayout FrameLayoutFromResolutionScale(u16 res_scale) {
  38. int width, height;
  39. if (Settings::values.use_docked_mode) {
  40. width = ScreenDocked::WidthDocked * res_scale;
  41. height = ScreenDocked::HeightDocked * res_scale;
  42. } else {
  43. width = ScreenUndocked::Width * res_scale;
  44. height = ScreenUndocked::Height * res_scale;
  45. }
  46. return DefaultFrameLayout(width, height);
  47. }
  48. } // namespace Layout