info = { type = "image", title = "Pop-up book", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Paper castle, owl forest and ship pop up", api = 44, } local tr = ({ sk = { ["Page speed"] = "Listovanie", ["Slow"] = "Pomaly", ["Normal"] = "Normálne", ["Fast"] = "Rýchlo", ["Scenes"] = "Scény", ["All"] = "Všetky", ["Castle"] = "Hrad", ["Forest"] = "Les", ["Ship"] = "Loď" }, cs = { ["Page speed"] = "Listování", ["Slow"] = "Pomalu", ["Normal"] = "Normálně", ["Fast"] = "Rychle", ["Scenes"] = "Scény", ["All"] = "Všechny", ["Castle"] = "Hrad", ["Forest"] = "Les", ["Ship"] = "Loď" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pace", label = T("Page speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, { key = "scene", label = T("Scenes"), type = "int", default = "all", opts = { keys = { "all", "castle", "forest", "ship" }, T("All"), T("Castle"), T("Forest"), T("Ship") } }, } local rect, px, blit_indexed = gfx.rect, gfx.px, gfx.blit_indexed local floor, ceil, sin, cos, sqrt, max, min = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.max, math.min local char, unpack, concat = string.char, table.unpack, table.concat local W, H = gfx.W, gfx.H local function r(x, y, w, h, c) return { "r", x, y, w, h, c } end local function e(cx, cy, rx, ry, c) return { "e", cx, cy, rx, ry, c } end local function t(x1, y1, x2, y2, x3, y3, c) return { "t", x1, y1, x2, y2, x3, y3, c } end local function raster(prims) local G, minx, maxx, miny, maxy = {}, 1e9, -1e9, 1e9, -1e9 local function span(y, a, b, c) if a > b then return end local row = G[y] if not row then row = {}; G[y] = row end for x = a, b do row[x] = c end if a < minx then minx = a end if b > maxx then maxx = b end if y < miny then miny = y end if y > maxy then maxy = y end end for _, p in ipairs(prims) do local k = p[1] if k == "r" then for y = p[3], p[3] + p[5] - 1 do span(y, p[2], p[2] + p[4] - 1, p[6]) end elseif k == "e" then for y = ceil(p[3] - p[5]), floor(p[3] + p[5]) do local d = (y - p[3]) / p[5] local hw = p[4] * sqrt(max(0, 1 - d * d)) span(y, ceil(p[2] - hw), floor(p[2] + hw), p[6]) end else local ya, yb = min(p[3], p[5], p[7]), max(p[3], p[5], p[7]) for y = ya, yb do local lo, hi = 1e9, -1e9 for i = 0, 2 do local x1, y1, x2, y2 = p[2 + 2 * i], p[3 + 2 * i], p[2 + 2 * ((i + 1) % 3)], p[3 + 2 * ((i + 1) % 3)] if y1 ~= y2 and y >= min(y1, y2) and y <= max(y1, y2) then local x = x1 + (x2 - x1) * (y - y1) / (y2 - y1) lo, hi = min(lo, x), max(hi, x) elseif y1 == y2 and y == y1 then lo, hi = min(lo, x1, x2), max(hi, x1, x2) end end span(y, ceil(lo - 0.01), floor(hi + 0.01), p[8]) end end end local w, h = maxx - minx + 1, maxy - miny + 1 local rows, rv = {}, {} for j = 1, h do local row = G[miny + j - 1] for i = 1, w do rv[i] = row and row[minx + i - 1] or 0 end rows[j] = char(unpack(rv, 1, w)) end return { rows = rows, w = w, h = h, ox = minx, oy = miny } end local SPR = {} local JOBS = {} local function cut(name, prims) JOBS[#JOBS + 1] = function() SPR[name] = raster(prims) end end cut("c1", { r(20, 26, 24, 18, 1), r(20, 24, 3, 2, 1), r(26, 24, 3, 2, 1), r(35, 24, 3, 2, 1), r(41, 24, 3, 2, 1), r(12, 20, 9, 24, 1), t(11, 21, 22, 21, 16, 12, 2), r(43, 22, 9, 22, 1), t(42, 23, 53, 23, 47, 14, 2), r(27, 9, 10, 35, 1), t(26, 10, 38, 10, 32, 3, 2), r(31, 15, 2, 4, 4), r(31, 26, 2, 4, 3), r(15, 26, 2, 4, 3), r(46, 28, 2, 4, 3) }) do local p = { r(6, 38, 52, 12, 1), r(4, 28, 8, 22, 1), t(3, 29, 12, 29, 8, 21, 2), r(52, 28, 8, 22, 1), t(51, 29, 60, 29, 56, 21, 2), r(28, 44, 8, 6, 3), e(32, 44, 4, 5, 3), r(8, 34, 2, 3, 3), r(54, 34, 2, 3, 3) } for x = 12, 48, 6 do p[#p + 1] = r(x, 36, 3, 2, 1) end cut("c2", p) end cut("c3", { e(12, 52, 7, 4, 1), e(18, 53, 6, 3, 2), e(50, 52, 8, 4, 1), e(44, 53, 5, 3, 2), r(10, 49, 1, 1, 3), r(53, 49, 1, 1, 3), r(47, 51, 1, 1, 3), r(14, 51, 1, 1, 3), r(0, 55, 64, 1, 2) }) cut("f1", { e(25, 9, 5, 5, 3), e(18, 40, 26, 12, 1), e(50, 42, 22, 10, 2), r(0, 43, 64, 1, 2), t(30, 32, 36, 32, 33, 23, 4), t(24, 35, 30, 35, 27, 27, 4), t(56, 36, 62, 36, 59, 28, 4) }) cut("f2", { t(14, 12, 7, 26, 21, 26, 1), t(14, 19, 5, 34, 23, 34, 1), t(14, 27, 3, 44, 25, 44, 1), r(13, 44, 3, 6, 2), t(50, 22, 44, 34, 56, 34, 1), t(50, 28, 42, 42, 58, 42, 1), r(49, 42, 3, 8, 2) }) cut("f3", { r(40, 16, 7, 40, 1), r(44, 16, 3, 40, 2), r(28, 28, 13, 3, 1), e(44, 14, 9, 5, 5), e(38, 17, 6, 4, 5), e(31, 24, 3, 5, 3), e(31, 20, 3, 3, 3), t(28, 19, 30, 19, 28, 16, 3), t(32, 19, 34, 19, 34, 16, 3), e(31, 26, 2, 3, 4), r(4, 53, 2, 3, 5), r(8, 54, 2, 2, 5), r(14, 53, 2, 3, 5), e(54, 53, 3, 2, 6), r(53, 54, 2, 2, 4) }) do local sky = { r(8, 16, 48, 28, 1), e(32, 16, 24, 5, 1), e(42, 24, 6, 6, 2), r(14, 22, 12, 2, 3), r(17, 20, 6, 2, 3) } cut("s1", sky) local wave = { r(4, 40, 56, 10, 1) } for i = 0, 7 do wave[#wave + 1] = e(8 + i * 7, 40, 4, 3, 1); wave[#wave + 1] = r(7 + i * 7, 37, 3, 1, 2) end cut("s2", wave) cut("s0", { r(19, 40, 26, 2, 1), r(21, 42, 22, 2, 1), r(23, 44, 18, 3, 1), r(31, 12, 2, 28, 2), t(34, 14, 34, 36, 46, 36, 3), t(30, 18, 30, 36, 20, 36, 3), r(32, 9, 5, 3, 4) }) local front = { r(0, 50, 64, 6, 1) } for i = 0, 8 do front[#front + 1] = e(4 + i * 8, 50, 5, 3, 1); front[#front + 1] = r(2 + i * 8, 46, 3, 1, 2) end cut("s3", front) end local function pal(...) return { 0, 0, 0, ... } end local SCENES = { castle = { name = "castle", layers = { { "c1", pal(176, 184, 208, 96, 106, 150, 52, 56, 88, 232, 204, 124), { 30, 20, 16 } }, { "c2", pal(222, 196, 152, 170, 80, 64, 70, 48, 38), { 110, 118, 146 } }, { "c3", pal(92, 138, 90, 62, 104, 70, 200, 70, 70), { 120, 100, 78 } }, } }, forest = { name = "forest", layers = { { "f1", pal(126, 160, 120, 146, 176, 132, 246, 238, 200, 92, 128, 96), { 30, 20, 16 } }, { "f2", pal(64, 104, 76, 112, 80, 54), { 84, 110, 82 } }, { "f3", pal(140, 98, 64, 116, 80, 52, 186, 150, 106, 228, 208, 170, 74, 118, 80, 190, 70, 60), { 44, 74, 54 } }, } }, ship = { name = "ship", layers = { { "s1", pal(156, 190, 214, 252, 230, 170, 240, 244, 248), { 30, 20, 16 } }, { "s2", pal(104, 148, 184, 150, 190, 214), { 100, 122, 144 } }, { "s0", pal(128, 84, 52, 88, 60, 40, 248, 240, 214, 196, 64, 56), { 60, 90, 120 } }, { "s3", pal(70, 112, 156, 120, 160, 196), { 56, 80, 110 } }, } }, } local ORDER = { "castle", "forest", "ship" } local FOLD, TURN, ERECT = 40, 45, 40 local LIFE, SEQ = 200, ORDER local PACES = { slow = 330, normal = 200, fast = 110 } local FS = { 0.12, 0.4, 0.75, 1 } function on_settings() LIFE = PACES[cfg.pace] or 200 SEQ = cfg.scene ~= "all" and { cfg.scene } or ORDER end local function height(u, j, n) local up = u - FOLD - TURN - (j - 1) * 7 if up < 0 then return 0 end if up < 20 then return FS[up // 5 + 1] end local down = u - (FOLD + TURN + ERECT + LIFE) - (n - j) * 7 if down < 0 then return 1 end if down >= 20 then return 0 end return FS[4 - down // 5 - 1] or 0.12 end local CACHE = {} local function squash(name, fh) local key = name .. fh local c = CACHE[key] if c then return c end local s = SPR[name] local out = {} for j = 0, fh - 1 do out[j + 1] = s.rows[floor(j * s.h / fh) + 1] end c = concat(out) CACHE[key] = c return c end local function draw_layer(ly, f, dx, dy) local s = SPR[ly[1]] if not s or f <= 0 then return end local fh = max(1, floor(s.h * f + 0.5)) local cells = squash(ly[1], fh) local y = s.oy + s.h - fh + dy if not ly.shp then ly.shp = { 0, 0, 0 } for i = 4, #ly[2], 3 do ly.shp[#ly.shp + 1] = ly[3][1]; ly.shp[#ly.shp + 1] = ly[3][2]; ly.shp[#ly.shp + 1] = ly[3][3] end end blit_indexed(cells, ly.shp, s.ox + dx + 2, y + 1, s.w, fh, s.w, fh, true) blit_indexed(cells, ly[2], s.ox + dx, y, s.w, fh, s.w, fh, true) end local PAGE, PAGE_D, EDGE = { 246, 234, 204 }, { 226, 210, 174 }, { 214, 196, 160 } local LEAF = { { 58, 0 }, { 52, 4 }, { 45, 9 }, { 38, 13 }, { 32, 15 }, { 26, 13 }, { 19, 9 }, { 12, 4 }, { 6, 0 } } local function book() rect(0, 0, W, H, 52, 32, 24) for y = 6, 60, 14 do rect(0, y, W, 1, 44, 26, 20) end rect(2, 35, 60, 25, 118, 42, 46) rect(5, 36, 54, 21, PAGE[1], PAGE[2], PAGE[3]) for i = 0, 4 do local c = 246 - (4 - i) * 5 rect(27 + i, 36, 1, 21, c, c - 12, c - 42); rect(36 - i, 36, 1, 21, c, c - 12, c - 42) end rect(32, 36, 1, 21, 196, 176, 140) rect(5, 57, 54, 1, EDGE[1], EDGE[2], EDGE[3]); rect(5, 58, 54, 1, 232, 218, 186) rect(7, 40, 14, 1, 224, 206, 170); rect(7, 43, 12, 1, 224, 206, 170); rect(7, 46, 14, 1, 224, 206, 170) rect(44, 52, 12, 1, 224, 206, 170); rect(46, 54, 9, 1, 224, 206, 170) rect(31, 58, 2, 5, 170, 40, 44) end local function leaf(k) local xk, lift = LEAF[k][1], LEAF[k][2] local c = k <= 5 and PAGE or PAGE_D local x0, x1 = 32, xk local step = x1 >= x0 and 1 or -1 for x = x0, x1, step do local u = (x - x0) / (x1 - x0 == 0 and 1 or (x1 - x0)) local top, bot = floor(36 - lift * u + 0.5), floor(57 - lift * u * 0.5 + 0.5) rect(x, top, 1, bot - top + 1, c[1], c[2], c[3]) end rect(xk, floor(36 - lift + 0.5), 1, 21 - floor(lift * 0.5 + 0.5), 200, 182, 146) end local NJ = 0 function render() NJ = NJ + 1 if JOBS[NJ] then JOBS[NJ]() end gfx.ready(JOBS[NJ + 1] == nil) book() local n = #SEQ local P = FOLD + TURN + ERECT + LIFE local ft = frame + FOLD + TURN local si = (ft // P) % n + 1 local u = ft % P local prev = SCENES[SEQ[(si - 2) % n + 1]] local cur = SCENES[SEQ[si]] if u < FOLD then local L = prev.layers for j = 1, #L do draw_layer(L[j], height(u + P, j, #L), 0, 0) end end if u >= FOLD and u < FOLD + TURN then leaf((u - FOLD) // 5 + 1) end local L = cur.layers local a = frame * 0.07 for j = 1, #L do local f = height(u, j, #L) local dx, dy = 0, 0 if f >= 1 and cur.name == "ship" then if j == 2 then dx = floor(sin(a * 1.1) * 1.5 + 0.5) elseif j == 3 then dx = floor(sin(a) * 2.5 + 0.5); dy = floor(sin(2 * a + 1) * 1.2 + 0.5) elseif j == 4 then dx = -floor(sin(a * 0.9 + 1) * 1.5 + 0.5) end end draw_layer(L[j], f, dx, dy) end if height(u, 1, #L) >= 1 and height(u, #L, #L) >= 1 then if cur.name == "castle" then rect(32, 0, 1, 4, 52, 56, 88) if (frame // 7) % 2 == 0 then rect(33, 0, 4, 2, 214, 70, 60) else rect(33, 1, 4, 2, 214, 70, 60) end elseif cur.name == "forest" then local shut = frame % 90 < 3 if shut then rect(29, 20, 1, 1, 60, 40, 30); rect(32, 20, 1, 1, 60, 40, 30) else rect(29, 19, 2, 2, 250, 214, 90); rect(32, 19, 2, 2, 250, 214, 90); px(30, 20, 40, 28, 20); px(32, 20, 40, 28, 20) end end end end