Update refresh rate when the window is moved to another display

This commit is contained in:
Tamás Bálint Misius
2024-12-21 23:55:50 +01:00
parent 5ff963a40d
commit 2286440b46

View File

@@ -144,6 +144,21 @@ void blit(pixel *vid)
SDL_RenderPresent(sdl_renderer);
}
void UpdateRefreshRate()
{
std::optional<int> refreshRate;
int displayIndex = SDL_GetWindowDisplayIndex(sdl_window);
if (displayIndex >= 0)
{
SDL_DisplayMode displayMode;
if (!SDL_GetCurrentDisplayMode(displayIndex, &displayMode) && displayMode.refresh_rate)
{
refreshRate = displayMode.refresh_rate;
}
}
ui::Engine::Ref().SetRefreshRate(refreshRate);
}
void SDLOpen()
{
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
@@ -156,7 +171,6 @@ void SDLOpen()
SDLSetScreen();
int displayIndex = SDL_GetWindowDisplayIndex(sdl_window);
std::optional<int> refreshRate;
if (displayIndex >= 0)
{
SDL_Rect rect;
@@ -165,13 +179,8 @@ void SDLOpen()
desktopWidth = rect.w;
desktopHeight = rect.h;
}
SDL_DisplayMode displayMode;
if (!SDL_GetCurrentDisplayMode(displayIndex, &displayMode) && displayMode.refresh_rate)
{
refreshRate = displayMode.refresh_rate;
}
}
ui::Engine::Ref().SetRefreshRate(refreshRate);
UpdateRefreshRate();
StopTextInput();
}
@@ -431,6 +440,10 @@ static void EventProcess(const SDL_Event &event)
calculatedInitialMouse = true;
}
break;
case SDL_WINDOWEVENT_DISPLAY_CHANGED:
UpdateRefreshRate();
break;
}
break;
}