calculating Framesper seconds

Started by
7 comments, last by CadetUmfer 15 years, 1 month ago
I made a code for calculating frames per second(fps) but I know its not very precise. Can you help he improve it, or help me create a better one. here is the code :


void FPS()				
{
	static float fps    = 0.0f;						
        static float previousTime  = 0.0f;	
	static char  strFPS[20]    = {0};
	
	float currentTime = (GetTickCount() * 0.001f);				

    ++fps;

    if( currentTime - previousTime > 1.0f )
    {
	    previousTime = currentTime;
		sprintf(strFPS, "FPS: %d", int(fps));
		std::cout<<strFPS<<std::endl;
        fps = 0.0f;
    }
}

Also I am using this in opengl. Is there a way to display this on the title of the window
Our whole life is a opengl application.
Advertisement
You can use SetWindowText method to display it on the title bar.
Info

anyone?
Our whole life is a opengl application.
Black Night answered your question.
NO I mean to make the function more accurate.
Our whole life is a opengl application.
if you want more acc, use a high performance time.

http://frank.mtsu.edu/~csjudy/directX/HighPerformanceTimer.html
http://msdn.microsoft.com/en-us/library/ms644900(VS.85).aspx

I believe the GetTickCount has a acc of 10 ms, which is very bad if your doing anything really times sensitive.

Good Luck!

-ddn
If you want you can use fraps to display the frames per second of your game. Just open up fraps and then run your game and it will display the FPS in your game screen.
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Hi
This is not how I've usually seen the FPS calculated in a game. I'm used to (pseudocode)
begin = getTime()// frameend = getTime()fps = 1 / (end-begin)


and sometimes if you want to smooth the values you can use a moving average with a queue of size N

begin = getTime()// frameend = getTime()queue.push(end-begin)fps = queue.size() / queue.sum()


(you define the size of the queue: 1, 10, 20, 60, whatever)
I just call this every frame. dt is delta time in seconds, but could be an int with milliseconds or whatever you're using.

#define FPS_FRAMES 8#define FPS_FRAMES_MASK (FPS_FRAMES - 1)static void DrawFps(double dt) {	static double prevTimes[FPS_FRAMES];	static unsigned index;	prevTimes[index & FPS_FRAMES_MASK] = dt;	++index;	if (index > FPS_FRAMES) {		double *i, total = 0;		for (i = prevTimes; i < prevTimes + FPS_FRAMES; ++i) {			total += *i;		}#if 1		/* milliseconds per frame */		total /= FPS_FRAMES;		total *= 1000;#else		/* frames per second */		total = FPS_FRAMES / total;#endif		/* draw */	}}
Anthony Umfer

This topic is closed to new replies.

Advertisement