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.