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) mode = "boxes" active = "sprite" scx = 0 scy = 0 dcx = 0 dcy = 0 init_box_pals() init_row_pals() end function init_box_pals() box_pals = {} for x=0,box_cols do local pal_col = {} for y=0,box_rows do pal_col[y] = load_box_palette(1+x+16*(y\2)+(0.5*y%2)) end box_pals[x] = pal_col end end function init_row_pals() row_pals = {} for x=0,row_cols do local pal_col = {} for y=0,row_rows do pal_col[y] = load_row_palette( 8+16*x, 16+2*y ) end row_pals[x] = pal_col end end function _update() if (btnp(🅾️)) mode = (mode == "boxes") and "rows" or "boxes" if (btnp(❎)) active = (active == "sprite") and "draw" or "sprite" if active == "sprite" then if (btnp(⬆️)) scy -= 1 if (btnp(⬇️)) scy += 1 if (btnp(⬅️)) scx -= 1 if (btnp(➡️)) scx += 1 else if (btnp(⬆️)) dcy -= 1 if (btnp(⬇️)) dcy += 1 if (btnp(⬅️)) dcx -= 1 if (btnp(➡️)) dcx += 1 end scx %= (mode == "boxes") and box_cols or rows_cols dcx %= (mode == "boxes") and box_cols or rows_cols scy %= (mode == "boxes") and box_rows or rows_rows dcy %= (mode == "boxes") and box_rows or rows_rows end function _draw() cls() draw_hud() local pals = (mode == "boxes") and box_pals or row_pals pals[scx][scy]:do_both(0) pals[dcx][dcy]:do_both(1) draw_bars() end function draw_hud() palt() if (mode == "boxes") return draw_box_hud() draw_rows_hud() end function print_shadow(s,x,y,c) print(s,x+1,y+1,1) print(s,x,y,c or 7) end box_rows = 4 box_cols = 4 function draw_box_hud() spr(1,3,0,4,2) local toff = time() * 4 \ 1 % 2 spr(107 + (active == "sprite" and toff or 0), 3+8*scx,4*scy,1,0.5) spr(109 + (active == "draw" and toff or 0), 3+8*dcx,4*dcy,1,0.5) print_shadow("spr",42,5,12) sspr(8+8*scx,4*scy,8,4,55,4,16,8) print_shadow("draw",73,5,14) sspr(8+8*dcx,4*dcy,8,4,91,4,16,8) end rows_rows = 8 rows_cols = 2 function draw_rows_hud() spr(33,0,0,2,2) spr(35,22,0,2,2) local toff = time() * 4 \ 1 % 2 spr(103 + (active == "sprite" and toff or 0), 17 + 22*scx, 2*scy, 0.25, 0.25) spr(105 + (active == "draw" and toff or 0), 17 + 22*dcx, 2*dcy, 0.25, 0.25) print_shadow("spr",48,0,12) sspr(8+16*scx,16+2*scy,16,2,61,2,32,4) print_shadow("draw",44,8,14) sspr(8+16*dcx,16+2*dcy,16,2,61,10,32,4) end function draw_bars() for i=0,15 do local i6 = i * 6 rectfill(0,16+i6,128,i6+21,i) end for i=0,15 do for y=16,120,8 do spr(i+112,9+i*7,y,0.625,1) end end for y=16,120,8 do spr(111,120,y) 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) return x 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 -->8 -- load palettes function load_box_palette(sidx) local sint,sfrac=sidx\1,sidx%1 local x0,y0=sint%16*8,sint\16*8+sfrac*8\1 local cols,tr={},{} for i=0,15 do local x,y=x0+i%4,y0+i\4 local c,f= sget(x,y),sget(x+4,y) if (f&0x1 ~= 0) c += 128 if (f&0x2 ~= 0) tr[i]=true if (f&0x4 ~= 0) tr[i]=false if (f&0x8 == 0) cols[i] = c end local ret=palette.new{cols=cols,tr=tr} ret:compile_palt() return ret end function load_row_palette(sx, sy) local cols,tr={},{} for i=0,15 do local c,f = sget(sx+i,sy),sget(sx+i,sy+1) if (f&0x1 ~= 0) c += 128 if (f&0x2 ~= 0) tr[i]=true if (f&0x4 ~= 0) tr[i]=false if (f&0x8 == 0) cols[i] = c end local ret=palette.new{cols=cols,tr=tr} ret:compile_palt() return ret end __gfx__ 00000000012300000123555501230000012355550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000456700004567555545670000456755550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0070070089ab000089ab555589ab000089ab55550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00077000cdef0000cdef5555cdef0000cdef55550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00077000012324440123355501232444012335550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00700700456744444567555545674444456755550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000089ab444489ab555589ab444489ab55550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000cdef4444cdef5555cdef4444cdef55550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000012301230123012301230123012301230000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000456745674567456745674567456745670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000089ab89ab89ab89ab89ab89ab89ab89ab0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000cdefcdefcdefcdefcdefcdefcdefcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000012301230123012301230123012301230000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000456745674567456745674567456745670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000089ab89ab89ab89ab89ab89ab89ab89ab0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000cdefcdefcdefcdefcdefcdefcdefcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000124456789abcdef0124456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000244444444444444424444444444444440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000555555555555555555555555555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000355555555555555535555555555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000355555554444444435555555444444440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000333333334444555533333333444455550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000123456789abcdef0123456789abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000444422225555333344442222555533330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000c00000007000000e0000000700000000c0c0c0c07070707e0e0e0e07070707001230123 00000000000000000000000000000000000000000000000000000000c0000000700000000e00000007000000c0000000700000000000000e0000000745674567 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c00000007e00000007000000089ab89ab 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0c0c0c0707070700e0e0e0e07070707cdefcdef 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001230123 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000045674567 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000089ab89ab 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cdefcdef 5500055500111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff000 500055550111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff0000 00055555111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff00000 500055550111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff0000 5500055500111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff000 500055550111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff0000 00055555111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff00000 500055550111000002220000033300000444000005550000066600000777000008880000099900000aaa00000bbb00000ccc00000ddd00000eee00000fff0000