pal_from_spr/pal_from_spr.p8

102 lines
2.4 KiB
Lua

pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
-- sprite-sampled palette
-- kistaro@gmail.com
function _init()
-- lock top 16 rows to default palette
pal({[0]=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, 2)
poke(0x5f5f, 0x10)
poke2(0x5f70, 0xffff)
end
function _update()
end
function _draw()
cls()
draw_hud()
draw_bars()
end
function draw_hud()
end
function draw_bars()
for i=0,15 do
local i7 = i * 7
rectfill(0,16+i7,128,i7+22,i)
end
end
-->8
-- palette structure
palette = {
-- cols: table. identical
-- format to table-type
-- pal call
-- tr: transparency table.
-- keys: [0]..[15]
-- (color indexes)
-- vals: true/false/nil
-- for transparency of
-- that color. false
-- sets opaque, nil
-- means "do not set".
-- tables with no absent
-- values can be compiled
-- for faster do_palt.
-- cpl_tr: compiled tr. set by
-- compile_palt iff all
-- tr values in range
-- are nonnil. do_palt
-- is faster this way
}
palette_mt = {__index=palette}
function palette.new(x)
x = x or {}
if (not x.cols) x.cols = {}
if (not x.tr) x.tr = {}
setmetatable(x, palette_mt)
end
function palette:do_pal(p)
pal(self.cols, p)
end
function palette:do_palt()
if (self.cpl_tr) return palt(self.cpl_tr)
local tr = self.tr
for i=0,15 do
if (tr[i] ~= nil) palt(i, tr[i])
end
end
function palette:do_both(p)
self:do_palt()
self:do_pal(p)
end
function palette:compile_palt()
self.cpl_tr = nil
local bits, tr = 0, self.tr
for i = 15,0,-1 do
if (tr[i] == nil) return
bits <<= 1
if (tr[i]) bits += 1
end
self.cpl_tr = bits
return bits
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000