# Waiting for vsync with SDL 1.3 and DirectX

**URL:** <https://discourse.libsdl.org/t/waiting-for-vsync-with-sdl-1-3-and-directx/17646>\
**Category:** SDL Development\
**Created:** [September 21, 2010, 7:01pm UTC](https://discourse.libsdl.org/t/waiting-for-vsync-with-sdl-1-3-and-directx/17646 "2010-09-21T19:01:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxim\_Khitrov](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Maxim\_Khitrov](https://discourse.libsdl.org/u/Maxim_Khitrov)\
**Post date:** [September 21, 2010, 7:01pm UTC](https://discourse.libsdl.org/t/waiting-for-vsync-with-sdl-1-3-and-directx/17646/1 "2010-09-21T19:01:06Z")

</div>

Hello everyone,

I have fairly limited experience with SDL, but I’m figuring things out  
one bit at a time. I have a problem that requires my software to know  
as close as possible when the next frame is drawn on the screen. That  
basically means that I need to have at least 2 buffers - one that I  
can draw on without anything showing up on the monitor, and an active  
buffer that is being displayed. At some point, I need to issue a  
command that will wait until the next vertical refresh cycle and swap  
the two buffers as quickly as possible.

I’m using SDL 1.3 with DirectX driver on Windows XP. Right now I’m at  
a point where I can create a full-screen window, attach the correct  
renderer, and do some basic color changes using  
SDL\_SetRenderDrawColor, SDL\_RenderClear, and SDL\_RenderPresent. The  
renderer is initialized using the following flags:

SDL\_RENDERER\_PRESENTFLIP2  
SDL\_RENDERER\_PRESENTVSYNC  
SDL\_RENDERER\_ACCELERATED

Unfortunately, I don’t have sufficient understanding of DirectX or SDL  
to know whether will give me the desired results. I’d appreciate any  
example that shows how to properly swap video buffers just prior to a  
vertical refresh.

They key is that this must be a synchronous operation. If the swap  
command is issued just after the previous refresh, I need it to block  
for however many milliseconds necessary until the next refresh cycle.

Thanks for your help,

- Max

---

<div class="post-metadata">

**Author:** ![Maxim\_Khitrov](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Maxim\_Khitrov](https://discourse.libsdl.org/u/Maxim_Khitrov)\
**Post date:** [September 22, 2010, 1:58pm UTC](https://discourse.libsdl.org/t/waiting-for-vsync-with-sdl-1-3-and-directx/17646/2 "2010-09-22T13:58:44Z")

</div>

Hello again,

I’ve done a bit more experimenting this morning. It looks like using  
SDL\_RENDERER\_PRESENTVSYNC for initialization and calling  
SDL\_RenderPresent function does almost what I need. If I call this  
function in a loop while counting the number of iterations per second  
(or FPS), it gives me exactly the refresh rate of the monitor.

However, the first 3 or 4 iterations return immediately. Here is the  
actual number of seconds that each call to SDL\_RenderPreset took,  
starting with the first:

0.002488  
0.000065  
0.000066  
0.006132  
0.013291  
0.013291  
0.013291  
…

The refresh rate is 75 Hz, so 13.3 ms is exactly what I should be  
getting. Does anyone have an explanation for why the first few frames  
are rendered instantly? Is it the fact that vsync goes into effect  
only after the first frame is shown and my loop is able to generate 4  
or 5 frames prior to the first refresh cycle?

My final question is regarding the actual state of the display when  
SDL\_RenderPresent returns. In my first message I asked for a function  
that would block until just before the next refresh. However, I have a  
feeling that SDL\_RenderPresent blocks until just after.

Once we’re past the display of the first frame, am I correct in  
assuming that SDL\_RenderPresent will return as soon as the buffer flip  
was performed, which can happen only after the previous refresh is  
finished, and only once per refresh cycle. Which of the following two  
sequences is correct for understanding what is going on in the  
background?

Sequence 1:

1. SDL\_RenderPresent call 1 blocks
2. SDL\_RenderPresent call 1 returns
3. Monitor refresh
4. SDL\_RenderPresent call 2 blocks
5. SDL\_RenderPresent call 2 returns
6. Monitor refresh

Sequence 2:

1. SDL\_RenderPresent call 1 blocks
2. Monitor refresh
3. SDL\_RenderPresent call 1 returns
4. SDL\_RenderPresent call 2 blocks
5. Monitor refresh
6. SDL\_RenderPresent call 2 returns

- MaxOn Tue, Sep 21, 2010 at 3:01 PM, Maxim Khitrov \<@Maxim\_Khitrov\> wrote:

> Hello everyone,
> 
> I have fairly limited experience with SDL, but I’m figuring things out  
> one bit at a time. I have a problem that requires my software to know  
> as close as possible when the next frame is drawn on the screen. That  
> basically means that I need to have at least 2 buffers - one that I  
> can draw on without anything showing up on the monitor, and an active  
> buffer that is being displayed. At some point, I need to issue a  
> command that will wait until the next vertical refresh cycle and swap  
> the two buffers as quickly as possible.
> 
> I’m using SDL 1.3 with DirectX driver on Windows XP. Right now I’m at  
> a point where I can create a full-screen window, attach the correct  
> renderer, and do some basic color changes using  
> SDL\_SetRenderDrawColor, SDL\_RenderClear, and SDL\_RenderPresent. The  
> renderer is initialized using the following flags:
> 
> SDL\_RENDERER\_PRESENTFLIP2  
> SDL\_RENDERER\_PRESENTVSYNC  
> SDL\_RENDERER\_ACCELERATED
> 
> Unfortunately, I don’t have sufficient understanding of DirectX or SDL  
> to know whether will give me the desired results. I’d appreciate any  
> example that shows how to properly swap video buffers just prior to a  
> vertical refresh.
> 
> They key is that this must be a synchronous operation. If the swap  
> command is issued just after the previous refresh, I need it to block  
> for however many milliseconds necessary until the next refresh cycle.
> 
> Thanks for your help,
> 
> - Max
