PDA

View Full Version : Frames Per Second??



01-23-2004, 04:05 AM
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

M/\dm/\n
01-23-2004, 06:10 AM
#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.

Azazel
01-26-2004, 03:15 AM
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).]

cwc36
01-26-2004, 09:21 PM
Hi,

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

01-27-2004, 03:33 AM
thanks a lot M/\dm/\n and cwc36, that helped me out a lot ... pretty good site cwc36 got a few good tuts on it

01-27-2004, 03:48 AM
thanks a lot M/\dm/\n and cwc36, that helped me out a lot ... pretty good site cwc36 got a few good tuts on it

Puma1140
01-27-2004, 04:05 AM
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
}

Obli
01-28-2004, 02:29 AM
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.