Assorted micro-optimizations. (#3)

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.

Reviewed-on: pyrex/chameleonic#3
Co-authored-by: Kistaro Windrider <kistaro@gmail.com>
Co-committed-by: Kistaro Windrider <kistaro@gmail.com>
This commit is contained in:
Kistaro Windrider 2022-12-18 01:11:42 +00:00 committed by Pyrex
parent 823b3edc30
commit db157b7952

View File

@ -106,14 +106,8 @@ function sgn0(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={}
@ -143,6 +137,7 @@ function level:advance()
self:reinit(self.ix+1)
end
pitpal = {[0]=1, [7]=0}
function level:draw()
cls(1)
pal(1,0)
@ -154,11 +149,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)
@ -183,7 +178,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
@ -209,7 +204,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,
@ -232,8 +227,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
@ -243,16 +238,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