From: stormreaver
Written: 2010-04-02 13:33:10.50897
Subject: Calculating Frames Per Second
Written: 2010-04-02 13:33:10.50897
Subject: Calculating Frames Per Second
Calculating the frames per second for display purposes is very simple. The basic idea is that you want to calculate the fraction of a second that was required to render the frame, then invert it to a whole number.
In your main game loop:
1) Get the starting time (I use microseconds) before processing your frame.
2) Process your frame.
3) Get the ending time (again, I use microseconds) after processing your frame.
4) Calculate the elapsed time (ending time minus starting time).
5) The fraction of a second that it took to render the frame is 1 / elapsed time. This is a decimal number like 0.25.
6) Now the decimal needs to be converted to a whole number. This is 1/decimal number. For example: if your frame rendered in 0.25 seconds, then your loop runs at 4 frames per second (1/0.25 equals 4).
Steps 5 and 6 can be combined into a single calculation:
fps = 1 / (1 / elapsed time)
In your main game loop:
1) Get the starting time (I use microseconds) before processing your frame.
2) Process your frame.
3) Get the ending time (again, I use microseconds) after processing your frame.
4) Calculate the elapsed time (ending time minus starting time).
5) The fraction of a second that it took to render the frame is 1 / elapsed time. This is a decimal number like 0.25.
6) Now the decimal needs to be converted to a whole number. This is 1/decimal number. For example: if your frame rendered in 0.25 seconds, then your loop runs at 4 frames per second (1/0.25 equals 4).
Steps 5 and 6 can be combined into a single calculation:
fps = 1 / (1 / elapsed time)