MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / updateFullscreenResolutions

Function updateFullscreenResolutions

src/OpenLoco/src/Ui.cpp:714–770  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

712 }
713
714 void updateFullscreenResolutions()
715 {
716 // Query number of display modes
717 int32_t displayIndex = SDL_GetDisplayForWindow(_window);
718
719 // Get desktop aspect ratio
720 const SDL_DisplayMode* desktopMode = SDL_GetDesktopDisplayMode(displayIndex);
721 float desktopAspectRatio = (float)desktopMode->w / desktopMode->h;
722
723 // Get resolutions
724 int32_t numDisplayModes{};
725 SDL_DisplayMode** displayModes = SDL_GetFullscreenDisplayModes(displayIndex, &numDisplayModes);
726 if (!displayModes || numDisplayModes <= 0)
727 {
728 Logging::error("SDL_GetFullscreenDisplayModes failed: {}", SDL_GetError());
729 return;
730 }
731
732 auto resolutions = std::vector<Resolution>();
733 for (int32_t i = 0; i < numDisplayModes; i++)
734 {
735 const auto* mode = displayModes[i];
736 if (mode->w > 0 && mode->h > 0)
737 {
738 float aspectRatio = (float)mode->w / mode->h;
739 if (_resolutionsAllowAnyAspectRatio || std::fabs(desktopAspectRatio - aspectRatio) < 0.0001f)
740 {
741 resolutions.push_back({ mode->w, mode->h });
742 }
743 }
744 }
745
746 SDL_free(displayModes);
747
748 // Sort by area
749 std::sort(resolutions.begin(), resolutions.end(), [](const Resolution& a, const Resolution& b) -> bool {
750 int32_t areaA = a.width * a.height;
751 int32_t areaB = b.width * b.height;
752 return areaA < areaB;
753 });
754
755 // Remove duplicates
756 auto last = std::unique(resolutions.begin(), resolutions.end(), [](const Resolution& a, const Resolution& b) -> bool {
757 return (a.width == b.width && a.height == b.height);
758 });
759 resolutions.erase(last, resolutions.end());
760
761 // Update configured fullscreen resolution if not set
762 auto& cfg = Config::get().display.fullscreenResolution;
763 if (!cfg.isPositive())
764 {
765 cfg.width = resolutions.back().width;
766 cfg.height = resolutions.back().height;
767 }
768
769 _fsResolutions = resolutions;
770 }
771

Callers 1

getFullscreenResolutionsFunction · 0.85

Calls 6

errorFunction · 0.85
push_backMethod · 0.80
isPositiveMethod · 0.80
getFunction · 0.70
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected