Archived

This topic is now archived and is closed to further replies.

omegasyphon

how to calculate fps

Recommended Posts

+AA_970+    122
Here's an easy way to do it.

1. increment a variable (int) after a frame is drawn (usually at the bottom of your game loop)

2. Calculate when 1 second (use GetTickCount or similar) has elapsed. (you'll need two variables one for the current time and one for the original time [1 second earlier])

3. After 1 second has elapsed the variable will represent the frames per second. You can then store this value in another variable to display it on the screen, calculate average fps. Reset the variable (set it = to 0).

4.The process begins again.

Here's some psuedo code

        

// end of loop

fps++;

if(current_time - original_time >= 1000)
{
fps_previous = fps; // stores fps value

fps=0;
original_time = GetTickCount();
}





+AA_970+

Edited by - +AA_970+ on July 22, 2000 2:41:40 PM

Share this post


Link to post
Share on other sites
Esap1    122
Howa bout dis, straight out my own Engine

    

//Frames Per Second = Num Frame / Elasped Time in Seconds

class CFPS_Counter {
private:
DWORD StartTime; //The Start Time

DWORD CurrTime; //Current Time

DWORD NumFrame; //Number of Frames since start

float Fps; //Current Frames Per Second

float Spf; //Current Seconds Per Frame

char FPSstring[128];//Dont Overflow<img src="smile.gif" width=15 height=15 align=middle>

unsigned long delay;

public:
int StartFPS(); //Starts the FPS Counter

int UpdateFPS(); //Updates the Fps Variable

float ReturnFPS(){return Fps;} //Returns the Fps variable

int DrawFPS();
//Returns the Speed Per Frame from the Num per second

float NumPerSecond(float in){return in*Spf;}
CFPS_Counter(){}
~CFPS_Counter(){}
};






//Draws the FPS

int CFPS_Counter::DrawFPS()
{
if(sprintf(FPSstring,"%f",ReturnFPS())<=0)return FALSE;
Console.Font.glPrint(0,0,FPSstring,1);

return TRUE;
}


int CFPS_Counter::StartFPS()
{
NumFrame=0;
delay=0;
StartTime = GetTickCount();
return TRUE;
}


int CFPS_Counter::UpdateFPS()
{
float tempFPS;
NumFrame++;
CurrTime = GetTickCount();
tempFPS = 1000.0f*((float)NumFrame/((float)CurrTime-(float)StartTime));
Spf = 1/tempFPS;
if(NumFrame>200)StartFPS();

delay++;
if(delay>15)
{
Fps=tempFPS;
delay=0;
}

return TRUE;
}



Hope it helped

Share this post


Link to post
Share on other sites