framebuffer_layout.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. namespace Layout {
  8. // Finds the largest size subrectangle contained in window area that is confined to the aspect ratio
  9. template <class T>
  10. static MathUtil::Rectangle<T> maxRectangle(MathUtil::Rectangle<T> window_area,
  11. float screen_aspect_ratio) {
  12. float scale = std::min(static_cast<float>(window_area.GetWidth()),
  13. window_area.GetHeight() / screen_aspect_ratio);
  14. return MathUtil::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)),
  15. static_cast<T>(std::round(scale * screen_aspect_ratio))};
  16. }
  17. FramebufferLayout DefaultFrameLayout(unsigned width, unsigned height) {
  18. ASSERT(width > 0);
  19. ASSERT(height > 0);
  20. // The drawing code needs at least somewhat valid values for both screens
  21. // so just calculate them both even if the other isn't showing.
  22. FramebufferLayout res{width, height};
  23. const float emulation_aspect_ratio{static_cast<float>(ScreenUndocked::Height) /
  24. ScreenUndocked::Width};
  25. MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height};
  26. MathUtil::Rectangle<unsigned> screen = maxRectangle(screen_window_area, emulation_aspect_ratio);
  27. float window_aspect_ratio = static_cast<float>(height) / width;
  28. if (window_aspect_ratio < emulation_aspect_ratio) {
  29. screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2);
  30. } else {
  31. screen = screen.TranslateY((height - screen.GetHeight()) / 2);
  32. }
  33. res.screen = screen;
  34. return res;
  35. }
  36. } // namespace Layout