From: Ben Sherratt Date: Wed, 23 Jun 2021 23:05:54 +0000 (+0100) Subject: Initial Commit X-Git-Url: https://git.bts.cx/p8-perfgraph.git/commitdiff_plain/f0474005338b4400393bdacf8198e77515f2252d?ds=inline Initial Commit Contains code pulled from Racers project. --- f0474005338b4400393bdacf8198e77515f2252d diff --git a/README.md b/README.md new file mode 100644 index 0000000..c978e30 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Perfgraph for PICO-8 + +This is a super simple way to get a performance graph into your PICO-8 carts. + +![See it in action!](demo1.gif) + +1. `#include perfgraph.lua`. +2. Use `p_start(name)` and `p_end(name)` around the code you need to measure. +3. Call `p_show(x,y,width)` to show a graph! (The frame times will reset after you call this.) + +Full example: + +```lua + +#include perfgraph.lua + +::l:: + +p_start("complicated 1") +for i=0,100 do + circfill(rnd()*128,rnd()*128,rnd()*100,rnd()*16) +end +p_end("complicated 1") + +p_start("complicated 2") +for i=0,100 do + circ(rnd()*128,rnd()*128,rnd()*100,rnd()*16) +end +p_end("complicated 2") + +p_show(1,1,32) + +flip() + +``` + +Have fun. You can find a demo in perfgraphdemo.p8 in this repo. If you like this then feel free to follow me on Twitter: [@btsherratt](http://twitter.com/btsherratt/). diff --git a/demo1.gif b/demo1.gif new file mode 100644 index 0000000..580b908 Binary files /dev/null and b/demo1.gif differ diff --git a/perfgraph.lua b/perfgraph.lua new file mode 100644 index 0000000..c7faee1 --- /dev/null +++ b/perfgraph.lua @@ -0,0 +1,62 @@ +-- Perfgraph for PICO-8 + +_p_perf={} +_p_pfo={} + +function p_start(name,col) + if col==nil then + col=rnd()*15+1 + end + + local pp=_p_perf[name] + if pp==nil then + pp={} + pp.col=col + pp.ct=0 + _p_perf[name]=pp + end + + if pp.q!=true then + add(_p_pfo, name) + pp.q=true + end + + pp.st=stat(1) +end + +function p_end(name) + local pp=_p_perf[name] + if pp then + pp.ct+=max(stat(1)-pp.st,0) + end +end + +function p_show(x,y,w,compact) + camera() + clip() + fillp() + for n in all(_p_pfo) do + local tp=_p_perf[n] + local pw=tp.ct*w + local p=flr((tp.ct*100)+0.5) + + if compact then + rectfill(x,y,x+w,y+4,0) + rectfill(x,y,x+pw,y+4,tp.col) + print(n..":"..p.."%",x+pw+2,y,7) + y+=4+2 + else + rectfill(x,y,x+w,y+4,0) + rectfill(x,y,x+pw,y+4,tp.col) + local lab=n..":"..p.."%" + rectfill(x+w+2,y,x+w+2+#lab*4,y+4,1) + print(lab,x+w+2,y,7) + y+=4+2 + end + + tp.q=false + tp.ct=0 + end + + _p_pfo={} +end \ No newline at end of file diff --git a/perfgraphdemo.p8 b/perfgraphdemo.p8 new file mode 100644 index 0000000..a4d539e --- /dev/null +++ b/perfgraphdemo.p8 @@ -0,0 +1,33 @@ +pico-8 cartridge // http://www.pico-8.com +version 32 +__lua__ +#include perfgraph.lua + +::l:: + +p_start("frame") +p_start("complicated 1") +for i=0,100 do + circfill(rnd()*128,rnd()*128,rnd()*100,rnd()*16) +end +p_end("complicated 1") + +p_start("complicated 2") +for i=0,100 do + circ(rnd()*128,rnd()*128,rnd()*100,rnd()*16) +end +p_end("complicated 2") +p_end("frame") + +p_show(1,1,32) + +flip() + +goto l +__gfx__ +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000