info = { type = "image", title = "TV channel surfer", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "An old TV flips channels, then switches off", api = 33, } local tr = ({ sk = { ["Channel time"] = "Čas kanála", ["slow"] = "pomaly", ["normal"] = "normálne", ["fast"] = "rýchlo", ["Switch off"] = "Vypnúť", ["Cabinet"] = "Skrinka" }, cs = { ["Channel time"] = "Čas kanálu", ["slow"] = "pomalu", ["normal"] = "normálně", ["fast"] = "rychle", ["Switch off"] = "Vypnout", ["Cabinet"] = "Skříňka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Channel time"), type = "int", default = 1, opts = { T("slow"), T("normal"), T("fast") } }, { key = "off", label = T("Switch off"), type = "bool", default = true }, { key = "cab", label = T("Cabinet"), type = "color", default = "#B4733C" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, abs, min, max = math.floor, math.sin, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local WALL1, WALL2 = { 38, 82, 104 }, { 44, 96, 120 } local TABLE, TABLE_D, FLOOR = { 126, 76, 44 }, { 84, 52, 34 }, { 56, 44, 72 } local BEZEL, KNOB = { 58, 54, 52 }, { 206, 204, 196 } local CAB, CAB_L, CAB_D = { 180, 115, 60 }, { 206, 140, 80 }, { 112, 70, 38 } local ANT, BALL = { 190, 192, 200 }, { 240, 240, 230 } local SX, SY, SW, SH = 12, 19, 32, 24 local NPAL = { 24, 24, 24, 56, 56, 56, 88, 88, 88, 124, 124, 124, 160, 160, 160, 196, 196, 196, 228, 228, 228, 250, 250, 250 } local NZ = {} for k = 1, 4 do local rows = {} for y = 0, 11 do local row = {} for x = 0, 31 do local h = hash32((k * 64 + y) * 0x9E3779B1 + (x // 10) * 0x85EBCA6B) row[x + 1] = (h >> (3 * (x % 10))) & 7 end rows[y + 1] = string.char(table.unpack(row, 1, 32)) end NZ[k] = table.concat(rows) end local BG = nil local function rr(x, y, w, h, c) rect(x + 2, y, w - 4, h, c[1], c[2], c[3]) rect(x, y + 2, w, h - 4, c[1], c[2], c[3]) rect(x + 1, y + 1, w - 2, h - 2, c[1], c[2], c[3]) end local function build_bg() for x = 0, W - 1, 8 do local c = (x // 8) % 2 == 0 and WALL1 or WALL2 rect(x, 0, 8, 54, c[1], c[2], c[3]) end rect(0, 54, W, 3, TABLE[1], TABLE[2], TABLE[3]) rect(0, 54, W, 1, CAB_L[1], CAB_L[2], CAB_L[3]) rect(0, 57, W, 1, TABLE_D[1], TABLE_D[2], TABLE_D[3]) rect(0, 58, W, 6, FLOOR[1], FLOOR[2], FLOOR[3]) rect(12, 50, 4, 4, CAB_D[1], CAB_D[2], CAB_D[3]) rect(48, 50, 4, 4, CAB_D[1], CAB_D[2], CAB_D[3]) rr(6, 15, 52, 35, CAB) rect(8, 15, 48, 1, CAB_L[1], CAB_L[2], CAB_L[3]) rect(8, 48, 48, 2, CAB_D[1], CAB_D[2], CAB_D[3]) rr(10, 17, 36, 28, BEZEL) rect(28, 12, 8, 3, BEZEL[1], BEZEL[2], BEZEL[3]) for _, cy in ipairs({ 24, 33 }) do rect(51, cy - 3, 3, 1, KNOB[1], KNOB[2], KNOB[3]) rect(50, cy - 2, 5, 3, KNOB[1], KNOB[2], KNOB[3]) rect(51, cy + 1, 3, 1, KNOB[1], KNOB[2], KNOB[3]) rect(50, cy + 2, 5, 1, CAB_D[1], CAB_D[2], CAB_D[3]) end for k = 0, 3 do rect(49, 39 + k * 2, 7, 1, CAB_D[1], CAB_D[2], CAB_D[3]) end BG = gfx.snapshot(0, 0, W, H) end local LENS0 = { 128, 86, 98, 80 } local LENS, CUM, TOTAL = {}, {}, 0 local ON_N, OFF_N = 16, 64 local POWER = true function on_settings() local c = cfg.cab or { r = 180, g = 115, b = 60 } CAB = { c.r, c.g, c.b } CAB_L = { math.min(255, c.r * 115 // 100), math.min(255, c.g * 115 // 100), math.min(255, c.b * 115 // 100) } CAB_D = { c.r * 62 // 100, c.g * 62 // 100, c.b * 62 // 100 } BG = nil local f = ({ [0] = 1.4, [1] = 1.0, [2] = 0.6 })[cfg.speed or 1] or 1.0 TOTAL = 0 for i = 1, 4 do LENS[i] = floor(LENS0[i] * f + 0.5) CUM[i] = TOTAL TOTAL = TOTAL + LENS[i] end POWER = cfg.off ~= false end local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local BARS = { { 235, 235, 235 }, { 235, 235, 40 }, { 40, 225, 225 }, { 40, 215, 40 }, { 225, 40, 225 }, { 225, 40, 40 }, { 40, 40, 225 } } local function prog_bars(t) for i = 0, 6 do local x0, x1 = SX + (i * SW) // 7, SX + ((i + 1) * SW) // 7 local c = BARS[i + 1] rect(x0, SY, x1 - x0, SH - 6, c[1], c[2], c[3]) local d = BARS[7 - i] if i % 2 == 0 then rect(x0, SY + SH - 6, x1 - x0, 6, d[1], d[2], d[3]) else rect(x0, SY + SH - 6, x1 - x0, 6, 14, 14, 20) end end end local function prog_scope(t) rect(SX, SY, SW, SH, 18, 34, 80) for x = 4, SW - 1, 8 do rect(SX + x, SY, 1, SH, 34, 60, 120) end rect(SX, SY + SH // 2, SW, 1, 34, 60, 120) local amp = 7 * (0.6 + 0.4 * sin(t * 0.05)) local py = nil for x = 0, SW - 1 do local y = SY + SH // 2 + floor(amp * sin(x * 0.42 - t * 0.3) * (0.7 + 0.3 * sin(x * 0.17 + t * 0.11)) + 0.5) if py then local a, b = min(py, y), max(py, y) rect(SX + x, a, 1, b - a + 1, 80, 255, 140) else px(SX + x, y, 80, 255, 140) end py = y end end local function prog_ball(t) rect(SX, SY, SW, SH, 80, 170, 245) rect(SX, SY + SH - 8, SW, 8, 50, 170, 70) rect(SX, SY + SH - 8, SW, 1, 110, 210, 90) local cx = SX + 25 rect(cx - 2, SY + 3, 5, 1, 255, 220, 60); rect(cx - 3, SY + 4, 7, 3, 255, 220, 60); rect(cx - 2, SY + 7, 5, 1, 255, 220, 60) local cl = floor((t * 0.25) % (SW + 12)) - 10 blend_rect(SX + cl, SY + 3, 9, 2, 255, 255, 255, 0.85) blend_rect(SX + cl + 2, SY + 2, 5, 1, 255, 255, 255, 0.85) local P = 40 local u = (t % P) / P local h = 4 * u * (1 - u) * 12 local tri = abs(((t / (2 * P)) % 1) * 2 - 1) local bx = SX + 5 + floor(tri * (SW - 10) + 0.5) local by = SY + SH - 11 - floor(h + 0.5) rect(bx - 2, SY + SH - 7, 5, 1, 30, 120, 50) if h < 1.5 then rect(bx - 3, by + 1, 7, 3, 240, 50, 50); rect(bx - 2, by, 5, 1, 240, 50, 50); rect(bx - 2, by + 4, 5, 1, 240, 50, 50) else rect(bx - 1, by - 1, 3, 1, 240, 50, 50); rect(bx - 2, by, 5, 3, 240, 50, 50); rect(bx - 1, by + 3, 3, 1, 240, 50, 50) px(bx - 1, by, 255, 200, 200) end end local function tri(t, per, lo, hi) local u = abs(((t / per) % 1) * 2 - 1) return lo + (hi - lo) * u end local function prog_pong(t) rect(SX, SY, SW, SH, 22, 26, 60) rect(SX, SY, SW, 1, 150, 150, 170); rect(SX, SY + SH - 1, SW, 1, 150, 150, 170) for k = 0, 5 do rect(SX + SW // 2, SY + 2 + k * 4, 1, 2, 120, 120, 150) end local function ball(tt) return tri(tt, 56, 5, SW - 7), tri(tt, 38, 3, SH - 5) end local bx, by = ball(t) local _, ly = ball(t - 5) local rx, ry = ball(t - 9) local y1 = max(1, min(SH - 8, floor(ly - 2))) local y2 = max(1, min(SH - 8, floor(ry - 2))) rect(SX + 2, SY + y1, 2, 7, 235, 235, 235) rect(SX + SW - 4, SY + y2, 2, 7, 235, 235, 235) rect(SX + floor(bx), SY + floor(by), 2, 2, 255, 255, 255) end local PROGS = { prog_bars, prog_scope, prog_ball, prog_pong } local function noise(f, x, y, w, h) gfx.blit_indexed(NZ[f % 4 + 1], NPAL, x, y, w, h, 32, 12) end local function antenna(a, len) local k = math.tan(a) local last = 0 for i = 1, len do local y = 12 - i local x = 32 + k * i local ix = floor(x) local f = x - ix blend(ix, y, ANT[1], ANT[2], ANT[3], 1 - f) blend(ix + 1, y, ANT[1], ANT[2], ANT[3], f) last = x end local bx = floor(last + 0.5) rect(bx - 1, 12 - len - 1, 2, 2, BALL[1], BALL[2], BALL[3]) end function render() if not BG then build_bg() end if TOTAL == 0 then on_settings() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local on_n, off_n = POWER and ON_N or 0, POWER and OFF_N or 0 local cyc = TOTAL + on_n + off_n local p = t % cyc local mode, pi, tp, since = 0, 1, 0, 999 if p < on_n then mode, since = 1, p elseif p < on_n + TOTAL then local q = p - on_n for i = 4, 1, -1 do if q >= CUM[i] then pi = i tp = q - CUM[i] break end end since = tp else mode, tp = 2, p - on_n - TOTAL end local env = 0 if since < 36 then env = (1 - since / 36) ^ 2 end local sw = env * 0.22 * sin(t * 1.7) + 0.012 * sin(t * 0.31) antenna(-0.62 + sw, 11) antenna(0.5 + env * 0.2 * sin(t * 1.9 + 1.3) + 0.012 * sin(t * 0.27 + 2), 12) local lit = true if mode == 0 then gfx.clip(SX, SY, SW, SH) if tp < 6 then noise(t, SX, SY, SW, SH) else PROGS[pi](tp) end local y = floor((t * 0.45) % (SH + 6)) - 6 blend_rect(SX, SY + y, SW, 6, 255, 255, 255, 0.10) gfx.clip() elseif mode == 1 then local u = p / ON_N if u < 0.4 then local w = 2 + floor((SW - 2) * ease(u / 0.4) + 0.5) rect(SX + (SW - w) // 2, SY + SH // 2 - 1, w, 2, 255, 255, 255) else local h = 2 + floor((SH - 2) * ease((u - 0.4) / 0.6) + 0.5) local y0 = SY + (SH - h) // 2 gfx.clip(SX, y0, SW, h) noise(t, SX, SY, SW, SH) gfx.clip() end else local v = tp if v < 12 then local u = v / 12 local h = max(2, floor(SH - (SH - 2) * u * u + 0.5)) local y0 = SY + (SH - h) // 2 gfx.clip(SX, y0, SW, h) PROGS[4](TOTAL - CUM[4] - 1 + v) blend_rect(SX, y0, SW, h, 255, 255, 255, 0.55 * u) gfx.clip() else lit = false rect(SX, SY, SW, SH, 0, 0, 0) if v < 20 then local w = max(2, floor(SW - (SW - 2) * ease((v - 12) / 8) + 0.5)) rect(SX + (SW - w) // 2, SY + SH // 2 - 1, w, 2, 255, 255, 255) elseif v < 44 then local c = floor(255 * (1 - (v - 20) / 24) ^ 2) if c > 4 then rect(SX + SW // 2 - 1, SY + SH // 2 - 1, 2, 2, c, c, c) end end end end local B = BEZEL for _, c in ipairs({ { SX, SY, 1, 1 }, { SX + SW - 1, SY, -1, 1 }, { SX, SY + SH - 1, 1, -1 }, { SX + SW - 1, SY + SH - 1, -1, -1 } }) do px(c[1], c[2], B[1], B[2], B[3]); px(c[1] + c[3], c[2], B[1], B[2], B[3]); px(c[1], c[2] + c[4], B[1], B[2], B[3]) end blend_rect(SX + 2, SY + 1, 6, 1, 255, 255, 255, 0.22) blend_rect(SX + 1, SY + 2, 1, 4, 255, 255, 255, 0.22) local ch = (mode == 0) and (pi - 1) or 0 local a = ch * 0.9 px(51 + floor(2 * sin(a) + 0.5), 24 - floor(2 * math.cos(a) + 0.5), 40, 30, 24) px(51 + floor(2 * sin(2.4 - ch * 0.5) + 0.5), 33 - floor(2 * math.cos(2.4 - ch * 0.5) + 0.5), 40, 30, 24) if lit then rect(52, 46, 2, 1, 60, 255, 90) else rect(52, 46, 2, 1, 36, 70, 44) end end