Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/ui/canvas.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / ui / canvas.c` 814 lines ยท 35243 bytes Raw Blame History 1 /* canvas.c -- Allen's canvas, the light side; see canvas.h and docs/ALLEN_CANVAS.md. */ 2 #include "canvas.h" 3 4 #define S(v) ((int32_t)(((int64_t)(v) * (int64_t)c->k + 128) / 256)) 5 6 /* The palette (docs/ALLEN_PRESENCE_RESEARCH.md, ui/allen_ui.c): nothing else is drawn. */ 7 #define C_NIGHT CK_RGB(6, 8, 18) 8 #define C_TEXT CK_RGB(236, 240, 252) 9 #define C_HINT CK_RGB(150, 160, 196) 10 #define C_GLOW CK_RGB(150, 170, 255) 11 #define C_MIST CK_RGB(200, 210, 255) 12 #define C_INK CK_RGB(64, 86, 196) 13 #define C_WARM CK_RGB(255, 212, 150) 14 #define C_WARM2 CK_RGB(255, 222, 180) 15 16 #define OPEN_MS 450u /* a bloom: the spark's flight before the thing opens */ 17 #define SPARK_MS 650u 18 #define COME_MS 700u /* each line eases in over this */ 19 #define ROW_MS 70u /* and the next line starts this much later */ 20 #define GO_MS 700u /* leaving, and the sky's change of tone */ 21 #define BETWEEN_MS 260u /* one thing after another */ 22 23 /* The large digits: one pool, rasterized whole at start so drawing never writes to it. */ 24 static uint8_t big_pool[384u << 10]; 25 static const char big_set[] = "`0123456789`.,:%$+-/x\""; 26 27 static int is_big(char ch) 28 { 29 for (const char *p = big_set; *p; p++) 30 if (*p == ch) 31 return 1; 32 return 0; 33 } 34 35 static unsigned len_of(const char *s) 36 { 37 unsigned n = 0; 38 while (s[n]) 39 n++; 40 return n; 41 } 42 43 static uint32_t role_rgb(unsigned role) 44 { 45 static const uint32_t rgb[4] = {C_TEXT, C_MIST, C_HINT, C_WARM2}; 46 return rgb[role & 3]; 47 } 48 49 static ck_af_face *face_of(const ck_allen_canvas *c, unsigned f) 50 { 51 switch (f) { 52 case 0: 53 return c->voice; 54 case 1: 55 return c->body; 56 case 2: 57 return c->small; 58 default: 59 return (ck_af_face *)&c->big; /* drawing reads only: every glyph it may meet was made at start */ 60 } 61 } 62 63 static int32_t cap_of(const ck_af_face *f) { return (int32_t)(f->s256 * 20u / 256u); } 64 65 int ck_allen_canvas_init(ck_allen_canvas *c, int32_t w, int32_t h, uint32_t k, int32_t cx, int32_t cy, 66 ck_af_face *voice, ck_af_face *body, ck_af_face *small) 67 { 68 for (size_t i = 0; i < sizeof *c; i++) 69 ((char *)c)[i] = 0; 70 c->w = w, c->h = h, c->k = k, c->cx = cx, c->cy = cy; 71 c->voice = voice, c->body = body, c->small = small; 72 unsigned cap = 60u * k / 256u; 73 for (;;) { 74 ck_af_face_init(&c->big, cap, 700, big_pool, sizeof big_pool); 75 int ok = 1; 76 for (const char *p = big_set; *p && ok; p++) 77 ok = ck_af_glyph_get(&c->big, *p) != 0; 78 if (ok) 79 return 0; 80 if (cap < 20) 81 return -1; 82 cap = cap * 9 / 10; 83 } 84 } 85 86 /* ---- laying a thing out ---- */ 87 88 static int put(ck_canvas_item *it, const char *s, unsigned n) 89 { 90 if ((unsigned)it->used + n + 1 > CK_CANVAS_TEXT) 91 return -1; 92 int at = it->used; 93 for (unsigned i = 0; i < n; i++) 94 it->text[it->used++] = s[i]; 95 it->text[it->used++] = 0; 96 return at; 97 } 98 99 static void row(ck_canvas_item *it, int off, unsigned len, int32_t dx, int32_t dy, unsigned face, unsigned role, 100 unsigned dot) 101 { 102 if (off < 0 || !len || it->nrows >= CK_CANVAS_ROWS) 103 return; 104 it->row[it->nrows++] = (ck_canvas_row){(uint16_t)off, (uint16_t)len, dx, dy, (uint8_t)face, (uint8_t)role, 105 (uint8_t)dot}; 106 } 107 108 /* Field text at off (len letters) wrapped to width with face f, at most max lines, from top; each line's 109 * start x is indent, or centred in width when centre is set. The next line's top is returned. A text that 110 * does not fit ends with "...". */ 111 static int32_t wrapped(const ck_allen_canvas *c, ck_canvas_item *it, int off, unsigned face, unsigned role, 112 int32_t indent, int32_t width, int32_t top, int32_t lh, unsigned max, int centre, unsigned dot) 113 { 114 if (off < 0) 115 return top; 116 ck_af_face *f = face_of(c, face); 117 const char *s = it->text + off; 118 unsigned n = len_of(s), i = 0, lines = 0; 119 int32_t cap = cap_of(f); 120 while (i < n && lines < max && it->nrows < CK_CANVAS_ROWS) { 121 while (i < n && s[i] == ' ') 122 i++; 123 if (i >= n) 124 break; 125 unsigned start = i, last_space = 0, j = i; 126 int32_t wpx = 0; 127 while (j < n) { 128 int32_t aw = (int32_t)((ck_af_advance(f, s[j]) + 255) / 256); 129 if (wpx + aw > width && j > start) 130 break; 131 if (s[j] == ' ') 132 last_space = j; 133 wpx += aw; 134 j++; 135 } 136 if (j < n && last_space > start) 137 j = last_space; 138 unsigned ln = j - start; 139 int lo = off + (int)start; 140 if (j < n && (lines + 1 == max || it->nrows + 1 == CK_CANVAS_ROWS)) { /* the last line there is room for */ 141 char tmp[CK_CANVAS_TEXT]; 142 unsigned m = ln; 143 while (m && (int32_t)ck_af_measure(f, s + start, m) + (int32_t)ck_af_measure(f, "...", 3) > width) 144 m--; 145 while (m && s[start + m - 1] == ' ') 146 m--; 147 for (unsigned q = 0; q < m; q++) 148 tmp[q] = s[start + q]; 149 tmp[m] = '.', tmp[m + 1] = '.', tmp[m + 2] = '.'; 150 lo = put(it, tmp, m + 3); 151 ln = m + 3; 152 j = n; 153 } 154 int32_t dx = indent; 155 if (centre && lo >= 0) 156 dx = indent + (width - (int32_t)ck_af_measure(f, it->text + lo, ln)) / 2; 157 row(it, lo, ln, dx, top + cap, face, role, dot && lines == 0); 158 top += lh; 159 lines++; 160 i = j; 161 } 162 return top; 163 } 164 165 static int field(ck_canvas_item *it, unsigned i) { return i < it->nf ? (int)it->f[i] : -1; } 166 167 /* Rows and shapes for a thing of width W; its height. */ 168 static int32_t lay_out(const ck_allen_canvas *c, ck_canvas_item *it, int32_t W) 169 { 170 int32_t y = 0, ind = S(26); 171 unsigned lead = it->warm ? 3u : 0u; 172 it->nrows = 0, it->rr = 0, it->used = it->base; /* what an earlier lay-out added goes */ 173 switch (it->kind) { 174 case CK_THING_NOTE: { 175 int title = it->nf >= 2 ? field(it, 0) : -1, body = it->nf >= 2 ? field(it, 1) : field(it, 0); 176 if (title >= 0) 177 y = wrapped(c, it, title, 0, lead, ind, W - ind, y, S(40), 2, 0, 0) + S(6); 178 y = wrapped(c, it, body, 1, 1, ind, W - ind, y, S(33), 6, 0, 0); 179 break; 180 } 181 case CK_THING_LIST: { 182 unsigned first = 0; 183 if (it->nf >= 2) { 184 y = wrapped(c, it, field(it, 0), 1, it->warm ? 3u : 2u, ind, W - ind, y, S(32), 1, 0, 0) + S(6); 185 first = 1; 186 } 187 for (unsigned i = first; i < it->nf; i++) 188 y = wrapped(c, it, field(it, i), 1, 0, ind + S(24), W - ind - S(24), y, S(37), 2, 0, 1); 189 break; 190 } 191 case CK_THING_NUMBER: { 192 const char *v = it->text + it->f[0]; 193 unsigned n = len_of(v), b = 0; 194 while (b < n && (is_big(v[b]) || (v[b] == ' ' && b + 1 < n && is_big(v[b + 1]) && v[b + 1] != '"'))) 195 b++; 196 while (b && v[b - 1] == ' ') 197 b--; 198 unsigned u = b; 199 while (u < n && v[u] == ' ') 200 u++; 201 int32_t bw = b ? (int32_t)ck_af_measure(&c->big, v, b) : 0; 202 int32_t uw = u < n ? (int32_t)ck_af_measure(c->voice, v + u, n - u) : 0; 203 int32_t gap = b && u < n ? S(10) : 0, x0 = (W - bw - gap - uw) / 2, bc = cap_of(&c->big); 204 if (b) 205 row(it, it->f[0], b, x0, bc, 3, lead, 0); 206 if (u < n) 207 row(it, it->f[0] + (int)u, n - u, x0 + bw + gap, b ? bc : cap_of(c->voice), 0, b ? 2u : lead, 0); 208 y = (b ? bc : cap_of(c->voice)) + S(26); 209 if (it->nf >= 2) 210 y = wrapped(c, it, field(it, 1), 1, 2, 0, W, y, S(31), 2, 1, 0); 211 it->rx = W / 2, it->ry = bc / 2; /* where the digits' glow sits */ 212 break; 213 } 214 case CK_THING_QUOTE: { 215 y = wrapped(c, it, field(it, 0), 0, 0, ind, W - ind, 0, S(40), 5, 0, 0); 216 if (it->nf >= 2) 217 y = wrapped(c, it, field(it, 1), 1, 2, ind + S(30), W - ind - S(30), y + S(6), S(32), 1, 0, 0); 218 break; 219 } 220 case CK_THING_PERSON: { 221 it->rx = S(42), it->ry = S(46), it->rr = S(38); 222 const char *name = it->text + it->f[0]; 223 char ini = name[0] >= 'a' && name[0] <= 'z' ? (char)(name[0] - 32) : name[0]; 224 int io = put(it, &ini, 1); 225 row(it, io, 1, it->rx - (int32_t)ck_af_measure(c->voice, &ini, 1) / 2, it->ry + cap_of(c->voice) / 2, 0, 3, 0); 226 int32_t nx = S(104), top = S(4); 227 if (it->way) { 228 const char *wd = it->way == CK_WAY_TO ? "a message for" : "a message from"; 229 top = wrapped(c, it, put(it, wd, len_of(wd)), 2, 2, nx, W - nx, top, S(28), 1, 0, 0); 230 } 231 top = wrapped(c, it, field(it, 0), 0, lead, nx, W - nx, top, S(40), 1, 0, 0) + S(4); 232 y = wrapped(c, it, field(it, 1), 1, 1, nx, W - nx, top, S(33), 4, 0, 0); 233 y = ck_max(y, it->ry + it->rr + S(10)); 234 break; 235 } 236 case CK_THING_TIMER: { 237 it->rx = W / 2, it->ry = S(66), it->rr = S(62); 238 const char *when = it->text + it->f[0]; 239 it->dur = ck_scene_duration_ms(when); 240 y = it->ry + it->rr + S(22); 241 int label = it->nf >= 2 ? field(it, 1) : -1; 242 if (!it->dur && (int32_t)ck_af_measure(c->voice, when, len_of(when)) <= it->rr * 17 / 10) { 243 int32_t ww = (int32_t)ck_af_measure(c->voice, when, len_of(when)); 244 row(it, it->f[0], len_of(when), it->rx - ww / 2, it->ry + cap_of(c->voice) / 2, 0, lead, 0); 245 } else if (!it->dur) { /* too long for the ring: under it, and the label after */ 246 y = wrapped(c, it, it->f[0], 0, lead, 0, W, y, S(38), 2, 1, 0); 247 } 248 if (label >= 0) 249 y = wrapped(c, it, label, 1, 0, 0, W, y, S(33), 2, 1, 0); 250 break; 251 } 252 default: { /* a picture or a video, still on its way */ 253 it->rx = 0, it->ry = 0, it->rr = W * 9 / 16; 254 y = wrapped(c, it, field(it, 0), 1, 0, S(6), W - S(6), it->rr + S(22), S(33), 2, 0, 0); 255 if (it->nf >= 2) 256 y = wrapped(c, it, field(it, 1), 2, 2, S(6), W - S(6), y, S(28), 1, 0, 0); 257 break; 258 } 259 } 260 return y; 261 } 262 263 /* ---- arriving, staying, leaving ---- */ 264 265 static uint32_t opens(const ck_canvas_item *it) { return it->born + (it->arrive == CK_ARRIVE_BLOOM ? OPEN_MS : 0u); } 266 static uint32_t come_ms(const ck_canvas_item *it) { return it->arrive == CK_ARRIVE_RISE ? 950u : COME_MS; } 267 268 /* how far line j has come in at t, 0..256, eased */ 269 static uint32_t come(const ck_canvas_item *it, unsigned j, uint32_t t) 270 { 271 uint32_t o = opens(it) + j * ROW_MS; 272 return ck_ease_out(ck_prog(t, o, o + come_ms(it))); 273 } 274 275 static uint32_t stays(const ck_canvas_item *it, uint32_t t) 276 { 277 uint32_t keep = it->gone ? 256u - ck_smooth(ck_prog(t, it->gone, it->gone + GO_MS)) : 256u; 278 return it->quiet ? keep * 176u >> 8 : keep; 279 } 280 281 static int32_t lift(const ck_allen_canvas *c, const ck_canvas_item *it, uint32_t e) 282 { 283 if (it->arrive == CK_ARRIVE_FADE) 284 return 0; 285 return (int32_t)((int64_t)S(it->arrive == CK_ARRIVE_RISE ? 24 : 10) * (256 - e) >> 8); 286 } 287 288 static uint32_t arrived_by(const ck_canvas_item *it) { return opens(it) + (it->nrows + 1u) * ROW_MS + come_ms(it) + 40u; } 289 290 static int timer_runs(const ck_canvas_item *it, uint32_t t) 291 { 292 return it->kind == CK_THING_TIMER && it->dur && t < opens(it) + it->dur + 10000u; 293 } 294 295 /* where the spark is at t (16ths of a pixel), k steps back along its trail */ 296 static void spark_at(const ck_allen_canvas *c, const ck_canvas_item *it, int32_t p, int32_t *sx, int32_t *sy) 297 { 298 uint32_t e = ck_smooth(ck_clamp(p, 0, 256)); 299 *sx = c->hx + (int32_t)((int64_t)(it->ax - c->hx) * e >> 8); 300 *sy = c->hy + (int32_t)((int64_t)(it->ay - c->hy) * e >> 8) - (int32_t)((int64_t)S(70) * 16 * (e * (256 - e) >> 6) >> 8); 301 } 302 303 /* the part of the screen the spark lights at t: its head and trail with the halo round them */ 304 static ck_rect spark_box(const ck_allen_canvas *c, const ck_canvas_item *it, uint32_t t) 305 { 306 if (t < it->born || t >= it->born + SPARK_MS + 40) 307 return (ck_rect){0, 0, 0, 0}; 308 int32_t p = ck_prog(t, it->born, it->born + SPARK_MS), x0, y0, x1, y1, m = S(36) + 2; 309 spark_at(c, it, p, &x0, &y0); 310 spark_at(c, it, p - 5 * 14, &x1, &y1); 311 ck_rect a = {ck_min(x0, x1) / 16 - m, ck_min(y0, y1) / 16 - m, 0, 0}; 312 a.w = ck_max(x0, x1) / 16 + m - a.x, a.h = ck_max(y0, y1) / 16 + m - a.y; 313 return a; 314 } 315 316 static ck_rect ring_box(const ck_allen_canvas *c, const ck_canvas_item *it) 317 { 318 int32_t m = it->rr + S(14); 319 return (ck_rect){it->x + it->rx - m, it->y + it->ry - m, 2 * m, 2 * m}; 320 } 321 322 static ck_rect field_box(const ck_canvas_item *it) { return (ck_rect){it->x, it->y, it->w, it->rr}; } 323 324 unsigned ck_allen_canvas_count(const ck_allen_canvas *c) 325 { 326 unsigned n = 0; 327 for (unsigned i = 0; i < CK_CANVAS_SLOTS; i++) 328 n += c->item[i].live && !c->item[i].gone; 329 return n; 330 } 331 332 void ck_allen_canvas_clear(ck_allen_canvas *c, uint32_t t) 333 { 334 for (unsigned i = 0; i < CK_CANVAS_SLOTS; i++) 335 if (c->item[i].live && !c->item[i].gone) 336 c->item[i].gone = t ? t : 1; 337 } 338 339 /* where things go: two columns beside Allen and one place above him */ 340 enum { COL_LEFT, COL_RIGHT, COL_ABOVE }; 341 342 void ck_allen_canvas_show(ck_allen_canvas *c, const ck_scene *s, uint32_t t) 343 { 344 ck_allen_canvas_clear(c, t); 345 uint8_t from = c->tone_w >= 128 ? c->tone_to : c->tone_from; 346 c->tone_from = from, c->tone_to = s ? s->tone : 0, c->tone_t = t ? t : 1, c->tone_w = 0; 347 c->water = s ? s->water : 0; 348 if (!s) 349 return; 350 int32_t colw = S(440), abovew = S(600), top_min = c->cy - S(362), bot_max = c->cy + S(210); 351 int32_t col_h[3] = {0, 0, 0}; 352 unsigned col_n[3] = {0, 0, 0}, mine[CK_SCENE_THINGS] = {0}, col_of[CK_SCENE_THINGS] = {0}, n = 0; 353 for (unsigned i = 0; i < s->nthings && i < CK_SCENE_THINGS; i++) { 354 const ck_scene_thing *th = &s->thing[i]; 355 /* a slot: a free one, else the one furthest gone */ 356 unsigned slot = CK_CANVAS_SLOTS; 357 for (unsigned j = 0; j < CK_CANVAS_SLOTS && slot == CK_CANVAS_SLOTS; j++) 358 if (!c->item[j].live) 359 slot = j; 360 for (unsigned j = 0; j < CK_CANVAS_SLOTS && slot == CK_CANVAS_SLOTS; j++) { 361 int taken = 0; 362 for (unsigned q = 0; q < n; q++) 363 taken |= mine[q] == j; 364 if (!taken && (slot == CK_CANVAS_SLOTS || c->item[j].gone < c->item[slot].gone)) 365 slot = j; 366 } 367 if (slot == CK_CANVAS_SLOTS) 368 break; 369 ck_canvas_item *it = &c->item[slot]; 370 for (size_t b = 0; b < sizeof *it; b++) 371 ((char *)it)[b] = 0; 372 it->kind = th->kind, it->warm = th->warm, it->quiet = th->quiet, it->arrive = th->arrive, it->way = th->way; 373 for (unsigned f = 0; f < th->nf && f < CK_SCENE_FIELDS; f++) { 374 const char *v = ck_scene_field(s, th, f); 375 int at = put(it, v, len_of(v)); 376 if (at < 0) 377 break; 378 it->f[it->nf++] = (uint16_t)at; 379 } 380 if (!it->nf) 381 continue; 382 it->base = it->used; 383 /* its place: asked for if there is room, else wherever there is */ 384 int32_t hw = lay_out(c, it, colw), ha = 0; 385 int ok[3]; 386 ok[COL_LEFT] = col_n[COL_LEFT] < 2 && col_h[COL_LEFT] + (col_n[COL_LEFT] ? S(56) : 0) + hw <= bot_max - top_min; 387 ok[COL_RIGHT] = col_n[COL_RIGHT] < 2 && col_h[COL_RIGHT] + (col_n[COL_RIGHT] ? S(56) : 0) + hw <= bot_max - top_min; 388 ok[COL_ABOVE] = 0; 389 if (!col_n[COL_ABOVE] && (th->place == CK_PLACE_ABOVE || (!ok[COL_LEFT] && !ok[COL_RIGHT]))) { 390 ha = lay_out(c, it, abovew); 391 ok[COL_ABOVE] = ha <= S(206); 392 } 393 int col = -1; 394 if (th->place == CK_PLACE_ABOVE && ok[COL_ABOVE]) 395 col = COL_ABOVE; 396 else if (th->place == CK_PLACE_LEFT && ok[COL_LEFT]) 397 col = COL_LEFT; 398 else if (th->place == CK_PLACE_RIGHT && ok[COL_RIGHT]) 399 col = COL_RIGHT; 400 else if (ok[COL_RIGHT] && (!ok[COL_LEFT] || col_h[COL_RIGHT] <= col_h[COL_LEFT])) 401 col = COL_RIGHT; 402 else if (ok[COL_LEFT]) 403 col = COL_LEFT; 404 else if (ok[COL_ABOVE]) 405 col = COL_ABOVE; 406 if (col < 0) { 407 c->dropped++; 408 continue; 409 } 410 if (col != COL_ABOVE && ha) /* laid out for above, going to a column after all */ 411 lay_out(c, it, colw); 412 else if (col == COL_ABOVE && !ha) 413 ha = lay_out(c, it, abovew); 414 it->w = col == COL_ABOVE ? abovew : colw; 415 it->h = col == COL_ABOVE ? ha : hw; 416 it->y = col_h[col] + (col_n[col] ? S(56) : 0); /* the offset within its column, for now */ 417 col_h[col] = it->y + it->h; 418 col_n[col]++; 419 it->live = 1; 420 it->born = t + 120u + n * BETWEEN_MS; 421 mine[n] = slot, col_of[n] = (unsigned)col, n++; 422 c->shown++; 423 } 424 /* the columns stand centred on Allen's height; the place above, near the top of the sky */ 425 for (unsigned q = 0; q < n; q++) { 426 ck_canvas_item *it = &c->item[mine[q]]; 427 unsigned col = col_of[q]; 428 if (col == COL_ABOVE) { 429 it->x = c->cx - it->w / 2; 430 it->y = c->cy - S(366) + (S(206) - it->h) / 2; 431 } else { 432 int32_t top = ck_clamp(c->cy - col_h[col] / 2, top_min, bot_max - col_h[col]); 433 it->x = col == COL_LEFT ? c->cx - S(770) : c->cx + S(330); 434 it->y += top; 435 } 436 it->ax = (it->x + (it->rr && it->kind != CK_THING_PICTURE && it->kind != CK_THING_VIDEO ? it->rx : S(2))) * 16; 437 it->ay = (it->y + (it->rr ? (it->kind == CK_THING_PICTURE || it->kind == CK_THING_VIDEO ? it->rr / 2 : it->ry) 438 : it->h / 2)) * 16; 439 if (it->kind == CK_THING_PICTURE || it->kind == CK_THING_VIDEO) 440 it->ax = (it->x + it->w / 2) * 16; 441 it->box = (ck_rect){it->x - S(124), it->y - S(132), it->w + S(248), it->h + S(256)}; 442 } 443 } 444 445 void ck_allen_canvas_at(ck_allen_canvas *c, uint32_t t, int32_t hx, int32_t hy, ck_flow *flow, void (*mark)(ck_rect)) 446 { 447 c->t = t, c->hx = hx, c->hy = hy; 448 if (c->tone_t) { 449 c->tone_w = ck_smooth(ck_prog(t, c->tone_t, c->tone_t + GO_MS)); 450 if (t <= c->tone_t + GO_MS + 40 && c->tone_from != c->tone_to) 451 mark((ck_rect){0, 0, c->w, c->h}); 452 } 453 if (flow && c->water == CK_WATER_LIVELY && t - c->stir_t >= 1400u) { 454 /* lively water: a gentle current pushed round Allen now and then */ 455 c->stir_t = t; 456 uint32_t ang = ck_hash((int32_t)(t / 1400u), 7, 11) & 0xffffu; 457 int32_t r = S(330), px = c->cx + (int32_t)((int64_t)r * ck_cos(ang) >> 14), 458 py = c->cy + (int32_t)((int64_t)r * ck_sin(ang) >> 14); 459 int32_t vx = (int32_t)((int64_t)-90 * ck_sin(ang) >> 14), vy = (int32_t)((int64_t)90 * ck_cos(ang) >> 14); 460 ck_flow_touch(flow, px, py, vx, vy, 1500, S(70)); 461 } 462 for (unsigned i = 0; i < CK_CANVAS_SLOTS; i++) { 463 ck_canvas_item *it = &c->item[i]; 464 if (!it->live) 465 continue; 466 if (!it->gone && t > it->born + CK_CANVAS_HOLD_MS && !timer_runs(it, t)) 467 it->gone = t; 468 if (it->gone && t >= it->gone + GO_MS) { 469 mark(it->box); 470 it->live = 0; 471 continue; 472 } 473 if ((t + 40 >= it->born && t <= opens(it) + come_ms(it) + 40) || it->gone) 474 mark(it->box); /* its shade and shapes are still easing in, or it is leaving: all of it */ 475 else if (t + 40 >= it->born && t <= arrived_by(it)) 476 for (unsigned j = 1; j < it->nrows; j++) { /* the shade has settled: only the lines still coming */ 477 uint32_t o = opens(it) + j * ROW_MS; 478 if (t + 40 < o || t > o + come_ms(it) + 40) 479 continue; 480 const ck_canvas_row *rw = &it->row[j]; 481 int32_t cap = cap_of(face_of(c, rw->face)), up = cap + S(26) + lift(c, it, 0); 482 mark((ck_rect){it->x - S(6), it->y + rw->dy - up, it->w + S(12), up + cap / 2 + S(12)}); 483 } 484 if (it->arrive == CK_ARRIVE_BLOOM && t + 40 >= it->born && t <= it->born + SPARK_MS + 80) { 485 ck_rect sb = spark_box(c, it, t); /* only where the spark is and was: the sky between is left alone */ 486 mark(sb); 487 mark(it->spark_was); 488 it->spark_was = sb; 489 } 490 if (flow && it->arrive == CK_ARRIVE_BLOOM && !it->landed && t >= it->born + SPARK_MS) { 491 it->landed = 1; /* the spark lands on the water */ 492 ck_flow_touch(flow, it->ax / 16, it->ay / 16, 0, -50, 9000, S(36)); 493 } 494 uint32_t step = it->kind == CK_THING_TIMER ? 250u : it->rr && it->kind != CK_THING_PERSON ? 66u : 0u; 495 if (step) { 496 uint32_t tq = t - t % step; 497 if (tq != it->tq) { 498 it->tq = tq; 499 mark(it->kind == CK_THING_TIMER ? ring_box(c, it) : field_box(it)); 500 } 501 } 502 } 503 } 504 505 /* ---- drawing ---- */ 506 507 /* the sky's tone at a row: added colour and its weight, and how much of the sky is kept */ 508 static void tone_row(unsigned tone, int32_t y, int32_t h, uint32_t *rgb, uint32_t *a, uint32_t *keep) 509 { 510 *rgb = 0, *a = 0, *keep = 256; 511 if (tone == CK_TONE_DAWN) { /* a warm light low on the horizon */ 512 uint32_t s = ck_smooth(ck_prog((uint32_t)y, (uint32_t)(h * 40 / 100), (uint32_t)h)); 513 *rgb = C_WARM, *a = 40u * (s * s >> 8) >> 8; 514 } else if (tone == CK_TONE_CLEAR) { /* a cool lift high in the sky */ 515 *rgb = C_MIST, *a = 22u * (256u - ck_smooth(ck_prog((uint32_t)y, 0, (uint32_t)(h * 62 / 100)))) >> 8; 516 } else if (tone == CK_TONE_DEEP) { /* the night deeper and quieter */ 517 *keep = 200; 518 } 519 } 520 521 static void draw_tone(const ck_allen_canvas *c, ck_canvas *cv, const ck_rect *r) 522 { 523 if (c->tone_from == CK_TONE_NIGHT && c->tone_to == CK_TONE_NIGHT) 524 return; 525 uint32_t w = c->tone_t ? c->tone_w : 256; 526 for (int32_t y = r->y; y < r->y + r->h; y++) { 527 uint32_t c0, a0, k0, c1, a1, k1; 528 tone_row(c->tone_from, y, c->h, &c0, &a0, &k0); 529 tone_row(c->tone_to, y, c->h, &c1, &a1, &k1); 530 a0 = a0 * (256 - w) >> 8, a1 = a1 * w >> 8; 531 uint32_t keep = (k0 * (256 - w) + k1 * w) >> 8; 532 if (!a0 && !a1 && keep >= 256) 533 continue; 534 uint32_t *px = cv->px + (size_t)y * (size_t)cv->w; 535 for (int32_t x = r->x; x < r->x + r->w; x++) { 536 uint32_t v = keep < 256 ? ck_scale(px[x], keep) : px[x]; 537 if (a0) 538 v = ck_add(v, c0, a0); 539 if (a1) 540 v = ck_add(v, c1, a1); 541 px[x] = v; 542 } 543 } 544 } 545 546 /* The shade words rest in: an oval over the rectangle a, full across most of it and easing to nothing at its 547 * rim, so it never shows a straight edge even over the bright band of the sky. */ 548 static void shade(ck_canvas *cv, const ck_rect *r, ck_rect a, uint32_t amt) 549 { 550 ck_rect d = a, all = {0, 0, cv->w, cv->h}; 551 if (!amt || a.w < 4 || a.h < 4 || !ck_clip(&d, r) || !ck_clip(&d, &all)) 552 return; 553 int64_t hx = a.w / 2, hy = a.h / 2, cx = a.x + hx, cy = a.y + hy; 554 int64_t ix = ((int64_t)1 << 32) / (hx * hx), iy = ((int64_t)1 << 32) / (hy * hy); 555 const int64_t inner = 30000, inv = (256 << 16) / (65536 - inner); /* the rim eases over the last part */ 556 for (int32_t y = d.y; y < d.y + d.h; y++) { 557 int64_t dy = y - cy, qy = dy * dy * iy >> 16; 558 if (qy >= 65536) 559 continue; 560 /* only the span of this row inside the oval */ 561 int64_t lim = (int64_t)ck_isqrt((uint64_t)(((65536 - qy) << 16) / ix)) + 1; 562 int32_t x0 = (int32_t)ck_max(d.x, (int32_t)(cx - lim)), x1 = (int32_t)ck_min(d.x + d.w, (int32_t)(cx + lim + 1)); 563 uint32_t *px = cv->px + (size_t)y * (size_t)cv->w; 564 for (int32_t x = x0; x < x1; x++) { 565 int64_t dx = x - cx, q = qy + (dx * dx * ix >> 16); /* 65536 at the rim */ 566 if (q >= 65536) 567 continue; 568 if (q <= inner) { 569 px[x] = ck_mix(px[x], C_NIGHT, amt); 570 continue; 571 } 572 uint32_t k = (256 - ck_smooth((int32_t)((q - inner) * inv >> 16))) * amt >> 8; 573 if (k) 574 px[x] = ck_mix(px[x], C_NIGHT, k); 575 } 576 } 577 } 578 579 /* A soft oval of light filling the rectangle a (no edge: it fades to nothing at the rim, (1 - d^2)^2). */ 580 static void soft_oval(ck_canvas *cv, const ck_rect *r, ck_rect a, uint32_t color, uint32_t amt) 581 { 582 ck_rect d = a, all = {0, 0, cv->w, cv->h}; 583 if (!amt || a.w < 4 || a.h < 4 || !ck_clip(&d, r) || !ck_clip(&d, &all)) 584 return; 585 int64_t hx = a.w / 2, hy = a.h / 2, cx = a.x + hx, cy = a.y + hy; 586 int64_t ix = ((int64_t)1 << 32) / (hx * hx), iy = ((int64_t)1 << 32) / (hy * hy); /* 1/half^2, 2^32 */ 587 for (int32_t y = d.y; y < d.y + d.h; y++) { 588 int64_t dy = y - cy, qy = dy * dy * iy >> 16; 589 if (qy >= 65536) 590 continue; 591 uint32_t *px = cv->px + (size_t)y * (size_t)cv->w; 592 for (int32_t x = d.x; x < d.x + d.w; x++) { 593 int64_t dx = x - cx, q = qy + (dx * dx * ix >> 16); 594 if (q >= 65536) 595 continue; 596 uint32_t f = (uint32_t)(65536 - q) >> 8; /* 0..256 */ 597 uint32_t k = (f * f >> 8) * amt >> 8; 598 if (k) 599 px[x] = ck_add(px[x], color, k); 600 } 601 } 602 } 603 604 /* A ring of light: centre (cx, cy) px, radius rad px, half its width hw (16ths of a px); the part from the 605 * top clockwise to `upto` (65536ths of a turn) at a_on, the rest at a_off. */ 606 static void ring(ck_canvas *cv, const ck_rect *r, int32_t cx, int32_t cy, int32_t rad, int32_t hw, uint32_t color, 607 uint32_t on_color, uint32_t a_on, uint32_t a_off, uint32_t upto) 608 { 609 ck_rect d = {cx - rad - 2, cy - rad - 2, 2 * rad + 5, 2 * rad + 5}, all = {0, 0, cv->w, cv->h}; 610 if (!ck_clip(&d, r) || !ck_clip(&d, &all)) 611 return; 612 int64_t R = (int64_t)rad * 16, lo = R - hw - 16, hi = R + hw + 16; 613 lo = lo < 0 ? 0 : lo; 614 for (int32_t y = d.y; y < d.y + d.h; y++) { 615 int64_t dy = (int64_t)(y - cy) * 16; 616 uint32_t *px = cv->px + (size_t)y * (size_t)cv->w; 617 for (int32_t x = d.x; x < d.x + d.w; x++) { 618 int64_t dx = (int64_t)(x - cx) * 16, d2 = dx * dx + dy * dy; 619 if (d2 < lo * lo || d2 > hi * hi) 620 continue; 621 int64_t dist = (int64_t)ck_isqrt((uint64_t)d2), e = dist > R ? dist - R : R - dist; 622 int64_t cov = hw + 8 - e; 623 if (cov <= 0) 624 continue; 625 uint32_t cv16 = cov >= 16 ? 256u : (uint32_t)cov * 16u; 626 int lit = upto >= 65536u || ck_atan2((int32_t)dx, (int32_t)-dy) < upto; 627 uint32_t a = (lit ? a_on : a_off) * cv16 >> 8; 628 if (a) 629 px[x] = ck_add(px[x], lit ? on_color : color, a); 630 } 631 } 632 } 633 634 static unsigned put_2(char *s, uint32_t v) 635 { 636 s[0] = (char)('0' + v / 10 % 10), s[1] = (char)('0' + v % 10); 637 return 2; 638 } 639 640 /* a running timer's time left at tq, as "m:ss" or "h:mm:ss" */ 641 static unsigned clock_text(const ck_canvas_item *it, char *s, uint32_t *left) 642 { 643 uint32_t start = opens(it), now = it->tq > start ? it->tq - start : 0; 644 *left = now >= it->dur ? 0 : it->dur - now; 645 uint32_t sec = (*left + 999) / 1000, n = 0; 646 if (!sec) { 647 s[0] = 'n', s[1] = 'o', s[2] = 'w'; 648 return 3; 649 } 650 if (sec >= 3600) { 651 uint32_t hh = sec / 3600; 652 if (hh >= 10) 653 s[n++] = (char)('0' + hh / 10); 654 s[n++] = (char)('0' + hh % 10); 655 s[n++] = ':'; 656 n += put_2(s + n, sec / 60 % 60); 657 } else { 658 uint32_t mm = sec / 60; 659 if (mm >= 10) 660 s[n++] = (char)('0' + mm / 10); 661 s[n++] = (char)('0' + mm % 10); 662 } 663 s[n++] = ':'; 664 n += put_2(s + n, sec % 60); 665 return n; 666 } 667 668 static void text(const ck_allen_canvas *c, ck_canvas *cv, const ck_rect *r, unsigned face, int32_t x, int32_t y, 669 const char *s, unsigned n, uint32_t color, uint32_t a) 670 { 671 ck_af_face *f = face_of(c, face); 672 uint64_t pen = (uint64_t)(x < 0 ? 0 : x) * 256; 673 for (unsigned i = 0; i < n; i++) { 674 if (face == 3 && !is_big(s[i])) { /* never a glyph the large face has not made */ 675 pen += ck_af_advance(f, s[i]); 676 continue; 677 } 678 pen = ck_letter(cv, r, f, pen, y, s[i], color, a, 0); 679 } 680 } 681 682 static void draw_item(const ck_allen_canvas *c, const ck_canvas_item *it, ck_canvas *cv, const ck_rect *r, uint32_t t) 683 { 684 if (!ck_meets(it->box, r) || t < opens(it)) 685 return; 686 uint32_t keep = stays(it, t), e0 = come(it, 0, t), D = e0 * keep >> 8; 687 if (!D) 688 return; 689 int32_t x = it->x, y = it->y - lift(c, it, e0); 690 uint32_t edge = it->warm ? C_WARM2 : C_GLOW; 691 int pict = it->kind == CK_THING_PICTURE || it->kind == CK_THING_VIDEO; 692 /* the shade its words rest in: no edge, so the sky is never cut by a frame */ 693 shade(cv, r, (ck_rect){x - S(110), y - S(80), it->w + S(220), it->h + S(160)}, 118 * D >> 8); 694 switch (it->kind) { 695 case CK_THING_NOTE: 696 case CK_THING_LIST: 697 case CK_THING_QUOTE: { /* a thread of light down its side, brightest a third of the way down */ 698 int32_t tx = (x + S(2)) * 16 + 8, y0 = (y - S(6)) * 16, y1 = (y + it->h / 3) * 16, y2 = (y + it->h + S(6)) * 16; 699 uint32_t a = 190 * D >> 8; 700 if (it->kind == CK_THING_QUOTE) { /* a quotation: warm, and a short stroke before who said it */ 701 edge = C_WARM2; 702 if (it->nrows > 1) { 703 const ck_canvas_row *w = &it->row[it->nrows - 1]; 704 int32_t wy = (y + w->dy - cap_of(c->body) / 3) * 16; 705 ck_thread(cv, r, (x + S(26)) * 16, wy, (x + S(48)) * 16, wy, (int32_t)c->k / 16, 706 C_HINT, 150 * D >> 8, 150 * D >> 8); 707 } 708 } 709 ck_thread(cv, r, tx, y0, tx, y1, (int32_t)c->k / 16, edge, 0, a); 710 ck_thread(cv, r, tx, y1, tx, y2, (int32_t)c->k / 16, edge, a, 0); 711 ck_glow(cv, r, tx, y1, S(5) * 16, edge, 120 * D >> 8); 712 break; 713 } 714 case CK_THING_NUMBER: 715 ck_glow(cv, r, (x + it->rx) * 16, (y + it->ry) * 16, S(150) * 16, it->warm ? C_WARM : C_GLOW, 30 * D >> 8); 716 break; 717 case CK_THING_PERSON: { 718 int32_t px = x + it->rx, py = y + it->ry; 719 ck_glow(cv, r, px * 16, py * 16, it->rr * 16 * 3 / 2, it->way == CK_WAY_FROM ? C_WARM : C_GLOW, 34 * D >> 8); 720 ring(cv, r, px, py, it->rr, S(1) * 16 / 2 + 6, C_GLOW, it->warm ? C_WARM2 : C_MIST, 170 * D >> 8, 0, 65536u); 721 break; 722 } 723 case CK_THING_TIMER: { 724 int32_t px = x + it->rx, py = y + it->ry; 725 uint32_t left = 0, upto = 65536u; 726 char s[16]; 727 unsigned n = 0; 728 if (it->dur) { 729 n = clock_text(it, s, &left); 730 upto = (uint32_t)((uint64_t)left * 65536u / it->dur); 731 } 732 ck_glow(cv, r, px * 16, py * 16, it->rr * 16 * 5 / 4, C_INK, 46 * D >> 8); 733 ring(cv, r, px, py, it->rr, S(1) * 16 / 2 + 6, C_GLOW, it->warm ? C_WARM2 : C_MIST, 210 * D >> 8, 734 (it->dur ? 52u : 120u) * D >> 8, upto); 735 if (it->dur && left) { /* the moving end of the light */ 736 int32_t hx = px * 16 + (int32_t)((int64_t)it->rr * 16 * ck_sin(upto) >> 14); 737 int32_t hy = py * 16 - (int32_t)((int64_t)it->rr * 16 * ck_cos(upto) >> 14); 738 ck_glow(cv, r, hx, hy, S(7) * 16, C_WARM, 230 * D >> 8); 739 } 740 if (n) { 741 int32_t ww = (int32_t)ck_af_measure(c->voice, s, n); 742 text(c, cv, r, 0, px - ww / 2, py + cap_of(c->voice) / 2, s, n, left ? C_TEXT : C_WARM2, 235 * D >> 8); 743 } 744 break; 745 } 746 default: 747 break; 748 } 749 if (pict) { /* the place where the picture will be: light gathering, no frame */ 750 ck_rect fbox = {x, y, it->w, it->rr}; 751 /* a soft field of deep blue light where it will be, its edges dissolving into the sky, and one 752 * brighter light drifting slowly across it, as if the picture were gathering */ 753 soft_oval(cv, r, fbox, C_INK, 92 * D >> 8); 754 int32_t mx = x + it->w / 2, my = y + it->rr / 2; 755 uint32_t ang = (uint32_t)((uint64_t)it->tq * 65536u / 19000u); 756 int32_t gx = mx + (int32_t)((int64_t)(it->w / 5) * ck_sin(ang) >> 14); 757 ck_glow(cv, r, gx * 16, my * 16, it->rr * 16 * 11 / 20, C_GLOW, 30 * D >> 8); 758 if (it->kind == CK_THING_VIDEO) { /* a still play mark, drawn in light */ 759 int32_t s3 = S(15); 760 ring(cv, r, mx, my, S(40), S(1) * 16 / 2 + 6, C_GLOW, C_MIST, 150 * D >> 8, 0, 65536u); 761 int32_t ax = (mx - s3 * 2 / 3) * 16, bx = (mx + s3) * 16, ty = (my - s3) * 16, by = (my + s3) * 16; 762 uint32_t a = 210 * D >> 8; 763 ck_thread(cv, r, ax, ty, ax, by, (int32_t)c->k / 16, C_MIST, a, a); 764 ck_thread(cv, r, ax, ty, bx, my * 16, (int32_t)c->k / 16, C_MIST, a, a); 765 ck_thread(cv, r, ax, by, bx, my * 16, (int32_t)c->k / 16, C_MIST, a, a); 766 } 767 static const char wait[] = "not here yet"; 768 int32_t ww = (int32_t)ck_af_measure(c->small, wait, sizeof wait - 1); 769 text(c, cv, r, 2, mx - ww / 2, y + it->rr - S(22), wait, sizeof wait - 1, C_HINT, 120 * D >> 8); 770 } 771 /* the lines of type, one after another */ 772 for (unsigned j = 0; j < it->nrows; j++) { 773 const ck_canvas_row *rw = &it->row[j]; 774 uint32_t e = come(it, j, t), a = e * keep >> 8; 775 if (!a) 776 continue; 777 int32_t ry = it->y - lift(c, it, e) + rw->dy, rx = x + rw->dx; 778 if (rw->dot) 779 ck_glow(cv, r, (x + S(26) + S(7)) * 16, (ry - cap_of(face_of(c, rw->face)) / 2) * 16, S(4) * 16, 780 it->warm ? C_WARM : C_GLOW, 210 * a >> 8); 781 uint32_t aa = rw->face == 3 && it->kind == CK_THING_QUOTE ? a * 120 >> 8 : a; 782 text(c, cv, r, rw->face, rx, ry, it->text + rw->off, rw->len, role_rgb(rw->role), aa); 783 } 784 } 785 786 static void draw_spark(const ck_allen_canvas *c, const ck_canvas_item *it, ck_canvas *cv, const ck_rect *r, uint32_t t) 787 { 788 if (it->arrive != CK_ARRIVE_BLOOM || t < it->born || t >= it->born + SPARK_MS + 40) 789 return; 790 int32_t p = ck_prog(t, it->born, it->born + SPARK_MS); 791 /* a warm point with a soft halo and a fading trail, on a gentle arc up and over to its place */ 792 uint32_t fade = 256 - ck_smooth(ck_prog(t, it->born + SPARK_MS - 120, it->born + SPARK_MS + 40)); 793 for (int k = 0; k < 6; k++) { 794 int32_t pk = p - k * 14, sx, sy; 795 if (pk < 0) 796 break; 797 spark_at(c, it, pk, &sx, &sy); 798 uint32_t a = (256u - (uint32_t)k * 40u) * fade >> 8; 799 if (!k) 800 ck_glow(cv, r, sx, sy, S(34) * 16, C_GLOW, 70 * a >> 8); 801 ck_glow(cv, r, sx, sy, S(9 - k) * 16, C_WARM2, 240 * a >> 8); 802 } 803 } 804 805 void ck_allen_canvas_draw(const ck_allen_canvas *c, ck_canvas *cv, const ck_rect *r) 806 { 807 draw_tone(c, cv, r); 808 for (unsigned i = 0; i < CK_CANVAS_SLOTS; i++) 809 if (c->item[i].live) 810 draw_item(c, &c->item[i], cv, r, c->t); 811 for (unsigned i = 0; i < CK_CANVAS_SLOTS; i++) 812 if (c->item[i].live) 813 draw_spark(c, &c->item[i], cv, r, c->t); 814 }