aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / allen-scene / adapter.c
| 1 | /* Temporary, explicitly authorized C-on-web boundary. Native implementation: |
| 2 | * aien-dev/aienux@da55660ea7337f2b6a42095fcd1fb6ddebe1cc7c/native/kernel/ui. |
| 3 | * Files in native/ are unchanged. Replace this adapter and those C units with |
| 4 | * seeded Omega -> irwasm only after native-frame/audio parity, not by renaming C. |
| 5 | * The web opening reuses native DUSK, then native dawn/formation; this ordering |
| 6 | * is a web interaction, not the original two-hole boot sequence (replay=2). |
| 7 | * No I/O, imports, heap allocator, threads, GPU interface or browser privileges. |
| 8 | */ |
| 9 | #include "native/night.h" |
| 10 | #include "native/mind.h" |
| 11 | #include "native/flow.h" |
| 12 | #include "native/chirp.h" |
| 13 | #include "native/sky_real.h" |
| 14 | |
| 15 | void *memset(void *p, int c, size_t n) { unsigned char *q=p; while(n--) *q++=(unsigned char)c; return p; } |
| 16 | void *memcpy(void *d, const void *s, size_t n) { unsigned char *q=d; const unsigned char *p=s; while(n--) *q++=*p++; return d; } |
| 17 | |
| 18 | static struct { |
| 19 | ck_night night; |
| 20 | ck_mind mind; |
| 21 | ck_flow flow; |
| 22 | ck_night_picture kept; |
| 23 | ck_canvas cv; |
| 24 | ck_af_face face; |
| 25 | uint32_t *rgba, *back; |
| 26 | void *scratch, *keep; |
| 27 | int32_t w,h,cx,cy,far16; |
| 28 | uint32_t k,t,kind,compact,word_t,key_t; |
| 29 | int initialized; |
| 30 | } ui; |
| 31 | static int16_t audio[2*48000]; |
| 32 | #define S(v) ((int32_t)(((int64_t)(v)*(int64_t)ui.k+128)/256)) |
| 33 | #define EXPORT __attribute__((visibility("default"))) |
| 34 | |
| 35 | /* Verbatim state law from allen_ui.c:horizon_at; only struct ownership changes. */ |
| 36 | static void horizon_at(uint32_t d) |
| 37 | { |
| 38 | ck_night_dusk k = {0}; |
| 39 | uint32_t q = ck_smooth(ck_prog(d, 0, 3400)); |
| 40 | q = (q * q >> 8) * q >> 8; |
| 41 | k.fallen = (int64_t)ui.far16 * ui.far16 * (int64_t)q >> 8; |
| 42 | k.swirl = 65536u * 6 / 10; |
| 43 | k.calm = S(120) * 16; |
| 44 | k.crowd = 256 + (768 * ck_smooth(ck_prog(d, 300, 2400)) >> 8); |
| 45 | k.e = (int32_t)((int64_t)S(90) * 16 * ck_smooth(ck_prog(d, 0, 2000)) >> 8); |
| 46 | int32_t rs0 = (int32_t)((int64_t)S(60) * 16 * ck_smooth(ck_prog(d, 0, 2000)) >> 8); |
| 47 | k.rs = rs0 + (int32_t)((int64_t)S(60) * 16 * ck_smooth(ck_prog(d, 3400, 4200)) >> 8); |
| 48 | uint32_t rise = ck_smooth(ck_prog(d, 900, 2800)), fall = 256 - ck_smooth(ck_prog(d, 3100, 3900)); |
| 49 | k.disk = rise < fall ? rise : fall; |
| 50 | k.beam = 16384u + d * 65536u / 9000u; |
| 51 | k.keep = 256 - ck_ease_out(ck_prog(d, 2800, 4000)); |
| 52 | k.near = S(260) * 16, k.far = ui.far16; |
| 53 | k.shade = 230 * ck_smooth(ck_prog(d, 400, 2600)) >> 8; |
| 54 | k.fine = S(300) * 16; |
| 55 | ck_night_fall(&ui.night, &k); |
| 56 | } |
| 57 | |
| 58 | /* Verbatim merger flash from allen_ui.c:flash/unflash. */ |
| 59 | static int flash(uint32_t t, int32_t *rad, uint32_t *a) |
| 60 | { |
| 61 | if (t < CK_NIGHT_MERGE_MS || t >= CK_NIGHT_MERGE_MS + 1600) return 0; |
| 62 | uint32_t tm = t - CK_NIGHT_MERGE_MS; |
| 63 | *rad = (S(40) + (int32_t)((int64_t)S(220) * ck_ease_out(ck_prog(tm, 0, 1200)) >> 8)) * 16; |
| 64 | uint32_t rise = ck_smooth(ck_prog(tm, 0, 120)); |
| 65 | *a = rise * (ck_exp_neg(tm > 120 ? (tm - 120) * 256 / 450 : 0) >> 8) * 230 >> 16; |
| 66 | return *a > 0; |
| 67 | } |
| 68 | static void unflash(const ck_rect *r, uint32_t t) |
| 69 | { |
| 70 | uint32_t tm=t-CK_NIGHT_MERGE_MS; |
| 71 | if(ui.night.holes!=1 || tm<100) return; |
| 72 | uint32_t keep=256-ck_smooth(ck_prog(tm,100,400)); |
| 73 | int32_t hx=ui.night.hole[0].x,hy=ui.night.hole[0].y,rs=ui.night.hole[0].rs; |
| 74 | ck_rect box={(hx-rs)>>4,(hy-rs)>>4,(2*rs>>4)+2,(2*rs>>4)+2}; |
| 75 | if(rs<=16 || !ck_clip(&box,r)) return; |
| 76 | for(int32_t y=box.y;y<box.y+box.h;y++) for(int32_t x=box.x;x<box.x+box.w;x++) { |
| 77 | int32_t dx=x*16+8-hx,dy=y*16+8-hy; |
| 78 | uint32_t d=ck_isqrt((uint32_t)(dx*dx+dy*dy)); |
| 79 | if(d>=(uint32_t)rs) continue; |
| 80 | uint32_t in=(uint32_t)rs-d>=16?256:((uint32_t)rs-d)*16; |
| 81 | uint32_t k=256-(in*(256-keep)>>8); |
| 82 | ui.back[(size_t)y*ui.w+x]=ck_scale(ui.back[(size_t)y*ui.w+x],k); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | EXPORT int scene_init(uint32_t w,uint32_t h,uint32_t compact) |
| 87 | { |
| 88 | if(w<64 || h<64 || w>1920 || h>1920 || (uint64_t)w*h>2073600) return 0; |
| 89 | memset(&ui,0,sizeof ui); |
| 90 | ui.w=w;ui.h=h;ui.compact=compact; |
| 91 | ui.k=ck_min(w*256/1920,h*256/1080); |
| 92 | /* Compact is a native design-scale crop, not a different particle scene. */ |
| 93 | if(compact) ui.k=ck_min(w,h)*256/400; |
| 94 | ui.cx=w/2;ui.cy=compact?h/2:h*40/100; |
| 95 | size_t pix=(size_t)w*h*4; |
| 96 | size_t night=ck_night_mem_bytes(w,h,ui.k), kept=ck_night_picture_bytes(w,h); |
| 97 | size_t needed=3*pix+night+kept+262144+64; |
| 98 | #ifdef __wasm__ |
| 99 | extern unsigned char __heap_base; |
| 100 | uintptr_t base=((uintptr_t)&__heap_base+15)&~(uintptr_t)15; |
| 101 | size_t end=base+needed, pages=(end+65535)/65536; |
| 102 | size_t have=__builtin_wasm_memory_size(0); |
| 103 | if(pages>have && __builtin_wasm_memory_grow(0,pages-have)==(size_t)-1) return 0; |
| 104 | unsigned char *p=(unsigned char*)base; |
| 105 | #else |
| 106 | static unsigned char arena[64*1024*1024]; |
| 107 | if(needed>sizeof arena) return 0; |
| 108 | unsigned char *p=arena; |
| 109 | #endif |
| 110 | ui.rgba=(uint32_t*)p;p+=pix; |
| 111 | ui.back=(uint32_t*)p;p+=pix; |
| 112 | ui.scratch=p;p+=pix; |
| 113 | ui.cv=(ck_canvas){ui.back,(int32_t)w,(int32_t)h}; |
| 114 | if(ck_night_init(&ui.night,w,h,ui.k,ui.cx,ui.cy,p,night,ui.scratch)) return 0; |
| 115 | p+=night;ui.keep=p;p+=kept; |
| 116 | ck_af_face_init(&ui.face,ck_max(8,S(18)),1250,p,262144); |
| 117 | for(int c=CK_AF_FIRST;c<=CK_AF_LAST;c++) (void)ck_af_glyph_get(&ui.face,(char)c); |
| 118 | for(int i=0;i<4;i++) { |
| 119 | int64_t dx=(i&1?ui.w:0)-ui.cx,dy=(i&2?ui.h:0)-ui.cy; |
| 120 | ui.far16=ck_max(ui.far16,(int32_t)ck_isqrt((uint64_t)(dx*dx+dy*dy))*16); |
| 121 | } |
| 122 | ck_night_picture_take(&ui.night,ui.night.sky,ui.keep,&ui.kept); |
| 123 | ck_mind_init(&ui.mind,ui.cx,ui.cy,ui.k,12300); |
| 124 | ck_flow_init(&ui.flow,w,h,ui.cx,ui.cy,ui.mind.R,ui.cx,ui.h-S(177),0); |
| 125 | ui.initialized=1; |
| 126 | return 1; |
| 127 | } |
| 128 | EXPORT uintptr_t scene_pixels(void) { return (uintptr_t)ui.rgba; } |
| 129 | /* Native dusk consumes the whole preceding picture, not only the sky. For a |
| 130 | * fresh web opening use the original unwoken UI at 20000 ms: no bar/words and |
| 131 | * sleeping fluid, but the fully formed mind and twinkling sky are present. |
| 132 | * oracle-ui.c independently exercises original allen_ui.c for this state. */ |
| 133 | static void opening_picture(void) |
| 134 | { |
| 135 | ck_rect all={0,0,ui.w,ui.h}; |
| 136 | ck_mind_init(&ui.mind,ui.cx,ui.cy,ui.k,12300); |
| 137 | for(uint32_t t=0;t<=20000;t+=16) ck_mind_at(&ui.mind,t); |
| 138 | ck_mind_at(&ui.mind,20000); |
| 139 | ck_night_at(&ui.night,20000); |
| 140 | ck_night_base(&ui.night,&ui.cv,&all); |
| 141 | ck_night_lights(&ui.night,&ui.cv,&all,0,0); |
| 142 | ck_mind_draw(&ui.mind,&ui.cv,&all); |
| 143 | ck_night_picture_take(&ui.night,ui.back,ui.keep,&ui.kept); |
| 144 | } |
| 145 | EXPORT void scene_enter(uint32_t kind) |
| 146 | { |
| 147 | if(!ui.initialized) return; |
| 148 | ui.kind=kind>2?0:kind;ui.t=0;ui.word_t=ui.key_t=0; |
| 149 | if(kind==1) opening_picture(); |
| 150 | ck_mind_init(&ui.mind,ui.cx,ui.cy,ui.k,kind==1?4200:12300); |
| 151 | ck_flow_init(&ui.flow,ui.w,ui.h,ui.cx,ui.cy,ui.mind.R,ui.cx,ui.h-S(177),0); |
| 152 | if(kind!=1) ck_night_picture_take(&ui.night,ui.night.sky,ui.keep,&ui.kept); |
| 153 | } |
| 154 | EXPORT void scene_key(uint32_t t) { if(ui.initialized) {ck_mind_key(&ui.mind,t);ck_flow_key(&ui.flow,t);ui.key_t=t;} } |
| 155 | EXPORT void scene_word(uint32_t t) { if(ui.initialized) {ck_mind_word(&ui.mind,t);ck_flow_word(&ui.flow,t);ui.word_t=t;} } |
| 156 | EXPORT void scene_step(uint32_t t,uint32_t mode,uint32_t voice) |
| 157 | { |
| 158 | if(!ui.initialized) return; |
| 159 | ui.t=t; |
| 160 | ck_mind_mode(&ui.mind,(int)(mode&3),t); |
| 161 | ck_mind_voice(&ui.mind,voice); |
| 162 | int formed=t>=ui.mind.born+CK_MIND_FORM_MS; |
| 163 | ck_flow_mode(&ui.flow,formed?(int)(mode&3):CK_FLOW_SLEEP,t); |
| 164 | if(!ui.compact) ck_flow_at(&ui.flow,t); |
| 165 | int32_t vx=0,vy=0; |
| 166 | if(!ui.compact) ck_flow_velocity(&ui.flow,ui.cx,ui.cy,&vx,&vy); |
| 167 | ui.mind.cx=ui.cx+ck_clamp(vx*S(10)/1000,-S(10),S(10)); |
| 168 | ui.mind.cy=ui.cy+ck_clamp(vy*S(10)/1000,-S(10),S(10)); |
| 169 | ck_mind_at(&ui.mind,t); |
| 170 | } |
| 171 | /* Native allen_ui.c per-word voice envelope; no browser floating approximation. */ |
| 172 | EXPORT uint32_t scene_voice(uint32_t t,uint32_t word) |
| 173 | { |
| 174 | return t>=word?ck_exp_neg((t-word)*256/200)>>8:0; |
| 175 | } |
| 176 | EXPORT void scene_draw(uint32_t shimmer,uint32_t voice) |
| 177 | { |
| 178 | if(!ui.initialized) return; |
| 179 | ck_rect all={0,0,ui.w,ui.h}; |
| 180 | uint32_t t=ui.t; |
| 181 | ck_night_at(&ui.night,ui.kind==2?t:20000); |
| 182 | if(ui.kind==1 && t<4200) { |
| 183 | horizon_at(t); |
| 184 | ck_night_base_from(&ui.night,&ui.kept,&ui.cv,&all); |
| 185 | } else { |
| 186 | ck_night_base(&ui.night,&ui.cv,&all); |
| 187 | ck_night_lights(&ui.night,&ui.cv,&all,shimmer,voice*shimmer>>9); |
| 188 | if(!ui.compact) ck_flow_draw(&ui.flow,&ui.cv,&all,256); |
| 189 | int32_t rad;uint32_t a; |
| 190 | if(ui.kind==2 && flash(t,&rad,&a)) { |
| 191 | ck_glow(&ui.cv,&all,ui.cx*16+8,ui.cy*16+8,rad,CK_RGB(200,210,255),a/3); |
| 192 | ck_glow(&ui.cv,&all,ui.cx*16+8,ui.cy*16+8,rad/3,CK_RGB(255,240,224),a); |
| 193 | unflash(&all,t); |
| 194 | } |
| 195 | ck_mind_draw(&ui.mind,&ui.cv,&all); |
| 196 | /* Native dawn duration is 900 ms; only its placement after dusk is web-specific. */ |
| 197 | if(ui.kind==1 && t<5100) { |
| 198 | uint32_t gain=ck_prog(t,4200,5100); |
| 199 | for(size_t i=0;i<(size_t)ui.w*ui.h;i++) ui.back[i]=ck_scale(ui.back[i],gain); |
| 200 | } |
| 201 | } |
| 202 | for(size_t i=0;i<(size_t)ui.w*ui.h;i++) { |
| 203 | uint32_t c=ui.back[i]; |
| 204 | ui.rgba[i]=0xff000000u|(c&0xff00u)|(c>>16&255u)|((c&255u)<<16); |
| 205 | } |
| 206 | } |
| 207 | /* Pan/zoom the source's existing sky camera; no generated replacement stars. */ |
| 208 | EXPORT int scene_view(int32_t ra,int32_t dec,int32_t fov) |
| 209 | { |
| 210 | if(!ui.initialized || ra<0 || ra>360000000 || dec< -89000000 || dec>89000000 || fov<10000 || fov>120000) return 0; |
| 211 | ck_sky_view v; |
| 212 | if(ra==113697000 && dec== -72757000 && fov==80000) v=ck_sky_the_view; |
| 213 | else ck_sky_view_at(&v,ra,dec,fov,0); |
| 214 | ck_night_view(&ui.night,&v,ui.scratch); |
| 215 | ck_night_picture_take(&ui.night,ui.night.sky,ui.keep,&ui.kept); |
| 216 | return 1; |
| 217 | } |
| 218 | /* Native stroke glyph drawn above RGBA: no system or external font dependency. |
| 219 | * Letters flying from the web input are an intentional browser enhancement. */ |
| 220 | EXPORT void scene_glyph(uint32_t c,int32_t x,int32_t y,uint32_t alpha) |
| 221 | { |
| 222 | if(!ui.initialized || c<32 || c>126 || alpha>256) return; |
| 223 | ck_rect all={0,0,ui.w,ui.h}; |
| 224 | (void)ck_letter(&ui.cv,&all,&ui.face,(uint64_t)ck_max(0,x)*256,y,(char)c,CK_RGB(206,222,255),alpha,1); |
| 225 | const ck_af_glyph *g=ck_af_glyph_get(&ui.face,(char)c); |
| 226 | if(!g) return; |
| 227 | ck_rect box={x+g->ox,y+g->oy,g->w,g->h}; |
| 228 | if(!ck_clip(&box,&all)) return; |
| 229 | for(int32_t yy=box.y;yy<box.y+box.h;yy++) for(int32_t xx=box.x;xx<box.x+box.w;xx++) { |
| 230 | size_t i=(size_t)yy*ui.w+xx;uint32_t col=ui.back[i]; |
| 231 | ui.rgba[i]=0xff000000u|(col&0xff00u)|(col>>16&255u)|((col&255u)<<16); |
| 232 | } |
| 233 | } |
| 234 | EXPORT uintptr_t scene_audio(uint32_t from,uint32_t frames) |
| 235 | { |
| 236 | if(frames>48000 || from>CK_CHIRP_FRAMES) return 0; |
| 237 | ck_chirp_fill(audio,from,frames);return (uintptr_t)audio; |
| 238 | } |