Last active
December 31, 2019 10:52
-
-
Save 1bardesign/a8085ef66fb13333c94973dfc1854629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--baseline vs churn "try it yourself" love demonstration | |
--byo jprof | |
PROF_CAPTURE = true | |
local prof = require("jprof") | |
--starting parameters | |
local churn = 1e3 | |
local baseline = 1e5 | |
local baseline_change_per_frame = 1e3 | |
--build keybinds up | |
local keybinds_in_order = { | |
{"up", "increase churn", function() | |
churn = churn * 10 | |
end}, | |
{"down", "decrease churn", function() | |
churn = churn / 10 | |
end}, | |
{"left", "decrease baseline", function() | |
baseline = baseline / 10 | |
end}, | |
{"right", "increase baseline", function() | |
baseline = baseline * 10 | |
end}, | |
} | |
local keybinds_map = {} | |
for i,v in ipairs(keybinds_in_order) do | |
keybinds_map[v[1]] = v[3] | |
end | |
--maintain the working set | |
local working_set = {} | |
local function maintain_baseline() | |
for i = 1, baseline_change_per_frame do | |
if #working_set < baseline then | |
table.insert(working_set, {#working_set}) | |
end | |
if #working_set > baseline then | |
table.remove(working_set) | |
end | |
end | |
end | |
--on load, connect to jprof and set up the baseline | |
function love.load() | |
print(prof, prof.connect(false)) | |
maintain_baseline() | |
end | |
--storage for the per-frame garbage line | |
local line = {} | |
function love.update() | |
prof.push("frame") | |
prof.push("baseline") | |
maintain_baseline() | |
prof.pop("baseline") | |
prof.push("churn") | |
--this line is all churn; each segment is a table of 2 random values; | |
--the table is regenerated every frame | |
line = {} | |
for i = 1, churn do | |
table.insert(line, {love.math.random(), love.math.random()}) | |
end | |
prof.pop("churn") | |
end | |
function love.draw() | |
prof.push("draw") | |
prof.push("clear") | |
love.graphics.clear(0.5, 0.5, 0.5, 1.0) | |
prof.pop("clear") | |
prof.push("line") | |
local xs = love.graphics.getWidth() / #line | |
local ys = 80 | |
local y0 = love.graphics.getHeight() - 160 | |
for i, v in ipairs(line) do | |
local x1, y1 = (i - 1), v[1] | |
local x2, y2 = i, v[2] | |
love.graphics.line( | |
x1 * xs, y1 * ys + y0, | |
x2 * xs, y2 * ys + y0 | |
) | |
end | |
prof.pop("line") | |
prof.push("text") | |
--render some debug text so we can see what's going on | |
love.graphics.print(string.format("churn: %8d\nbaseline%8d", churn, baseline), 10, 10) | |
for i, v in ipairs(keybinds_in_order) do | |
love.graphics.print(table.concat{v[1], ": ", v[2]}, 10, 40 + i * 17) | |
end | |
prof.pop("text") | |
prof.pop("draw") | |
prof.pop("frame") | |
end | |
function love.keypressed(k) | |
--do keybind callbacks | |
if keybinds_map[k] then | |
keybinds_map[k]() | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment