Best way to sync frames in SW 2D rendering?

Started by
0 comments, last by Ravyne 13 years, 1 month ago
Hi,

I'm currently using TinyPTC in GDI mode (yeah I know, outdated and stuff) to be able to use a backbuffer and flip it to the GDI surface, it's pretty neat for doing simple 2D rendering the old pixel-fashioned way.
I want to flip the buffer every frame, being 60Hz, how can I do this in a very timing-precise way?

Currently I use a very slow and unaccurate jittering way like this:


unsigned int buffer[WIDTH * HEIGHT];

static int next_tick = 0;
static int tick_interval = (int)(1.0f * 1000.0f / 60.0f);

void syncframe(void)
{
if (next_tick >= timeGetTime())
{
Sleep(next_tick - timeGetTime());
}

next_tick = timeGetTime() + tick_interval;
}

int main()
{
while (running)
{
/* clear buffer */

/* run functions that plot pixels to buffer */

/* flip buffer and sync frame */
ptc_update(buffer);
syncframe();
}
}

It sure does work, but it lags rhythmically. I know there's no way to get 100% fullspeed on a multitask system, because there are several low-level threads in use by the OS....
But there should at least be a way to get 60Hz smooth, 60Hz is not extremely fast - would triple buffering help? Any known tricks and tips to get this working in a simple way? As said, this is simple software 2D stuff.

Any help appreciated!
Advertisement
Unfotunately GDI and the win32 APIs don't expose the vsync signal.

Basically you do what you're doing now, more or less. You can probably smooth things out a bit more by sleeping for your current amount, less 5ms (the timer (or sleep) is only guaranteed accurate to 5ms increments, IIRC)... then busy-wait for the last <5ms. This may or may not actually prove better.

In my own renderer I do basically this, IIRC, and framerate is locked pretty tight on 60fps, with an occasional flash into 59fps reported.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement