From ba878b501ff6beac52b9a0d88ab7678a490df5a6 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sat, 17 Dec 2022 17:03:24 -0800 Subject: [PATCH] Assorted micro-optimizations. Some of these are pure token optimizations, some spend tokens for performance. Function calls are particularly expensive in Pico-8, so using the table form of `pal` whenever changing multiple colors seems worth it. Also fixes a bug in palette swapping. --- chameleonic.p8 | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index eeb517c..d16ca25 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -101,19 +101,13 @@ function _apply(x,ts,a) end function sgn0(x) - if (x==0) return x - return sgn(x) + if (x==0) return x + return sgn(x) end function _mnmx(x,y) - return min(x,y),max(x,y) -end --->8 -bg={} -add(modules,bg) - -function bg:draw0() - cls(0) + if (x>y)return y,x + return x,y end -->8 level={} @@ -139,6 +133,7 @@ function level:advance() self:reinit(self.ix+1) end +pitpal = {[0]=1, [7]=0} function level:draw() cls(1) pal(1,0) @@ -150,11 +145,11 @@ function level:draw() for _,pit in pairs(self._pits) do spr(pit.s,pit.px,pit.py) if pit.contents then - pal(7,0) - pal(0,1) + pal(pitpal) palt(0,false) spr(pit.contents,pit.px,pit.py) pal() + pal(1,0) end for _,crate in pairs(self._crates) do spr(crate.s,crate.px,crate.py) @@ -179,7 +174,7 @@ function level:update() if #crate.todo==0 then local pit=self._pits[_mix(crate.mx,crate.my)] - if pit!=nil and pit.contents==nil then + if pit and not pit.contents then add(remove,cix) crate.dead=true pit.contents=crate.s @@ -205,7 +200,7 @@ function level:load_dynobjs() local px,py=mx*8,my*8 local s=self:_mget(mx,my) local def=self:_get_cratedef(s) - if def!=nil then + if def then self._crates[mxy]={ s=s,def=def, mx=mx,my=my, @@ -228,8 +223,8 @@ end function level:recollide() self._coll={} - for mx=0,15,1 do - for my=0,15,1 do + for mx=0,15 do + for my=0,15 do local mxy=_mix(mx,my) self._coll[mxy]= fget(self:_mget(mx,my),7) or @@ -239,16 +234,16 @@ function level:recollide() end function level:reanchor(remove) - if remove or self._anch==nil then + if remove or not self._anch then self._anch={} end - for ax0=0,31,1 do - for ay0=0,31,1 do - local ax1,ay1=ax0-1+2*(ax0%2),ay0-1+2*(ay0%2) - - local mx0,my0=ax0\2,ay0\2 - local mx1,my1=ax1\2,ay1\2 + for ax0=0,31 do + local ax1 = ax0-1+2*(ax0%2) + local mx0,mx1 = ax0\2,ax1\2 + for ay0=0,31 do + local ay1=ay0-1+2*(ay0%2) + local my0,my1=ay0\2,ay1\2 if ( not self:mcoll(mx0,my0) and