# How calculate the frame per seconds

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?
tnx

All you are calculating there is the time per frame. You want to find how
many times you can do that per second. So fps should be 1.f / (
(float)(newtime - oldtime) / 1000.f ). Although you would be better of
taking a sample over a number of frames…

Will McGugan> -----Original Message-----

From: sdl-bounces+will=willmcgugan.com at libsdl.org
[mailto:sdl-bounces+will=willmcgugan.com at libsdl.org]On Behalf Of
NighTiger
Sent: 18 May 2004 14:25
To: SDL
Subject: [SDL] How calculate the frame per seconds

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?
tnx

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

That’s the msec difference, do this to get FPS:

``````	msec = SDL_GetTicks() - frameStart;
if(msec > 0)
fps = 1000.0 / (double) msec;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?
tnx

Simon Ejsing, Systemudvikler
esoft ApS, http://www.esoft.dk
Skibhusvej 52C, DK-5000 Odense C.
Tlf: 70 222 466, Fax: 63 122 466

I’d be interested to see what others do. What I do is I take the amount of time since the previous frame, invert it (frequency), and average it with a weighted number (the number reported on the previous frame). I do this so that the fps will rarely flip between close numbers (such as wiggling quickly between 29 and 30-- that creates a headache).

The problem with your code is you aren’t counting frames per second, you’re describing the period (amount of time between two frames). Invert the amount of time between two frames (1 / period) in seconds to get the frequency. If I had my code available right now I’d past it. Do note I said “in seconds”, not miliseconds, less you’ll get what… kilofps? :)-----Original Message-----
From: sdl-bounces+bjc9019=rit.edu at libsdl.org on behalf of NighTiger
Sent: Tue 2004-05-18 09:24
To: SDL
Cc:
Subject: [SDL] How calculate the frame per seconds

``````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?
tnx

_______________________________________________
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
``````

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/ms-tnef
Size: 4618 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040518/64a3a097/attachment.bin

I’m using this kind of FPS code. After drawing a frame, I increment
frames_drawn by one. In the main loop I keep track of the time and
update the ‘fps’ variable which holds the FPS count once per second.

------------------snip-----------------------

// Here’s the vars I’m using before the loop
int frames_drawn = 0;
Uint32 fps_counter = 0;
float fps = 0.0f;
Uint32 prev_ticks = SDL_GetTicks;

--------loop---------

Uint32 ticks_now = SDL_GetTicks();
Uint32 diff = ticks_now - prev_ticks;
fps_counter += diff;
prev_ticks = ticks_now;

if(fps_counter >= 1000) {
fps = (float)frames_drawn / (float)(fps_counter/1000.0f);
frames_drawn = 0;
fps_counter = 0;
}

// Now var ‘fps’ contains the FPS amount–
Mika Halttunen
@Mika_Halttunen

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.nethttp://www.reologica.se —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);

Oops. Make that
fps = 1000.0f / (newtime - oldtime);

(Still dealing with ms here.

//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.nethttp://www.reologica.se —On Tuesday 18 May 2004 15.39, David Olofson wrote: