[SDL] How calculate the frame per seconds
David Olofson
david at olofson.net
Tue May 18 06:39:53 PDT 2004
On Tuesday 18 May 2004 15.24, NighTiger wrote:
> I have write this code:
>
> newtime = 0;
> while ( !quit )
> {
> oldtime = newtime;
> newtime = SDL_GetTicks();
> quit = Keyboard();
> DrawObject();
> fps = (newtime - oldtime);
> printf("%i\n", fps);
> }
>
> it's wrong?
Yeah, that's "mspf"; milliseconds per frame. What you want is
fps = 1 / (newtime - oldtime);
(Check that newtime != oldtime, of course, or you'll get a division by
zero exception if the fps goes above 1000 and/or the timer has a
hickup...)
However, you'll probably want some filtering or averaging on that.
Timing one frame at a time isn't very accurate, part because of
scheduling jitter; part because there just aren't too many ms per
frame with decent frame rates.
The easiest way is to just calculate the average fps every N frames.
That has the bonus effect of keeping the on-screen fps display from
flickering hysterically.
//David Olofson - Programmer, Composer, Open Source Advocate
.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,... |
`-----------------------------------> http://audiality.org -'
--- http://olofson.net --- http://www.reologica.se ---
More information about the SDL
mailing list