aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / allen-scene / native / flow.h
| 1 | /* flow.h -- Allen's ink: a fluid that fills the screen around him. |
| 2 | * |
| 3 | * The whole screen is a shallow tank of water with a little luminous ink in |
| 4 | * it. What Allen does moves the water, and the ink shows the movement: |
| 5 | * idle a slow warm current turns round him, the ink drifts and thins. |
| 6 | * listening every key typed drops ink at the bar and lifts it toward him; |
| 7 | * the line you type flows up into him before he answers. |
| 8 | * thinking the water round him turns into a vortex, ink streams out of |
| 9 | * his heart along it; the longer he thinks, the deeper it turns. |
| 10 | * speaking each word pushes a ring of water out from the heart. |
| 11 | * asleep nothing pushes; the water stills and the ink fades. |
| 12 | * No state is ever cut to: forces ease in and out, and the water carries |
| 13 | * everything that was moving on until it settles. |
| 14 | * |
| 15 | * The solver is Stam's stable fluid (Jos Stam, "Real-Time Fluid Dynamics for |
| 16 | * Games", GDC 2003; "Stable Fluids", SIGGRAPH 1999): add sources, diffuse, |
| 17 | * move along the flow (semi-Lagrangian), make the velocity divergence free |
| 18 | * (a Gauss-Seidel pressure solve). It never blows up whatever the time step, |
| 19 | * which is what a screen that must never stutter needs. Everything is |
| 20 | * integer: cells, velocities and ink in 16.16 fixed point, on a coarse grid |
| 21 | * (CK_FLOW_W x CK_FLOW_H cells over the screen), drawn with bilinear |
| 22 | * interpolation so the ink has no edges. Freestanding, no allocation (the |
| 23 | * fields live in the struct), host tested in ui/tests/allen_ui_test.c. |
| 24 | * ck_flow_at moves it on (one caller); ck_flow_draw only reads, so several |
| 25 | * cores can draw different parts of the screen at once. */ |
| 26 | #ifndef AIENOS_FLOW_H |
| 27 | #define AIENOS_FLOW_H |
| 28 | #include <stddef.h> |
| 29 | #include <stdint.h> |
| 30 | #include "paint.h" |
| 31 | |
| 32 | #define CK_FLOW_W 64u |
| 33 | #define CK_FLOW_H 36u |
| 34 | #define CK_FLOW_CELLS ((CK_FLOW_W + 2u) * (CK_FLOW_H + 2u)) /* with the border cells */ |
| 35 | |
| 36 | enum { CK_FLOW_IDLE, CK_FLOW_LISTEN, CK_FLOW_THINK, CK_FLOW_SPEAK, CK_FLOW_SLEEP }; |
| 37 | |
| 38 | typedef struct { |
| 39 | int32_t w, h; /* the screen, px */ |
| 40 | int32_t cw, ch; /* one cell, 1/256 px (the grid covers the screen exactly) */ |
| 41 | int32_t cx, cy, R; /* Allen's centre and radius, px */ |
| 42 | int32_t bx, by; /* where typed lines start from (the bar), px */ |
| 43 | uint32_t t, stepped; /* now, and the time of the last solver step (ms) */ |
| 44 | int mode; |
| 45 | uint32_t weight[5]; /* how far each mode's pull has eased in, 0..256; they add up */ |
| 46 | uint32_t think_t, word_t, key_t; /* when thinking began (0: not), the last word, the last key */ |
| 47 | uint32_t rng; |
| 48 | /* the fields, 16.16: velocity in cells per second, ink 0..1 */ |
| 49 | int32_t u[CK_FLOW_CELLS], v[CK_FLOW_CELLS], d[CK_FLOW_CELLS]; |
| 50 | int32_t u0[CK_FLOW_CELLS], v0[CK_FLOW_CELLS], d0[CK_FLOW_CELLS]; |
| 51 | int32_t shown[CK_FLOW_CELLS]; /* the ink each cell showed at the last mark */ |
| 52 | uint16_t warm[CK_FLOW_CELLS]; /* how fast the water moves in each cell, 0..256, for the colour */ |
| 53 | uint16_t shown_warm[CK_FLOW_CELLS]; |
| 54 | uint32_t steps; |
| 55 | } ck_flow; |
| 56 | |
| 57 | /* The screen w x h, Allen at (cx, cy) with radius R, the bar's mouth at (bx, by). */ |
| 58 | void ck_flow_init(ck_flow *f, int32_t w, int32_t h, int32_t cx, int32_t cy, int32_t R, int32_t bx, int32_t by, |
| 59 | uint32_t t); |
| 60 | void ck_flow_mode(ck_flow *f, int mode, uint32_t t); |
| 61 | void ck_flow_key(ck_flow *f, uint32_t t); /* a key was typed: a drop at the bar, lifted toward Allen */ |
| 62 | void ck_flow_word(ck_flow *f, uint32_t t); /* a word was spoken: a ring from the heart */ |
| 63 | /* A push at (px, py): velocity (vx, vy) in px per second and ink (0..65536) over radius r px. */ |
| 64 | void ck_flow_touch(ck_flow *f, int32_t px, int32_t py, int32_t vx, int32_t vy, int32_t ink, int32_t r); |
| 65 | /* Moves the water to time t (ms); the solver steps at most every CK_FLOW_STEP_MS). */ |
| 66 | #define CK_FLOW_STEP_MS 16u |
| 67 | void ck_flow_at(ck_flow *f, uint32_t t); |
| 68 | /* The water's velocity at a point, px per second (for things that float on it). */ |
| 69 | void ck_flow_velocity(const ck_flow *f, int32_t px, int32_t py, int32_t *vx, int32_t *vy); |
| 70 | /* Calls mark(rect) for every cell whose ink changed enough to redraw since the last call. */ |
| 71 | void ck_flow_mark(ck_flow *f, void (*mark)(ck_rect)); |
| 72 | /* The ink into the part r of out (light added). gain scales the brightness (256 = as is). */ |
| 73 | void ck_flow_draw(const ck_flow *f, ck_canvas *out, const ck_rect *r, uint32_t gain); |
| 74 | /* Total ink in the tank, 16.16 (for tests: it rises when touched and fades when left). */ |
| 75 | int64_t ck_flow_ink(const ck_flow *f); |
| 76 | #endif |