FPS working but too fast....

what should i use?? i tried usleep() but that is a linux call. is there a windows version of usleep? maybe i have the wrong header file i dunno. anyway i got my FPS to show up but it is doing it every frame rather i would like to do it every 20 or 25 frames, or every second maybe. what would be the best way to go about this. printing out every frame per second is so fast you cant tell really what the number is. thanks in advance!

You can use GetTickCount() which is in windows.h
It gives you the number of seconds since your program started. So you could use it in the following way to time your program.

int DELAY = 30; //meaning update screen every 30 ms

int start;
int current;
void Init() {
start = GetTickCount();
}

void Draw()
{
//drawing code
}

void Update()
{
current=GetTickCount();
if(current-start > DELAY)
{
start=current;
Draw();
}
}

ok thanks but how does update get called? i am using Glut to get the elapsed time to display by FPS on the screen just FYI. heres how my program is layed out.

void initStuff()
{
GL Calls etc etc
BuildFont2D(); //loads 2D Font

return TRUE;
}

void DrawGLScene();
{
//all the usual stuff

static int lasttime;
int dt;
dt=glutGet(GLUT_ELAPSED_TIME)-lasttime;
lasttime=glutGet(GLUT_ELAPSED_TIME);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
printSetup2D(frames(dt)); //prints FPS

drawData(); //draws something located in a header file, it could be anything.
glFlush();
return TRUE;

} //end drawGLScene

//heres what frames does(called by printSetup2D above)

float frames(int LastFrameTime)
{
static int LastTimes[10]={0,0,0,0,0,0,0,0,0,0};
static int c=0;
int i;
float average=0;
c=(c+1)%10;
LastTimes[c]=LastFrameTime;

for(i=0;i<10;i++)
average+=LastTimes[i];
return 1000.0/(average/10.0);
}

[This message has been edited by cutting_crew (edited 11-13-2003).]

You folks are making this too complicated, IMO.

Step 1: write a general-use function called something like getTime() that returns a time in seconds (using doubles, ideally):

double getTime()
{
static LARGE_INTEGER newCount, frequency;
static bool first = TRUE;

if (first)
{
QueryPerformanceFrequency(&frequency);
first = false;
}

QueryPerformanceCounter(&newCount);
return (double)newCount.QuadPart / (double)frequency.QuadPart;
}

[This can obviously be optimized. You may also want to subtract out the time it takes to call getTime() for certain calculations.]

Step two: whenever you want to know the fps:

if (++frames_elapsed > wait)
{
// don’t forget to initialize vars
this_time = getTime();
fps = (double)frames_elapsed / (this_time - last_time);
last_time = this_time;
frames_elapsed = 0;
}

You can wait as many frames as you want between computations and fps will naturally contain the average FPS over that span of time.

Avi

[This message has been edited by Cyranose (edited 11-13-2003).]