Synchronized framerate

Hi,
Is it possible to force a refresh of a SDL renderer exactly when a screen
refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something (color fill)
and then wait (with SDL_Delay) for 20ms before drawing again. However with
any rendering backend (directx, opengl and software) and with vsync on and
off, sometimes I get exactly 50 frames in one second, sometimes 51.
Moreover this approach won’t work if I set the screen to 60Hz as you’d have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any pointer how
they would do it?

Cheers,
Vittorio

There’s a hint that can be used to enable and disable vsync. Just
replace its default value (ideally give the option to the user in the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync, SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:> Hi,

Is it possible to force a refresh of a SDL renderer exactly when a screen
refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something (color fill)
and then wait (with SDL_Delay) for 20ms before drawing again. However with
any rendering backend (directx, opengl and software) and with vsync on and
off, sometimes I get exactly 50 frames in one second, sometimes 51.
Moreover this approach won’t work if I set the screen to 60Hz as you’d have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any pointer how
they would do it?

Cheers,
Vittorio

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1” :
“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <@Sik_the_hedgehog>:> There’s a hint that can be used to enable and disable vsync. Just

replace its default value (ideally give the option to the user in the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly when a screen
refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something (color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again. However
with
any rendering backend (directx, opengl and software) and with vsync on
and
off, sometimes I get exactly 50 frames in one second, sometimes 51.
Moreover this approach won’t work if I set the screen to 60Hz as you’d
have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high
resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any pointer how
they would do it?

Cheers,
Vittorio

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
VittorioOn Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1” :
“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <sik.the.hedgehog at gmail.com>:

There’s a hint that can be used to enable and disable vsync. Just
replace its default value (ideally give the option to the user in the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly when a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something (color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again. However
with
any rendering backend (directx, opengl and software) and with vsync on
and
off, sometimes I get exactly 50 frames in one second, sometimes 51.
Moreover this approach won’t work if I set the screen to 60Hz as you’d
have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high
resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any pointer how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:> Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < @Sik_the_hedgehog> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1” :
“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <@Sik_the_hedgehog>:

There’s a hint that can be used to enable and disable vsync. Just
replace its default value (ideally give the option to the user in the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly when a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something (color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again. However
with
any rendering backend (directx, opengl and software) and with vsync on
and
off, sometimes I get exactly 50 frames in one second, sometimes 51.
Moreover this approach won’t work if I set the screen to 60Hz as you’d
have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high
resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well at least now we know more about the interaction between the hint and
the renderer.

Returning to the original question (Is it possible to force a refresh of a
SDL renderer exactly when a screen refresh happens?) is there any other
suggestion?
Best,
VittorioOn Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1” :
“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <sik.the.hedgehog at gmail.com>:

There’s a hint that can be used to enable and disable vsync. Just
replace its default value (ideally give the option to the user in the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly when a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something (color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again. However
with
any rendering backend (directx, opengl and software) and with vsync
on

and
off, sometimes I get exactly 50 frames in one second, sometimes 51.
Moreover this approach won’t work if I set the screen to 60Hz as
you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high
resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either a
SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:> Well at least now we know more about the interaction between the hint and

the renderer.

Returning to the original question (Is it possible to force a refresh of a
SDL renderer exactly when a screen refresh happens?) is there any other
suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <@Sik_the_hedgehog wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < @Sik_the_hedgehog> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1” :
“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <@Sik_the_hedgehog>:

There’s a hint that can be used to enable and disable vsync. Just
replace its default value (ideally give the option to the user in
the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own
setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly when a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something
(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again.
However
with
any rendering backend (directx, opengl and software) and with vsync
on

and
off, sometimes I get exactly 50 frames in one second, sometimes 51.
Moreover this approach won’t work if I set the screen to 60Hz as
you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high
resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Are you sure SDL_RenderPresent blocks the thread until the next frame
starts?
If that’s the case, then you shouldn’t be adding SDL_Delay between each
frame no?
How are you going to deal with events in the meantime otherwise?
VittorioOn Wed, Feb 6, 2013 at 9:39 AM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either a
SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Well at least now we know more about the interaction between the hint and
the renderer.

Returning to the original question (Is it possible to force a refresh of
a
SDL renderer exactly when a screen refresh happens?) is there any other
suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1” :
“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <sik.the.hedgehog at gmail.com>:

There’s a hint that can be used to enable and disable vsync. Just
replace its default value (ideally give the option to the user in
the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own
setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly when a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something
(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again.
However
with
any rendering backend (directx, opengl and software) and with
vsync

on

and
off, sometimes I get exactly 50 frames in one second, sometimes

Moreover this approach won’t work if I set the screen to 60Hz as
you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high
resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL_RenderPresent tells SDL to swap the front and back buffers. If
vsync is enabled, this will halt the thread until the current refresh
is over (effectively making it wait until the next frame starts).

I deal with events right after the video update (where this function
would be called) is done. There really isn’t much of point on
processing events more than once per frame (and if you don’t want to
be limited to frame steps then you probably don’t care about vsync for
starters).

Using SDL_Delay for timing is a bad idea, use timers instead. You will
probably need this for game logic timing anyway as you can’t predict
what will be the refresh rate of the video mode you’re using (unless
you’re forcing it, but that’s as bad as not letting the user change
the resolution and doesn’t work everywhere).

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:> Are you sure SDL_RenderPresent blocks the thread until the next frame

starts?
If that’s the case, then you shouldn’t be adding SDL_Delay between each
frame no?
How are you going to deal with events in the meantime otherwise?
Vittorio

On Wed, Feb 6, 2013 at 9:39 AM, Sik the hedgehog <@Sik_the_hedgehog wrote:

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either a
SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Well at least now we know more about the interaction between the hint
and
the renderer.

Returning to the original question (Is it possible to force a refresh
of
a
SDL renderer exactly when a screen refresh happens?) is there any other
suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <@Sik_the_hedgehog wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < @Sik_the_hedgehog> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1” :
“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <@Sik_the_hedgehog>:

There’s a hint that can be used to enable and disable vsync. Just
replace its default value (ideally give the option to the user in
the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own
setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly when
a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something
(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again.
However
with
any rendering backend (directx, opengl and software) and with
vsync

on

and
off, sometimes I get exactly 50 frames in one second, sometimes

Moreover this approach won’t work if I set the screen to 60Hz as
you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the high
resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

According to my tests, even when not polling for events and just calling
SDL_RenderPresent on a vsynced renderer I still get 50 to 51 fps.
Tested on win 7 using d3d, ogl and sw.

Probably using timers and an associated callback I could get a higher
resolution in respected timings, but I would get the same problem as soon
as i try to render something on 60Hz (where you have to call refresh every
16,6666 milliseconds).

Is there any other way that vsync and timers to get a frame refresh
synchronized with the screen framerate?
Even not using SDL if there is no such functionality?

Thanks,
VittorioOn Wed, Feb 6, 2013 at 11:21 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

SDL_RenderPresent tells SDL to swap the front and back buffers. If
vsync is enabled, this will halt the thread until the current refresh
is over (effectively making it wait until the next frame starts).

I deal with events right after the video update (where this function
would be called) is done. There really isn’t much of point on
processing events more than once per frame (and if you don’t want to
be limited to frame steps then you probably don’t care about vsync for
starters).

Using SDL_Delay for timing is a bad idea, use timers instead. You will
probably need this for game logic timing anyway as you can’t predict
what will be the refresh rate of the video mode you’re using (unless
you’re forcing it, but that’s as bad as not letting the user change
the resolution and doesn’t work everywhere).

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Are you sure SDL_RenderPresent blocks the thread until the next frame
starts?
If that’s the case, then you shouldn’t be adding SDL_Delay between each
frame no?
How are you going to deal with events in the meantime otherwise?
Vittorio

On Wed, Feb 6, 2013 at 9:39 AM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either a
SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Well at least now we know more about the interaction between the hint
and
the renderer.

Returning to the original question (Is it possible to force a refresh
of
a
SDL renderer exactly when a screen refresh happens?) is there any
other

suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ? “1”
:

“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <sik.the.hedgehog at gmail.com>:

There’s a hint that can be used to enable and disable vsync.
Just

replace its default value (ideally give the option to the user
in

the
program). Not sure if it requires reinitializing the video mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then you
probably want to override the hint to ensure the program’s own
setting
is used (if you don’t provide the setting then don’t override it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly
when

a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something
(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again.
However
with
any rendering backend (directx, opengl and software) and with
vsync

on

and
off, sometimes I get exactly 50 frames in one second, sometimes

Moreover this approach won’t work if I set the screen to 60Hz
as

you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the
high

resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

The only way to get perfect sync is to use vsync for timing and
nothing else. There’s absolutely nothing you can do about this, the
only thing in sync with the video signal is the video hardware itself.
Timers are not going to help you here.

Also even then expect to miss some frames, since the scheduling in any
multitasking operating system is completely unpredictable. It could
decide to resume the program right after a refresh, missing the vsync
and forcing the program to wait an entire frame.

If I was you I’d try to get as close to possible to perfect framerate
and stay there. External factors will always get in the way no matter
how hard you try.

2013/2/7, Vittorio Giovara <vitto.giova at yahoo.it>:> According to my tests, even when not polling for events and just calling

SDL_RenderPresent on a vsynced renderer I still get 50 to 51 fps.
Tested on win 7 using d3d, ogl and sw.

Probably using timers and an associated callback I could get a higher
resolution in respected timings, but I would get the same problem as soon
as i try to render something on 60Hz (where you have to call refresh every
16,6666 milliseconds).

Is there any other way that vsync and timers to get a frame refresh
synchronized with the screen framerate?
Even not using SDL if there is no such functionality?

Thanks,
Vittorio

On Wed, Feb 6, 2013 at 11:21 AM, Sik the hedgehog < @Sik_the_hedgehog> wrote:

SDL_RenderPresent tells SDL to swap the front and back buffers. If
vsync is enabled, this will halt the thread until the current refresh
is over (effectively making it wait until the next frame starts).

I deal with events right after the video update (where this function
would be called) is done. There really isn’t much of point on
processing events more than once per frame (and if you don’t want to
be limited to frame steps then you probably don’t care about vsync for
starters).

Using SDL_Delay for timing is a bad idea, use timers instead. You will
probably need this for game logic timing anyway as you can’t predict
what will be the refresh rate of the video mode you’re using (unless
you’re forcing it, but that’s as bad as not letting the user change
the resolution and doesn’t work everywhere).

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Are you sure SDL_RenderPresent blocks the thread until the next frame
starts?
If that’s the case, then you shouldn’t be adding SDL_Delay between each
frame no?
How are you going to deal with events in the meantime otherwise?
Vittorio

On Wed, Feb 6, 2013 at 9:39 AM, Sik the hedgehog <@Sik_the_hedgehog wrote:

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either a
SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Well at least now we know more about the interaction between the
hint
and
the renderer.

Returning to the original question (Is it possible to force a
refresh
of
a
SDL renderer exactly when a screen refresh happens?) is there any
other

suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <@Sik_the_hedgehog wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < @Sik_the_hedgehog> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ?
“1”
:

“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <@Sik_the_hedgehog>:

There’s a hint that can be used to enable and disable vsync.
Just

replace its default value (ideally give the option to the user
in

the
program). Not sure if it requires reinitializing the video
mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then
you
probably want to override the hint to ensure the program’s own
setting
is used (if you don’t provide the setting then don’t override
it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly
when

a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing
something
(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again.
However
with
any rendering backend (directx, opengl and software) and with
vsync

on

and
off, sometimes I get exactly 50 frames in one second,
sometimes

Moreover this approach won’t work if I set the screen to 60Hz
as

you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the
high

resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Is this just about how long to delay so you can hit the max framerate? If
vsync is on, just delay a minimal amount (SDL_Delay(10)) to be nice to the
OS and let vsync take care of throttling the framerate. There’s not much
more you can do.

If your program needs to know how fast time is going, then use one of:
Absolute timing (SDL_GetTicks()), incremental timing (SDL_GetTicks() -
lastFrameTime), or fixed rate (float dt = 0.010f seconds, so matter what).
Fixed rates are good for keeping physics simulations reliable and
deterministic.

Also, it’s sometimes useful to separate event processing/physics updates
and the drawing calls as much as you can. If you can run your event and
physics updates at a faster rate than the drawing framerate, then you’ll
have a very responsive program.

Jonny DOn Thu, Feb 7, 2013 at 10:41 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

The only way to get perfect sync is to use vsync for timing and
nothing else. There’s absolutely nothing you can do about this, the
only thing in sync with the video signal is the video hardware itself.
Timers are not going to help you here.

Also even then expect to miss some frames, since the scheduling in any
multitasking operating system is completely unpredictable. It could
decide to resume the program right after a refresh, missing the vsync
and forcing the program to wait an entire frame.

If I was you I’d try to get as close to possible to perfect framerate
and stay there. External factors will always get in the way no matter
how hard you try.

2013/2/7, Vittorio Giovara <vitto.giova at yahoo.it>:

According to my tests, even when not polling for events and just calling
SDL_RenderPresent on a vsynced renderer I still get 50 to 51 fps.
Tested on win 7 using d3d, ogl and sw.

Probably using timers and an associated callback I could get a higher
resolution in respected timings, but I would get the same problem as soon
as i try to render something on 60Hz (where you have to call refresh
every
16,6666 milliseconds).

Is there any other way that vsync and timers to get a frame refresh
synchronized with the screen framerate?
Even not using SDL if there is no such functionality?

Thanks,
Vittorio

On Wed, Feb 6, 2013 at 11:21 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

SDL_RenderPresent tells SDL to swap the front and back buffers. If
vsync is enabled, this will halt the thread until the current refresh
is over (effectively making it wait until the next frame starts).

I deal with events right after the video update (where this function
would be called) is done. There really isn’t much of point on
processing events more than once per frame (and if you don’t want to
be limited to frame steps then you probably don’t care about vsync for
starters).

Using SDL_Delay for timing is a bad idea, use timers instead. You will
probably need this for game logic timing anyway as you can’t predict
what will be the refresh rate of the video mode you’re using (unless
you’re forcing it, but that’s as bad as not letting the user change
the resolution and doesn’t work everywhere).

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Are you sure SDL_RenderPresent blocks the thread until the next frame
starts?
If that’s the case, then you shouldn’t be adding SDL_Delay between
each

frame no?
How are you going to deal with events in the meantime otherwise?
Vittorio

On Wed, Feb 6, 2013 at 9:39 AM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either
a

SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Well at least now we know more about the interaction between the
hint
and
the renderer.

Returning to the original question (Is it possible to force a
refresh
of
a
SDL renderer exactly when a screen refresh happens?) is there any
other

suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively
force

vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ?
“1”
:

“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <sik.the.hedgehog at gmail.com>:

There’s a hint that can be used to enable and disable vsync.
Just

replace its default value (ideally give the option to the
user

in

the
program). Not sure if it requires reinitializing the video
mode,
didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then
you
probably want to override the hint to ensure the program’s
own

setting
is used (if you don’t provide the setting then don’t override
it
please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly
when

a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing
something
(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing
again.

However
with
any rendering backend (directx, opengl and software) and
with

vsync

on

and
off, sometimes I get exactly 50 frames in one second,
sometimes

Moreover this approach won’t work if I set the screen to
60Hz

as

you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the
high

resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

2013/2/7, Jonathan Dearborn :

Is this just about how long to delay so you can hit the max framerate? If
vsync is on, just delay a minimal amount (SDL_Delay(10)) to be nice to the
OS and let vsync take care of throttling the framerate. There’s not much
more you can do.

Polling the events as usual should do the job, no need to use
SDL_Delay (in fact, using SDL_Delay can potentially make the framerate
more prone to be unstable - exactly the opposite of the goal here).

Can we see your loop and how you’re getting the frame rate? Otherwise, in
windows with some processors, the timers are only accurate to 10s of
milliseconds, which could possibly explain a 51 rather than a 60.

Try something like this to test:

#include

// … Setup your renderer with vsync enabled
UINT32 offsets[21];
offsets[0] = SDL_GetTicks();
for(int i=1; i < 21; i++)
{
SDL_RenderPresent( your_renderer );
offsets[i] = SDL_GetTicks();
}
for(int j=1; j < 21; j++)
printf( “Iteration #%d took %dms”, j, (offsets[j-1] - offsets[j]) );

It’s my suspicion that it’s just a timer resolution issue, but that loop
may give you some more insight into it.

Otherwise, I may suggest that it’s almost never a good idea to tie your
code to a specific framerate, since you have been able to demonstrate some
issues with milliseconds between frames. Take a look at a framerate
independent movement
http://lazyfoo.net/SDL_tutorials/lesson32/index.phparticle.On Thu, Feb 7, 2013 at 10:33 AM, Vittorio Giovara <vitto.giova at yahoo.it>wrote:

According to my tests, even when not polling for events and just calling
SDL_RenderPresent on a vsynced renderer I still get 50 to 51 fps.
Tested on win 7 using d3d, ogl and sw.

Probably using timers and an associated callback I could get a higher
resolution in respected timings, but I would get the same problem as soon
as i try to render something on 60Hz (where you have to call refresh every
16,6666 milliseconds).

Is there any other way that vsync and timers to get a frame refresh
synchronized with the screen framerate?
Even not using SDL if there is no such functionality?

Thanks,
Vittorio

On Wed, Feb 6, 2013 at 11:21 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

SDL_RenderPresent tells SDL to swap the front and back buffers. If
vsync is enabled, this will halt the thread until the current refresh
is over (effectively making it wait until the next frame starts).

I deal with events right after the video update (where this function
would be called) is done. There really isn’t much of point on
processing events more than once per frame (and if you don’t want to
be limited to frame steps then you probably don’t care about vsync for
starters).

Using SDL_Delay for timing is a bad idea, use timers instead. You will
probably need this for game logic timing anyway as you can’t predict
what will be the refresh rate of the video mode you’re using (unless
you’re forcing it, but that’s as bad as not letting the user change
the resolution and doesn’t work everywhere).

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Are you sure SDL_RenderPresent blocks the thread until the next frame
starts?
If that’s the case, then you shouldn’t be adding SDL_Delay between each
frame no?
How are you going to deal with events in the meantime otherwise?
Vittorio

On Wed, Feb 6, 2013 at 9:39 AM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either a
SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Well at least now we know more about the interaction between the hint
and
the renderer.

Returning to the original question (Is it possible to force a refresh
of
a
SDL renderer exactly when a screen refresh happens?) is there any
other

suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively force
vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ?
“1” :

“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <sik.the.hedgehog at gmail.com>:

There’s a hint that can be used to enable and disable vsync.
Just

replace its default value (ideally give the option to the user
in

the
program). Not sure if it requires reinitializing the video
mode,

didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then
you

probably want to override the hint to ensure the program’s own
setting
is used (if you don’t provide the setting then don’t override
it

please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly
when

a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing something
(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing again.
However
with
any rendering backend (directx, opengl and software) and with
vsync

on

and
off, sometimes I get exactly 50 frames in one second,
sometimes

Moreover this approach won’t work if I set the screen to 60Hz
as

you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the
high

resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Otherwise, I may suggest that it’s almost never a good idea to tie your
code to a specific framerate

I disagree, in fact a fixed frame rate, have a couple of advantages,
despite of its disavantages:

  • Easier to calculate movements, the code feels more readable, no *
    deltaTime everywhere
  • Easier to be deterministic, which is good if you want to create a replay
    system and/or make an online game.
  • Better for timing games (if you can keep the framerate well enough)

Of course, If you go with fixed framerate, it will be necessary to
implement a Frameskip system, which is not hard, but tricky.

Message: 1> Date: Thu, 7 Feb 2013 11:59:33 -0500

From: Alex Barry <alex.barry at gmail.com>
To: SDL Development List
Subject: Re: [SDL] synchronized framerate
Message-ID:
<
CAJSO58Mx4aa2sDtm15qmFh4uXZEYVjZtrweWNid18PnNWWsnUw at mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

Can we see your loop and how you’re getting the frame rate? Otherwise, in
windows with some processors, the timers are only accurate to 10s of
milliseconds, which could possibly explain a 51 rather than a 60.

Try something like this to test:

#include

// … Setup your renderer with vsync enabled
UINT32 offsets[21];
offsets[0] = SDL_GetTicks();
for(int i=1; i < 21; i++)
{
SDL_RenderPresent( your_renderer );
offsets[i] = SDL_GetTicks();
}
for(int j=1; j < 21; j++)
printf( “Iteration #%d took %dms”, j, (offsets[j-1] - offsets[j]) );

It’s my suspicion that it’s just a timer resolution issue, but that loop
may give you some more insight into it.

Otherwise, I may suggest that it’s almost never a good idea to tie your
code to a specific framerate, since you have been able to demonstrate some
issues with milliseconds between frames. Take a look at a framerate
independent movement
http://lazyfoo.net/SDL_tutorials/lesson32/index.phparticle.

On Thu, Feb 7, 2013 at 10:33 AM, Vittorio Giovara <vitto.giova at yahoo.it wrote:

According to my tests, even when not polling for events and just calling
SDL_RenderPresent on a vsynced renderer I still get 50 to 51 fps.
Tested on win 7 using d3d, ogl and sw.

Probably using timers and an associated callback I could get a higher
resolution in respected timings, but I would get the same problem as soon
as i try to render something on 60Hz (where you have to call refresh
every
16,6666 milliseconds).

Is there any other way that vsync and timers to get a frame refresh
synchronized with the screen framerate?
Even not using SDL if there is no such functionality?

Thanks,
Vittorio

On Wed, Feb 6, 2013 at 11:21 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

SDL_RenderPresent tells SDL to swap the front and back buffers. If
vsync is enabled, this will halt the thread until the current refresh
is over (effectively making it wait until the next frame starts).

I deal with events right after the video update (where this function
would be called) is done. There really isn’t much of point on
processing events more than once per frame (and if you don’t want to
be limited to frame steps then you probably don’t care about vsync for
starters).

Using SDL_Delay for timing is a bad idea, use timers instead. You will
probably need this for game logic timing anyway as you can’t predict
what will be the refresh rate of the video mode you’re using (unless
you’re forcing it, but that’s as bad as not letting the user change
the resolution and doesn’t work everywhere).

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Are you sure SDL_RenderPresent blocks the thread until the next frame
starts?
If that’s the case, then you shouldn’t be adding SDL_Delay between
each

frame no?
How are you going to deal with events in the meantime otherwise?
Vittorio

On Wed, Feb 6, 2013 at 9:39 AM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Enabling vsync should do the trick, that’s the whole point of vsync
after all. If that doesn’t work then it means something else (either
a

SDL bug or a driver bug or something like that) is interfering,
assuming you aren’t overriding it with an environment variable.

The program really shouldn’t need to do anything besides enabling
vsync. Then whenever SDL_RenderPresent is called the thread will halt
until the next frame starts.

2013/2/6, Vittorio Giovara <vitto.giova at yahoo.it>:

Well at least now we know more about the interaction between the
hint

and
the renderer.

Returning to the original question (Is it possible to force a
refresh

of
a
SDL renderer exactly when a screen refresh happens?) is there any
other

suggestion?
Best,
Vittorio

On Tue, Feb 5, 2013 at 3:33 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Welp, forgot about that. Went to check the code, and found this:

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
    if (*hint == '0') {
        flags &= ~SDL_RENDERER_PRESENTVSYNC;
    } else {
        flags |= SDL_RENDERER_PRESENTVSYNC;
    }
}

Looks like the hint overrides the flag if present and does nothing
otherwise. So yeah, go with the flag instead, sorry.

Interesting how I can set the hint to “01” and it effectively
force

vsync off, though… :stuck_out_tongue:

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Isn’t that the same of doing

SDL_CreateRenderer(sdlwindow, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);

or does the hint impacts a different section of code?
Vittorio

On Tue, Feb 5, 2013 at 11:37 AM, Sik the hedgehog < sik.the.hedgehog at gmail.com> wrote:

…and this is when I wish I could edit e-mails.

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync ?
“1” :

“0”, SDL_HINT_OVERRIDE);

Still, you get the idea.

2013/2/5, Sik the hedgehog <sik.the.hedgehog at gmail.com>:

There’s a hint that can be used to enable and disable vsync.
Just

replace its default value (ideally give the option to the
user

in

the
program). Not sure if it requires reinitializing the video
mode,

didn’t get around using it yet.

SDL_SetHint(SDL_HINT_RENDER_VSYNC, “1”);

If you’re providing the setting from within the program then
you

probably want to override the hint to ensure the program’s
own

setting
is used (if you don’t provide the setting then don’t override
it

please!):

SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, enable_vsync,
SDL_HINT_OVERRIDE);

2013/2/5, Vittorio Giovara <vitto.giova at yahoo.it>:

Hi,
Is it possible to force a refresh of a SDL renderer exactly
when

a
screen

refresh happens?

Right now I have the screen set at 50Hz, I’m drawing
something

(color
fill)
and then wait (with SDL_Delay) for 20ms before drawing
again.

However
with
any rendering backend (directx, opengl and software) and
with

vsync

on

and
off, sometimes I get exactly 50 frames in one second,
sometimes

Moreover this approach won’t work if I set the screen to
60Hz

as

you’d

have
to wait for 16,66666ms.

What can it be done about this? I was thinking of using the
high

resolution
timer but not sure I’m going into the right direction.
If it is not possible to do it with SDL, can anyone give any
pointer
how
they would do it?

Cheers,
Vittorio


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

-------------- next part --------------
An HTML attachment was scrubbed…
URL: <
http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20130207/cef76690/attachment.html



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

End of SDL Digest, Vol 74, Issue 12


2013/2/7, Rodrigo :

Of course, If you go with fixed framerate, it will be necessary to
implement a Frameskip system, which is not hard, but tricky.

I think the problem is that the approach that was being discussed
didn’t even take frameskip into account :confused: He wants perfect framerate
and nothing else.

Otherwise, I may suggest that it’s almost never a good idea to tie your
code to a specific framerate

I disagree, in fact a fixed frame rate, have a couple of advantages, despite
of its disavantages:

  • Easier to calculate movements, the code feels more readable, no *
    deltaTime everywhere
  • Easier to be deterministic, which is good if you want to create a replay
    system and/or make an online game.
  • Better for timing games (if you can keep the framerate well enough)

Very good points - which is why I tend to stick with this type of
solution. (The Fixed Rate Pig example, Kobo Deluxe and Kobo II so
far.)

However…

Of course, If you go with fixed framerate, it will be necessary to implement
a Frameskip system, which is not hard, but tricky.

Actual frame skipping is not very nice from an animation/smoothness
point of view - and it’s absolutely horrible if you can’t get a
display refresh rate very close to 60 Hz, or whatever you’ve designed
for. (Some displays only have a fixed refresh rate - and it’s not
always 60 Hz. In other cases, the OS or drivers won’t let you select
refresh rates at all. Basically cannot rely on that at all.)

The solution is to implement interpolation, also known as tweening in
this context. The downside of this is that it adds one “logic” frame
to the input-to-display latency, which isn’t desirable in fast action
games, obviously.

Another option is to use extrapolation. (I’m doing that by default in
Kobo II.) This can theoretically cause objects to overshoot slightly
at drastic velocity changes. However, if the game logic is running at
a “reasonable” rate (100 Hz in the case of Kobo II), this is only an
issue with extremely fast moving objects - so fast that they shouldn’t
really be rendered as plain sprites anyway. (Those objects would
instead “undershoot” with interpolation/tweening, so that’s not really
a solution either.)

I hope that makes some sense… :-DOn Thu, Feb 7, 2013 at 9:57 PM, Rodrigo wrote:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

Dunno, in my experience the framerate not matching the refresh rate
(or a multiple of it) isn’t really bad, unless it involves stuff like
blink-every-other-frame and such :stuck_out_tongue: Make sure to disable vsync in such
cases though, because otherwise it will get noticeable (ironically
in this case the tearing actually helps hide the rate mismatch)

This reminds me, some systems do really wacky stuff when vsync isn’t
enabled. Just look at this old screenshot and you’ll understand.
Curiously in motion it’s impossible to notice, in fact it pretty much
hides any sign of tearing at all. In this case the framerate was 60Hz
and the refresh rate was 85Hz.

http://sik.mdscene.net/.sol/Pantallazo-Sol-10.png

2013/2/7, David Olofson :> On Thu, Feb 7, 2013 at 9:57 PM, Rodrigo wrote:

Otherwise, I may suggest that it’s almost never a good idea to tie your
code to a specific framerate

I disagree, in fact a fixed frame rate, have a couple of advantages,
despite
of its disavantages:

  • Easier to calculate movements, the code feels more readable, no *
    deltaTime everywhere
  • Easier to be deterministic, which is good if you want to create a
    replay
    system and/or make an online game.
  • Better for timing games (if you can keep the framerate well enough)

Very good points - which is why I tend to stick with this type of
solution. (The Fixed Rate Pig example, Kobo Deluxe and Kobo II so
far.)

However…

Of course, If you go with fixed framerate, it will be necessary to
implement
a Frameskip system, which is not hard, but tricky.

Actual frame skipping is not very nice from an animation/smoothness
point of view - and it’s absolutely horrible if you can’t get a
display refresh rate very close to 60 Hz, or whatever you’ve designed
for. (Some displays only have a fixed refresh rate - and it’s not
always 60 Hz. In other cases, the OS or drivers won’t let you select
refresh rates at all. Basically cannot rely on that at all.)

The solution is to implement interpolation, also known as tweening in
this context. The downside of this is that it adds one “logic” frame
to the input-to-display latency, which isn’t desirable in fast action
games, obviously.

Another option is to use extrapolation. (I’m doing that by default in
Kobo II.) This can theoretically cause objects to overshoot slightly
at drastic velocity changes. However, if the game logic is running at
a “reasonable” rate (100 Hz in the case of Kobo II), this is only an
issue with extremely fast moving objects - so fast that they shouldn’t
really be rendered as plain sprites anyway. (Those objects would
instead “undershoot” with interpolation/tweening, so that’s not really
a solution either.)

I hope that makes some sense… :smiley:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Dunno, in my experience the framerate not matching the refresh rate
(or a multiple of it) isn’t really bad, unless it involves stuff like
blink-every-other-frame and such :stuck_out_tongue: Make sure to disable vsync in such
cases though, because otherwise it will get noticeable (ironically
in this case the tearing actually helps hide the rate mismatch)

Different frames of reference, I think… :slight_smile:

I’m used to the silky smooth scrolling and animation of arcade
machines and 8/16 bit consoles and computers, where games were
hardcoded for a known, fixed video standard, such as PAL or NTSC. The
only way to get close to that these days is with retrance sync’ed
rendering and subpixel positioning. (That is, you pretty much have to
use OpenGL or Direct3D, unless you’re using very high resolutions.)

This reminds me, some systems do really wacky stuff when vsync isn’t
enabled. Just look at this old screenshot and you’ll understand.
Curiously in motion it’s impossible to notice, in fact it pretty much
hides any sign of tearing at all. In this case the framerate was 60Hz
and the refresh rate was 85Hz.

http://sik.mdscene.net/.sol/Pantallazo-Sol-10.png

Yep, you can get really weird effects from the combination of tearing
and not rendering progressively (scanline by scanline) into the frame
buffer…On Thu, Feb 7, 2013 at 11:10 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com> wrote:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

2013/2/7, David Olofson :

Yep, you can get really weird effects from the combination of tearing
and not rendering progressively (scanline by scanline) into the frame
buffer…

The rendering progressively part should be taken care by double
buffering. Also that screenshot is particularly fun in that it seems
to be using data from three frames at the same time. I really don’t
know what’s going on, but whatever it is, tearing seems to be
impossible to notice during motion (I needed a screenshot to notice
it’s happening in the first place).