info = { type = "image", title = "Terrace sunset", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sun sinking into the sea past a railing", api = 44, } local tr = ({ sk = { ["Sun height"] = "Výška slnka", ["Low"] = "Nízko", ["Mid"] = "Stredne", ["High"] = "Vysoko", ["Sun position"] = "Poloha slnka", ["Left"] = "Vľavo", ["Centre"] = "Stred", ["Right"] = "Vpravo", ["Clouds"] = "Oblaky" }, cs = { ["Sun height"] = "Výška slunce", ["Low"] = "Nízko", ["Mid"] = "Středně", ["High"] = "Vysoko", ["Sun position"] = "Poloha slunce", ["Left"] = "Vlevo", ["Centre"] = "Střed", ["Right"] = "Vpravo", ["Clouds"] = "Mraky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sun", label = T("Sun height"), type = "int", default = 0, opts = { T("Low"), T("Mid"), T("High") } }, { key = "side", label = T("Sun position"), type = "int", default = 1, opts = { T("Left"), T("Centre"), T("Right") } }, { key = "clouds", label = T("Clouds"), type = "int", min = 0, max = 3, default = 2 }, } local px, rect, blend, brect = gfx.px, gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, sqrt, exp, abs = math.floor, math.sin, math.sqrt, math.exp, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local HOR = 34 local function mix(a, b, t) return a + (b - a) * t end local function grad(stops, y) local n = #stops if y <= stops[1][1] then return stops[1][2], stops[1][3], stops[1][4] end for i = 2, n do local a, b = stops[i - 1], stops[i] if y <= b[1] then local t = (y - a[1]) / (b[1] - a[1]) return mix(a[2], b[2], t), mix(a[3], b[3], t), mix(a[4], b[4], t) end end local l = stops[n] return l[2], l[3], l[4] end local SKY = { { 0, 44, 52, 120 }, { 9, 112, 80, 150 }, { 18, 212, 104, 130 }, { 26, 250, 142, 78 }, { 34, 255, 198, 112 } } local SEA = { { 34, 238, 152, 98 }, { 37, 212, 128, 112 }, { 42, 140, 96, 128 }, { 48, 86, 78, 128 }, { 60, 50, 58, 108 } } local SX, SY = 30, 32 local NCLOUD = 2 local function smooth(t) if t > 1 then t = 1 end return t * t * (3 - 2 * t) end local function far_h(x) local a = smooth(abs(x - SX) / 26) return floor(2 + a * (5.5 + 2.2 * sin(x * 0.21 + 1) + 1.4 * sin(x * 0.53 + 2)) + 0.5) end local function near_h(x) local a = smooth(abs(x - SX) / 34) return floor(1 + a * (3.2 + 1.6 * sin(x * 0.37) + 0.9 * sin(x * 0.91)) + 0.5) end local BAL = {} for k = 0, 7 do BAL[k + 1] = 4 + 8 * k end local CUR, BG = nil, nil local stage = 0 local STAGES = {} STAGES[1] = function() for y = 0, HOR - 1 do rect(0, y, W, 1, grad(SKY, y)) end end local function glow_rows(y0, y1) local R = 19 for y = y0, y1 do local dy = y + 0.5 - SY for x = SX - R, SX + R do local dx = x + 0.5 - SX local d = sqrt(dx * dx + dy * dy) if d < R then blend(x, y, 255, 216, 134, 0.62 * (1 - d / R)) end end end end STAGES[2] = function() glow_rows(math.max(0, SY - 19), SY - 6) end STAGES[3] = function() glow_rows(SY - 5, HOR - 1) end STAGES[4] = function() local R = 5.4 for y = SY - 6, SY + 6 do if y < HOR then local ty = (y - (SY - R)) / (2 * R) local r, g, b = mix(255, 255, ty), mix(250, 206, ty), mix(228, 112, ty) local ey = (y + 0.5 - SY) / R for x = SX - 6, SX + 6 do local ex = (x + 0.5 - SX) / R local q = ex * ex + ey * ey if q < 0.9 then px(x, y, r, g, b) elseif q < 1.25 then blend(x, y, r, g, b, (1.25 - q) / 0.35) end end end end for x = 0, W - 1 do local w = exp(-((x - SX) / 22) ^ 2) local hf = far_h(x) local top = HOR - hf local c1 = { mix(92, 190, w), mix(72, 104, w), mix(128, 120, w) } local c2 = { mix(120, 236, w), mix(88, 146, w), mix(142, 112, w) } local m = top + floor(hf * 0.5) rect(x, top, 1, m - top, c1[1], c1[2], c1[3]) rect(x, m, 1, HOR - m, c2[1], c2[2], c2[3]) blend(x, top, 255, 190, 140, 0.35 * w) local hn = near_h(x) local nt = HOR - hn rect(x, nt, 1, hn, mix(62, 132, w), mix(54, 76, w), mix(98, 92, w)) rect(x, HOR - 1, 1, 1, mix(86, 188, w), mix(66, 104, w), mix(110, 100, w)) end brect(0, HOR - 2, W, 2, 255, 190, 130, 0.22) end STAGES[5] = function() for y = HOR, 59 do rect(0, y, W, 1, grad(SEA, y)) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local y = HOR + 2 + umod(h, 22) local x = umod(h >> 8, W - 14) brect(x, y, 6 + umod(h >> 16, 9), 1, 20, 24, 60, 0.16) end for y = HOR + 1, 59 do local hw = 1.6 + (y - HOR) * 0.3 local a = 0.11 + 0.07 * (1 - (y - HOR) / 26) for k = 1, 3 do local w = hw * (1.25 - 0.3 * k) brect(floor(SX - w + 0.5), y, floor(2 * w + 0.5), 1, 255, 176, 96, a) end end end STAGES[6] = function() rect(0, 50, W, 1, 168, 94, 78) rect(0, 51, W, 1, 28, 20, 36) for k = 1, #BAL do local x = BAL[k] rect(x, 52, 2, 8, 20, 15, 30) if x < SX then px(x + 1, 52, 120, 64, 66); rect(x + 1, 53, 1, 7, 70, 42, 56) else px(x, 52, 120, 64, 66); rect(x, 53, 1, 7, 70, 42, 56) end end rect(0, 46, 3, 14, 22, 16, 32); rect(61, 46, 3, 14, 22, 16, 32) px(2, 46, 110, 60, 62); rect(2, 47, 1, 13, 62, 38, 54) px(61, 46, 110, 60, 62); rect(61, 47, 1, 13, 62, 38, 54) rect(0, 60, W, 1, 98, 54, 52) rect(0, 61, W, 3, 22, 16, 30) end local function bar(xf, y, w, r, g, b, a) local x0 = floor(xf) local f = xf - x0 brect(x0 + 1, y, w - 1, 1, r, g, b, a) blend(x0, y, r, g, b, a * (1 - f)) blend(x0 + w, y, r, g, b, a * f) end local SHAPES = { { { 0, 5, 9, 1 }, { 1, 2, 17, 1 }, { 2, 0, 22, 2 }, { 3, 3, 16, 3 } }, { { 0, 3, 7, 1 }, { 1, 0, 14, 2 }, { 2, 1, 11, 3 } }, { { 0, 6, 10, 1 }, { 1, 3, 18, 1 }, { 2, 0, 24, 2 }, { 3, 1, 21, 3 }, { 4, 5, 12, 3 } }, } local CSH = { { 255, 208, 166 }, { 244, 150, 134 }, { 170, 102, 142 } } local CLOUD = { { 6, 1, 2.1, 0 }, { 15, 2, 1.4, 37 }, { 22, 3, 2.7, 17 } } local function draw_clouds(t) for i = 1, NCLOUD do local c = CLOUD[i] local rows = SHAPES[c[2]] local L = 26 local x = (c[4] + t * c[3] / FPS) % (W + L) - L for k = 1, #rows do local r = rows[k] local col = CSH[r[4]] bar(x + r[2], c[1] + r[1], r[3], col[1], col[2], col[3], 0.78) end end end local NG = 30 local GX, GY, GL, GP, GR = {}, {}, {}, {}, {} local function glitter_init() for i = 1, NG do local h = hash32(i * 0x85EBCA6B) local r = umod(h, 1000) / 1000 local y = HOR + 1 + floor(24 * r * r) if y == 50 or y == 51 then y = 49 end local hw = 1.6 + (y - HOR) * 0.3 local len = 1 + (y - HOR) // 9 local x = floor(SX - hw + (umod(h >> 10, 1000) / 1000) * 2 * hw) if y >= 52 then for k = 1, #BAL do if x + len > BAL[k] and x < BAL[k] + 2 then x = BAL[k] + 2 end end end GX[i], GY[i], GL[i] = x, y, len GP[i] = umod(h >> 20, 1000) / 1000 GR[i] = 0.9 + umod(h >> 4, 14) / 10 end end local function draw_glitter(t) for i = 1, NG do local s = sin(TAU * (t / (GR[i] * FPS) + GP[i])) if s > 0.1 then brect(GX[i], GY[i], GL[i], 1, 255, 238, 188, 0.95 * s * s) end end end local function draw_gull(t) local x = (t * 3.2 / FPS + 20) % (W + 16) - 8 local y = floor(14 + 3 * sin(TAU * t / (9 * FPS))) local up = floor(t / FPS * 4.3) % 2 == 0 local f = x - floor(x) local ix = floor(x) local c1, c2, c3 = 58, 38, 66 for dx = -2, 2 do local dy = (abs(dx) == 2) and (up and -1 or 0) or ((abs(dx) == 1) and (up and 0 or -1) or 0) if dx == 0 then dy = 0 end blend(ix + dx, y + dy, c1, c2, c3, 1 - f) blend(ix + dx + 1, y + dy, c1, c2, c3, f) end end function on_settings() local sun = cfg.sun or 0 SY = sun == 0 and 32 or (sun == 1 and 24 or 15) local s = cfg.side or 1 SX = s == 0 and 16 or (s == 1 and 30 or 46) NCLOUD = cfg.clouds or 2 glitter_init() stage, BG = 0, nil end function render() if not BG then stage = stage + 1 if stage > 1 then gfx.blit_rgb(CUR, 0, 0, W, H) end STAGES[stage]() CUR = gfx.snapshot(0, 0, W, H) if stage == #STAGES then BG = CUR else gfx.ready(false) end return end local t = frame gfx.blit_rgb(BG, 0, 0, W, H) draw_clouds(t) draw_gull(t) draw_glitter(t) end