WebGL map: keep the pictures' keys, the waves and the clouds out of the garbage
A frame joined a key for every city, label, quiet dot and stamp it drew, built a closure to paint each even when the picture was already there, concatenated a prefix onto every key, made an array per city per copy of the map for its place on screen, and rebuilt the waves' and the clouds' arrays. Keys are now kept per city (packed into one number for the yes/no parts), per label text and look, per stamp and scale, and joined again only when the picture changes; pictures already painted are found before any closure is made; the card's and the 2D map's pictures are two maps, so keys need no prefix; the cities' places and the POP lights are computed in place; the waves and the clouds are written into arrays kept from frame to frame. Sampled heap profile, TTL save, RTX 4060 Ti: allocations under drawFrame 68 -> 37 MB/s at x1 and 62 -> 43 MB/s during a pan/zoom sweep; under glBegin 5.4 -> 0.5 MB/s.
This commit is contained in:
parent
3482142ae5
commit
87b153db6f
8 changed files with 153 additions and 37 deletions
|
|
@ -14,6 +14,7 @@ All notable changes to this project are documented here. The project follows
|
||||||
- Long zooms stay on the graphics card: the pictures of cities and labels are painted on one shared canvas instead of each keeping its own, so the browser no longer runs out of room for them and switches to the slower drawing.
|
- Long zooms stay on the graphics card: the pictures of cities and labels are painted on one shared canvas instead of each keeping its own, so the browser no longer runs out of room for them and switches to the slower drawing.
|
||||||
- Zooming is smoother on big maps: while you zoom, cities and ships keep their picture and are only redrawn sharp once the zoom stops, instead of being repainted at every small step.
|
- Zooming is smoother on big maps: while you zoom, cities and ships keep their picture and are only redrawn sharp once the zoom stops, instead of being repainted at every small step.
|
||||||
- The graphics card draws the coastlines with fewer points when you zoom out, where the extra detail was smaller than a pixel, so the map costs it a lot less work, which helps phones most.
|
- The graphics card draws the coastlines with fewer points when you zoom out, where the extra detail was smaller than a pixel, so the map costs it a lot less work, which helps phones most.
|
||||||
|
- The map leaves far less memory behind it on every frame for the browser to clean up while you play.
|
||||||
|
|
||||||
## [2.9.1] — 2026-11-04
|
## [2.9.1] — 2026-11-04
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -74,13 +74,17 @@ function glLand(gl,ks){const G=glLandGeo(gl,lodFar(),coastLevel(cam.z));
|
||||||
const WAVE_COL=glColor('rgba(255,255,255,.45)');
|
const WAVE_COL=glColor('rgba(255,255,255,.45)');
|
||||||
function glWaves(gl,now,ks){const A=GLX.wa||(GLX.wa=new Float32Array(WAVES.length*8*LINE_STRIDE)),ph0=now/900;let m=0;
|
function glWaves(gl,now,ks){const A=GLX.wa||(GLX.wa=new Float32Array(WAVES.length*8*LINE_STRIDE)),ph0=now/900;let m=0;
|
||||||
for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3;
|
for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3;
|
||||||
for(const [x0,cx,cy,x1] of [[x-9+o,x-4.5+o,y-4,x+o],[x+o,x+4.5+o,y+4,x+9+o]]){let px=x0,py=y;
|
// Two curves: up from x-9 to x, then down to x+9 (no array literals per wave: they were garbage every frame).
|
||||||
|
for(let c=0;c<2;c++){const x0=x+o+(c?0:-9),cx=x0+4.5,cy=y+(c?4:-4),x1=x0+9;let px=x0,py=y;
|
||||||
for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y;glSegAt(A,m++,px,py,qx,qy,0,2,0,0,WAVE_COL[3],WAVE_COL);px=qx;py=qy;}}}
|
for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y;glSegAt(A,m++,px,py,qx,qy,0,2,0,0,WAVE_COL[3],WAVE_COL);px=qx;py=qy;}}}
|
||||||
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.wv.b);gl.bufferData(gl.ARRAY_BUFFER,A.subarray(0,m*LINE_STRIDE),gl.DYNAMIC_DRAW);glLines(gl,GLX.wv.v,m,ks);}
|
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.wv.b);gl.bufferData(gl.ARRAY_BUFFER,A.subarray(0,m*LINE_STRIDE),gl.DYNAMIC_DRAW);glLines(gl,GLX.wv.v,m,ks);}
|
||||||
// The clouds: their puffs are stencilled as one shape, then filled once, so overlapping puffs do not stack.
|
// The clouds: their puffs are stencilled as one shape, then filled once, so overlapping puffs do not stack.
|
||||||
function glClouds(gl,now,ks){const A=[];for(const k of ks)for(const c of CLOUDS){const x=((c.x+now/1000*c.v)%(W+200))-100+k*W,y=c.y;
|
function glClouds(gl,now,ks){const need=ks.length*CLOUDS.length*12;if(!need)return;
|
||||||
A.push(x,y,16*c.s,0, x+18*c.s,y-8*c.s,20*c.s,0, x+40*c.s,y,15*c.s,0);}
|
// Written into one array kept from frame to frame, grown when more copies of the map show.
|
||||||
if(!A.length)return;const F=new Float32Array(A),P=GLX.circ;gl.bindBuffer(gl.ARRAY_BUFFER,GLX.cl.b);gl.bufferData(gl.ARRAY_BUFFER,F,gl.DYNAMIC_DRAW);
|
if(!GLX.ca||GLX.ca.length<need)GLX.ca=new Float32Array(need);const A=GLX.ca;let n=0;
|
||||||
|
for(const k of ks)for(const c of CLOUDS){const x=((c.x+now/1000*c.v)%(W+200))-100+k*W,y=c.y,s=c.s;
|
||||||
|
A[n++]=x;A[n++]=y;A[n++]=16*s;A[n++]=0;A[n++]=x+18*s;A[n++]=y-8*s;A[n++]=20*s;A[n++]=0;A[n++]=x+40*s;A[n++]=y;A[n++]=15*s;A[n++]=0;}
|
||||||
|
const F=A.subarray(0,n),P=GLX.circ;gl.bindBuffer(gl.ARRAY_BUFFER,GLX.cl.b);gl.bufferData(gl.ARRAY_BUFFER,F,gl.DYNAMIC_DRAW);
|
||||||
gl.enable(gl.STENCIL_TEST);gl.stencilFunc(gl.ALWAYS,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.REPLACE);gl.colorMask(false,false,false,false);
|
gl.enable(gl.STENCIL_TEST);gl.stencilFunc(gl.ALWAYS,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.REPLACE);gl.colorMask(false,false,false,false);
|
||||||
gl.useProgram(P.p);gl.bindVertexArray(GLX.cl.v);gl.uniform2f(P.u.uView,CW,CH);gl.uniform3f(P.u.uCam,cam.x,cam.y,cam.z);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,F.length/4);
|
gl.useProgram(P.p);gl.bindVertexArray(GLX.cl.v);gl.uniform2f(P.u.uView,CW,CH);gl.uniform3f(P.u.uCam,cam.x,cam.y,cam.z);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,F.length/4);
|
||||||
gl.colorMask(true,true,true,true);gl.stencilFunc(gl.EQUAL,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.KEEP);glFill(gl,CLOUD_COL);gl.disable(gl.STENCIL_TEST);}
|
gl.colorMask(true,true,true,true);gl.stencilFunc(gl.EQUAL,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.KEEP);glFill(gl,CLOUD_COL);gl.disable(gl.STENCIL_TEST);}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@
|
||||||
as instanced quads, with the small round things (lights, steam, glows) as discs, in one draw per frame after the
|
as instanced quads, with the small round things (lights, steam, glows) as discs, in one draw per frame after the
|
||||||
tubes. Without WebGL the same calls draw the pictures with the 2D canvas, so the map's code does not care which. */
|
tubes. Without WebGL the same calls draw the pictures with the 2D canvas, so the map's code does not care which. */
|
||||||
const SPR_MAX=1400,SPR_STRIDE=13;
|
const SPR_MAX=1400,SPR_STRIDE=13;
|
||||||
const SPRS=new Map();
|
// The card's pictures (SPRS) and the 2D map's (SPRC), by key; both at the screen's pixel density SPR_DPR.
|
||||||
|
const SPRS=new Map(),SPRC=new Map();let SPR_DPR=0;
|
||||||
const GLS={prog:null,vao:null,buf:null,tex:null,size:4096,shx:0,shy:0,shh:0,frame:false,data:new Float32Array(SPR_STRIDE*2048),n:0};
|
const GLS={prog:null,vao:null,buf:null,tex:null,size:4096,shx:0,shy:0,shh:0,frame:false,data:new Float32Array(SPR_STRIDE*2048),n:0};
|
||||||
/* One record is 13 floats: x,y,w,h (CSS px) | u0,v0,u1,v1 (atlas) | r,g,b,a | kind. A picture (kind 0) takes its
|
/* One record is 13 floats: x,y,w,h (CSS px) | u0,v0,u1,v1 (atlas) | r,g,b,a | kind. A picture (kind 0) takes its
|
||||||
colours from the atlas and a from the record; a disc (kind 1) is centred on x,y with radius w, in colour r,g,b,a. */
|
colours from the atlas and a from the record; a disc (kind 1) is centred on x,y with radius w, in colour r,g,b,a. */
|
||||||
|
|
@ -25,7 +26,7 @@ function glsInit(gl){GLS.prog=glProg(gl,SPR_VS,SPR_FS);const {v,b}=glVao(gl,[4,4
|
||||||
for(const k of [gl.TEXTURE_WRAP_S,gl.TEXTURE_WRAP_T])gl.texParameteri(gl.TEXTURE_2D,k,gl.CLAMP_TO_EDGE);
|
for(const k of [gl.TEXTURE_WRAP_S,gl.TEXTURE_WRAP_T])gl.texParameteri(gl.TEXTURE_2D,k,gl.CLAMP_TO_EDGE);
|
||||||
GLS.shx=GLS.shy=GLS.shh=0;}
|
GLS.shx=GLS.shy=GLS.shh=0;}
|
||||||
// Forgets every picture and starts the atlas again, after the pictures already queued are drawn (they point into it).
|
// Forgets every picture and starts the atlas again, after the pictures already queued are drawn (they point into it).
|
||||||
function spritesReset(){glsDrawNow();SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
|
function spritesReset(){glsDrawNow();SPRS.clear();SPRC.clear();GLS.shx=GLS.shy=GLS.shh=0;}
|
||||||
// The card given up: its pictures go (they have no canvas to be drawn from in 2D) and the atlas is forgotten.
|
// The card given up: its pictures go (they have no canvas to be drawn from in 2D) and the atlas is forgotten.
|
||||||
function spritesDrop(){GLS.n=0;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
|
function spritesDrop(){GLS.n=0;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
|
||||||
/* The card's pictures are painted on one canvas, reused, and each goes into the atlas as soon as it is painted: a
|
/* The card's pictures are painted on one canvas, reused, and each goes into the atlas as soon as it is painted: a
|
||||||
|
|
@ -35,10 +36,14 @@ const GLS_PAD={cv:null,g:null};
|
||||||
function glsPad(pw,ph){const P=GLS_PAD;if(!P.cv){P.cv=document.createElement('canvas');P.cv.width=P.cv.height=256;}
|
function glsPad(pw,ph){const P=GLS_PAD;if(!P.cv){P.cv=document.createElement('canvas');P.cv.width=P.cv.height=256;}
|
||||||
if(P.cv.width<pw||P.cv.height<ph){P.cv.width=Math.max(P.cv.width,Math.ceil(pw/64)*64);P.cv.height=Math.max(P.cv.height,Math.ceil(ph/64)*64);P.g=null;}
|
if(P.cv.width<pw||P.cv.height<ph){P.cv.width=Math.max(P.cv.width,Math.ceil(pw/64)*64);P.cv.height=Math.max(P.cv.height,Math.ceil(ph/64)*64);P.g=null;}
|
||||||
if(!P.g)P.g=P.cv.getContext('2d',{willReadFrequently:true});return P;}
|
if(!P.g)P.g=P.cv.getContext('2d',{willReadFrequently:true});return P;}
|
||||||
|
// Where this frame's pictures are kept: the card's or the 2D map's. A new pixel density starts both again.
|
||||||
|
function sprMap(){if(DPR!==SPR_DPR){SPR_DPR=DPR;spritesReset();}return GLS.frame&&!!GLX.gl&&!!GLS.prog?SPRS:SPRC;}
|
||||||
|
// The picture already painted under key, if any: callers ask first, so a picture drawn every frame costs no closure.
|
||||||
|
const spriteHas=key=>sprMap().get(key);
|
||||||
// A picture of w×h CSS px with its anchor at (ax,ay): painted once by paint(g) (the 2D canvas, at the screen's pixel
|
// A picture of w×h CSS px with its anchor at (ax,ay): painted once by paint(g) (the 2D canvas, at the screen's pixel
|
||||||
// density, which is part of the key), and kept in SPRS. The card's pictures and the 2D map's are kept apart ('g', 'c').
|
// density), and kept by key in SPRS (the card's) or SPRC (the 2D map's).
|
||||||
function spriteEntry(key,w,h,ax,ay,paint){const gpu=GLS.frame&&!!GLX.gl&&!!GLS.prog;key=(gpu?'g':'c')+DPR+'|'+key;let e=SPRS.get(key);if(e)return e;
|
function spriteEntry(key,w,h,ax,ay,paint){const M=sprMap(),gpu=M===SPRS;let e=M.get(key);if(e)return e;
|
||||||
if(SPRS.size>=SPR_MAX)spritesReset();
|
if(SPRS.size+SPRC.size>=SPR_MAX)spritesReset();
|
||||||
const pw=Math.ceil(w*DPR),ph=Math.ceil(h*DPR),saved=ctx;
|
const pw=Math.ceil(w*DPR),ph=Math.ceil(h*DPR),saved=ctx;
|
||||||
e={cv:null,pw,ph,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1};
|
e={cv:null,pw,ph,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1};
|
||||||
if(gpu&&pw<=GLS.size&&ph<=GLS.size){const P=glsPad(pw,ph),g=P.g;
|
if(gpu&&pw<=GLS.size&&ph<=GLS.size){const P=glsPad(pw,ph),g=P.g;
|
||||||
|
|
@ -48,7 +53,7 @@ function spriteEntry(key,w,h,ax,ay,paint){const gpu=GLS.frame&&!!GLX.gl&&!!GLS.p
|
||||||
try{if(!glsPut(GLX.gl,e,P.cv))glsPut(GLX.gl,e,P.cv);}catch(x){throw glErr(x);}
|
try{if(!glsPut(GLX.gl,e,P.cv))glsPut(GLX.gl,e,P.cv);}catch(x){throw glErr(x);}
|
||||||
}else{const cv=document.createElement('canvas');cv.width=pw;cv.height=ph;const g=cv.getContext('2d');ctx=g;
|
}else{const cv=document.createElement('canvas');cv.width=pw;cv.height=ph;const g=cv.getContext('2d');ctx=g;
|
||||||
try{g.setTransform(DPR,0,0,DPR,0,0);paint(g);}finally{ctx=saved;}e.cv=cv;}
|
try{g.setTransform(DPR,0,0,DPR,0,0);paint(g);}finally{ctx=saved;}e.cv=cv;}
|
||||||
SPRS.set(key,e);return e;}
|
M.set(key,e);return e;}
|
||||||
// Puts a picture with its own canvas into the atlas (once), shelf by shelf. A full atlas starts again; this picture then waits a frame.
|
// Puts a picture with its own canvas into the atlas (once), shelf by shelf. A full atlas starts again; this picture then waits a frame.
|
||||||
function glsEnsure(e){if(e.up)return true;const gl=GLX.gl;if(!gl||!GLS.prog||!e.cv)return false;
|
function glsEnsure(e){if(e.up)return true;const gl=GLX.gl;if(!gl||!GLS.prog||!e.cv)return false;
|
||||||
try{return glsPut(gl,e,e.cv);}catch(x){throw glErr(x);}}
|
try{return glsPut(gl,e,e.cv);}catch(x){throw glErr(x);}}
|
||||||
|
|
@ -85,8 +90,11 @@ const sprQ=k=>Math.max(1/CSPR_STEP,Math.round(k*ZH.f*CSPR_STEP)/CSPR_STEP);
|
||||||
// (k in steps of 1/24, as the cities; zoomed: k grows with the zoom, and is held while it moves, sprQ()), then drawn with
|
// (k in steps of 1/24, as the cities; zoomed: k grows with the zoom, and is held while it moves, sprQ()), then drawn with
|
||||||
// the anchor at (x,y). Things that bob or pulse are drawn as these at a moving place, so the overlay above the card
|
// the anchor at (x,y). Things that bob or pulse are drawn as these at a moving place, so the overlay above the card
|
||||||
// stays untouched (repainting it costs a whole screen of pixels).
|
// stays untouched (repainting it costs a whole screen of pixels).
|
||||||
|
// The full key of a stamp at scale kq, kept per key: it was built again for every stamp on every frame.
|
||||||
|
const STK=new Map();
|
||||||
function stamp(key,w,h,ax,ay,k,x,y,paint,zoomed){const kq=zoomed?sprQ(k):Math.max(1/CSPR_STEP,Math.round(k*CSPR_STEP)/CSPR_STEP),r=k/kq;
|
function stamp(key,w,h,ax,ay,k,x,y,paint,zoomed){const kq=zoomed?sprQ(k):Math.max(1/CSPR_STEP,Math.round(k*CSPR_STEP)/CSPR_STEP),r=k/kq;
|
||||||
const e=spriteEntry(key+'|'+kq,Math.ceil(w*kq),Math.ceil(h*kq),ax*kq,ay*kq,g=>{g.translate(ax*kq,ay*kq);g.scale(kq,kq);paint(g);});
|
let m=STK.get(key);if(!m||m.kq!==kq){if(STK.size>4000)STK.clear();STK.set(key,m={kq,k:key+'|'+kq});}
|
||||||
|
const e=spriteHas(m.k)||spriteEntry(m.k,Math.ceil(w*kq),Math.ceil(h*kq),ax*kq,ay*kq,g=>{g.translate(ax*kq,ay*kq);g.scale(kq,kq);paint(g);});
|
||||||
blit(e,x-e.ax*r,y-e.ay*r,e.w*r,e.h*r);}
|
blit(e,x-e.ax*r,y-e.ay*r,e.w*r,e.h*r);}
|
||||||
// Text with an outline, centred on (x,y), as a picture placed on whole device pixels; tw is its width, px its size.
|
// Text with an outline, centred on (x,y), as a picture placed on whole device pixels; tw is its width, px its size.
|
||||||
function textStamp(t,font,px,tw,fill,stroke,lw,x,y){const W=Math.ceil(tw+lw+4),H=Math.ceil(px*1.5+lw+2);
|
function textStamp(t,font,px,tw,fill,stroke,lw,x,y){const W=Math.ceil(tw+lw+4),H=Math.ceil(px*1.5+lw+2);
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ function drawFrame(now){
|
||||||
// cities
|
// cities
|
||||||
UI.feed=null;if(UI.sel&&UI.sel.type==='link'){const sl=S.links.find(x=>x.id===UI.sel.id);const rec=sl&&R.ef[edgeKey(sl)];if(rec){UI.feed=new Set(Object.keys(rec.eg).map(k=>R.egDesc[k].city));UI.dest=new Set(Object.keys(rec.dst));}}
|
UI.feed=null;if(UI.sel&&UI.sel.type==='link'){const sl=S.links.find(x=>x.id===UI.sel.id);const rec=sl&&R.ef[edgeKey(sl)];if(rec){UI.feed=new Set(Object.keys(rec.eg).map(k=>R.egDesc[k].city));UI.dest=new Set(Object.keys(rec.dst));}}
|
||||||
// Place labels: your POPs and the selected city first, then others; a label that would overlap moves above its city or hides.
|
// Place labels: your POPs and the selected city first, then others; a label that would overlap moves above its city or hides.
|
||||||
const vis=[];for(const C of CITIES){if(C.off&&!cityOn(C.id))continue;if(pzHide(C.id))continue;for(const k of ks){const [sx,sy]=toS(C.x,C.y,k);if(sx<-60||sx>CW+60||sy<-60||sy>CH+60)continue;vis.push({C,sx,sy});}}
|
const vis=[];for(const C of CITIES){const sy=(C.y-cam.y)*cam.z+CH/2;if(sy<-60||sy>CH+60)continue;if(C.off&&!cityOn(C.id))continue;if(pzHide(C.id))continue;for(const k of ks){const sx=(C.x+k*W-cam.x)*cam.z+CW/2;if(sx<-60||sx>CW+60)continue;vis.push({C,sx,sy});}}
|
||||||
const prio=v=>(UI.sel&&UI.sel.type==='city'&&UI.sel.id===v.C.id?0:S.pops[v.C.id]?1:2);vis.sort((a,b)=>prio(a)-prio(b)||b.C.P-a.C.P);
|
const prio=v=>(UI.sel&&UI.sel.type==='city'&&UI.sel.id===v.C.id?0:S.pops[v.C.id]?1:2);vis.sort((a,b)=>prio(a)-prio(b)||b.C.P-a.C.P);
|
||||||
const rects=new RGrid(),labs=new RGrid(),hit=(r)=>rects.hits(r);
|
const rects=new RGrid(),labs=new RGrid(),hit=(r)=>rects.hits(r);
|
||||||
const farL=lodFar();
|
const farL=lodFar();
|
||||||
|
|
@ -116,7 +116,7 @@ function drawFrame(now){
|
||||||
if(ly===null&&prio(v)===1)for(const y of [below,above]){const r={x:v.sx-tw/2,y:y-10,w:tw,h:21};if(!labs.hits(r)){ly=y;rects.push(r);labs.push(r);break;}}
|
if(ly===null&&prio(v)===1)for(const y of [below,above]){const r={x:v.sx-tw/2,y:y-10,w:tw,h:21};if(!labs.hits(r)){ly=y;rects.push(r);labs.push(r);break;}}
|
||||||
if(ly===null&&(prio(v)===0||(prio(v)===1&&(v.C.id===S.home||UI.hover===v.C.id)))){ly=below;const r={x:v.sx-tw/2,y:below-10,w:tw,h:21};rects.push(r);labs.push(r);}
|
if(ly===null&&(prio(v)===0||(prio(v)===1&&(v.C.id===S.home||UI.hover===v.C.id)))){ly=below;const r={x:v.sx-tw/2,y:below-10,w:tw,h:21};rects.push(r);labs.push(r);}
|
||||||
v.ly=ly;}
|
v.ly=ly;}
|
||||||
if(pzOn()){for(const v of vis.slice().reverse())pzDrawCity(v,csc,now);pzDrawMarks(ks,now);}else for(const v of vis.slice().reverse())drawCity(v.C,v.sx,v.sy,csc,now,v.ly,v.dem);
|
if(pzOn()){for(let i=vis.length-1;i>=0;i--)pzDrawCity(vis[i],csc,now);pzDrawMarks(ks,now);}else for(let i=vis.length-1;i>=0;i--){const v=vis[i];drawCity(v.C,v.sx,v.sy,csc,now,v.ly,v.dem);}
|
||||||
// cable ships at sea (88-cable-ships.js), over the city labels so a ship leaving port stays in sight
|
// cable ships at sea (88-cable-ships.js), over the city labels so a ship leaving port stays in sight
|
||||||
drawShips(ks,now);
|
drawShips(ks,now);
|
||||||
// Architect runs: what the tubes between two cities are (one trench, or separate paths), above the cities.
|
// Architect runs: what the tubes between two cities are (one trench, or separate paths), above the cities.
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
// Label widths by text (city labels by id, link labels and far-view labels by text): measuring text every frame is slow.
|
// Label widths by text (city labels by id, link labels and far-view labels by text): measuring text every frame is slow.
|
||||||
const LBLW={},LINKW=new Map(),LBLF=new Map(),CM={off:new Set(),big:new Set(),inc:new Set(),far:false};
|
const LBLW={},LINKW=new Map(),LBLF=new Map(),FKEY=new Map(),QKEY=new Map(),CM={off:new Set(),big:new Set(),inc:new Set(),far:false};
|
||||||
try{document.fonts&&document.fonts.ready.then(()=>{for(const k in LBLW)delete LBLW[k];LINKW.clear();LBLF.clear();FRAME.length=0;spritesReset();});}catch(e){}
|
try{document.fonts&&document.fonts.ready.then(()=>{for(const k in LBLW)delete LBLW[k];LINKW.clear();LBLF.clear();FRAME.length=0;spritesReset();});}catch(e){}
|
||||||
// A label on a pill: its text and look never change, so it is painted once (spriteEntry) and drawn as a picture. The
|
// A label on a pill: its text and look never change, so it is painted once (spriteEntry) and drawn as a picture. The
|
||||||
// centre is x, y; the text sits at y+ty. The pill's box is w×h with corners r; the picture also takes the outline.
|
// centre is x, y; the text sits at y+ty. The pill's box is w×h with corners r; the picture also takes the outline.
|
||||||
|
// Keys are kept by text (PILLK), one per look: joining them for every label on every frame was most of the map's garbage.
|
||||||
|
const PILLK=new Map();
|
||||||
function pill(t,font,w,h,r,fill,ink,stroke,lw,x,y,ty){
|
function pill(t,font,w,h,r,fill,ink,stroke,lw,x,y,ty){
|
||||||
const pad=lw+2,W=Math.ceil(w+pad*2),H=Math.ceil(h+pad*2),key=['pill',t,font,w,h,r,fill,ink,stroke,lw,ty].join('|');
|
const pad=lw+2,W=Math.ceil(w+pad*2),H=Math.ceil(h+pad*2);let L=PILLK.get(t),m=null;
|
||||||
const e=spriteEntry(key,W,H,W/2,H/2,g=>{g.translate(W/2,H/2);g.fillStyle=fill;g.strokeStyle=stroke;g.lineWidth=lw;
|
if(L)for(let i=0;i<L.length;i++){const q=L[i];if(q.font===font&&q.w===w&&q.h===h&&q.r===r&&q.fill===fill&&q.ink===ink&&q.stroke===stroke&&q.lw===lw&&q.ty===ty){m=q;break;}}
|
||||||
|
if(!m){if(!L){if(PILLK.size>4000)PILLK.clear();PILLK.set(t,L=[]);}L.push(m={font,w,h,r,fill,ink,stroke,lw,ty,k:['pill',t,font,w,h,r,fill,ink,stroke,lw,ty].join('|')});}
|
||||||
|
const e=spriteHas(m.k)||spriteEntry(m.k,W,H,W/2,H/2,g=>{g.translate(W/2,H/2);g.fillStyle=fill;g.strokeStyle=stroke;g.lineWidth=lw;
|
||||||
rr(-w/2,-h/2,w,h,r);g.fill();g.stroke();g.fillStyle=ink;g.font=font;g.textAlign='center';g.textBaseline='middle';g.fillText(t,0,ty);});
|
rr(-w/2,-h/2,w,h,r);g.fill();g.stroke();g.fillStyle=ink;g.font=font;g.textAlign='center';g.textBaseline='middle';g.fillText(t,0,ty);});
|
||||||
// Whole device pixels, so the text stays as sharp as before.
|
// Whole device pixels, so the text stays as sharp as before.
|
||||||
blit(e,Math.round((x-e.w/2)*DPR)/DPR,Math.round((y-e.h/2)*DPR)/DPR,e.w,e.h);}
|
blit(e,Math.round((x-e.w/2)*DPR)/DPR,Math.round((y-e.h/2)*DPR)/DPR,e.w,e.h);}
|
||||||
|
|
@ -13,8 +17,10 @@ function pill(t,font,w,h,r,fill,ink,stroke,lw,x,y,ty){
|
||||||
function drawCityFar(C,sx,sy,sc,now,lyOverride){
|
function drawCityFar(C,sx,sy,sc,now,lyOverride){
|
||||||
const p=S.pops[C.id],down=p&&p.down>S.day,sel=UI.sel&&UI.sel.type==='city'&&UI.sel.id===C.id,hov=UI.hover===C.id||(UI.snap&&UI.snap.id===C.id);
|
const p=S.pops[C.id],down=p&&p.down>S.day,sel=UI.sel&&UI.sel.type==='city'&&UI.sel.id===C.id,hov=UI.hover===C.id||(UI.snap&&UI.snap.id===C.id);
|
||||||
if(p&&!down&&R.cityDem[C.id]>0&&sfOf(R,C.id)<0.9)dot(sx,sy,12,'rgba(255,77,94,1)',.35+.25*Math.sin(now/200));
|
if(p&&!down&&R.cityDem[C.id]>0&&sfOf(R,C.id)<0.9)dot(sx,sy,12,'rgba(255,77,94,1)',.35+.25*Math.sin(now/200));
|
||||||
const t=cTier(C.id),ph=sel?Math.floor(now/40)%8:-1,key=['far',p?1:0,down?1:0,p&&p.site2?1:0,p?(p.routers||1)>=2?1:0:0,t,hov?1:0,hasIxAt(C.id)?1:0,CM.off.has(C.id)?1:0,CM.inc.has(C.id)?1:0,ph].join('|');
|
const t=cTier(C.id),ph=sel?Math.floor(now/40)%8:-1,f=(p?1:0)|(down?2:0)|(p&&p.site2?4:0)|(p&&(p.routers||1)>=2?8:0)|(hov?16:0)|(hasIxAt(C.id)?32:0)|(CM.off.has(C.id)?64:0)|(CM.inc.has(C.id)?128:0);
|
||||||
stamp(key,36,36,18,18,1,sx,sy,()=>cityFarStatic(C,p,down,sel,hov,ph));
|
// The key, kept per city and built again only when the picture changes.
|
||||||
|
let m=FKEY.get(C.id);if(!m||m.f!==f||m.t!==t||m.ph!==ph)FKEY.set(C.id,m={f,t,ph,k:'far|'+t+'|'+f+'|'+ph});
|
||||||
|
stamp(m.k,36,36,18,18,1,sx,sy,()=>cityFarStatic(C,p,down,sel,hov,ph));
|
||||||
if(lyOverride!==null&&(p||sel||hov)){ctx.font=`800 ${p?12:11}px "Baloo 2", sans-serif`;const t=C.name,wk=C.id+(p?1:0);let tw=LBLF.get(wk);if(tw===undefined)LBLF.set(wk,tw=ctx.measureText(t).width+12);const ly=lyOverride==null?sy+15:lyOverride;
|
if(lyOverride!==null&&(p||sel||hov)){ctx.font=`800 ${p?12:11}px "Baloo 2", sans-serif`;const t=C.name,wk=C.id+(p?1:0);let tw=LBLF.get(wk);if(tw===undefined)LBLF.set(wk,tw=ctx.measureText(t).width+12);const ly=lyOverride==null?sy+15:lyOverride;
|
||||||
pill(t,ctx.font,tw,16,8,p?'#FF4F9A':'#FFFFFF',p?'#FFFFFF':'#1E2640','#1E2640',1.6,sx,ly,1);}
|
pill(t,ctx.font,tw,16,8,p?'#FF4F9A':'#FFFFFF',p?'#FFFFFF':'#1E2640','#1E2640',1.6,sx,ly,1);}
|
||||||
}
|
}
|
||||||
|
|
@ -79,18 +85,25 @@ function cityStatic(C,p,t,cs,k,ph,st){const down=st.down;
|
||||||
if(st.inc){ctx.fillStyle='#FF4D5E';ctx.beginPath();ctx.arc(-20,-h+2,7,0,7);ctx.fill();ctx.lineWidth=2;ctx.stroke();ctx.fillStyle='#fff';ctx.font='900 11px Nunito, sans-serif';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText('!',-20,-h+3);}
|
if(st.inc){ctx.fillStyle='#FF4D5E';ctx.beginPath();ctx.arc(-20,-h+2,7,0,7);ctx.fill();ctx.lineWidth=2;ctx.stroke();ctx.fillStyle='#fff';ctx.font='900 11px Nunito, sans-serif';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText('!',-20,-h+3);}
|
||||||
}
|
}
|
||||||
// The picture of a city in a given state, at the quantised scale kq: painted once and kept until it is not needed.
|
// The picture of a city in a given state, at the quantised scale kq: painted once and kept until it is not needed.
|
||||||
function citySprite(C,p,t,cs,kq,ph,st){
|
// Its key is kept per city (CKEY) and joined again only when one of its parts changes: the yes/no parts are bits of f.
|
||||||
const key=['city',C.id,t,kq,st.down?1:0,p?1:0,p&&p.site2?1:0,p&&p.site2&&siteDown(C.id,'b')?1:0,p&&p.ringcut>S.day?1:0,p?(p.routers||1):0,p&&p.gen?1:0,p?(p.rgen||0):0,C.au?1:0,
|
const CKEY=new Map();
|
||||||
st.ixDraw?1:0,st.ixHot?1:0,ixName(C.id)||'',S.myIx[C.id]?1:0,hasIxAt(C.id)?1:0,!p&&ixQuiet(C.id)?1:0,st.feed?1:0,st.sel?ph.sel:-1,ph.dd,st.inc?1:0].join('|');
|
function citySprite(C,p,t,cs,kq,ph,st){const id=C.id,
|
||||||
|
f=(st.down?1:0)|(p?2:0)|(p&&p.site2?4:0)|(p&&p.site2&&siteDown(id,'b')?8:0)|(p&&p.ringcut>S.day?16:0)|(p&&p.gen?32:0)|(C.au?64:0)|(st.ixDraw?128:0)|
|
||||||
|
(st.ixHot?256:0)|(S.myIx[id]?512:0)|(hasIxAt(id)?1024:0)|(!p&&ixQuiet(id)?2048:0)|(st.feed?4096:0)|(st.inc?8192:0),
|
||||||
|
ro=p?(p.routers||1):0,rg=p?(p.rgen||0):0,ix=ixName(id)||'',sp=st.sel?ph.sel:-1,dd=ph.dd;
|
||||||
|
let m=CKEY.get(id);
|
||||||
|
if(!m||m.t!==t||m.kq!==kq||m.f!==f||m.ro!==ro||m.rg!==rg||m.ix!==ix||m.sp!==sp||m.dd!==dd)CKEY.set(id,m={t,kq,f,ro,rg,ix,sp,dd,k:['city',id,t,kq,f,ro,rg,ix,sp,dd].join('|')});
|
||||||
|
const e=spriteHas(m.k);if(e)return e;
|
||||||
const W=Math.ceil(92*kq),H=Math.ceil(84*kq),ax=45*kq,ay=46*kq;
|
const W=Math.ceil(92*kq),H=Math.ceil(84*kq),ax=45*kq,ay=46*kq;
|
||||||
return spriteEntry(key,W,H,ax,ay,g=>{g.translate(ax,ay);cityStatic(C,p,t,cs,kq,ph,st);});}
|
return spriteEntry(m.k,W,H,ax,ay,g=>{g.translate(ax,ay);cityStatic(C,p,t,cs,kq,ph,st);});}
|
||||||
// The parts that move, in the city's frame at the true scale k.
|
// The parts that move, in the city's frame at the true scale k.
|
||||||
function cityMoving(C,p,t,cs,k,sx,sy,now,h,st,hot){const down=st.down,K=k*cs;
|
function cityMoving(C,p,t,cs,k,sx,sy,now,h,st,hot){const down=st.down,K=k*cs;
|
||||||
if(p){// The POP's own frame is shifted 2 down, and mirrored Down Under (C.au): mp() takes a point of that frame to the city's.
|
if(p){// The POP's own frame is shifted 2 down, and mirrored Down Under (C.au): mp() takes a point of that frame to the city's.
|
||||||
const fl=!!C.au,mp=(x,y)=>fl?[-x,-y-2]:[x,y+2],sf=sfOf(R,C.id),at=(x,y)=>{const q=mp(x,y);return [sx+q[0]*K,sy+q[1]*K];};
|
// A point (x,y) of that frame is at sx+(fl?-x:x)*K, sy+(fl?-y-2:y+2)*K (written out: no array per light per frame).
|
||||||
if(!down&&(R.nodeLoad[C.id]||0)>=(R.nodeCap[C.id]||1e12)*0.95){for(let i=0;i<3;i++){const tt=(now/700+i/3)%1,[x,y]=at(6+i*3,-12-tt*18);dot(x,y,(3+tt*4)*K,'rgba(90,98,130,1)',0.55*(1-tt));}}
|
const fl=!!C.au,sf=sfOf(R,C.id);
|
||||||
|
if(!down&&(R.nodeLoad[C.id]||0)>=(R.nodeCap[C.id]||1e12)*0.95){for(let i=0;i<3;i++){const tt=(now/700+i/3)%1,x=6+i*3,y=-12-tt*18;dot(sx+(fl?-x:x)*K,sy+(fl?-y-2:y+2)*K,(3+tt*4)*K,'rgba(90,98,130,1)',0.55*(1-tt));}}
|
||||||
// The four lights: a disc each, lit or not.
|
// The four lights: a disc each, lit or not.
|
||||||
for(let i=0;i<4;i++){const lit=down?false:(((now/(160+i*37))|0)+i)%3!==0,[x,y]=at(-10+i*6.6,1),col=down?'#3B4260':lit?(sf<0.9?'#FF4D5E':'#7CFFB2'):'#2A3150';dot(x,y,2.1*K,col,1);}
|
for(let i=0;i<4;i++){const lit=down?false:(((now/(160+i*37))|0)+i)%3!==0,x=-10+i*6.6,col=down?'#3B4260':lit?(sf<0.9?'#FF4D5E':'#7CFFB2'):'#2A3150';dot(sx+(fl?-x:x)*K,sy+(fl?-3:3)*K,2.1*K,col,1);}
|
||||||
}
|
}
|
||||||
// A flashing IX: two pictures, one per colour.
|
// A flashing IX: two pictures, one per colour.
|
||||||
if(st.ixDraw&&st.ixHot){const on=Math.sin(now/180)>0,ix=p?19:SKY_W[t]/2+4;stamp('ixhot'+(on?1:0),22,22,11,11,K,sx+ix*K,sy+(2-h)*K,()=>{ctx.translate(-ix,h-2);ctx.strokeStyle='#1E2640';cityIX(C,p,t,h,true,on?1:-1);},true);}
|
if(st.ixDraw&&st.ixHot){const on=Math.sin(now/180)>0,ix=p?19:SKY_W[t]/2+4;stamp('ixhot'+(on?1:0),22,22,11,11,K,sx+ix*K,sy+(2-h)*K,()=>{ctx.translate(-ix,h-2);ctx.strokeStyle='#1E2640';cityIX(C,p,t,h,true,on?1:-1);},true);}
|
||||||
|
|
@ -106,7 +119,8 @@ function drawCity(C,sx,sy,sc,now,lyOverride,dem){
|
||||||
const p=S.pops[C.id],down=p&&p.down>S.day,sel=UI.sel&&UI.sel.type==='city'&&UI.sel.id===C.id,hov=UI.hover===C.id||(UI.snap&&UI.snap.id===C.id);
|
const p=S.pops[C.id],down=p&&p.down>S.day,sel=UI.sel&&UI.sel.type==='city'&&UI.sel.id===C.id,hov=UI.hover===C.id||(UI.snap&&UI.snap.id===C.id);
|
||||||
// No room for its skyline here: a quiet dot, a little bigger for bigger cities, until you zoom in (cityRoom).
|
// No room for its skyline here: a quiet dot, a little bigger for bigger cities, until you zoom in (cityRoom).
|
||||||
if(dem&&!p&&!sel&&!hov){const r=(2.6+cTier(C.id)*0.35)*sc,rq=Math.round(r*4)/4,fill=hasIxAt(C.id)?'#FFD23F':'#FFFFFF',S2=Math.ceil(rq*2+4);
|
if(dem&&!p&&!sel&&!hov){const r=(2.6+cTier(C.id)*0.35)*sc,rq=Math.round(r*4)/4,fill=hasIxAt(C.id)?'#FFD23F':'#FFFFFF',S2=Math.ceil(rq*2+4);
|
||||||
const e=spriteEntry(['quiet',rq,fill].join('|'),S2,S2,S2/2,S2/2,g=>{g.translate(S2/2,S2/2);g.fillStyle=fill;g.strokeStyle='#1E2640';g.lineWidth=1.6;g.beginPath();g.arc(0,0,rq,0,7);g.fill();g.stroke();});
|
const qk=rq*8+(fill==='#FFFFFF'?0:1);let key=QKEY.get(qk);if(!key)QKEY.set(qk,key=['quiet',rq,fill].join('|'));
|
||||||
|
const e=spriteHas(key)||spriteEntry(key,S2,S2,S2/2,S2/2,g=>{g.translate(S2/2,S2/2);g.fillStyle=fill;g.strokeStyle='#1E2640';g.lineWidth=1.6;g.beginPath();g.arc(0,0,rq,0,7);g.fill();g.stroke();});
|
||||||
blit(e,sx-e.ax,sy+2*sc-e.ay,e.w,e.h);return;}
|
blit(e,sx-e.ax,sy+2*sc-e.ay,e.w,e.h);return;}
|
||||||
const t=cTier(C.id),cs=p?1:CSC[t],k=sc*(hov?1.15:1),kq=sprQ(k);
|
const t=cTier(C.id),cs=p?1:CSC[t],k=sc*(hov?1.15:1),kq=sprQ(k);
|
||||||
const ixn=ixName(C.id),ixDraw=!!ixn&&(ixShown(C.id,p)||(hot&&hot.ix)||sel||hov),ixHot=!!(ixDraw&&hot&&hot.ix);
|
const ixn=ixName(C.id),ixDraw=!!ixn&&(ixShown(C.id,p)||(hot&&hot.ix)||sel||hov),ixHot=!!(ixDraw&&hot&&hot.ix);
|
||||||
|
|
|
||||||
89
tests/webgl-frame-garbage.test.js
Normal file
89
tests/webgl-frame-garbage.test.js
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
// A frame on the card makes little garbage (src/game/96-map/57-webgl-background.js, 59-gl-sprites.js, 80-cities.js):
|
||||||
|
// the keys of the pictures are kept per city and per label and built again only when the picture changes, and the
|
||||||
|
// waves and the clouds are written into arrays kept from frame to frame. What is drawn stays the same.
|
||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
const { load } = require('./harness');
|
||||||
|
|
||||||
|
const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
|
||||||
|
|
||||||
|
function open() {
|
||||||
|
const h = load({ seed: 777 });
|
||||||
|
h.ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();S.nextFail=S.day+999;S.nextEvent=S.day+999;compute();`);
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
const STUB = `(()=>{const calls=[];const gl=new Proxy({calls},{get(t,k){if(k in t)return t[k];if(typeof k==='string'&&/^[A-Z_0-9]+$/.test(k))return k;
|
||||||
|
return (...a)=>{calls.push([k,...a]);return {};};}});
|
||||||
|
const c=document.getElementById('mapgl'),f=document.getElementById('mapfx');
|
||||||
|
const P=()=>({p:{},u:{}}),B=()=>({v:{},b:{}});
|
||||||
|
Object.assign(GLX,{want:true,dead:false,gl,c,f,fx:f.getContext('2d'),line:P(),pk:P(),fill:P(),fan:P(),ell:P(),land:{},circ:P(),tex:{},lv:B(),hv:B(),pv:B(),cb:B(),wv:B(),cl:B(),empty:{},key:[],gpu:'stub'});
|
||||||
|
GLS.prog=P();GLS.vao={};GLS.buf={};GLS.tex={};GLS.size=4096;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;return gl;})()`;
|
||||||
|
|
||||||
|
// Counts the keys joined during one frame (Array#join with '|', as the picture keys are), but the ships' (a handful).
|
||||||
|
const JOINS = (body) => `(()=>{let n=0;const j=Array.prototype.join;Array.prototype.join=function(s){if(s==='|'&&this[0]!=='ship')n++;return j.apply(this,arguments);};
|
||||||
|
try{${body}}finally{Array.prototype.join=j;}return n;})()`;
|
||||||
|
|
||||||
|
for (const [view, z] of [['near', 1.4], ['far', 0.6]]) {
|
||||||
|
test(`an unchanged frame on the card joins no picture key again (${view} view)`, () => {
|
||||||
|
const { ev, errors } = open();
|
||||||
|
ev(STUB);
|
||||||
|
ev(`const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=${z};UI.sel=null;draw(1000);draw(1016);`);
|
||||||
|
const first = ev(JOINS(`SPRS.clear();CKEY.clear();PILLK.clear();FKEY.clear();QKEY.clear();draw(1032);`));
|
||||||
|
const again = ev(JOINS('draw(1048);'));
|
||||||
|
assert.ok(first > 20, `a first frame builds its keys (${first})`);
|
||||||
|
assert.equal(again, 0, 'the next one builds none');
|
||||||
|
assert.deepEqual(errors, []);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test('a city whose state changes gets a new picture, and its old one back after', () => {
|
||||||
|
const { ev, errors } = open();
|
||||||
|
ev(STUB);
|
||||||
|
const r = JSON.parse(ev(`(()=>{const id=S.home,C=CITY[id],p=S.pops[id];cam.x=C.x;cam.y=C.y;cam.z=1.4;UI.sel=null;
|
||||||
|
const pic=()=>{draw(1000);return CKEY.get(id).k;};const a=pic();const was=p.down;p.down=S.day+3;const b=pic();p.down=was;const c=pic();
|
||||||
|
UI.sel={type:'city',id};const d=pic();UI.sel=null;
|
||||||
|
const before=CKEY.get(id).k,gen=p.gen;p.gen=!gen;const e=pic();p.gen=gen;
|
||||||
|
return JSON.stringify({a,b,c,d,e,before});})()`));
|
||||||
|
assert.notEqual(r.b, r.a, 'down: another picture');
|
||||||
|
assert.equal(r.c, r.a, 'back up: the same picture');
|
||||||
|
assert.notEqual(r.d, r.a, 'selected: another picture');
|
||||||
|
assert.notEqual(r.e, r.before, 'a generator: another picture');
|
||||||
|
assert.deepEqual(errors, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('labels with the same text but another look are other pictures', () => {
|
||||||
|
const { ev, errors } = open();
|
||||||
|
ev(STUB);
|
||||||
|
const r = JSON.parse(ev(`(()=>{GLS.frame=true;GLS.n=0;ctx=GLX.fx;const f='800 12px "Baloo 2", sans-serif';
|
||||||
|
pill('Lagos',f,50,19,9,'#FFFFFF','#1E2640','#1E2640',2,10,10,.5);pill('Lagos',f,50,19,9,'#FF4F9A','#FFFFFF','#1E2640',2,10,10,.5);pill('Lagos',f,50,19,9,'#FFFFFF','#1E2640','#1E2640',2,10,10,.5);
|
||||||
|
const n=GLS.n,L=PILLK.get('Lagos').length,u=new Set([0,1,2].map(i=>GLS.data[i*SPR_STRIDE+4]+','+GLS.data[i*SPR_STRIDE+5])).size;GLS.frame=false;GLS.n=0;ctx=CTX0;
|
||||||
|
return JSON.stringify({n,L,u});})()`));
|
||||||
|
assert.equal(r.n, 3);
|
||||||
|
assert.equal(r.L, 2, 'two looks kept for one text');
|
||||||
|
assert.equal(r.u, 2, 'two pictures in the atlas, the third label reuses the first');
|
||||||
|
assert.deepEqual(errors, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the waves and the clouds are what they were, written into arrays kept from frame to frame', () => {
|
||||||
|
const { ev, errors } = open();
|
||||||
|
ev(STUB);
|
||||||
|
const r = JSON.parse(ev(`(()=>{const ks=[-1,0,1],now=12345,gl=GLX.gl;
|
||||||
|
glWaves(gl,now,ks);const wa=GLX.wa,ph0=now/900,want=[];let m=0,bad=0;
|
||||||
|
for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3;for(const [x0,cx,cy,x1] of [[x-9+o,x-4.5+o,y-4,x+o],[x+o,x+4.5+o,y+4,x+9+o]]){let px=x0,py=y;
|
||||||
|
for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y,o2=m*LINE_STRIDE;
|
||||||
|
if(wa[o2]!==Math.fround(px)||wa[o2+1]!==Math.fround(py)||wa[o2+2]!==Math.fround(qx)||wa[o2+3]!==Math.fround(qy))bad++;m++;px=qx;py=qy;}}}
|
||||||
|
glWaves(gl,now+16,ks);const sameW=GLX.wa===wa;
|
||||||
|
glClouds(gl,now,ks);const ca=GLX.ca,A=[];for(const k of ks)for(const c of CLOUDS){const x=((c.x+now/1000*c.v)%(W+200))-100+k*W,y=c.y;A.push(x,y,16*c.s,0, x+18*c.s,y-8*c.s,20*c.s,0, x+40*c.s,y,15*c.s,0);}
|
||||||
|
const sent=gl.calls.filter(c=>c[0]==='bufferData').pop()[2],cbad=A.filter((v,i)=>sent[i]!==Math.fround(v)).length;
|
||||||
|
glClouds(gl,now+16,ks);
|
||||||
|
return JSON.stringify({bad,m,sameW,cbad,clen:sent.length,alen:A.length,sameC:GLX.ca===ca});})()`));
|
||||||
|
assert.equal(r.bad, 0, `every wave segment where it was (${r.m} segments)`);
|
||||||
|
assert.equal(r.sameW, true);
|
||||||
|
assert.equal(r.clen, r.alen, 'one record per puff');
|
||||||
|
assert.equal(r.cbad, 0, 'every puff where it was');
|
||||||
|
assert.equal(r.sameC, true, 'the clouds’ array is kept');
|
||||||
|
assert.deepEqual(errors, []);
|
||||||
|
});
|
||||||
|
|
@ -35,7 +35,7 @@ test('on the card, pictures are painted on one canvas and each goes into the atl
|
||||||
const r = JSON.parse(ev(`(()=>{const subs=GLX.gl.calls.filter(c=>c[0]==='texSubImage2D'),es=[...SPRS.values()];
|
const r = JSON.parse(ev(`(()=>{const subs=GLX.gl.calls.filter(c=>c[0]==='texSubImage2D'),es=[...SPRS.values()];
|
||||||
return JSON.stringify({canvases:__canvases,subs:subs.length,sprites:es.length,mine:es.filter(e=>e.cv).length,
|
return JSON.stringify({canvases:__canvases,subs:subs.length,sprites:es.length,mine:es.filter(e=>e.cv).length,
|
||||||
allUp:es.every(e=>e.up),rect:subs.every(c=>c.length===10&&c[9]===GLS_PAD.cv&&c[5]>0&&c[6]>0),
|
allUp:es.every(e=>e.up),rect:subs.every(c=>c.length===10&&c[9]===GLS_PAD.cv&&c[5]>0&&c[6]>0),
|
||||||
fit:subs.every(c=>c[5]<=GLS_PAD.cv.width&&c[6]<=GLS_PAD.cv.height),keys:es.length?[...SPRS.keys()].every(k=>k[0]==='g'):false});})()`));
|
fit:subs.every(c=>c[5]<=GLS_PAD.cv.width&&c[6]<=GLS_PAD.cv.height),keys:es.length>0&&SPRC.size===0});})()`));
|
||||||
assert.ok(r.sprites > 50, `a zoom paints many pictures (${r.sprites})`);
|
assert.ok(r.sprites > 50, `a zoom paints many pictures (${r.sprites})`);
|
||||||
assert.ok(r.canvases <= 1, `one canvas for all of them, not one each (${r.canvases} made)`);
|
assert.ok(r.canvases <= 1, `one canvas for all of them, not one each (${r.canvases} made)`);
|
||||||
assert.equal(r.mine, 0, 'no picture keeps a canvas of its own');
|
assert.equal(r.mine, 0, 'no picture keeps a canvas of its own');
|
||||||
|
|
@ -53,7 +53,7 @@ test('a full atlas starts again and the picture that did not fit goes into the n
|
||||||
const r = JSON.parse(ev(`(()=>{GLS.frame=true;GLS.n=0;ctx=GLX.fx;GLS.shy=GLS.size-4;GLS.shx=GLS.size-2;GLS.shh=4;
|
const r = JSON.parse(ev(`(()=>{GLS.frame=true;GLS.n=0;ctx=GLX.fx;GLS.shy=GLS.size-4;GLS.shx=GLS.size-2;GLS.shh=4;
|
||||||
const before=SPRS.size;let draws=0;const gd=glsDraw;glsDraw=function(){draws++;};
|
const before=SPRS.size;let draws=0;const gd=glsDraw;glsDraw=function(){draws++;};
|
||||||
const e=spriteEntry('big',40,40,20,20,g=>{g.fillRect(0,0,40,40);});blit(e,10,10,40,40);
|
const e=spriteEntry('big',40,40,20,20,g=>{g.fillRect(0,0,40,40);});blit(e,10,10,40,40);
|
||||||
glsDraw=gd;const r={up:e.up,u0:e.u0,v0:e.v0,kept:SPRS.get('g'+DPR+'|big')===e,size:SPRS.size,n:GLS.n,cv:!!e.cv,shy:GLS.shy};GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify(r);})()`));
|
glsDraw=gd;const r={up:e.up,u0:e.u0,v0:e.v0,kept:SPRS.get('big')===e,size:SPRS.size,n:GLS.n,cv:!!e.cv,shy:GLS.shy};GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify(r);})()`));
|
||||||
assert.equal(r.up, true, 'the picture is in the atlas');
|
assert.equal(r.up, true, 'the picture is in the atlas');
|
||||||
assert.equal(r.u0, 0); assert.equal(r.v0, 0);
|
assert.equal(r.u0, 0); assert.equal(r.v0, 0);
|
||||||
assert.equal(r.kept, true, 'and kept under its key after the restart');
|
assert.equal(r.kept, true, 'and kept under its key after the restart');
|
||||||
|
|
@ -66,7 +66,7 @@ test('a full atlas starts again and the picture that did not fit goes into the n
|
||||||
test('the 2D map keeps a canvas per picture, and its pictures are not the card’s', () => {
|
test('the 2D map keeps a canvas per picture, and its pictures are not the card’s', () => {
|
||||||
const { ev, errors } = open();
|
const { ev, errors } = open();
|
||||||
ev(`mapGl(false);const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=1.4;draw(1000);`);
|
ev(`mapGl(false);const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=1.4;draw(1000);`);
|
||||||
const r = JSON.parse(ev(`(()=>{const es=[...SPRS.entries()];return JSON.stringify({n:es.length,cv:es.every(([k,e])=>k[0]==='c'&&e.cv&&e.cv.width===e.pw&&e.cv.height===e.ph),up:es.some(([,e])=>e.up)});})()`));
|
const r = JSON.parse(ev(`(()=>{const es=[...SPRC.entries()];return JSON.stringify({n:es.length,cv:SPRS.size===0&&es.every(([k,e])=>e.cv&&e.cv.width===e.pw&&e.cv.height===e.ph),up:es.some(([,e])=>e.up)});})()`));
|
||||||
assert.ok(r.n > 10);
|
assert.ok(r.n > 10);
|
||||||
assert.equal(r.cv, true, 'each with its own canvas, at its size');
|
assert.equal(r.cv, true, 'each with its own canvas, at its size');
|
||||||
assert.equal(r.up, false, 'nothing went to a card');
|
assert.equal(r.up, false, 'nothing went to a card');
|
||||||
|
|
@ -82,8 +82,8 @@ test('giving the card up drops its pictures; the 2D map paints its own', () => {
|
||||||
assert.equal(ev('SPRS.size'), 0, 'no picture without a canvas is left behind');
|
assert.equal(ev('SPRS.size'), 0, 'no picture without a canvas is left behind');
|
||||||
assert.equal(ev('GLS.shx+GLS.shy+GLS.shh'), 0, 'the atlas is forgotten');
|
assert.equal(ev('GLS.shx+GLS.shy+GLS.shh'), 0, 'the atlas is forgotten');
|
||||||
ev('draw(1016)');
|
ev('draw(1016)');
|
||||||
assert.ok(ev('SPRS.size') > 10);
|
assert.ok(ev('SPRC.size') > 10);
|
||||||
assert.equal(ev(`[...SPRS.values()].every(e=>e.cv)`), true, 'the 2D map’s pictures have their canvas');
|
assert.equal(ev(`[...SPRC.values()].every(e=>e.cv)`), true, 'the 2D map’s pictures have their canvas');
|
||||||
assert.ok(ev('ctx===CTX0'));
|
assert.ok(ev('ctx===CTX0'));
|
||||||
assert.deepEqual(errors, []);
|
assert.deepEqual(errors, []);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ const STUB = `(()=>{const calls=[];const gl=new Proxy({calls},{get(t,k){if(k in
|
||||||
// One POP's city pictures painted during frames from zoom z0 to z1 (the clock moving 16 ms a frame), by scale.
|
// One POP's city pictures painted during frames from zoom z0 to z1 (the clock moving 16 ms a frame), by scale.
|
||||||
const ZOOM = (z0, z1, n) => `(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const seen=new Set(),bad=[];
|
const ZOOM = (z0, z1, n) => `(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const seen=new Set(),bad=[];
|
||||||
for(let i=0;i<=${n};i++){cam.z=${z0}+(${z1}-${z0})*i/${n};__T+=16;draw(__T);
|
for(let i=0;i<=${n};i++){cam.z=${z0}+(${z1}-${z0})*i/${n};__T+=16;draw(__T);
|
||||||
for(const k of SPRS.keys()){const p=k.split('|');if(p[1]==='city'&&p[2]===id)seen.add(+p[4]);}
|
for(const k of SPRS.keys()){const p=k.split('|');if(p[0]==='city'&&p[1]===id)seen.add(+p[3]);}
|
||||||
const k=cityScale(iconScale()),kq=[...SPRS.keys()].filter(x=>x.split('|')[1]==='city'&&x.split('|')[2]===id).map(x=>+x.split('|')[4]);
|
const k=cityScale(iconScale()),kq=[...SPRS.keys()].filter(x=>x.split('|')[0]==='city'&&x.split('|')[1]===id).map(x=>+x.split('|')[3]);
|
||||||
if(kq.length&&!kq.some(q=>Math.max(q/k,k/q)<=1.25*(1+.5/24/0.7)))bad.push(i);}
|
if(kq.length&&!kq.some(q=>Math.max(q/k,k/q)<=1.25*(1+.5/24/0.7)))bad.push(i);}
|
||||||
return JSON.stringify({steps:[...seen].sort(),bad});})()`;
|
return JSON.stringify({steps:[...seen].sort(),bad});})()`;
|
||||||
|
|
||||||
|
|
@ -45,7 +45,7 @@ test('on the card a zoom keeps each city’s picture for a while, and paints it
|
||||||
assert.equal(ev('zoomMoving()'), true);
|
assert.equal(ev('zoomMoving()'), true);
|
||||||
ev('__T+=300;draw(__T)');
|
ev('__T+=300;draw(__T)');
|
||||||
const rest = JSON.parse(ev(`(()=>{const id=S.home,want=Math.round(cityScale(iconScale())*24)/24;
|
const rest = JSON.parse(ev(`(()=>{const id=S.home,want=Math.round(cityScale(iconScale())*24)/24;
|
||||||
return JSON.stringify({f:ZH.f,want,has:[...SPRS.keys()].some(k=>{const p=k.split('|');return p[1]==='city'&&p[2]===id&&+p[4]===want;}),moving:zoomMoving()});})()`));
|
return JSON.stringify({f:ZH.f,want,has:[...SPRS.keys()].some(k=>{const p=k.split('|');return p[0]==='city'&&p[1]===id&&+p[3]===want;}),moving:zoomMoving()});})()`));
|
||||||
assert.equal(rest.moving, false);
|
assert.equal(rest.moving, false);
|
||||||
assert.equal(rest.f, 1, 'at rest nothing is stretched');
|
assert.equal(rest.f, 1, 'at rest nothing is stretched');
|
||||||
assert.equal(rest.has, true, 'the city is painted again at its own step');
|
assert.equal(rest.has, true, 'the city is painted again at its own step');
|
||||||
|
|
@ -56,8 +56,8 @@ test('the 2D map paints cities at their own step on every frame of a zoom, as be
|
||||||
const { ev, errors } = open();
|
const { ev, errors } = open();
|
||||||
ev(`mapGl(false);cam.z=0.8;draw(__T);`);
|
ev(`mapGl(false);cam.z=0.8;draw(__T);`);
|
||||||
const r = JSON.parse(ev(`(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const bad=[];
|
const r = JSON.parse(ev(`(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const bad=[];
|
||||||
for(let i=0;i<=30;i++){cam.z=0.8+i/30;__T+=16;SPRS.clear();draw(__T);const want=Math.round(cityScale(iconScale())*24)/24;
|
for(let i=0;i<=30;i++){cam.z=0.8+i/30;__T+=16;SPRC.clear();draw(__T);const want=Math.round(cityScale(iconScale())*24)/24;
|
||||||
if(ZH.f!==1||![...SPRS.keys()].some(k=>{const p=k.split('|');return p[1]==='city'&&p[2]===id&&+p[4]===want;}))bad.push(i);}
|
if(ZH.f!==1||![...SPRC.keys()].some(k=>{const p=k.split('|');return p[0]==='city'&&p[1]===id&&+p[3]===want;}))bad.push(i);}
|
||||||
return JSON.stringify({bad});})()`));
|
return JSON.stringify({bad});})()`));
|
||||||
assert.deepEqual(r.bad, []);
|
assert.deepEqual(r.bad, []);
|
||||||
assert.deepEqual(errors, []);
|
assert.deepEqual(errors, []);
|
||||||
|
|
@ -83,7 +83,7 @@ test('pictures that keep their size whatever the zoom (far-view cities, cones, d
|
||||||
ev(STUB);
|
ev(STUB);
|
||||||
const r = JSON.parse(ev(`(()=>{cam.z=1;draw(__T);__T+=1000;cam.z=1;draw(__T);cam.z=1.4;__T+=16;draw(__T);GLS.frame=true;GLS.n=0;ctx=GLX.fx;
|
const r = JSON.parse(ev(`(()=>{cam.z=1;draw(__T);__T+=1000;cam.z=1;draw(__T);cam.z=1.4;__T+=16;draw(__T);GLS.frame=true;GLS.n=0;ctx=GLX.fx;
|
||||||
const f=ZH.f;drawCone(100,100,0);drawBackhoe(200,100,0,false);const D=GLS.data,o=[D[2],D[3],D[SPR_STRIDE+2],D[SPR_STRIDE+3]];
|
const f=ZH.f;drawCone(100,100,0);drawBackhoe(200,100,0,false);const D=GLS.data,o=[D[2],D[3],D[SPR_STRIDE+2],D[SPR_STRIDE+3]];
|
||||||
const keys=[...SPRS.keys()].filter(k=>['cone','hoe'].includes(k.split('|')[1]));GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify({f,o,keys});})()`));
|
const keys=[...SPRS.keys()].filter(k=>['cone','hoe'].includes(k.split('|')[0]));GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify({f,o,keys});})()`));
|
||||||
assert.notEqual(r.f, 1, 'the zoom is moving and city pictures are held');
|
assert.notEqual(r.f, 1, 'the zoom is moving and city pictures are held');
|
||||||
assert.deepEqual(r.o, [26, 28, 38, 32], 'drawn at their own size');
|
assert.deepEqual(r.o, [26, 28, 38, 32], 'drawn at their own size');
|
||||||
assert.ok(r.keys.length >= 2 && r.keys.every(k => k.endsWith('|1')), 'painted at scale 1: ' + r.keys);
|
assert.ok(r.keys.length >= 2 && r.keys.every(k => k.endsWith('|1')), 'painted at scale 1: ' + r.keys);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue