FPS indicator

Because i was sick of FPS calculations cluttering draw, i came up with this:

FPS = class()
function FPS:init(typ,col,x,y,fs,auto)
    FPS.col = col or FPS.col or 256
    FPS.fontsize = fs or FPS.fontsize or 25
    FPS.x,FPS.y=x or FPS.x or WIDTH-FPS.fontsize,y or FPS.y or HEIGHT-FPS.fontsize
    FPS.auto = auto or FPS.auto or false
    FPS:settype(typ or FPS.type or 2)
    if FPS.col == 256 then
        FPS.style = function()
            fill(FPS.col);blendMode(ONE_MINUS_DST_COLOR,ONE_MINUS_SRC_COLOR);
            textMode(CENTER);fontSize(FPS.fontsize);font("Futura-Medium")
        end
    else
        FPS.style = function()
            fill(FPS.col);textMode(CENTER);fontSize(FPS.fontsize);font("Futura-Medium")
        end
    end
    if not FPS.active then
        FPS.active = true
        FPS.odraw = draw
        if FPS.auto then
            draw = function()
                FPS.odraw()
                pushStyle()
                FPS.style()
                text(FPS.calc(),WIDTH-FPS.fontsize+5,HEIGHT-FPS.fontsize+10)
                popStyle()
            end
        else
            draw = function()
                FPS.odraw()
                pushStyle()
                FPS.style()
                text(FPS.calc(),FPS.x,FPS.y)
                popStyle()
            end
        end
    end
end
function FPS:remove()
    draw = FPS.odraw
    FPS.active = nil
end
function FPS:settype(typ)
    FPS.type = typ
    FPS.calc = FPS.types[typ][1]
    FPS.types[typ][2]()
end
FPS.types = {{
-- TYPE 1 --
function()
    FPS.frame = FPS.frame + 1
    FPS.etime = FPS.etime + DeltaTime
    return math.floor(FPS.frame/FPS.etime+.5)
end,
function()
    FPS.frame = 0
    FPS.etime = 0
end },{
-- TYPE 2 --
function()
    FPS.fps = .95*FPS.fps+.05*(1/DeltaTime)
    return math.floor(FPS.fps+.5)
end,
function()
    FPS.fps = 60
end },{
-- TYPE 3 --
function()
    table.insert(FPS.deltable,1,DeltaTime)
    FPS.etime = FPS.etime + DeltaTime - FPS.deltable[120]
    table.remove(FPS.deltable,120)
    return math.min(math.floor(119/FPS.etime+.5),60)
end,
function()
    FPS.deltable = {}
    for i=1,120 do
        FPS.deltable[i] = 0
    end
    FPS.etime = 0
end }}

To use it simply call FPS() in setup and it will make the indicator in the top right corner. You can then remove the indicator with FPS:remove()

By default the color is the negative of whatever is behind it

Hi @Monkeyman32123,

Very neat, not sure how it works but impressive. Could this be extended to include colour definition and position. Final refinement - change orientation and it adjusts.

This has gone straight into my library.

Thanks,

Bri_G

:smiley:

You can define the color and position:

function setup()
      local x,y = WIDTH/2,HEIGHT/2
      local col = color(255)
      FPS(nil,col,x,y)
end

And thank you @Bri_G :slight_smile: im glad you like it. Ill update when i make it adjust for orientation

EDIT: if, when calling FPS(), you set the sixth argument as true, it will override any x or y setting and keep the FPS indicator in the top right corner, adjusting for orientation

EDIT EDIT: I hope to refactor this code later, making it cleaner and more user-friendly, so stay tuned :slight_smile:

@Monkeyman32123,

Thanks - that just finishes it off for me. I use it as a dependency in my trials, really useful.

Thanks again.

Bri_G

:smiley:

Glad you like it :slight_smile:
And you are very welcome!