How do I get the vsync rate?

I want the number that gets used for vsync.

I’m not sure about the differences between monitor refresh rate and os display rate or program display rate, but basically, I want to know what the vsync fps is.

I looked in SDL_RenderPresent(), and if you simulate vsync with SDL_RenderSimulateVSync(), the rate is already stored in the render struct in renderer->simulate_vsync_interval (at least that’s how I interpreted it)

But for the regular vsync, I don’t know.

I found several functions to get info about the display, but I am unsure which one I need:


The last one is the struct, and in the wiki it says you need to initialize the last one to 0, but the example at the bottom goes out of its way to not initialize it even with a simple = {0} :skull:

I don’t need the continuous display rate, just the one that gets made after creating the renderer (hope the user doesn’t change the rate in the middle of the program or I would have to listen for it maybe. But I think I’m good with just the general idea of what the vsync is. I’m not doing anything important with it, just some sanity checks.)

Thanks! :blush:

SDL_GetCurrentDisplayMode is probably what you want.

SDL_GetDesktopDisplayMode might not give you the correct value in fullscreen mode.

1 Like

Do I have to free mode.driverdata ? How do I know when I have to free stuff manually and when not?

Don’t even touch it!

If you have to release something, it is mentioned in the documentation.

1 Like

I don’t think it’s necessary for the “get” functions because they will initialize it for you.

Do I have to free mode.driverdata ?


How do I know when I have to free stuff manually and when not?

If the wiki doesn’t mention anything I would assume it doesn’t need to be freed.

The exception is if the function name contains something like “Create” or “Alloc” in the name then there is often a corresponding “Destroy” or “Free” function that you’re supposed to use. For example SDL_CreateRGBSurface/SDL_FreeSurface and SDL_AllocFormat/SDL_FreeFormat.

1 Like

Any reason why CurrentDisplay would ever return a different refresh_rate than DesktopDisplay?

Also, how do I know which display_index I should use? With SDL_GetWindowDisplayIndex()?

You are right about the freeing. SDL_GetCurrentDisplayMode just copies the current display’s SDL_VideoDisplay struct, which has a member called current_mode, which is actually a SDL_DisplayMode struct. So nothing gets generated that hasn’t already existed.

SDL_SetWindowDisplayMode allows you to set the display mode to be used in fullscreen mode. It doesn’t necessarily have the same refresh rate as your normal desktop mode.

Also, how do I know which display_index I should use? With SDL_GetWindowDisplayIndex()?

Yeah, I think so, but I have no idea what will happen if you have a window that is partially visible on multiple monitors with different refresh rates. The docs for SDL_GetWindowDisplayIndex says it will return the one that contains the center of the window but is that guaranteed to be the one that dictates the vsync rate for the window?

Under normal circumstances I don’t think it should be necessary to know the exact refresh rate. If your game runs too slow and/or the refresh rate is too high you wouldn’t be able to keep up with it anyway but your game should still work.

1 Like

But… If you can literally override both the window and desktop display modes, how would VSYNC even work properly?

My understanding was that your literal monitor displays images at x Hertz, and the GPU then tries to sync the timing and update the screen at the same rate so no frames are half-drawn between frames.

So either refresh_rate has no connection to VSYNC, or only does initially.

PS: does this mean you can control the refresh rate of the program by editing this value? I thought you needed to sleep manually to achieve limited FPS without VSYNC on

Your monitor might support multiple refresh rates. If you use a lower resolution you might be able to use a higher refresh rate and vice versa.

Use SDL_GetNumDisplayModes and SDL_GetDisplayMode to check what display modes are supported.

1 Like