Jump to content

Calculating FPS per frame


H!tman

Recommended Posts

Well, you tried, and had a good amount done. Here is a better example:

  
local FPSLimit, lastTick, framesRendered, FPS = 36, getTickCount(), 0, 0 
local screenWidth, screenHeight = guiGetScreenSize(); local width, height = screenWidth - 5, screenHeight - 5; screenWidth = nil; screenHeight = nil 
  
--This function calcuates the client's FPS and draws it on every render. 
local function clientRender() 
    --Calculate the frames per second, based on the number of frames drawn in the last second. 
    local currentTick = getTickCount() 
    local elapsedTime = currentTick - lastTick 
    if elapsedTime >= 1000 then 
        FPS = framesRendered 
        lastTick = currentTick 
        framesRendered = 0 
    else 
        framesRendered = framesRendered + 1 
    end 
     
    --Draw the current FPS 
    local FPSPercent = (255 * FPS) / FPSLimit 
    local FPSColor = tocolor((255 - FPSPercent), FPSPercent, 0, 255) 
    dxDrawText(tostring(FPS), 0, 0, width, height, FPSColor, 1, "bankgothic", "right") 
end 
addEventHandler("onClientRender", root, clientRender) 
  
--This function returns the current FPS 
function getFPS() 
    return FPS 
end 
  

First, we declare some variables to store data for the calculation. We store the FPS limit (for the color of the actual text), the last tick (for measuring the elapsed time since the last update), the number of frames rendered since the last calculation, and lastly the actual FPS (defined to nil, but defined locally).

Second, we calculate the margins for the text.

Thirdly, we perform the actual calculation. If the time elapsed since the last frame update is equal to are greater than 1 second, then set our FPS counter to number of frames rendered in the last second. Then, reset the variables.

Lastly, we draw the actual text, adding a nice colored effect (green for good FPS, red for bad).

Hope this helps.

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...