/* A frame. An error from the card itself (glErr()) gives WebGL up for this session and draws the same frame again in 2D, once; any other error goes on to the loop's tickErr(). Either way the 2D context and the sprite queue are reset. */ function draw(now){for(let pass=0;;pass++){let err=null;DFC=new Map(); try{drawFrame(now);}catch(e){err=e;} try{glsFrameEnd();}catch(e){if(!err)err=e;}finally{DFC=null;GLS.frame=false;GLS.n=0;ctx=CTX0;} if(!err)return;if(!err.glErr||pass)throw err;glKill(err);}} // Rectangles on a 64 px grid: a new one is compared only with the few that share its cells (a plain list made the // label placement quadratic in the number of cities). class RGrid{constructor(){this.m=new Map();} cells(r,f){for(let cx=Math.floor(r.x/64);cx<=Math.floor((r.x+r.w)/64);cx++)for(let cy=Math.floor(r.y/64);cy<=Math.floor((r.y+r.h)/64);cy++)f(cx*100003+cy);} push(r){this.cells(r,k=>{const a=this.m.get(k);if(a)a.push(r);else this.m.set(k,[r]);});} hits(r){let h=false;this.cells(r,k=>{if(h)return;const a=this.m.get(k);if(a)for(const q of a)if(r.x