Provide a default refresh rate when it cannot be queried

This commit is contained in:
Tamás Bálint Misius
2025-01-23 23:31:38 +01:00
parent 79f2bbcae5
commit e87f404d90
4 changed files with 25 additions and 12 deletions

View File

@@ -146,14 +146,14 @@ void blit(pixel *vid)
void UpdateRefreshRate() void UpdateRefreshRate()
{ {
std::optional<int> refreshRate; RefreshRate refreshRate;
int displayIndex = SDL_GetWindowDisplayIndex(sdl_window); int displayIndex = SDL_GetWindowDisplayIndex(sdl_window);
if (displayIndex >= 0) if (displayIndex >= 0)
{ {
SDL_DisplayMode displayMode; SDL_DisplayMode displayMode;
if (!SDL_GetCurrentDisplayMode(displayIndex, &displayMode) && displayMode.refresh_rate) if (!SDL_GetCurrentDisplayMode(displayIndex, &displayMode) && displayMode.refresh_rate)
{ {
refreshRate = displayMode.refresh_rate; refreshRate = RefreshRateQueried{ displayMode.refresh_rate };
} }
} }
ui::Engine::Ref().SetRefreshRate(refreshRate); ui::Engine::Ref().SetRefreshRate(refreshRate);

View File

@@ -2618,13 +2618,13 @@ void GameView::OnDraw()
fpsInfo << "hindered"; fpsInfo << "hindered";
} }
fpsInfo << "\n Refresh rate: "; fpsInfo << "\n Refresh rate: ";
if (auto refreshRate = ui::Engine::Ref().GetRefreshRate()) auto refreshRate = ui::Engine::Ref().GetRefreshRate();
fpsInfo << std::visit([](auto &refreshRate) {
return refreshRate.value;
}, refreshRate);
if (std::holds_alternative<RefreshRateDefault>(refreshRate))
{ {
fpsInfo << *refreshRate; fpsInfo << " (default)";
}
else
{
fpsInfo << "unknown";
} }
} }

View File

@@ -370,7 +370,9 @@ std::optional<int> Engine::GetEffectiveDrawCap() const
} }
if (std::get_if<DrawLimitDisplay>(&drawLimit)) if (std::get_if<DrawLimitDisplay>(&drawLimit))
{ {
effectiveDrawCap = GetRefreshRate(); effectiveDrawCap = std::visit([](auto &&refreshRate) {
return refreshRate.value;
}, GetRefreshRate());
} }
return effectiveDrawCap; return effectiveDrawCap;
} }

View File

@@ -2,6 +2,7 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <stack> #include <stack>
#include <variant>
#include "common/String.h" #include "common/String.h"
#include "common/ExplicitSingleton.h" #include "common/ExplicitSingleton.h"
#include "graphics/Pixel.h" #include "graphics/Pixel.h"
@@ -10,6 +11,16 @@
#include <climits> #include <climits>
#include "FpsLimit.h" #include "FpsLimit.h"
struct RefreshRateDefault
{
int value = 60;
};
struct RefreshRateQueried
{
int value;
};
using RefreshRate = std::variant<RefreshRateDefault, RefreshRateQueried>;
class Graphics; class Graphics;
namespace ui namespace ui
{ {
@@ -96,7 +107,7 @@ namespace ui
Window* state_; Window* state_;
Point windowTargetPosition; Point windowTargetPosition;
bool ignoreEvents = false; bool ignoreEvents = false;
std::optional<int> refreshRate; RefreshRate refreshRate;
// saved appearances of windows that are in the backround and // saved appearances of windows that are in the backround and
// thus are not currently being redrawn // thus are not currently being redrawn
@@ -140,12 +151,12 @@ namespace ui
bool GetResizable () const { return windowFrameOps.resizable; } bool GetResizable () const { return windowFrameOps.resizable; }
bool GetBlurryScaling () const { return windowFrameOps.blurryScaling; } bool GetBlurryScaling () const { return windowFrameOps.blurryScaling; }
std::optional<int> GetRefreshRate() const RefreshRate GetRefreshRate() const
{ {
return refreshRate; return refreshRate;
} }
void SetRefreshRate(std::optional<int> newRefreshRate) void SetRefreshRate(RefreshRate newRefreshRate)
{ {
refreshRate = newRefreshRate; refreshRate = newRefreshRate;
} }