Jump to content

[CODE] Better FPS Formular


Gallardo9944

Recommended Posts

Hello.

I bet you have faced some issues with dxDraw* functions, especially with some situations when you have to use your FPS as a counter of *something* like movement of images or texts. You could use average FPS counted each per second and go nicely with it, but the drawing still won't be precise. So, I have decided to share a little FPS formular for developers who need more exact FPS information (basically, each frame). Here goes the code:

local d = false 
local t1 = 0 
local FPS = 0 
  
function fpsCount() 
  if d == true then 
    local t2 = getTickCount() 
    local diff = t2-t1 
    local ctr = 1000/diff 
    FPS = ctr 
    t1 = getTickCount() 
  else 
    t1 = getTickCount() 
    d = true 
  end 
end 
addEventHandler("onClientRender",getRootElement(),fpsCount) 

How it works:

We have a variable "d", which basically is changed after one frame, so that specifies whether we have the first tick counter or not. I don't use onClientPreRender together with that as far as it's not gonna be precise. Then, after it is changed, and if it equals to boolean false, we get current tick counter. This is our first state of time before the new frame starts. Then, "d" is changed once again, and it's boolean true now, so we get another tick counter and find the difference between the second and the first states. This is the difference between the previous frame and the current one, basically the time when one frame passes. Then it's easy to find the frames per second counter by dividing one second (1000ms) by our frame difference. That is our EXACT fps at the moment of time, no matter what happens. The value is float, so we have to use math.ceil or math.floor to get it as an integer, to output somewhere, if you need that.

Thanks for reading!

The thread was made to provide a better formular for those, who need exact fps information. I know the code is amazingly easy, but still, would be useful for those, who don't want to reinvent the wheel.

Link to comment
  • Moderators

I have also been testing fps counters, it is very usefully to check the amount of time between the frames.

Especially when you want to check somebody his lagg's.

But onClientRender triggers just the amount of frames and you are using time to calculate this.

A normal FPS counter will automatic do math.ceil because it doesn't count half frames.

Time isn't a frame, you never will know how many frames have been past in this second.

This isn't a Frames Per Second counter, but a Timer Per Frame calculation.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...