PDA

View Full Version : help with frames per second

mdog1234
12-06-2002, 09:56 AM
I am having trouble calculating my frames per second. I have a function that computes it but I know its wrong because its always less than 1 and there is no way thats right because I can move my object around and it doesnt even flicker.

here is my function what am I doing wrong

void calc_fps()
{
char fps[20] = {0};
float time,cur_time;
float total;
static float prev_time = 0.0;

time=glutGet(GLUT_ELAPSED_TIME);

cur_time=time-prev_time;

prev_time=time;

total = cur_time/1000.0;sprintf(fps,"FPS:%f", total);

}

jebus
12-06-2002, 10:00 AM
well, your taking the difference in times between frames (a very small number) and dividing it by 1000 (a larger number). this will always be below 1. search these forums for FPS calculation. in short, you want to calculate the time between each frame, but average it with the past 10 frames or so.

jebus

yakuza
12-06-2002, 11:32 AM
Dividing by 1000 isn't the problem, as glutGet(GLUT_ELAPSED_TIME) returns the time since glutInit was called (or the first time glutGet(GLUT_ELAPSED_TIME) was called) in milliseconds.

edit: I was wrong, gotta think about it some more.

[This message has been edited by yakuza (edited 12-06-2002).]

12-06-2002, 12:12 PM
The problem in your code is:
total = cur_time/1000.0;
should be:
total = 1000.0/cur_time;

ie: If the difference is 10 (10 milliseconds) then the fps is 100
If the difference is 50 ms then fps is 20

I am also assuming your code is "simplified" for this post - since your fps[] array is declared local to calc_fps(), it is unaccessible after the return, and nothing is done with it other than the sprintf. (Just checking!!)

a simple way of averaging would be:
#define FPS_UPDATE_INTERVAL 5 // 5 updates per second
char fps[20] = {0};

void calc_fps()
{
float time,cur_time;
static float total=0;
static float prev_time = 0.0;
static int count = 0;

time=glutGet(GLUT_ELAPSED_TIME);
cur_time=time-prev_time;

count++;

if (cur_time >= 1000.0/FPS_UPDATE_INTERVAL) {
total = count * 1000 / cur_time;
prev_time = time;
count = 0;
}

sprintf(fps,"FPS:%f", total);
}

I don't use glut so it's not tested but I use a similar approach in my code

mdog1234
12-06-2002, 12:26 PM
thanks everyone