How to get fps...
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):
GetTicks depends on your Language, Framework you are using, e.g. for SDL, it is SDL_GetTicks();
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();
Ok, Ludi's implementation is exactly the same as what I do, the basic method is like this:
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 :)
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
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.
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.
www.flashbang.se | www.thegeekstate.com | nehe.gamedev.net | glAux fix for lesson 6 | [twitter]thegeekstate[/twitter]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement