Advertisement
SneakySquid

FPS Graph

Mar 8th, 2020
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. local fps = {}
  2.  
  3. do
  4.     cvars.RemoveChangeCallback("fps_graph", "fps_graph")
  5.     cvars.RemoveChangeCallback("fps_graph_width", "fps_graph_width")
  6.     cvars.RemoveChangeCallback("fps_graph_points", "fps_graph_points")
  7.  
  8.     fps.X, fps.Y = 20, 20
  9.     fps.W, fps.H = 0, 150
  10.     fps.PointDistance = 0
  11.  
  12.     fps.max = GetConVar("fps_max")
  13.     fps.graph = CreateClientConVar("fps_graph", "0", nil, nil, "Draw the FPS counter and/or graph, = 1 draws counter, = 2 draws bar graph and counter, = 3 draws line graph and counter.")
  14.     fps.graph_points = CreateClientConVar("fps_graph_points", "60", nil, nil, "The number of points/bars the FPS graph will have.")
  15.     fps.graph_width = CreateClientConVar("fps_graph_width", "500", nil, nil, "The FPS graph's target width. Will change to match the points.")
  16.  
  17.     fps.Gradient = Material("vgui/gradient-d")
  18.     fps.Cache = setmetatable({}, {__index = function() return 0 end})
  19.  
  20.     fps.NextCount = 0
  21.     fps.SinceLastUpdate = 0
  22.     fps.LastFrameNumber = FrameNumber()
  23. end
  24.  
  25. local function CountFrames()
  26.     if (SysTime() < fps.NextCount) then return end
  27.     fps.NextCount = SysTime() + 1
  28.  
  29.     local frames = FrameNumber()
  30.     local diff = frames - fps.LastFrameNumber
  31.     local limit = math.max(fps.max:GetInt(), 60)
  32.  
  33.     table.insert(fps.Cache, 1, math.Clamp(diff / limit, 0, 1))
  34.  
  35.     if (#fps.Cache > fps.graph_points:GetInt() + 1) then
  36.         table.remove(fps.Cache)
  37.     end
  38.  
  39.     fps.SinceLastUpdate = diff
  40.     fps.LastFrameNumber = frames
  41. end
  42.  
  43. local function DrawCounter()
  44.     local x, y = fps.X, fps.Y
  45.     local w, h = fps.W, fps.H
  46.     local FPS = string.format("%i FPS", fps.SinceLastUpdate)
  47.  
  48.     if (fps.graph:GetInt() == 1) then
  49.         draw.SimpleText(FPS, "DebugOverlay", x, y)
  50.     else
  51.         local mult = fps.Cache[1]
  52.         local tx = x + w + 2
  53.         local ty = y + (h - (h * mult))
  54.  
  55.         draw.SimpleText(FPS, "DebugOverlay", tx, ty, _, _, TEXT_ALIGN_CENTER)
  56.     end
  57. end
  58.  
  59. local function DrawBarGraph()
  60.     local x, y = fps.X, fps.Y
  61.     local w, h = fps.W, fps.H
  62.  
  63.     local pd = fps.PointDistance
  64.     local points = fps.graph_points:GetInt()
  65.  
  66.     render.SetScissorRect(x, y, x + w, y + h, true)
  67.         surface.SetDrawColor(0, 0, 0, 200)
  68.         surface.DrawRect(x, y, w, h)
  69.  
  70.         for i = 1, points do
  71.             local mult = fps.Cache[i]
  72.  
  73.             local bh = math.floor(h * mult)
  74.             local bx = (x + w) - (i * pd)
  75.             local by = y + (h - bh)
  76.  
  77.             surface.SetDrawColor(Color(255 - 255 * mult, 255 * mult, 0))
  78.             surface.DrawRect(bx, by, pd, bh)
  79.         end
  80.     render.SetScissorRect(0, 0, 0, 0, false)
  81. end
  82.  
  83. local function DrawLineGraph()
  84.     local x, y = fps.X, fps.Y
  85.     local w, h = fps.W, fps.H
  86.  
  87.     local pd = fps.PointDistance
  88.     local points = fps.graph_points:GetInt()
  89.  
  90.     render.SetScissorRect(x, y, x + w, y + h, true)
  91.         surface.SetDrawColor(0, 0, 0, 200)
  92.         surface.DrawRect(x, y, w, h)
  93.  
  94.         render.ClearStencil()
  95.  
  96.         render.SetStencilEnable(true)
  97.             render.SetStencilTestMask(0xFF)
  98.             render.SetStencilWriteMask(0xFF)
  99.             render.SetStencilReferenceValue(0x01)
  100.  
  101.             render.SetStencilCompareFunction(STENCIL_ALWAYS)
  102.             render.SetStencilPassOperation(STENCIL_REPLACE)
  103.  
  104.             local first = fps.Cache[1]
  105.  
  106.             local pph = math.floor(h * first)
  107.             local ppx = (x + w) - 1
  108.             local ppy = y + (h - pph)
  109.  
  110.             for i = 2, points + 1 do
  111.                 local mult = fps.Cache[i]
  112.  
  113.                 local ph = math.floor(h * mult)
  114.                 local px = ((x + w) - ((i - 1) * pd)) - 1
  115.                 local py = y + (h - ph)
  116.  
  117.                 surface.SetDrawColor(0, 255, 0)
  118.                 surface.DrawLine(ppx, ppy, px, py)
  119.  
  120.                 ppx, ppy = px, py
  121.             end
  122.  
  123.             render.SetStencilCompareFunction(STENCIL_LESSEQUAL)
  124.             render.SetStencilFailOperation(STENCIL_KEEP)
  125.  
  126.             surface.SetMaterial(fps.Gradient)
  127.             surface.SetDrawColor(255, 0, 0)
  128.             surface.DrawTexturedRect(x, y, w, h)
  129.         render.SetStencilEnable(false)
  130.     render.SetScissorRect(0, 0, 0, 0, false)
  131. end
  132.  
  133. local function GraphChanged(_, old, new)
  134.     old, new = tonumber(old) or 0, tonumber(new) or 0
  135.  
  136.     if (new > 0) then
  137.         if (old <= 0) then
  138.             fps.SinceLastUpdate = 0
  139.             fps.LastFrameNumber = FrameNumber()
  140.         end
  141.  
  142.         hook.Add("PreRender", "FPS Counter", CountFrames)
  143.  
  144.         if (new >= 1) then
  145.             hook.Add("PostDrawHUD", "FPS Counter", DrawCounter)
  146.  
  147.             if (new == 2) then
  148.                 hook.Add("PostDrawHUD", "FPS Graph", DrawBarGraph)
  149.             elseif (new >= 3) then
  150.                 hook.Add("PostDrawHUD", "FPS Graph", DrawLineGraph)
  151.             else
  152.                 hook.Remove("PostDrawHUD", "FPS Graph")
  153.             end
  154.         end
  155.     else
  156.         table.Empty(fps.Cache)
  157.  
  158.         hook.Remove("PreRender", "FPS Counter")
  159.         hook.Remove("PostDrawHUD", "FPS Counter")
  160.         hook.Remove("PostDrawHUD", "FPS Graph")
  161.     end
  162. end
  163. cvars.AddChangeCallback("fps_graph", GraphChanged, "fps_graph")
  164.  
  165. local function FindClosest(width, points)
  166.     local low = width % points
  167.     local high = (width + low) % points
  168.  
  169.     return low <= high and width - low or width + high
  170. end
  171.  
  172. local function WidthChanged(_, _, new)
  173.     local points = fps.graph_points:GetInt()
  174.     fps.W = FindClosest(new, points)
  175.     fps.PointDistance = fps.W / points
  176. end
  177. cvars.AddChangeCallback("fps_graph_width", WidthChanged, "fps_graph_width")
  178.  
  179. local function PointsChanged(_, old, new)
  180.     old, new = tonumber(old) or 0, tonumber(new) or 60
  181.  
  182.     local target = fps.graph_width:GetInt()
  183.     fps.W = FindClosest(target, new)
  184.     fps.PointDistance = fps.W / new
  185.  
  186.     if (old < new) then return end
  187.  
  188.     local diff = old - new
  189.  
  190.     if (#fps.Cache - diff > new) then
  191.         for i = 1, diff do
  192.             table.remove(fps.Cache)
  193.         end
  194.     end
  195. end
  196. cvars.AddChangeCallback("fps_graph_points", PointsChanged, "fps_graph_points")
  197.  
  198. GraphChanged(_, _, fps.graph:GetInt())
  199. WidthChanged(_, _, fps.graph_width:GetInt())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement