Advertisement

How to get fps...

Started by April 09, 2005 04:47 PM
6 comments, last by Ludi83 19 years, 10 months ago
Is there any basecode for getting the fps, or should I just set up a variable which increments at every draw and then divides by the time that's passed?
This is my current implementation (this updates the fps every second):

void main() {int iCurrentTick = 0, iStartTick = 0, iFps = 0, iFrames = 0;...iStartTick = GetTicks();while(mainloop) {do your stuff;        iFrames++;        iCurrentTick = GetTicks();	if ((iCurrentTick - iStartTick) >= 1000) {	iFps = (int)((float)iFrames/(iCurrentTick-iStartTick)*1000.0f);	iFrames = 0;	iStartTick = iCurrentTick;	}}


GetTicks depends on your Language, Framework you are using, e.g. for SDL, it is SDL_GetTicks();
Advertisement
Ok, Ludi's implementation is exactly the same as what I do, the basic method is like this:
Get start timeFrame counter = 0Start game loop  Render frame  Increment frame counter by one  Get current time  If current time - start time > one second then    Frames per second = frame counter / (current time - start time)    Start time = 0    Frame counter = 0  End ifEnd game loop

So you increment a counter every frame then check if one or more seconds has elapsed, if it has then divide your frame count by the actual time elapsed and reset the counter and start time then start again.

Happy coding :)
--
Cheers,
Darren Clark


Does that code imply that 1000 ticks = 1 second?


-LW

-------------------LordsWarrior-------------------
ok here is aproximatly what i do in some kind of pseudo code.

int fps=0;
int framecount=0;
float time=0;

for every frame
{

time+=getTime(); // gets the current time passed since the last frame in seconds
framecount++;

if (time > 1.0f)
{
time-=1.0f;
fps= framecount;
framecount=0;
}

}


if the framecounter starts flickering back and forth every second in an anoying way, then you could add some kind of filtering on the fps counter.
@LordsWarrior: Yes I believe it does.
--
Cheers,
Darren Clark
Advertisement
hi,

if you don't get my code running in your project, paste it here at gamedev, and then we'll try to implement it together.

[Edited by - Ludi83 on November 5, 2007 5:01:25 AM]

This topic is closed to new replies.

Advertisement