Calculate simulated vsync interval based on display refresh rate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index aa6bd40..aaf2e85 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -933,6 +933,26 @@ static SDL_RenderLineMethod SDL_GetRenderLineMethod()
}
}
+static void SDL_CalculateSimulatedVSyncInterval(SDL_Renderer *renderer, SDL_Window *window)
+{
+ /* FIXME: SDL refresh rate API should return numerator/denominator */
+ int refresh_rate = 0;
+ int display_index = SDL_GetWindowDisplayIndex(window);
+ SDL_DisplayMode mode;
+
+ if (display_index < 0) {
+ display_index = 0;
+ }
+ if (SDL_GetDesktopDisplayMode(display_index, &mode) == 0) {
+ refresh_rate = mode.refresh_rate;
+ }
+ if (!refresh_rate) {
+ /* Pick a good default refresh rate */
+ refresh_rate = 60;
+ }
+ renderer->simulate_vsync_interval = (1000 / refresh_rate);
+}
+
SDL_Renderer *
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
{
@@ -1022,6 +1042,7 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
}
}
+ SDL_CalculateSimulatedVSyncInterval(renderer, window);
VerifyDrawQueueFunctions(renderer);
@@ -4273,7 +4294,7 @@ static void
SDL_RenderSimulateVSync(SDL_Renderer * renderer)
{
Uint32 now, elapsed;
- const Uint32 interval = (1000 / 60); /* FIXME: What FPS? */
+ const Uint32 interval = renderer->simulate_vsync_interval;
if (!interval) {
/* We can't do sub-ms delay, so just return here */
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 6deb5b2..86d5eaa 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -211,6 +211,7 @@ struct SDL_Renderer
/* Whether we should simulate vsync */
SDL_bool wanted_vsync;
SDL_bool simulate_vsync;
+ Uint32 simulate_vsync_interval;
Uint32 last_present;
/* The logical resolution for rendering */