Advertisement
Guest User

ranges

a guest
Jun 6th, 2020
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. [[nodiscard]] inline auto SDL::GetDisplays()
  2. {
  3.     const auto validDisplayIndices = ranges::views::ints(0, SDL_GetNumVideoDisplays());
  4.     constexpr auto fetchDisplayData = ranges::views::transform([](const int displayIndex) -> std::optional<Display> {
  5.         Display::DPI dpi;
  6.         if (SDL_GetDisplayDPI(displayIndex, &dpi.Diagonal, &dpi.Horizontal, &dpi.Vertical) != 0)
  7.             return std::nullopt;
  8.  
  9.         SDL_Rect bounds;
  10.         if (SDL_GetDisplayBounds(displayIndex, &bounds) != 0)
  11.             return std::nullopt;
  12.  
  13.         SDL_DisplayMode mode;
  14.         if (SDL_GetCurrentDisplayMode(displayIndex, &mode) != 0)
  15.             return std::nullopt;
  16.  
  17.         return Display{ displayIndex, SDL_GetDisplayName(displayIndex), Units::Pixels{ bounds.x }, Units::Pixels{ bounds.y }, Units::Pixels{ bounds.w },
  18.             Units::Pixels{ bounds.h }, dpi, mode.refresh_rate };
  19.     });
  20.     constexpr auto removeEmptyOptionals = ranges::views::filter([](const auto& display) { return display.has_value(); });
  21.     constexpr auto takeOptionalValues = ranges::views::transform([](const auto& opt) { return *opt; });
  22.     return validDisplayIndices | fetchDisplayData | removeEmptyOptionals | takeOptionalValues;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement