Frames Per Second??

I’m sorry if this has been asked before, i’d really like to find out how u can display the frames per second in you app…i’v seen some apps that display the frames per second in the title bar of the app … can anyone help me out? Thank you

#ifndef TIMING_H
#define TIMING_H

#include “main.h”

class CTimer
{
private:
__int64 base;
__int64 freq;
public:
CTimer()
{
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
}
~CTimer()
{
}
void UpdateBase(void);
float TimeElapsed(void);
float FPS(void);
};

#endif

#include “Timing.h”

void CTimer::UpdateBase()
{
QueryPerformanceCounter((LARGE_INTEGER*)&base);
}

float CTimer::TimeElapsed()
{
__int64 cur;
QueryPerformanceCounter((LARGE_INTEGER*)&cur);
if(cur>=base)
return (float)((double)(cur-base)/(double)freq);
else
return (float)((double)(base-cur)/(double)freq);
}

float CTimer::FPS()
{
__int64 cur;
QueryPerformanceCounter((LARGE_INTEGER*)&cur);
if(cur>=base)
return (float)((double)freq/(double)(cur-base));
else
return (float)((double)freq/(double)(base-cur));
}

And to display you need smthn like glutSetWindowTitle?? or SetWindowTitle(&hWnd) just a quick guess… But It’s better to use wgl/xglFontOutlines to display text ontop of your rendering.

Oh yes, it is hard to display fps in ANSI C++… It is not odd today that you have work by OpenGL and ask such silly questions. But is it etic to wait an answer? Im complitely sure that you have a boss and the work, cause you have to use freeware. Does real OGL lover need C++? Im using Delphi and there is no problems at all. OK, I ll tell you. After each frame you increase framecount. Then you must have timer that acts, for example, every two seconds. In timer event procedure you must divide framecount by 2 and that will be framerate. And dont forget to null the counter!

When you will be pushed into your megamillion project, please let me join you.

[This message has been edited by Azazel (edited 01-26-2004).]

[This message has been edited by Azazel (edited 01-26-2004).]

Hi,

Try to grab the fps example in this link.
http://myopendemo.hypermart.net

thanks a lot M/\dm/
and cwc36, that helped me out a lot … pretty good site cwc36 got a few good tuts on it

thanks a lot M/\dm/
and cwc36, that helped me out a lot … pretty good site cwc36 got a few good tuts on it

void FramesPerSecond()
{
static int framesPerSecond = 0; // This will store our fps
static long lastTime = 0;
char strFrameRate[50];
static long currentTime = 0;

time(&currentTime);

if( currentTime != lastTime)					//if turn to new second
{
	lastTime = currentTime;						// old time = current time
	sprintf(strFrameRate,"Current Frames Per Second: %d ", int(framesPerSecond));
	framesPerSecond = 0;						// set the framer per second to 0
	glutSetWindowTitle(strFrameRate);			// display the text on the title window
}
else
	++framesPerSecond;							// increase number of frame

}

Originally posted by Azazel:
After each frame you increase framecount. Then you must have timer that acts, for example, every two seconds. In timer event procedure you must divide framecount by 2 and that will be framerate. And dont forget to null the counter!

I guess I’ll have to repeat that this method does not work very well in some cases since it really samples the framerate only every N seconds. For my application, everything works much better by using a per-frame approach. This introduces some unnoticable performance hit and has a lot of advantages over the average-fps-over-time method. For example, while developing a culling algo you can get imemdiate feedback.
Also consider a user having 2000fps looking at $direction but only 2fps looking at $anotherDirection. He’ll be somewhat disappointed by seeing thathis average framerate is still 1999fps.

I made a detailed post on that months ago. Take a look. In the meanwhile, search MSDN for QueryPerformanceCounter and QueryPerformanceFrequency. I use them and I’m quite happy with this method.