interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / ui / night.h

160 lines · 9143 bytes Raw Blame History
1/* night.h -- the night sky AIENOS opens on, and the two black holes that
2 * merge in it.
3 *
4 * The sky is made once, into RAM, and it is the real sky (ui/sky_real.h):
5 * by default the piece of it GW150914, the first black-hole merger ever
6 * heard, came from; catalogue stars at their true places, brightness and colour
7 * on a black background. Diffuse Milky Way light is omitted from this UI. Each galaxy also keeps, per pixel, how bright it is
8 * and where along its long axis that pixel sits, so a band of light can
9 * later sweep along it (while Allen answers) without the galaxy itself
10 * changing.
11 *
12 * The startup, on the clock from ck_night_init (ms):
13 * 0 to CK_NIGHT_FADE_END_MS the eyes adjust to the dark: the brightest
14 * stars first, then the fainter ones, in slow patches across the sky.
15 * CK_NIGHT_HOLES_MS two black holes start to bend the starlight
16 * (gravitational lensing: stars behind them are
17 * pulled into arcs), then show their shadows,
18 * circling on a tilted orbit;
19 * CK_NIGHT_SPIRAL_MS they spiral in, faster and closer, the way a
20 * real pair does (separation as t^(1/4), angle
21 * as t^(5/8), t the time left);
22 * CK_NIGHT_MERGE_MS they merge; a ripple (the gravitational wave,
23 * stretching one way and squeezing the other)
24 * runs out through the stars; the one black
25 * hole left rings, then dissolves by
26 * CK_NIGHT_GONE_MS.
27 *
28 * The dusk (ck_night_fall): when Allen goes to sleep, one black hole opens
29 * where the pair merged and everything on the screen falls into it. The
30 * caller keeps the picture as it was (ck_night_picture_take) and the night
31 * draws it pulled in: a pixel at distance r from the hole shows what was at
32 * sqrt(r^2 + fallen), turned round the hole a set part of a turn for every
33 * doubling of that distance; so the picture streams inward, fastest near the
34 * hole, whipping round as it goes, and the light that crowds in there is
35 * drawn brighter and warmer. Round the shadow an accretion disk glows, warm
36 * white at its inner edge through gold and blue to the dark, brighter on the
37 * side turning toward the eye, with a thin bright photon ring just outside
38 * the shadow; then the whole picture is dimmed as the horizon closes.
39 *
40 * Every pixel is a pure function of its position and the time, so a frame
41 * redraws only the regions that changed, in any order. Integer only,
42 * freestanding, no heap. Host tested in ui/tests/allen_ui_test.c. */
43#ifndef AIENOS_NIGHT_H
44#define AIENOS_NIGHT_H
45#include <stddef.h>
46#include <stdint.h>
47
48#include "paint.h"
49#include "sky_data.h"
50
51#define CK_NIGHT_FADE_END_MS 6800u
52#define CK_NIGHT_HOLES_MS 2600u
53#define CK_NIGHT_SPIRAL_MS 6000u
54#define CK_NIGHT_MERGE_MS 12000u
55#define CK_NIGHT_RIPPLE_MS 3600u /* the ripple's run across the screen */
56#define CK_NIGHT_GONE_MS 15200u /* the last black hole has dissolved */
57
58#define CK_NIGHT_GALAXIES 6u
59#define CK_NIGHT_TWINKLES 256u
60#define CK_NIGHT_LEVELS 6u /* a kept picture and its copies at 1/2, 1/4, down to 1/32 size */
61#define CK_NIGHT_W_MAX 8192u /* the widest screen the dusk can draw (one row of 8-pixel blocks at a time) */
62
63/* How the dusk stands at a frame; see ck_night_fall. All distances in 16ths of a pixel. */
64typedef struct {
65 int64_t fallen; /* a pixel at distance r from the hole shows what was at sqrt(r^2 + fallen) */
66 uint32_t swirl; /* 65536ths of a turn the picture is turned for each doubling of that distance, close to the hole */
67 int32_t calm; /* the turning is weaker further out: by calm / (calm + r) */
68 uint32_t crowd; /* the most the crowded light near the hole is brightened: 256 not at all, 1024 four times */
69 int32_t e, rs; /* the hole: Einstein radius and shadow radius */
70 uint32_t disk; /* how bright the accretion disk and the photon ring are, 0..256 */
71 uint32_t beam; /* which way the disk's bright side faces (the side turning toward the eye), 65536ths of a turn */
72 uint32_t keep; /* how much of the picture's light is kept, 0..256; the horizon closing takes it to 0 */
73 int32_t near, far; /* the edges of the screen go dark first: the light falls off from near out to far ... */
74 uint32_t shade; /* ... by this much at far, 0..256 */
75} ck_night_dusk;
76
77typedef struct {
78 ck_rect box;
79 uint8_t *light, *phase; /* box.w * box.h each */
80 uint32_t tint; /* the colour of the light that sweeps it */
81 uint16_t period; /* ms for the sweep to come round */
82} ck_night_galaxy;
83
84typedef struct {
85 int32_t x, y; /* 16ths of a native pixel */
86 uint32_t peak; /* catalogue flux, linear Q16 */
87 uint16_t col[3]; /* linear colour, brightest channel 4096 */
88 uint16_t period, phase; /* ms */
89} ck_night_star;
90
91typedef struct {
92 int32_t w, h;
93 uint32_t k; /* 1/256 px per design px (1920 x 1080 design) */
94 int32_t cx, cy; /* where the black holes merge, px */
95 uint32_t *sky; /* the night, w * h */
96 uint8_t *appear; /* when each 4 x 4 cell comes out of the dark (0..255) */
97 int32_t aw;
98 ck_night_galaxy gal[CK_NIGHT_GALAXIES];
99 ck_night_star tw[CK_NIGHT_TWINKLES];
100 unsigned ntw;
101 /* state at the time ck_night_at was last given */
102 uint32_t t;
103 unsigned holes;
104 struct {
105 int32_t x, y, e, rs; /* 16ths of a pixel: centre, Einstein radius, shadow radius */
106 } hole[2];
107 ck_rect lens; /* where the light is bent (empty: nowhere) */
108 int32_t ripple_r, ripple_w; /* px; ripple_w 0: no ripple */
109 int32_t ripple_disp[1024]; /* 16ths of a pixel, by distance from the crest */
110 uint8_t ripple_light[1024];
111 int32_t ripple_c2, ripple_s2; /* the stretch direction, Q14 */
112 /* the dusk (ck_night_fall); falling 0: none */
113 int falling;
114 ck_night_dusk dusk;
115 uint8_t disk_a[1024]; /* the disk's light by distance from the hole, 0..255 */
116 uint32_t disk_c[1024]; /* and its colour there */
117 int32_t disk_far; /* the disk reaches this far, 16ths of a pixel */
118 ck_sky_view view; /* the current sky view (set by ck_night_init, updated by ck_night_view) */
119} ck_night;
120
121/* The picture kept for the dusk: the screen as it was, with smaller copies of it and of the bare sky.
122 * Beyond the picture's edges the dusk shows the sky mirrored, so the stars seem to come from past the
123 * screen and no edge is seen falling in. */
124typedef struct {
125 const uint32_t *level[CK_NIGHT_LEVELS]; /* the picture, then it at 1/2, 1/4 ... the width and height */
126 const uint32_t *sky[CK_NIGHT_LEVELS]; /* the bare sky the same way (sky[0] is the night's own) */
127} ck_night_picture;
128/* Memory for a w x h picture kept this way. */
129size_t ck_night_picture_bytes(uint32_t w, uint32_t h);
130/* Copies the w x h picture `from` (rows w apart) into mem (ck_night_picture_bytes) and makes the smaller
131 * copies of it and of n's sky, each a quarter of the one before by averaging 2 x 2 pixels' linear light. */
132void ck_night_picture_take(const ck_night *n, const uint32_t *from, void *mem, ck_night_picture *kept);
133
134/* Memory for a w x h night at scale k. */
135size_t ck_night_mem_bytes(uint32_t w, uint32_t h, uint32_t k);
136/* Makes the sky at native resolution into mem. (cx, cy): where the holes
137 * merge. No screen-grid scratch buffer is needed. Returns 0, or -1. */
138int ck_night_init(ck_night *n, uint32_t w, uint32_t h, uint32_t k, int32_t cx, int32_t cy, void *mem, size_t len);
139/* Rebuilds the native sky for a new view (changing sky mode or location). */
140void ck_night_view(ck_night *n, const ck_sky_view *v);
141/* Moves the night to t ms: the holes, the lens region, the ripple. Ends any dusk. */
142void ck_night_at(ck_night *n, uint32_t t);
143/* The dusk, after ck_night_at: one black hole at (cx, cy) with everything falling into it as d says.
144 * The picture is then drawn with ck_night_base_from. */
145void ck_night_fall(ck_night *n, const ck_night_dusk *d);
146/* The sky as seen at the current time (faded in, bent, rippled) into the
147 * rectangle r of out. */
148void ck_night_base(const ck_night *n, ck_canvas *out, const ck_rect *r);
149/* The kept picture (ck_night_picture_take) as the dusk shows it now: pulled into the hole, the shadow,
150 * the ring and the disk on it, the whole dimmed by keep; into the rectangle r of out. */
151void ck_night_base_from(const ck_night *n, const ck_night_picture *kept, ck_canvas *out, const ck_rect *r);
152/* Adds the twinkling and, with shimmer > 0 (0..256), the light sweeping the
153 * galaxies; voice (0..256) brightens their cores. */
154void ck_night_lights(const ck_night *n, ck_canvas *out, const ck_rect *r, uint32_t shimmer, uint32_t voice);
155/* The rectangle a twinkling star needs redrawn. */
156ck_rect ck_night_star_box(const ck_night *n, unsigned i);
157/* How far the sky has come out of the dark at (x, y) for light of level l
158 * (0..255): 0..256. */
159uint32_t ck_night_faded(const ck_night *n, int32_t x, int32_t y, uint32_t l);
160#endif