• ### Announcements

#### Archived

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

# how to calculate fps

## Recommended Posts

how do i calculate the frames per second for my game

##### Share on other sites
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 loopfps++;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 on other sites
well i tried to implement your method and i set all variables to 0 but all i get is a one displayed

##### Share on other sites
Howa bout dis, straight out my own Engine

  //Frames Per Second = Num Frame / Elasped Time in Secondsclass 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 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 FPSint 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

• ### Forum Statistics

• Total Topics
628278
• Total Posts
2981789

• 10
• 11
• 17
• 14
• 9