info = { type = "image", title = "Orchid in bloom", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A potted orchid sways by a sunny window", api = 33, } local tr = ({ sk = { ["Flower colour"] = "Farba kvetu", ["Butterfly"] = "Motýľ" }, cs = { ["Flower colour"] = "Barva květu", ["Butterfly"] = "Motýl" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bloom", label = T("Flower colour"), type = "color", default = "#F06EB4" }, { key = "fly", label = T("Butterfly"), type = "bool", default = true }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin = math.floor, math.sin local W, H = gfx.W, gfx.H local TAU = 6.28318 local WALL = { 212, 186, 144 } local WALL_LO = { 186, 158, 118 } local FRAME = { 244, 240, 228 } local GLASS = { 140, 198, 238 } local CLOUD = { 250, 252, 255 } local SILL = { 200, 140, 78 } local SILL_D = { 150, 100, 54 } local POT = { 48, 96, 198 } local POT_L = { 98, 148, 234 } local POT_D = { 34, 70, 158 } local RIM = { 76, 126, 222 } local BAND = { 236, 240, 250 } local BARK = { 124, 82, 48 } local BARK_L = { 170, 120, 72 } local LEAF = { 56, 138, 62 } local LEAF_L = { 110, 184, 84 } local ROOT = { 178, 192, 150 } local STEM = { 92, 132, 58 } local function c3(c) return c[1], c[2], c[3] end local BG = nil local function leaf(x0, y0, L, dir, rise, droop) for i = 0, L do local yc = y0 - rise * i + droop * i * i local th = floor(1 + 3.5 * sin(3.14159 * (i + 0.5) / (L + 1))) local y = floor(yc - th / 2 + 0.5) local x = x0 + dir * i rect(x, y, 1, th, c3(LEAF)) px(x, y, c3(LEAF_L)) end end local function build_bg() rect(0, 0, W, H, c3(WALL)) rect(3, 2, 38, 34, c3(FRAME)) rect(5, 4, 34, 30, c3(GLASS)) rect(21, 4, 2, 30, c3(FRAME)); rect(5, 18, 34, 2, c3(FRAME)) rect(0, 50, W, 2, c3(SILL)) rect(0, 52, W, 3, c3(SILL_D)) rect(0, 55, W, H - 55, c3(WALL_LO)) for y = 40, 49 do local ins = (y - 40) // 4 local x1, x2 = 30 + ins, 48 - ins rect(x1, y, x2 - x1 + 1, 1, c3(POT)) rect(x1 + 1, y, 3, 1, c3(POT_L)) rect(x2 - 2, y, 3, 1, c3(POT_D)) end rect(31, 43, 17, 3, c3(BAND)) for x = 33, 46, 3 do px(x, 44, c3(POT)) end rect(29, 37, 21, 3, c3(RIM)) rect(30, 37, 3, 3, c3(POT_L)) rect(31, 36, 17, 1, c3(BARK)) for x = 32, 46, 4 do px(x, 36, c3(BARK_L)) end leaf(38, 37, 19, -1, 0.5, 0.04) leaf(41, 37, 20, 1, 0.5, 0.04) for i = 0, 8 do px(floor(35 - i * 0.3 + sin(i * 0.9) + 0.5), 39 + i, c3(ROOT)) px(floor(44 + i * 0.25 - sin(i * 0.8) + 0.5), 39 + i, c3(ROOT)) end leaf(40, 39, 14, 1, 0.3, 0.06) leaf(38, 39, 12, -1, 0.3, 0.07) BG = gfx.snapshot(0, 0, W, H) end local P0X, P0Y, P1X, P1Y, P2X, P2Y = 42, 36, 44, 0, 6, 22 local function bez(s) local a, b, c = (1 - s) * (1 - s), 2 * s * (1 - s), s * s return a * P0X + b * P1X + c * P2X, a * P0Y + b * P1Y + c * P2Y end local NS = 52 local SX, SY, SS, LEN = {}, {}, {}, {} for i = 1, NS do local s = (i - 1) / (NS - 1) SX[i], SY[i] = bez(s) SS[i] = s * s LEN[i] = i == 1 and 0 or LEN[i - 1] + math.sqrt((SX[i] - SX[i - 1]) ^ 2 + (SY[i] - SY[i - 1]) ^ 2) end local function at_tip(d) local i = NS while i > 1 and LEN[NS] - LEN[i] < d do i = i - 1 end return i end local BX, BY, BW = {}, {}, {} for k = 1, 4 do local i = at_tip(7 + 8 * (k - 1)) BX[k], BY[k], BW[k] = SX[i] - 4, SY[i], SS[i] end local BUD = {} for k = 1, 2 do BUD[k] = at_tip((k - 1) * 3) end local FLOWER = { 0,3,5,1, 1,1,7,1, 2,0,8,1, 3,0,8,1, 4,0,8,1, 5,1,2,1, 5,6,7,1, 6,2,3,1, 6,5,6,1, 2,0,0,2, 2,8,8,2, 4,0,0,2, 4,8,8,2, 5,1,1,2, 5,7,7,2, 4,3,5,3, 5,4,4,3, 6,4,4,3, 3,4,4,4, } local BUD_SEG = { 0,1,1,2, 1,0,2,2, 2,1,1,2 } local PR, PG, PB = {}, {}, {} local function segs(S, x, top) local ix = floor(x) local f = x - ix local rx = ix + (f >= 0.5 and 1 or 0) for i = 1, #S, 4 do local y, a, b, c = top + S[i], S[i + 1], S[i + 2], S[i + 3] local r, g, bl = PR[c], PG[c], PB[c] if c == 1 and f > 0 then if b > a then rect(ix + a + 1, y, b - a, 1, r, g, bl) end blend(ix + a, y, r, g, bl, 1 - f) blend(ix + b + 1, y, r, g, bl, f) else rect((c == 1 and ix or rx) + a, y, b - a + 1, 1, r, g, bl) end end end local SWAY_W, NOD_W = TAU / 5, TAU / 2.67 local function draw_plant(t) local sw = 1.6 * sin(t * SWAY_W) local sr, sg, sb = c3(STEM) for i = 1, NS do px(floor(SX[i] + sw * SS[i] + 0.5), floor(SY[i] + 0.5), sr, sg, sb) end for k = 1, #BUD do local i = BUD[k] segs(BUD_SEG, SX[i] - 1 + sw * SS[i], floor(SY[i] + 0.5)) end for k = 1, #BX do local x = BX[k] + sw * BW[k] + 0.5 * sin(t * NOD_W + k * 1.3) segs(FLOWER, x, floor(BY[k] + 0.5)) end end local CLOUD_S = { 0,4,9, 1,1,12, 2,0,14 } local function srow(x, y, a, b, r, g, bl) local ix = floor(x) local f = x - ix if b > a then rect(ix + a + 1, y, b - a, 1, r, g, bl) end blend(ix + a, y, r, g, bl, 1 - f) if f > 0 then blend(ix + b + 1, y, r, g, bl, f) end end local function draw_window(t) gfx.clip(5, 4, 34, 30) local x = 5 + (20 + t * 2.4) % 50 - 16 for j = 1, #CLOUD_S, 3 do srow(x, 9 + CLOUD_S[j], CLOUD_S[j + 1], CLOUD_S[j + 2], c3(CLOUD)) end gfx.clip() rect(21, 4, 2, 30, c3(FRAME)); rect(5, 18, 34, 2, c3(FRAME)) end local PET_PER, LAND = 7, 51 local function draw_petal(t) local a, k = t % PET_PER, floor(t / PET_PER) local b = k % #BX + 1 local x0, y0 = BX[b] + 4, BY[b] + 4 local fall = (LAND - y0) / 7.2 local al = 1 local y, x if a < fall then y = y0 + a * 7.2 x = x0 - a * 1.5 + 1.5 * sin(a * 2.1) else y = LAND x = x0 - fall * 1.5 + 1.5 * sin(fall * 2.1) local rest = a - fall if rest > 1.83 then return end if rest > 1.33 then al = (1.83 - rest) / 0.5 end end local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local r, g, bl = PR[1], PG[1], PB[1] local cw = { 1 - fx, 1, fx } for cx = 0, 2 do local w = cw[cx + 1] * al if w > 0 then blend(ix + cx, iy, r, g, bl, w * (1 - fy)) if fy > 0 then blend(ix + cx, iy + 1, r, g, bl, w * fy) end end end end local FLY_OPEN = { -2,-3,-1,7, -2,1,3,7, -1,-3,3,7, 0,-2,2,7, 1,-2,-1,7, 1,1,2,7, -1,0,0,8, 0,0,0,8, 1,0,0,8, -2,-3,-3,9, -2,3,3,9 } local FLY_SHUT = { -3,-1,1,7, -2,-1,1,7, -1,-1,1,7, 0,0,0,8, 1,0,0,8 } PR[7], PG[7], PB[7] = 250, 152, 40 PR[8], PG[8], PB[8] = 74, 46, 40 PR[9], PG[9], PB[9] = 120, 62, 34 local function draw_fly(t) local x = 30 + 20 * sin(t * TAU / 8) + 3 * sin(t * TAU / 1.23) local y = floor(22 + 9 * sin(t * TAU / 5.67 + 1) + 1.5 * sin(t * TAU / 0.433) + 0.5) local S = (t % 0.2) < 0.133 and FLY_OPEN or FLY_SHUT local ix = floor(x) local f = x - ix local rx = ix + (f >= 0.5 and 1 or 0) for i = 1, #S, 4 do local c = S[i + 3] if c == 7 then srow(x, y + S[i], S[i + 1], S[i + 2], PR[c], PG[c], PB[c]) else rect(rx + S[i + 1], y + S[i], S[i + 2] - S[i + 1] + 1, 1, PR[c], PG[c], PB[c]) end end end local FLY = true function on_settings() local c = cfg.bloom or { r = 240, g = 110, b = 180 } PR[1], PG[1], PB[1] = c.r, c.g, c.b PR[2], PG[2], PB[2] = floor(c.r * 0.8), floor(c.g * 0.66), floor(c.b * 0.8) PR[3], PG[3], PB[3] = floor(c.r * 0.74), floor(c.g * 0.24), floor(c.b * 0.5) PR[4], PG[4], PB[4] = 252, 222, 80 FLY = cfg.fly ~= false end function render() if not BG then build_bg() end if not PR[1] then on_settings() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / gfx.FPS draw_window(t) draw_plant(t) draw_petal(t) if FLY then draw_fly(t) end end