WebGL map: keep every sprite, the attack ring, an empty title map and a 2D fallback

- A sprite or disc drawn as the queue grows was written into the old array and
  lost: the record is now taken before the array is read.
- A city's picture without an attack shared its cache key with step 0 of a
  DDoS ring (always, with animations off): no attack is now phase -1, and
  cityStatic() reads only what the key holds.
- Back at the title (or on a damaged save), drawFrame() returned before hiding
  the card's layers, so the last network stayed behind the title: the layers
  are now shown or hidden on entry.
- An error from the card (frame start, tube upload, atlas, sprite flush) now
  gives WebGL up and draws the same frame in 2D, once; the 2D context, the
  sprite queue and GLS.frame are reset whatever fails. Other errors still
  reach tickErr() and leave WebGL on.
- Selecting a tube no longer rebuilds and uploads every tube, packet and
  point (about 230 KB on the big-network fixture): its halo is its own small
  buffer.
This commit is contained in:
Antoine 2026-11-04 14:09:22 +01:00
parent cf9ab5e774
commit 8491b1a8f6
No known key found for this signature in database
GPG key ID: 728FF1CE15732B05
7 changed files with 170 additions and 37 deletions

View file

@ -36,21 +36,22 @@ function spriteEntry(key,w,h,ax,ay,paint){key=DPR+'|'+key;let e=SPRS.get(key);if
e={cv,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1};SPRS.set(key,e);return e;}
// Puts a picture 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)return false;
const pw=e.cv.width,ph=e.cv.height,A=GLS.size;if(pw>A||ph>A)return false;
try{return glsPut(gl,e);}catch(x){throw glErr(x);}}
function glsPut(gl,e){const pw=e.cv.width,ph=e.cv.height,A=GLS.size;if(pw>A||ph>A)return false;
if(GLS.shx+pw>A){GLS.shx=0;GLS.shy+=GLS.shh;GLS.shh=0;}
if(GLS.shy+ph>A){spritesReset();return false;}
const x=GLS.shx,y=GLS.shy;GLS.shx+=pw;GLS.shh=Math.max(GLS.shh,ph);
gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,true);
gl.texSubImage2D(gl.TEXTURE_2D,0,x,y,gl.RGBA,gl.UNSIGNED_BYTE,e.cv);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,false);
Object.assign(e,{up:true,u0:x/A,v0:y/A,u1:(x+pw)/A,v1:(y+ph)/A});return true;}
// The next record in GLS.data, which grows as needed.
// The next record in GLS.data, which grows as needed: take it before reading GLS.data, which it may replace.
function glsNext(){if((GLS.n+1)*SPR_STRIDE>GLS.data.length){const nd=new Float32Array(GLS.data.length*2);nd.set(GLS.data);GLS.data=nd;}return GLS.n++*SPR_STRIDE;}
// Draws a picture at (x,y) of w×h CSS px, on the card for a frame drawn there, else with the 2D canvas.
function blit(e,x,y,w,h){if(GLS.frame&&glsEnsure(e)){const d=GLS.data,o=glsNext();
function blit(e,x,y,w,h){if(GLS.frame&&glsEnsure(e)){const o=glsNext(),d=GLS.data;
d[o]=x;d[o+1]=y;d[o+2]=w;d[o+3]=h;d[o+4]=e.u0;d[o+5]=e.v0;d[o+6]=e.u1;d[o+7]=e.v1;d[o+8]=d[o+9]=d[o+10]=0;d[o+11]=ctx.globalAlpha;d[o+12]=0;}
else ctx.drawImage(e.cv,x,y,w,h);}
// A round dot of radius r at (x,y), in colour col (CSS colour string) at opacity a, on the card or the 2D canvas.
function dot(x,y,r,col,a){if(GLS.frame){const c=glColor(col),d=GLS.data,o=glsNext();
function dot(x,y,r,col,a){if(GLS.frame){const c=glColor(col),o=glsNext(),d=GLS.data;
d[o]=x;d[o+1]=y;d[o+2]=r;d[o+3]=d[o+4]=d[o+5]=d[o+6]=d[o+7]=0;d[o+8]=c[0];d[o+9]=c[1];d[o+10]=c[2];d[o+11]=(a===undefined?ctx.globalAlpha:a)*c[3];d[o+12]=1;}
else{const sa=ctx.globalAlpha;if(a!==undefined)ctx.globalAlpha=a;ctx.fillStyle=col;ctx.beginPath();ctx.arc(x,y,r,0,7);ctx.fill();ctx.globalAlpha=sa;}}
// A small picture w×h px at scale k with its anchor at (ax,ay), painted once by paint(g) with the origin on the anchor
@ -65,12 +66,13 @@ function textStamp(t,font,px,tw,fill,stroke,lw,x,y){const W=Math.ceil(tw+lw+4),H
if(lw){g.lineWidth=lw;g.lineJoin='round';g.strokeStyle=stroke;g.strokeText(t,0,0);}g.fillStyle=fill;g.fillText(t,0,0);});
blit(e,Math.round((x-e.w/2)*DPR)/DPR,Math.round((y-e.h/2)*DPR)/DPR,e.w,e.h);}
// Sends the queued pictures to the card in one draw.
function glsDrawNow(){const gl=GLX.gl;if(!gl||!GLS.prog||!GLS.n){GLS.n=0;return;}
gl.useProgram(GLS.prog.p);gl.bindVertexArray(GLS.vao);gl.bindBuffer(gl.ARRAY_BUFFER,GLS.buf);
gl.bufferData(gl.ARRAY_BUFFER,GLS.data.subarray(0,GLS.n*SPR_STRIDE),gl.STREAM_DRAW);
function glsDrawNow(){const gl=GLX.gl,n=GLS.n;GLS.n=0;if(!gl||!GLS.prog||!n)return;
try{glsDraw(gl,n);}catch(x){throw glErr(x);}}
function glsDraw(gl,n){gl.useProgram(GLS.prog.p);gl.bindVertexArray(GLS.vao);gl.bindBuffer(gl.ARRAY_BUFFER,GLS.buf);
gl.bufferData(gl.ARRAY_BUFFER,GLS.data.subarray(0,n*SPR_STRIDE),gl.STREAM_DRAW);
gl.uniform2f(GLS.prog.u.uDev,GLX.c.width,GLX.c.height);gl.uniform1f(GLS.prog.u.uDpr,DPR);gl.uniform1i(GLS.prog.u.uTex,0);
gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);
gl.enable(gl.BLEND);gl.blendFunc(gl.ONE,gl.ONE_MINUS_SRC_ALPHA);gl.disable(gl.DEPTH_TEST);
gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,GLS.n);gl.bindVertexArray(null);GLS.n=0;}
// The end of a frame on the card: the pictures go up over the tubes.
function glsFrameEnd(){if(GLS.frame){glsDrawNow();GLS.frame=false;}}
gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,n);gl.bindVertexArray(null);}
// The end of a frame on the card: the pictures go up over the tubes. The frame is over even if that draw fails.
function glsFrameEnd(){if(!GLS.frame)return;GLS.frame=false;glsDrawNow();}