Speed limit?

Hi,

I’ve been coding some simple OpenGL programs, and using SDL_GetTicks,
have measured its performance. As they are running at over 200fps
average, which is not very useful, I would like to know if there’s some
simple technique to delay the code to put a limit to this, for example
at 50fps.

I’m coding a very simple loop like this:

while ( ! done ) {
DrawGLScene();
{ SDL_Event event;
while ( SDL_PollEvent(&event) ) {
if ( event.type == SDL_QUIT ) {
done = 1;
}
if ( event.type == SDL_KEYDOWN ) {
if ( event.key.keysym.sym == SDLK_ESCAPE ) {
done = 1;
}
}
}
}
}

Also, I would like to know if SDL_GetTicks is suitable for finding out
how much time has passed between two consecutive frames; i.e. at current
speeds, there are 4 ticks between most frames, which is rather curious.

Best regards,

Alex

Alex the Koala wrote:

Hi,

I’ve been coding some simple OpenGL programs, and using SDL_GetTicks,
have measured its performance. As they are running at over 200fps
average, which is not very useful, I would like to know if there’s some
simple technique to delay the code to put a limit to this, for example
at 50fps.

here’s an idea:

Uint32 next_draw_tick = 0, cur_tick;

while ( ! done ) {

cur_tick = SDL_GetTicks();

if ( cur_tick >= next_draw_tick ) {
    DrawGLScene();
    next_draw_tick = cur_tick + 20;
}

... event handling here ...

}

this delays the drawing by at least 20 ms, making 50 fps
the best attainable fps.

jw–
// John Watson
// Software Engineer – STScI Archive Team