From 0464a50ab8f5f6c7028da80cb0d579cdfc84717a Mon Sep 17 00:00:00 2001 From: sean <43609023+spnda@users.noreply.github.com> Date: Tue, 9 Mar 2021 10:22:52 +0100 Subject: Add: Display refresh rate game option (#8813) --- src/video/allegro_v.cpp | 10 ++++++++++ src/video/allegro_v.h | 2 ++ src/video/cocoa/cocoa_v.h | 2 ++ src/video/cocoa/cocoa_v.mm | 25 +++++++++++++++++++++++++ src/video/sdl2_v.cpp | 11 +++++++++++ src/video/sdl2_v.h | 2 ++ src/video/video_driver.hpp | 9 +++++++++ src/video/win32_v.cpp | 21 +++++++++++++++++++++ src/video/win32_v.h | 2 ++ 9 files changed, 84 insertions(+) (limited to 'src/video') diff --git a/src/video/allegro_v.cpp b/src/video/allegro_v.cpp index 0c064873b..3d8ca65aa 100644 --- a/src/video/allegro_v.cpp +++ b/src/video/allegro_v.cpp @@ -236,6 +236,16 @@ bool VideoDriver_Allegro::ClaimMousePointer() return true; } +std::vector VideoDriver_Allegro::GetListOfMonitorRefreshRates() +{ + std::vector rates = {}; + + int refresh_rate = get_refresh_rate(); + if (refresh_rate != 0) rates.push_back(refresh_rate); + + return rates; +} + struct AllegroVkMapping { uint16 vk_from; byte vk_count; diff --git a/src/video/allegro_v.h b/src/video/allegro_v.h index d67778b7f..d1576fea9 100644 --- a/src/video/allegro_v.h +++ b/src/video/allegro_v.h @@ -31,6 +31,8 @@ public: bool ClaimMousePointer() override; + std::vector GetListOfMonitorRefreshRates() override; + const char *GetName() const override { return "allegro"; } protected: diff --git a/src/video/cocoa/cocoa_v.h b/src/video/cocoa/cocoa_v.h index 371e4b5bc..3d3db5454 100644 --- a/src/video/cocoa/cocoa_v.h +++ b/src/video/cocoa/cocoa_v.h @@ -47,6 +47,8 @@ public: void EditBoxLostFocus() override; + std::vector GetListOfMonitorRefreshRates() override; + /* --- The following methods should be private, but can't be due to Obj-C limitations. --- */ void MainLoopReal(); diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm index 8d1fd4447..9fb74cd04 100644 --- a/src/video/cocoa/cocoa_v.mm +++ b/src/video/cocoa/cocoa_v.mm @@ -43,6 +43,7 @@ #import /* for MAXPATHLEN */ #import /* gettimeofday */ +#include /** * Important notice regarding all modifications!!!!!!! @@ -228,6 +229,30 @@ void VideoDriver_Cocoa::EditBoxLostFocus() HandleTextInput(nullptr, true); } +/** + * Get refresh rates of all connected monitors. + */ +std::vector VideoDriver_Cocoa::GetListOfMonitorRefreshRates() +{ + std::vector rates{}; + + if (MacOSVersionIsAtLeast(10, 6, 0)) { + std::array displays; + + uint32_t count = 0; + CGGetActiveDisplayList(displays.size(), displays.data(), &count); + + for (uint32_t i = 0; i < count; i++) { + CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displays[i]); + int rate = (int)CGDisplayModeGetRefreshRate(mode); + if (rate > 0) rates.push_back(rate); + CGDisplayModeRelease(mode); + } + } + + return rates; +} + /** * Get the resolution of the main screen. */ diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index 62675280c..2d8558534 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -237,6 +237,17 @@ void VideoDriver_SDL_Base::EditBoxLostFocus() } } +std::vector VideoDriver_SDL_Base::GetListOfMonitorRefreshRates() +{ + std::vector rates = {}; + for (int i = 0; i < SDL_GetNumVideoDisplays(); i++) { + SDL_DisplayMode mode = {}; + if (SDL_GetDisplayMode(i, 0, &mode) != 0) continue; + if (mode.refresh_rate != 0) rates.push_back(mode.refresh_rate); + } + return rates; +} + struct SDLVkMapping { SDL_Keycode vk_from; diff --git a/src/video/sdl2_v.h b/src/video/sdl2_v.h index b1a64dd9c..bd43f71f8 100644 --- a/src/video/sdl2_v.h +++ b/src/video/sdl2_v.h @@ -39,6 +39,8 @@ public: void EditBoxLostFocus() override; + std::vector GetListOfMonitorRefreshRates() override; + const char *GetName() const override { return "sdl"; } protected: diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp index 4b2de253a..8c2b9d437 100644 --- a/src/video/video_driver.hpp +++ b/src/video/video_driver.hpp @@ -149,6 +149,15 @@ public: */ virtual void EditBoxGainedFocus() {} + /** + * Get a list of refresh rates of each available monitor. + * @return A vector of the refresh rates of all available monitors. + */ + virtual std::vector GetListOfMonitorRefreshRates() + { + return {}; + } + /** * Get a suggested default GUI zoom taking screen DPI into account. */ diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 8030380ce..a802cdafa 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -916,6 +916,27 @@ void VideoDriver_Win32Base::EditBoxLostFocus() SetCandidatePos(this->main_wnd); } +std::vector VideoDriver_Win32Base::GetListOfMonitorRefreshRates() +{ + std::vector rates = {}; + EnumDisplayMonitors(nullptr, nullptr, [](HMONITOR hMonitor, HDC hDC, LPRECT rc, LPARAM data) -> BOOL { + auto &list = *reinterpret_cast*>(data); + + MONITORINFOEX monitorInfo = {}; + monitorInfo.cbSize = sizeof(MONITORINFOEX); + GetMonitorInfo(hMonitor, &monitorInfo); + + DEVMODE devMode = {}; + devMode.dmSize = sizeof(DEVMODE); + devMode.dmDriverExtra = 0; + EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &devMode); + + if (devMode.dmDisplayFrequency != 0) list.push_back(devMode.dmDisplayFrequency); + return true; + }, reinterpret_cast(&rates)); + return rates; +} + Dimension VideoDriver_Win32Base::GetScreenSize() const { return { static_cast(GetSystemMetrics(SM_CXSCREEN)), static_cast(GetSystemMetrics(SM_CYSCREEN)) }; diff --git a/src/video/win32_v.h b/src/video/win32_v.h index 93ebdd83e..f6ca291f1 100644 --- a/src/video/win32_v.h +++ b/src/video/win32_v.h @@ -33,6 +33,8 @@ public: void EditBoxLostFocus() override; + std::vector GetListOfMonitorRefreshRates() override; + protected: HWND main_wnd; ///< Handle to system window. bool fullscreen; ///< Whether to use (true) fullscreen mode. -- cgit v1.2.3-54-g00ecf