@al8b/screen 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/drawing/sprite-screen.ts","../../src/drawing/primitives-screen.ts","../../src/core/base-screen.ts","../../src/tri/ttri.ts","../../src/drawing/text-screen.ts"],"sourcesContent":["/**\n * Sprite rendering for Screen class\n */\n\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\nimport type { TileMap as Map } from \"@al8b/map\";\nimport type { Sprite } from \"@al8b/sprites\";\n\nimport { PrimitiveScreen } from \"./primitives-screen\";\n\nexport class SpriteScreen extends PrimitiveScreen {\n\t// Cache imageSmoothingEnabled — only set when pixelated flag changes\n\tprivate _lastImageSmoothing = true;\n\n\t// Cache frame time once per draw frame instead of per-sprite\n\tprotected _frameTime = 0;\n\n\t/**\n\t * Initialize draw state (called before each draw frame)\n\t */\n\toverride initDraw(): void {\n\t\tsuper.initDraw();\n\t\tthis._frameTime = performance.now();\n\t}\n\n\t/**\n\t * Set imageSmoothingEnabled only when it actually changes\n\t */\n\tprotected setImageSmoothing(): void {\n\t\tconst smooth = !this.pixelated;\n\t\tif (smooth !== this._lastImageSmoothing) {\n\t\t\tthis.context.imageSmoothingEnabled = smooth;\n\t\t\tthis._lastImageSmoothing = smooth;\n\t\t}\n\t}\n\n\t/**\n\t * Get the canvas for the current sprite frame\n\t */\n\tprotected getSpriteFrame(sprite: Sprite | string | any): HTMLCanvasElement | null {\n\t\tlet frame: number | null = null;\n\n\t\t// Handle string sprite name\n\t\tif (typeof sprite === \"string\") {\n\t\t\tconst spriteName = sprite;\n\t\t\tlet spriteObj: Sprite | null = null;\n\n\t\t\tif (this.runtime && this.runtime.sprites) {\n\t\t\t\tspriteObj = this.runtime.sprites[sprite];\n\t\t\t}\n\n\t\t\t// Handle \"sprite.frame\" format (e.g., \"player.0\")\n\t\t\tif (!spriteObj) {\n\t\t\t\tconst parts = sprite.split(\".\");\n\t\t\t\tif (parts.length > 1 && this.runtime && this.runtime.sprites) {\n\t\t\t\t\tspriteObj = this.runtime.sprites[parts[0]];\n\t\t\t\t\tframe = Number.parseInt(parts[1]) || 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Report sprite not found error\n\t\t\tif (!spriteObj) {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7004, { spriteName });\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tsprite = spriteObj;\n\t\t}\n\t\t// Handle Image instances - check for objects with canvas but no frames\n\t\telse if (sprite && typeof sprite === \"object\" && (sprite as any).canvas && !(sprite as any).frames) {\n\t\t\treturn (sprite as any).canvas || (sprite as any).image || null;\n\t\t}\n\n\t\t// Validate sprite object\n\t\tif (!sprite || !(sprite as Sprite).ready) {\n\t\t\tconst spriteName = typeof sprite === \"string\" ? sprite : \"unknown\";\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7005, { spriteName });\n\t\t\treturn null;\n\t\t}\n\n\t\tconst spriteObj = sprite as Sprite;\n\n\t\t// Handle multi-frame sprites — use cached _frameTime instead of Date.now() per call\n\t\tif (spriteObj.frames && spriteObj.frames.length > 1) {\n\t\t\tif (frame === null) {\n\t\t\t\tif (spriteObj.animation_start === 0) {\n\t\t\t\t\tspriteObj.animation_start = this._frameTime;\n\t\t\t\t}\n\t\t\t\tconst dt = 1000 / spriteObj.fps;\n\t\t\t\tframe = Math.floor((this._frameTime - spriteObj.animation_start) / dt) % spriteObj.frames.length;\n\t\t\t}\n\t\t\tif (frame >= 0 && frame < spriteObj.frames.length) {\n\t\t\t\treturn spriteObj.frames[frame].canvas;\n\t\t\t}\n\t\t\treturn spriteObj.frames[0].canvas;\n\t\t}\n\t\t// Handle single-frame sprites\n\t\telse if (spriteObj.frames && spriteObj.frames[0]) {\n\t\t\treturn spriteObj.frames[0].canvas;\n\t\t}\n\n\t\treturn null;\n\t}\n\n\t/**\n\t * Draw a sprite\n\t */\n\tdrawSprite(sprite: Sprite | string | any, x: number, y: number, w?: number, h?: number): void {\n\t\tconst canvas = this.getSpriteFrame(sprite);\n\t\tif (!canvas) return;\n\n\t\tif (w == null) {\n\t\t\tw = canvas.width;\n\t\t}\n\n\t\tif (!h) {\n\t\t\th = (w / canvas.width) * canvas.height;\n\t\t}\n\n\t\t// Viewport culling: skip sprites entirely outside visible area\n\t\t// Only when no screen/object transforms are active (simple AABB check)\n\t\tif (\n\t\t\t!this.screen_transform &&\n\t\t\tthis.object_rotation === 0 &&\n\t\t\tthis.object_scale_x === 1 &&\n\t\t\tthis.object_scale_y === 1\n\t\t) {\n\t\t\tconst drawX = x - w / 2 - (this.anchor_x * w) / 2;\n\t\t\tconst drawY = -y - h / 2 + (this.anchor_y * h) / 2;\n\t\t\tconst halfW = this.width / 2;\n\t\t\tconst halfH = this.height / 2;\n\t\t\tif (drawX > halfW || drawX + w < -halfW || drawY > halfH || drawY + h < -halfH) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.setImageSmoothing();\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.drawImage(canvas, -w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.drawImage(canvas, x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n\n\t/**\n\t * Draw a portion of a sprite\n\t */\n\tdrawSpritePart(\n\t\tsprite: Sprite | string | any,\n\t\tsx: number,\n\t\tsy: number,\n\t\tsw: number,\n\t\tsh: number,\n\t\tx: number,\n\t\ty: number,\n\t\tw?: number,\n\t\th?: number,\n\t): void {\n\t\tconst canvas = this.getSpriteFrame(sprite);\n\t\tif (!canvas) return;\n\n\t\tif (w == null) {\n\t\t\tw = sw;\n\t\t}\n\n\t\tif (!h) {\n\t\t\th = (w / sw) * sh;\n\t\t}\n\n\t\t// Viewport culling: skip sprites entirely outside visible area\n\t\tif (\n\t\t\t!this.screen_transform &&\n\t\t\tthis.object_rotation === 0 &&\n\t\t\tthis.object_scale_x === 1 &&\n\t\t\tthis.object_scale_y === 1\n\t\t) {\n\t\t\tconst drawX = x - w / 2 - (this.anchor_x * w) / 2;\n\t\t\tconst drawY = -y - h / 2 + (this.anchor_y * h) / 2;\n\t\t\tconst halfW = this.width / 2;\n\t\t\tconst halfH = this.height / 2;\n\t\t\tif (drawX > halfW || drawX + w < -halfW || drawY > halfH || drawY + h < -halfH) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.setImageSmoothing();\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.drawImage(\n\t\t\t\tcanvas,\n\t\t\t\tsx,\n\t\t\t\tsy,\n\t\t\t\tsw,\n\t\t\t\tsh,\n\t\t\t\t-w / 2 - (this.anchor_x * w) / 2,\n\t\t\t\t-h / 2 + (this.anchor_y * h) / 2,\n\t\t\t\tw,\n\t\t\t\th,\n\t\t\t);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.drawImage(\n\t\t\t\tcanvas,\n\t\t\t\tsx,\n\t\t\t\tsy,\n\t\t\t\tsw,\n\t\t\t\tsh,\n\t\t\t\tx - w / 2 - (this.anchor_x * w) / 2,\n\t\t\t\t-y - h / 2 + (this.anchor_y * h) / 2,\n\t\t\t\tw,\n\t\t\t\th,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Draw a map\n\t */\n\tdrawMap(map: Map | string, x: number, y: number, w: number, h: number): void {\n\t\tlet mapObj: Map | null = null;\n\n\t\tif (typeof map === \"string\") {\n\t\t\tif (this.runtime && this.runtime.maps) {\n\t\t\t\tmapObj = this.runtime.maps[map];\n\t\t\t}\n\t\t} else {\n\t\t\tmapObj = map;\n\t\t}\n\n\t\tif (!(mapObj && mapObj.ready)) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.setImageSmoothing();\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tmapObj.draw(this.context, -w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tmapObj.draw(this.context, x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n}\n","import { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\nimport { BaseScreen } from \"../core/base-screen\";\n\n/**\n * Adds primitive shape drawing APIs on top of BaseScreen.\n */\nexport class PrimitiveScreen extends BaseScreen {\n\tfillRect(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\t// Validate drawing context\n\t\tif (!this.context) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7092, {});\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate drawing parameters\n\t\tif (!isFinite(x) || !isFinite(y) || !isFinite(w) || !isFinite(h) || w <= 0 || h <= 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7093, { error: `Invalid parameters: x=${x}, y=${y}, w=${w}, h=${h}` });\n\t\t\treturn;\n\t\t}\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.fillRect(-w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.fillRect(x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n\n\tfillRoundRect(x: number, y: number, w: number, h: number, round: number = 10, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tconst transform = this.initDrawOp(x, -y);\n\t\tconst rx = (transform ? -w / 2 : x - w / 2) - (this.anchor_x * w) / 2;\n\t\tconst ry = (transform ? -h / 2 : -y - h / 2) + (this.anchor_y * h) / 2;\n\n\t\tthis.context.beginPath();\n\t\tif (this.context.roundRect) {\n\t\t\tthis.context.roundRect(rx, ry, w, h, round);\n\t\t} else {\n\t\t\tconst r = Math.min(round, w / 2, h / 2);\n\t\t\tthis.context.moveTo(rx + r, ry);\n\t\t\tthis.context.lineTo(rx + w - r, ry);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry, rx + w, ry + r);\n\t\t\tthis.context.lineTo(rx + w, ry + h - r);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry + h, rx + w - r, ry + h);\n\t\t\tthis.context.lineTo(rx + r, ry + h);\n\t\t\tthis.context.quadraticCurveTo(rx, ry + h, rx, ry + h - r);\n\t\t\tthis.context.lineTo(rx, ry + r);\n\t\t\tthis.context.quadraticCurveTo(rx, ry, rx + r, ry);\n\t\t\tthis.context.closePath();\n\t\t}\n\t\tthis.context.fill();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tfillRound(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tw = Math.abs(w);\n\t\th = Math.abs(h);\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse((-this.anchor_x * w) / 2, (this.anchor_y * h) / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);\n\t\t\tthis.context.fill();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse(\n\t\t\t\tx - (this.anchor_x * w) / 2,\n\t\t\t\t-y + (this.anchor_y * h) / 2,\n\t\t\t\tw / 2,\n\t\t\t\th / 2,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\tMath.PI * 2,\n\t\t\t\tfalse,\n\t\t\t);\n\t\t\tthis.context.fill();\n\t\t}\n\t}\n\n\tdrawRect(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.strokeRect(-w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.strokeRect(x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n\n\tdrawRoundRect(x: number, y: number, w: number, h: number, round: number = 10, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tconst transform = this.initDrawOp(x, -y);\n\t\tconst rx = (transform ? -w / 2 : x - w / 2) - (this.anchor_x * w) / 2;\n\t\tconst ry = (transform ? -h / 2 : -y - h / 2) + (this.anchor_y * h) / 2;\n\n\t\tthis.context.beginPath();\n\t\tif (this.context.roundRect) {\n\t\t\tthis.context.roundRect(rx, ry, w, h, round);\n\t\t} else {\n\t\t\tconst r = Math.min(round, w / 2, h / 2);\n\t\t\tthis.context.moveTo(rx + r, ry);\n\t\t\tthis.context.lineTo(rx + w - r, ry);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry, rx + w, ry + r);\n\t\t\tthis.context.lineTo(rx + w, ry + h - r);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry + h, rx + w - r, ry + h);\n\t\t\tthis.context.lineTo(rx + r, ry + h);\n\t\t\tthis.context.quadraticCurveTo(rx, ry + h, rx, ry + h - r);\n\t\t\tthis.context.lineTo(rx, ry + r);\n\t\t\tthis.context.quadraticCurveTo(rx, ry, rx + r, ry);\n\t\t\tthis.context.closePath();\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawRound(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tw = Math.abs(w);\n\t\th = Math.abs(h);\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse((-this.anchor_x * w) / 2, (this.anchor_y * h) / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);\n\t\t\tthis.context.stroke();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse(\n\t\t\t\tx - (this.anchor_x * w) / 2,\n\t\t\t\t-y + (this.anchor_y * h) / 2,\n\t\t\t\tw / 2,\n\t\t\t\th / 2,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\tMath.PI * 2,\n\t\t\t\tfalse,\n\t\t\t);\n\t\t\tthis.context.stroke();\n\t\t}\n\t}\n\n\tdrawLine(x1: number, y1: number, x2: number, y2: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(x1, -y1);\n\t\tthis.context.lineTo(x2, -y2);\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawPolygon(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst len = Math.floor(points.length / 2);\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tfor (let i = 1; i < len; i++) {\n\t\t\tthis.context.lineTo(points[i * 2], -points[i * 2 + 1]);\n\t\t}\n\t\tthis.context.closePath();\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawPolyline(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst len = Math.floor(points.length / 2);\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tfor (let i = 1; i < len; i++) {\n\t\t\tthis.context.lineTo(points[i * 2], -points[i * 2 + 1]);\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tfillPolygon(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\n\t\tconst len = Math.floor(points.length / 2);\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tfor (let i = 1; i < len; i++) {\n\t\t\tthis.context.lineTo(points[i * 2], -points[i * 2 + 1]);\n\t\t}\n\t\tthis.context.fill();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawQuadCurve(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tlet index = 2;\n\t\twhile (index <= points.length - 4) {\n\t\t\tthis.context.quadraticCurveTo(points[index], -points[index + 1], points[index + 2], -points[index + 3]);\n\t\t\tindex += 4;\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawBezierCurve(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tlet index = 2;\n\t\twhile (index <= points.length - 6) {\n\t\t\tthis.context.bezierCurveTo(\n\t\t\t\tpoints[index],\n\t\t\t\t-points[index + 1],\n\t\t\t\tpoints[index + 2],\n\t\t\t\t-points[index + 3],\n\t\t\t\tpoints[index + 4],\n\t\t\t\t-points[index + 5],\n\t\t\t);\n\t\t\tindex += 6;\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawArc(\n\t\tx: number,\n\t\ty: number,\n\t\tradius: number,\n\t\tangle1: number,\n\t\tangle2: number,\n\t\tccw: boolean,\n\t\tcolor?: string | number,\n\t): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(0, 0, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.stroke();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(x, -y, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.stroke();\n\t\t}\n\t}\n\n\tfillArc(\n\t\tx: number,\n\t\ty: number,\n\t\tradius: number,\n\t\tangle1: number,\n\t\tangle2: number,\n\t\tccw: boolean,\n\t\tcolor?: string | number,\n\t): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(0, 0, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.fill();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(x, -y, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.fill();\n\t\t}\n\t}\n\n\tprivate extractPoints(args: any[]): {\n\t\tcolor?: string | number;\n\t\tpoints?: number[];\n\t} {\n\t\tlet color: string | number | undefined;\n\t\tlet points: number[] | undefined;\n\n\t\tif (args.length > 0 && args.length % 2 === 1 && typeof args[args.length - 1] === \"string\") {\n\t\t\tcolor = args[args.length - 1];\n\t\t\tpoints = args.slice(0, -1);\n\t\t} else if (Array.isArray(args[0])) {\n\t\t\tif (args[1] && typeof args[1] === \"string\") {\n\t\t\t\tcolor = args[1];\n\t\t\t}\n\t\t\tpoints = args[0];\n\t\t} else {\n\t\t\tpoints = args as number[];\n\t\t}\n\n\t\treturn {\n\t\t\tcolor,\n\t\t\tpoints,\n\t\t};\n\t}\n}\n","import { APIErrorCode, createDiagnostic, formatForBrowser, reportRuntimeError } from \"@al8b/diagnostics\";\nimport { ZBuffer } from \"../tri\";\nimport type { ScreenInterface, ScreenOptions } from \"../types\";\n\n/**\n * BaseScreen encapsulates canvas/context management plus shared drawing state.\n * Feature-specific behaviors (primitives, sprites, text, triangles) extend this class.\n */\nexport class BaseScreen {\n\tprotected canvas: HTMLCanvasElement;\n\tprotected context!: CanvasRenderingContext2D;\n\tprotected runtime: any;\n\n\tpublic width!: number;\n\tpublic height!: number;\n\n\t// Drawing state\n\tprotected alpha = 1;\n\tprotected pixelated = 1;\n\tprotected line_width = 1;\n\tprotected font = \"BitCell\";\n\n\t// Transformations\n\tprotected translation_x = 0;\n\tprotected translation_y = 0;\n\tprotected rotation = 0;\n\tprotected scale_x = 1;\n\tprotected scale_y = 1;\n\tprotected screen_transform = false;\n\n\t// Object transformations\n\tprotected object_rotation = 0;\n\tprotected object_scale_x = 1;\n\tprotected object_scale_y = 1;\n\tprotected anchor_x = 0;\n\tprotected anchor_y = 0;\n\n\t// Blending + font caches\n\tprotected blending: Record<string, string> = {};\n\tprotected font_load_requested: Record<string, boolean> = {};\n\tprotected font_loaded: Record<string, boolean> = {};\n\n\t// Interface cache\n\tprotected interfaceCache: ScreenInterface | null = null;\n\n\t// Cursor management\n\tprotected cursor: string = \"default\";\n\tprotected cursor_visibility: string = \"auto\";\n\tprotected last_mouse_move: number = Date.now();\n\n\t// 3D helper\n\tprotected zBuffer: ZBuffer;\n\n\tconstructor(options: ScreenOptions = {}) {\n\t\tthis.runtime = options.runtime;\n\n\t\tif (options.canvas) {\n\t\t\tthis.canvas = options.canvas;\n\t\t\tif (this.canvas.width === 0 || this.canvas.height === 0) {\n\t\t\t\tthis.canvas.width = options.width || 1080;\n\t\t\t\tthis.canvas.height = options.height || 1920;\n\t\t\t}\n\t\t} else {\n\t\t\tthis.canvas = document.createElement(\"canvas\");\n\t\t\tthis.canvas.width = options.width || 1080;\n\t\t\tthis.canvas.height = options.height || 1920;\n\t\t}\n\n\t\tthis.initContext();\n\n\t\tthis.blending = {\n\t\t\tnormal: \"source-over\",\n\t\t\tadditive: \"lighter\",\n\t\t};\n\n\t\tconst blendModes = [\n\t\t\t\"source-over\",\n\t\t\t\"source-in\",\n\t\t\t\"source-out\",\n\t\t\t\"source-atop\",\n\t\t\t\"destination-over\",\n\t\t\t\"destination-in\",\n\t\t\t\"destination-out\",\n\t\t\t\"destination-atop\",\n\t\t\t\"lighter\",\n\t\t\t\"copy\",\n\t\t\t\"xor\",\n\t\t\t\"multiply\",\n\t\t\t\"screen\",\n\t\t\t\"overlay\",\n\t\t\t\"darken\",\n\t\t\t\"lighten\",\n\t\t\t\"color-dodge\",\n\t\t\t\"color-burn\",\n\t\t\t\"hard-light\",\n\t\t\t\"soft-light\",\n\t\t\t\"difference\",\n\t\t\t\"exclusion\",\n\t\t\t\"hue\",\n\t\t\t\"saturation\",\n\t\t\t\"color\",\n\t\t\t\"luminosity\",\n\t\t];\n\n\t\tfor (const mode of blendModes) {\n\t\t\tthis.blending[mode] = mode;\n\t\t}\n\n\t\tthis.loadFont(this.font);\n\t\tthis.zBuffer = new ZBuffer(this.canvas.width, this.canvas.height);\n\n\t\tthis.cursor = \"default\";\n\n\t\tthis.canvas.addEventListener(\"mousemove\", () => {\n\t\t\tthis.last_mouse_move = Date.now();\n\t\t\tif (this.cursor !== \"default\" && this.cursor_visibility === \"auto\") {\n\t\t\t\tthis.cursor = \"default\";\n\t\t\t\tthis.canvas.style.cursor = \"default\";\n\t\t\t}\n\t\t});\n\n\t\t// When the context is lost and then restored, base transform needs to be reinstated\n\t\tthis.canvas.addEventListener(\"contextrestored\", () => {\n\t\t\tthis.initContext();\n\t\t});\n\n\t\tsetInterval(() => this.checkMouseCursor(), 1000);\n\t\tthis.cursor_visibility = \"auto\";\n\t}\n\n\tprotected initContext(): void {\n\t\tconst ctx = this.canvas.getContext(\"2d\", {\n\t\t\talpha: false,\n\t\t});\n\t\tif (!ctx) {\n\t\t\tconst diagnostic = createDiagnostic(APIErrorCode.E7001);\n\t\t\tconst formatted = formatForBrowser(diagnostic);\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7001, {});\n\n\t\t\tthrow new Error(formatted);\n\t\t}\n\n\t\tif (ctx !== this.context) {\n\t\t\tthis.context = ctx;\n\t\t} else {\n\t\t\tthis.context.restore();\n\t\t}\n\n\t\tthis.context.save();\n\t\tthis.context.translate(this.canvas.width / 2, this.canvas.height / 2);\n\n\t\t// Calculate ratio: Math.min(canvas.width/200, canvas.height/200)\n\t\tconst ratio = Math.min(this.canvas.width / 200, this.canvas.height / 200);\n\t\tthis.context.scale(ratio, ratio);\n\n\t\t// Set logical width/height\n\t\tthis.width = this.canvas.width / ratio;\n\t\tthis.height = this.canvas.height / ratio;\n\t\tthis.context.lineCap = \"round\";\n\t}\n\n\t/**\n\t * Initialize draw state (called before each draw frame)\n\t */\n\tinitDraw(): void {\n\t\tthis.alpha = 1;\n\t\tthis.line_width = 1;\n\t\t// Note: Supersampling not implemented in l8b\n\t\t// If needed, add: if (this.supersampling != this.previous_supersampling) { this.resize(); this.previous_supersampling = this.supersampling; }\n\t}\n\n\t/**\n\t * Update interface dimensions (called before each draw frame)\n\t */\n\tupdateInterface(): void {\n\t\t// Update interface cache if it exists\n\t\tif (this.interfaceCache) {\n\t\t\tthis.interfaceCache.width = this.width;\n\t\t\tthis.interfaceCache.height = this.height;\n\t\t}\n\t}\n\n\tclear(color?: string): void {\n\t\tthis.context.globalAlpha = 1;\n\t\tthis.context.globalCompositeOperation = \"source-over\";\n\t\tthis.context.fillStyle = color || \"#000\";\n\t\tthis.context.strokeStyle = color || \"#000\";\n\t\tthis.context.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);\n\t\tthis.zBuffer.clear();\n\t}\n\n\tsetColor(color: string | number): void {\n\t\tif (!color) return;\n\n\t\tif (!Number.isNaN(Number.parseInt(String(color)))) {\n\t\t\tconst num = Number.parseInt(String(color));\n\t\t\tconst r = ((Math.floor(num / 100) % 10) / 9) * 255;\n\t\t\tconst g = ((Math.floor(num / 10) % 10) / 9) * 255;\n\t\t\tconst b = ((num % 10) / 9) * 255;\n\t\t\tconst c = 0xff000000 + (r << 16) + (g << 8) + b;\n\t\t\tconst hex = \"#\" + c.toString(16).substring(2, 8);\n\t\t\tthis.context.fillStyle = hex;\n\t\t\tthis.context.strokeStyle = hex;\n\t\t} else if (typeof color === \"string\") {\n\t\t\t// Validate color format\n\t\t\tconst isValidColor =\n\t\t\t\t/^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(color) ||\n\t\t\t\t/^rgb\\(|^rgba\\(|^hsl\\(|^hsla\\(/.test(color) ||\n\t\t\t\t/^(red|green|blue|yellow|cyan|magenta|black|white|gray|grey|orange|pink|purple|brown|transparent)$/i.test(color);\n\n\t\t\tif (!isValidColor) {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7003, { color });\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.context.fillStyle = color;\n\t\t\tthis.context.strokeStyle = color;\n\t\t}\n\t}\n\n\tsetAlpha(alpha: number): void {\n\t\tthis.alpha = alpha;\n\t}\n\n\tsetPixelated(pixelated: number): void {\n\t\tthis.pixelated = pixelated;\n\t}\n\n\tsetBlending(blending: string): void {\n\t\tconst blend = this.blending[blending || \"normal\"];\n\n\t\tif (!blend) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7007, { blendMode: blending });\n\t\t\t// Fallback to normal blend mode\n\t\t\tthis.context.globalCompositeOperation = \"source-over\";\n\t\t\treturn;\n\t\t}\n\n\t\tthis.context.globalCompositeOperation = blend as GlobalCompositeOperation;\n\t}\n\n\tsetLineWidth(width: number): void {\n\t\tthis.line_width = width;\n\t}\n\n\tsetLineDash(dash: number[] | null): void {\n\t\tif (!Array.isArray(dash)) {\n\t\t\tthis.context.setLineDash([]);\n\t\t} else {\n\t\t\tthis.context.setLineDash(dash);\n\t\t}\n\t}\n\n\tsetLinearGradient(x1: number, y1: number, x2: number, y2: number, c1: string, c2: string): void {\n\t\tconst grd = this.context.createLinearGradient(x1, -y1, x2, -y2);\n\t\tgrd.addColorStop(0, c1);\n\t\tgrd.addColorStop(1, c2);\n\t\tthis.context.fillStyle = grd;\n\t\tthis.context.strokeStyle = grd;\n\t}\n\n\tsetRadialGradient(x: number, y: number, radius: number, c1: string, c2: string): void {\n\t\tconst grd = this.context.createRadialGradient(x, -y, 0, x, -y, radius);\n\t\tgrd.addColorStop(0, c1);\n\t\tgrd.addColorStop(1, c2);\n\t\tthis.context.fillStyle = grd;\n\t\tthis.context.strokeStyle = grd;\n\t}\n\n\tsetFont(font: string): void {\n\t\tthis.font = font || \"Verdana\";\n\t\tthis.loadFont(this.font);\n\t}\n\n\tloadFont(font: string = \"BitCell\"): void {\n\t\tif (this.font_load_requested[font]) {\n\t\t\treturn;\n\t\t}\n\t\tthis.font_load_requested[font] = true;\n\t\ttry {\n\t\t\tdocument.fonts?.load?.(`16pt ${font}`).catch(() => {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7006, { font });\n\t\t\t});\n\t\t} catch {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7006, { font });\n\t\t}\n\t}\n\n\tisFontReady(font: string = this.font): number {\n\t\tif (this.font_loaded[font]) {\n\t\t\treturn 1;\n\t\t}\n\n\t\ttry {\n\t\t\tconst ready = document.fonts?.check?.(`16pt ${font}`) ?? true;\n\t\t\tif (ready) {\n\t\t\t\tthis.font_loaded[font] = true;\n\t\t\t}\n\t\t\treturn ready ? 1 : 0;\n\t\t} catch {\n\t\t\treturn 1;\n\t\t}\n\t}\n\n\tsetTranslation(tx: number, ty: number): void {\n\t\tthis.translation_x = isFinite(tx) ? tx : 0;\n\t\tthis.translation_y = isFinite(ty) ? ty : 0;\n\t\tthis.updateScreenTransform();\n\t}\n\n\tsetScale(x: number, y: number): void {\n\t\tthis.scale_x = isFinite(x) && x !== 0 ? x : 1;\n\t\tthis.scale_y = isFinite(y) && y !== 0 ? y : 1;\n\t\tthis.updateScreenTransform();\n\t}\n\n\tsetRotation(rotation: number): void {\n\t\tthis.rotation = isFinite(rotation) ? rotation : 0;\n\t\tthis.updateScreenTransform();\n\t}\n\n\tprotected updateScreenTransform(): void {\n\t\tthis.screen_transform =\n\t\t\tthis.translation_x !== 0 ||\n\t\t\tthis.translation_y !== 0 ||\n\t\t\tthis.scale_x !== 1 ||\n\t\t\tthis.scale_y !== 1 ||\n\t\t\tthis.rotation !== 0;\n\t}\n\n\tsetDrawAnchor(ax: number, ay: number): void {\n\t\tthis.anchor_x = typeof ax === \"number\" ? ax : 0;\n\t\tthis.anchor_y = typeof ay === \"number\" ? ay : 0;\n\t}\n\n\tsetDrawRotation(rotation: number): void {\n\t\tthis.object_rotation = rotation;\n\t}\n\n\tsetDrawScale(x: number, y: number = x): void {\n\t\tthis.object_scale_x = x;\n\t\tthis.object_scale_y = y;\n\t}\n\n\tprotected initDrawOp(x: number, y: number, object_transform: boolean = true): boolean {\n\t\tlet res = false;\n\n\t\tif (this.screen_transform) {\n\t\t\tthis.context.save();\n\t\t\tres = true;\n\t\t\tthis.context.translate(this.translation_x, -this.translation_y);\n\t\t\tthis.context.scale(this.scale_x, this.scale_y);\n\t\t\tthis.context.rotate((-this.rotation / 180) * Math.PI);\n\t\t\tthis.context.translate(x, y);\n\t\t}\n\n\t\tif (object_transform && (this.object_rotation !== 0 || this.object_scale_x !== 1 || this.object_scale_y !== 1)) {\n\t\t\tif (!res) {\n\t\t\t\tthis.context.save();\n\t\t\t\tres = true;\n\t\t\t\tthis.context.translate(x, y);\n\t\t\t}\n\n\t\t\tif (this.object_rotation !== 0) {\n\t\t\t\tthis.context.rotate((-this.object_rotation / 180) * Math.PI);\n\t\t\t}\n\n\t\t\tif (this.object_scale_x !== 1 || this.object_scale_y !== 1) {\n\t\t\t\tthis.context.scale(this.object_scale_x, this.object_scale_y);\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tprotected closeDrawOp(): void {\n\t\tthis.context.restore();\n\t}\n\n\t/**\n\t * Check mouse cursor visibility\n\t * Auto-hides cursor after 4 seconds of inactivity\n\t */\n\tprotected checkMouseCursor(): void {\n\t\tif (Date.now() > this.last_mouse_move + 4000 && this.cursor_visibility === \"auto\") {\n\t\t\tif (this.cursor !== \"none\") {\n\t\t\t\tthis.cursor = \"none\";\n\t\t\t\tthis.canvas.style.cursor = \"none\";\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Set cursor visibility\n\t */\n\tsetCursorVisible(visible: boolean): void {\n\t\tthis.cursor_visibility = visible ? \"default\" : \"none\";\n\t\tif (visible) {\n\t\t\tthis.cursor = \"default\";\n\t\t\tthis.canvas.style.cursor = \"default\";\n\t\t} else {\n\t\t\tthis.cursor = \"none\";\n\t\t\tthis.canvas.style.cursor = \"none\";\n\t\t}\n\t}\n\n\tgetCanvas(): HTMLCanvasElement {\n\t\treturn this.canvas;\n\t}\n\n\tgetContext(): CanvasRenderingContext2D {\n\t\treturn this.context;\n\t}\n\n\tresize(width?: number, height?: number): void {\n\t\tif (width && height) {\n\t\t\t// Validate dimensions\n\t\t\tif (width <= 0 || height <= 0 || !isFinite(width) || !isFinite(height)) {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7002, { width, height });\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.canvas.width = width;\n\t\t\tthis.canvas.height = height;\n\t\t\tthis.initContext();\n\t\t\tthis.zBuffer.resize(width, height);\n\t\t\t// Update interface cache immediately after resize\n\t\t\tthis.updateInterface();\n\t\t}\n\t}\n}\n","/**\n * TTRI - Textured Triangle Rendering (Software Rasterization)\n *\n * Based on TIC-80's ttri implementation for 3D-style graphics.\n * This is NOT a 3D engine - it's pure 2D pixel manipulation using Canvas 2D API.\n *\n * How it works:\n * 1. Use getImageData() to get pixel buffer\n * 2. Rasterize triangle pixel-by-pixel (barycentric coordinates)\n * 3. Interpolate UV texture coordinates (perspective-correct with 1/z)\n * 4. Sample texture and write to pixel buffer\n * 5. Use putImageData() to update canvas\n *\n * This is software rendering like Doom, Quake software mode, and PlayStation 1.\n * No WebGL, no GPU - just CPU pixel manipulation.\n */\n\nimport type { TileMap as Map } from \"@al8b/map\";\nimport type { Sprite } from \"@al8b/sprites\";\n\nexport interface Vec2 {\n\tx: number;\n\ty: number;\n}\n\nexport interface Vec3 {\n\tx: number;\n\ty: number;\n\tz: number;\n}\n\nexport interface TexVert {\n\tx: number;\n\ty: number;\n\tu: number;\n\tv: number;\n\tz: number;\n}\n\nexport type TextureSource = \"tiles\" | \"map\" | \"screen\";\n\nexport interface TriangleData {\n\tcontext: CanvasRenderingContext2D;\n\twidth: number;\n\theight: number;\n\truntime?: any;\n\tpixelated: boolean;\n}\n\n/**\n * Z-Buffer for depth testing\n */\nexport class ZBuffer {\n\tprivate buffer: Float32Array;\n\tprivate width: number;\n\tprivate height: number;\n\n\tconstructor(width: number, height: number) {\n\t\tthis.width = width;\n\t\tthis.height = height;\n\t\tthis.buffer = new Float32Array(width * height);\n\t}\n\n\tclear(): void {\n\t\tthis.buffer.fill(0);\n\t}\n\n\tget(x: number, y: number): number {\n\t\treturn this.buffer[y * this.width + x] || 0;\n\t}\n\n\tset(x: number, y: number, z: number): void {\n\t\tthis.buffer[y * this.width + x] = z;\n\t}\n\n\tresize(width: number, height: number): void {\n\t\tif (this.width !== width || this.height !== height) {\n\t\t\tthis.width = width;\n\t\t\tthis.height = height;\n\t\t\tthis.buffer = new Float32Array(width * height);\n\t\t}\n\t}\n}\n\n/**\n * Edge function for triangle rasterization\n */\nfunction edgeFn(a: Vec2, b: Vec2, c: Vec2): number {\n\treturn (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);\n}\n\n/**\n * Get pixel from sprite/image\n */\nfunction getSpritePixel(\n\tsprite: Sprite | any,\n\tu: number,\n\tv: number,\n\truntime?: any,\n): {\n\tr: number;\n\tg: number;\n\tb: number;\n\ta: number;\n} | null {\n\t// Resolve sprite canvas from object or runtime registry\n\tlet canvas: HTMLCanvasElement | null = null;\n\n\tif (sprite && typeof sprite === \"object\" && sprite.canvas) {\n\t\tcanvas = sprite.canvas;\n\t} else if (typeof sprite === \"string\" && runtime?.sprites) {\n\t\tconst spriteObj = runtime.sprites[sprite];\n\t\tif (spriteObj?.frames?.[0]?.canvas) {\n\t\t\tcanvas = spriteObj.frames[0].canvas;\n\t\t}\n\t}\n\n\tif (!canvas) return null;\n\n\t// Apply texture coordinate wrapping (repeat mode)\n\tconst width = canvas.width;\n\tconst height = canvas.height;\n\tconst x = Math.floor(u) % width;\n\tconst y = Math.floor(v) % height;\n\tconst px = x < 0 ? x + width : x;\n\tconst py = y < 0 ? y + height : y;\n\n\t// Sample pixel color from sprite canvas\n\tconst ctx = canvas.getContext(\"2d\");\n\tif (!ctx) return null;\n\n\ttry {\n\t\tconst imageData = ctx.getImageData(px, py, 1, 1);\n\t\treturn {\n\t\t\tr: imageData.data[0],\n\t\t\tg: imageData.data[1],\n\t\t\tb: imageData.data[2],\n\t\t\ta: imageData.data[3],\n\t\t};\n\t} catch (e) {\n\t\treturn null;\n\t}\n}\n\n/**\n * Get pixel from map\n */\nfunction getMapPixel(\n\tmap: Map | any,\n\tu: number,\n\tv: number,\n\truntime?: any,\n): {\n\tr: number;\n\tg: number;\n\tb: number;\n\ta: number;\n} | null {\n\t// Get map object\n\tlet mapObj: any = null;\n\n\tif (map && typeof map === \"object\" && map.getCanvas) {\n\t\tmapObj = map;\n\t} else if (typeof map === \"string\" && runtime?.maps) {\n\t\tmapObj = runtime.maps[map];\n\t}\n\n\tif (!mapObj) return null;\n\n\t// Get canvas from map\n\tconst canvas = mapObj.getCanvas ? mapObj.getCanvas() : mapObj.canvas;\n\tif (!canvas) return null;\n\n\t// Wrap texture coordinates\n\tconst width = canvas.width;\n\tconst height = canvas.height;\n\tconst x = Math.floor(u) % width;\n\tconst y = Math.floor(v) % height;\n\tconst px = x < 0 ? x + width : x;\n\tconst py = y < 0 ? y + height : y;\n\n\t// Get pixel data\n\tconst ctx = canvas.getContext(\"2d\");\n\tif (!ctx) return null;\n\n\ttry {\n\t\tconst imageData = ctx.getImageData(px, py, 1, 1);\n\t\treturn {\n\t\t\tr: imageData.data[0],\n\t\t\tg: imageData.data[1],\n\t\t\tb: imageData.data[2],\n\t\t\ta: imageData.data[3],\n\t\t};\n\t} catch (e) {\n\t\treturn null;\n\t}\n}\n\n/**\n * Draw textured triangle with perspective correction\n */\nexport function drawTexturedTriangle(\n\tdata: TriangleData,\n\tv0: TexVert,\n\tv1: TexVert,\n\tv2: TexVert,\n\ttexture: Sprite | Map | string | any,\n\ttextureSource: TextureSource = \"tiles\",\n\tzBuffer?: ZBuffer,\n\tuseDepth: boolean = false,\n): void {\n\tconst { context, width, height, runtime, pixelated } = data;\n\n\t// Get bounding box\n\tconst minX = Math.max(0, Math.floor(Math.min(v0.x, v1.x, v2.x)));\n\tconst minY = Math.max(0, Math.floor(Math.min(v0.y, v1.y, v2.y)));\n\tconst maxX = Math.min(width, Math.ceil(Math.max(v0.x, v1.x, v2.x)));\n\tconst maxY = Math.min(height, Math.ceil(Math.max(v0.y, v1.y, v2.y)));\n\n\tif (minX >= maxX || minY >= maxY) return;\n\n\t// Calculate triangle area\n\tconst area = edgeFn(v0, v1, v2);\n\tif (Math.abs(area) < 0.001) return;\n\n\t// Backface culling\n\tif (area < 0) return;\n\n\t// Prepare perspective-correct interpolation\n\tconst useZ = useDepth && v0.z > 0 && v1.z > 0 && v2.z > 0;\n\n\tlet w0 = 1,\n\t\tw1 = 1,\n\t\tw2 = 1;\n\tlet u0 = v0.u,\n\t\tu1 = v1.u,\n\t\tu2 = v2.u;\n\tlet v0v = v0.v,\n\t\tv1v = v1.v,\n\t\tv2v = v2.v;\n\n\tif (useZ) {\n\t\tw0 = 1 / v0.z;\n\t\tw1 = 1 / v1.z;\n\t\tw2 = 1 / v2.z;\n\t\tu0 *= w0;\n\t\tu1 *= w1;\n\t\tu2 *= w2;\n\t\tv0v *= w0;\n\t\tv1v *= w1;\n\t\tv2v *= w2;\n\t}\n\n\t// Get image data for fast pixel manipulation\n\tconst imageData = context.getImageData(minX, minY, maxX - minX, maxY - minY);\n\tconst pixels = imageData.data;\n\n\t// Rasterize\n\tfor (let y = minY; y < maxY; y++) {\n\t\tfor (let x = minX; x < maxX; x++) {\n\t\t\tconst p = {\n\t\t\t\tx: x + 0.5,\n\t\t\t\ty: y + 0.5,\n\t\t\t};\n\n\t\t\t// Calculate barycentric coordinates\n\t\t\tconst w0b = edgeFn(v1, v2, p);\n\t\t\tconst w1b = edgeFn(v2, v0, p);\n\t\t\tconst w2b = edgeFn(v0, v1, p);\n\n\t\t\t// Check if point is inside triangle\n\t\t\tif (w0b >= 0 && w1b >= 0 && w2b >= 0) {\n\t\t\t\t// Normalize barycentric coordinates\n\t\t\t\tconst bary0 = w0b / area;\n\t\t\t\tconst bary1 = w1b / area;\n\t\t\t\tconst bary2 = w2b / area;\n\n\t\t\t\t// Depth test\n\t\t\t\tif (useZ && zBuffer) {\n\t\t\t\t\tconst z = bary0 * v0.z + bary1 * v1.z + bary2 * v2.z;\n\t\t\t\t\tconst currentZ = zBuffer.get(x, y);\n\t\t\t\t\tif (currentZ > 0 && currentZ >= z) continue;\n\t\t\t\t\tzBuffer.set(x, y, z);\n\t\t\t\t}\n\n\t\t\t\t// Interpolate texture coordinates\n\t\t\t\tlet u: number, v: number;\n\n\t\t\t\tif (useZ) {\n\t\t\t\t\tconst w = bary0 * w0 + bary1 * w1 + bary2 * w2;\n\t\t\t\t\tu = (bary0 * u0 + bary1 * u1 + bary2 * u2) / w;\n\t\t\t\t\tv = (bary0 * v0v + bary1 * v1v + bary2 * v2v) / w;\n\t\t\t\t} else {\n\t\t\t\t\tu = bary0 * v0.u + bary1 * v1.u + bary2 * v2.u;\n\t\t\t\t\tv = bary0 * v0.v + bary1 * v1.v + bary2 * v2.v;\n\t\t\t\t}\n\n\t\t\t\t// Sample texture\n\t\t\t\tlet pixel: {\n\t\t\t\t\tr: number;\n\t\t\t\t\tg: number;\n\t\t\t\t\tb: number;\n\t\t\t\t\ta: number;\n\t\t\t\t} | null = null;\n\n\t\t\t\tif (textureSource === \"map\") {\n\t\t\t\t\tpixel = getMapPixel(texture, u, v, runtime);\n\t\t\t\t} else {\n\t\t\t\t\tpixel = getSpritePixel(texture, u, v, runtime);\n\t\t\t\t}\n\n\t\t\t\t// Draw pixel\n\t\t\t\tif (pixel && pixel.a > 0) {\n\t\t\t\t\tconst idx = ((y - minY) * (maxX - minX) + (x - minX)) * 4;\n\t\t\t\t\tpixels[idx] = pixel.r;\n\t\t\t\t\tpixels[idx + 1] = pixel.g;\n\t\t\t\t\tpixels[idx + 2] = pixel.b;\n\t\t\t\t\tpixels[idx + 3] = pixel.a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Put image data back\n\tcontext.imageSmoothingEnabled = !pixelated;\n\tcontext.putImageData(imageData, minX, minY);\n}\n\n/**\n * Draw solid color triangle\n */\nexport function drawTriangle(context: CanvasRenderingContext2D, v0: Vec2, v1: Vec2, v2: Vec2, color: string): void {\n\tcontext.fillStyle = color;\n\tcontext.beginPath();\n\tcontext.moveTo(v0.x, v0.y);\n\tcontext.lineTo(v1.x, v1.y);\n\tcontext.lineTo(v2.x, v2.y);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\n/**\n * Draw triangle outline\n */\nexport function drawTriangleOutline(\n\tcontext: CanvasRenderingContext2D,\n\tv0: Vec2,\n\tv1: Vec2,\n\tv2: Vec2,\n\tcolor: string,\n\tlineWidth: number = 1,\n): void {\n\tcontext.strokeStyle = color;\n\tcontext.lineWidth = lineWidth;\n\tcontext.beginPath();\n\tcontext.moveTo(v0.x, v0.y);\n\tcontext.lineTo(v1.x, v1.y);\n\tcontext.lineTo(v2.x, v2.y);\n\tcontext.closePath();\n\tcontext.stroke();\n}\n","import { SpriteScreen } from \"./sprite-screen\";\n\nexport class TextScreen extends SpriteScreen {\n\t// Font string cache — avoid rebuilding template string every draw call\n\tprivate _cachedFontSize = -1;\n\tprivate _cachedFontName = \"\";\n\tprivate _cachedFontString = \"\";\n\n\tprivate getFontString(size: number): string {\n\t\tif (size !== this._cachedFontSize || this.font !== this._cachedFontName) {\n\t\t\tthis._cachedFontSize = size;\n\t\t\tthis._cachedFontName = this.font;\n\t\t\tthis._cachedFontString = `${size}pt ${this.font}`;\n\t\t}\n\t\treturn this._cachedFontString;\n\t}\n\n\ttextWidth(text: string, size: number): number {\n\t\tthis.context.font = this.getFontString(size);\n\t\treturn this.context.measureText(text).width;\n\t}\n\n\tdrawText(text: string, x: number, y: number, size: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.font = this.getFontString(size);\n\t\tthis.context.textAlign = \"center\";\n\t\tthis.context.textBaseline = \"middle\";\n\t\tconst w = this.context.measureText(text).width;\n\t\tconst h = size;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.fillText(text, 0 - (this.anchor_x * w) / 2, 0 + (this.anchor_y * h) / 2);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.fillText(text, x - (this.anchor_x * w) / 2, -y + (this.anchor_y * h) / 2);\n\t\t}\n\t}\n\n\tdrawTextOutline(text: string, x: number, y: number, size: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.font = this.getFontString(size);\n\t\tthis.context.lineWidth = this.line_width;\n\t\tthis.context.textAlign = \"center\";\n\t\tthis.context.textBaseline = \"middle\";\n\t\tconst w = this.context.measureText(text).width;\n\t\tconst h = size;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.strokeText(text, 0 - (this.anchor_x * w) / 2, 0 + (this.anchor_y * h) / 2);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.strokeText(text, x - (this.anchor_x * w) / 2, -y + (this.anchor_y * h) / 2);\n\t\t}\n\t}\n}\n"],"mappings":";;;;AAIA,SAASA,gBAAAA,eAAcC,sBAAAA,2BAA0B;;;ACJjD,SAASC,gBAAAA,eAAcC,sBAAAA,2BAA0B;;;ACAjD,SAASC,cAAcC,kBAAkBC,kBAAkBC,0BAA0B;;;ACoD9E,IAAMC,UAAN,MAAMA;EApDb,OAoDaA;;;EACJC;EACAC;EACAC;EAER,YAAYD,OAAeC,QAAgB;AAC1C,SAAKD,QAAQA;AACb,SAAKC,SAASA;AACd,SAAKF,SAAS,IAAIG,aAAaF,QAAQC,MAAAA;EACxC;EAEAE,QAAc;AACb,SAAKJ,OAAOK,K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AA,GAAI;AAC3B,WAAKL,QAAQe,SAAShB,MAAM,IAAK,KAAKiB,WAAWJ,IAAK,GAAG,IAAK,KAAKK,WAAWJ,IAAK,CAAA;AACnF,WAAKK,YAAW;IACjB,OAAO;AACN,WAAKlB,QAAQe,SAAShB,MAAMK,IAAK,KAAKY,WAAWJ,IAAK,GAAG,CAACP,IAAK,KAAKY,WAAWJ,IAAK,CAAA;IACrF;EACD;EAEAM,gBAAgBpB,MAAcK,GAAWC,GAAWT,MAAcU,OAA+B;AAChG,QAAIA,MAAO,MAAKC,SAASD,KAAAA;AACzB,SAAKN,QAAQQ,cAAc,KAAKC;AAChC,SAAKT,QAAQH,OAAO,KAAKF,cAAcC,IAAAA;AACvC,SAAKI,QAAQoB,YAAY,KAAKC;AAC9B,SAAKrB,QAAQU,YAAY;AACzB,SAAKV,QAAQW,eAAe;AAC5B,UAAMC,IAAI,KAAKZ,QAAQC,YAAYF,IAAAA,EAAMG;AACzC,UAAMW,IAAIjB;AACV,QAAI,KAAKkB,WAAWV,GAAG,CAACC,CAAAA,GAAI;AAC3B,WAAKL,QAAQsB,WAAWvB,MAAM,IAAK,KAAKiB,WAAWJ,IAAK,GAAG,IAAK,KAAKK,WAAWJ,IAAK,CAAA;AACrF,WAAKK,YAAW;IACjB,OAAO;AACN,WAAKlB,QAAQsB,WAAWvB,MAAMK,IAAK,KAAKY,WAAWJ,IAAK,GAAG,CAACP,IAAK,KAAKY,WAAWJ,IAAK,CAAA;IACvF;EACD;AACD;","names":["APIErrorCode","reportRuntimeError","APIErrorCode","reportRuntimeError","APIErrorCode","createDiagnostic","formatForBrowser","reportRuntimeError","ZBuffer","buffer","width","height","Float32Array","clear","fill","get","x","y","set","z","resize","BaseScreen","canvas","context","runtime","width","height","alpha","pixelated","line_width","font","translation_x","translation_y","rotation","scale_x","scale_y","screen_transform","object_rotation","object_scale_x","object_scale_y","anchor_x","anchor_y","blending","font_load_requested","font_loaded","interfaceCache","cursor","cursor_visibility","last_mouse_move","Date","now","zBuffer","options","document","createElement","initContext","normal","additive","blendModes","mode","loadFont","ZBuffer","addEventListener","style","setInterval","checkMouseCursor","ctx","getContext","diagnostic","createDiagnostic","APIErrorCode","E7001","formatted","formatForBrowser","reportRuntimeError","listener","Error","restore","save","translate","ratio","Math","min","scale","lineCap","initDraw","updateInterface","clear","color","globalAlpha","globalCompositeOperation","fillStyle","strokeStyle","fillRect","setColor","Number","isNaN","parseInt","String","num","r","floor","g","b","c","hex","toString","substring","isValidColor","test","E7003","setAlpha","setPixelated","setBlending","blend","E7007","blendMode","setLineWidth","setLineDash","dash","Array","isArray","setLinearGradient","x1","y1","x2","y2","c1","c2","grd","createLinearGradient","addColorStop","setRadialGradient","x","y","radius","createRadialGradient","setFont","fonts","load","catch","E7006","isFontReady","ready","check","setTranslation","tx","ty","isFinite","updateScreenTransform","setScale","setRotation","setDrawAnchor","ax","ay","setDrawRotation","setDrawScale","initDrawOp","object_transform","res","rotate","PI","closeDrawOp","setCursorVisible","visible","getCanvas","resize","E7002","PrimitiveScreen","BaseScreen","fillRect","x","y","w","h","color","context","reportRuntimeError","runtime","listener","APIErrorCode","E7092","isFinite","E7093","error","setColor","globalAlpha","alpha","initDrawOp","anchor_x","anchor_y","closeDrawOp","fillRoundRect","round","transform","rx","ry","beginPath","roundRect","r","Math","min","moveTo","lineTo","quadraticCurveTo","closePath","fill","fillRound","abs","ellipse","PI","drawRect","lineWidth","line_width","strokeRect","drawRoundRect","stroke","drawRound","drawLine","x1","y1","x2","y2","drawPolygon","args","points","extractPoints","length","len","floor","i","drawPolyline","fillPolygon","drawQuadCurve","index","drawBezierCurve","bezierCurveTo","drawArc","radius","angle1","angle2","ccw","arc","fillArc","slice","Array","isArray","SpriteScreen","PrimitiveScreen","_lastImageSmoothing","_frameTime","initDraw","performance","now","setImageSmoothing","smooth","pixelated","context","imageSmoothingEnabled","getSpriteFrame","sprite","frame","spriteName","spriteObj","runtime","sprites","parts","split","length","Number","parseInt","reportRuntimeError","listener","APIErrorCode","E7004","canvas","frames","image","ready","E7005","animation_start","dt","fps","Math","floor","drawSprite","x","y","w","h","width","height","screen_transform","object_rotation","object_scale_x","object_scale_y","drawX","anchor_x","drawY","anchor_y","halfW","halfH","globalAlpha","alpha","initDrawOp","drawImage","closeDrawOp","drawSpritePart","sx","sy","sw","sh","drawMap","map","mapObj","maps","draw","TextScreen","SpriteScreen","_cachedFontSize","_cachedFontName","_cachedFontString","getFontString","size","font","textWidth","text","context","measureText","width","drawText","x","y","color","setColor","globalAlpha","alpha","textAlign","textBaseline","w","h","initDrawOp","fillText","anchor_x","anchor_y","closeDrawOp","drawTextOutline","lineWidth","line_width","strokeText"]}
1
+ {"version":3,"sources":["../../src/tri/ttri.ts","../../src/core/base-screen.ts","../../src/drawing/primitives-screen.ts","../../src/drawing/sprite-screen.ts","../../src/drawing/text-screen.ts"],"sourcesContent":["/**\n * TTRI - Textured Triangle Rendering (Software Rasterization)\n *\n * Based on TIC-80's ttri implementation for 3D-style graphics.\n * This is NOT a 3D engine - it's pure 2D pixel manipulation using Canvas 2D API.\n *\n * How it works:\n * 1. Use getImageData() to get pixel buffer\n * 2. Rasterize triangle pixel-by-pixel (barycentric coordinates)\n * 3. Interpolate UV texture coordinates (perspective-correct with 1/z)\n * 4. Sample texture and write to pixel buffer\n * 5. Use putImageData() to update canvas\n *\n * This is software rendering like Doom, Quake software mode, and PlayStation 1.\n * No WebGL, no GPU - just CPU pixel manipulation.\n */\n\nimport type { TileMap as Map } from \"@al8b/map\";\nimport type { Sprite } from \"@al8b/sprites\";\n\nexport interface Vec2 {\n\tx: number;\n\ty: number;\n}\n\nexport interface Vec3 {\n\tx: number;\n\ty: number;\n\tz: number;\n}\n\nexport interface TexVert {\n\tx: number;\n\ty: number;\n\tu: number;\n\tv: number;\n\tz: number;\n}\n\nexport type TextureSource = \"tiles\" | \"map\" | \"screen\";\n\nexport interface TriangleData {\n\tcontext: CanvasRenderingContext2D;\n\twidth: number;\n\theight: number;\n\truntime?: any;\n\tpixelated: boolean;\n}\n\n/**\n * Z-Buffer for depth testing\n */\nexport class ZBuffer {\n\tprivate buffer: Float32Array;\n\tprivate width: number;\n\tprivate height: number;\n\n\tconstructor(width: number, height: number) {\n\t\tthis.width = width;\n\t\tthis.height = height;\n\t\tthis.buffer = new Float32Array(width * height);\n\t}\n\n\tclear(): void {\n\t\tthis.buffer.fill(0);\n\t}\n\n\tget(x: number, y: number): number {\n\t\treturn this.buffer[y * this.width + x] || 0;\n\t}\n\n\tset(x: number, y: number, z: number): void {\n\t\tthis.buffer[y * this.width + x] = z;\n\t}\n\n\tresize(width: number, height: number): void {\n\t\tif (this.width !== width || this.height !== height) {\n\t\t\tthis.width = width;\n\t\t\tthis.height = height;\n\t\t\tthis.buffer = new Float32Array(width * height);\n\t\t}\n\t}\n}\n\n/**\n * Edge function for triangle rasterization\n */\nfunction edgeFn(a: Vec2, b: Vec2, c: Vec2): number {\n\treturn (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);\n}\n\n/**\n * Get pixel from sprite/image\n */\nfunction getSpritePixel(\n\tsprite: Sprite | any,\n\tu: number,\n\tv: number,\n\truntime?: any,\n): {\n\tr: number;\n\tg: number;\n\tb: number;\n\ta: number;\n} | null {\n\t// Resolve sprite canvas from object or runtime registry\n\tlet canvas: HTMLCanvasElement | null = null;\n\n\tif (sprite && typeof sprite === \"object\" && sprite.canvas) {\n\t\tcanvas = sprite.canvas;\n\t} else if (typeof sprite === \"string\" && runtime?.sprites) {\n\t\tconst spriteObj = runtime.sprites[sprite];\n\t\tif (spriteObj?.frames?.[0]?.canvas) {\n\t\t\tcanvas = spriteObj.frames[0].canvas;\n\t\t}\n\t}\n\n\tif (!canvas) return null;\n\n\t// Apply texture coordinate wrapping (repeat mode)\n\tconst width = canvas.width;\n\tconst height = canvas.height;\n\tconst x = Math.floor(u) % width;\n\tconst y = Math.floor(v) % height;\n\tconst px = x < 0 ? x + width : x;\n\tconst py = y < 0 ? y + height : y;\n\n\t// Sample pixel color from sprite canvas\n\tconst ctx = canvas.getContext(\"2d\");\n\tif (!ctx) return null;\n\n\ttry {\n\t\tconst imageData = ctx.getImageData(px, py, 1, 1);\n\t\treturn {\n\t\t\tr: imageData.data[0],\n\t\t\tg: imageData.data[1],\n\t\t\tb: imageData.data[2],\n\t\t\ta: imageData.data[3],\n\t\t};\n\t} catch (e) {\n\t\treturn null;\n\t}\n}\n\n/**\n * Get pixel from map\n */\nfunction getMapPixel(\n\tmap: Map | any,\n\tu: number,\n\tv: number,\n\truntime?: any,\n): {\n\tr: number;\n\tg: number;\n\tb: number;\n\ta: number;\n} | null {\n\t// Get map object\n\tlet mapObj: any = null;\n\n\tif (map && typeof map === \"object\" && map.getCanvas) {\n\t\tmapObj = map;\n\t} else if (typeof map === \"string\" && runtime?.maps) {\n\t\tmapObj = runtime.maps[map];\n\t}\n\n\tif (!mapObj) return null;\n\n\t// Get canvas from map\n\tconst canvas = mapObj.getCanvas ? mapObj.getCanvas() : mapObj.canvas;\n\tif (!canvas) return null;\n\n\t// Wrap texture coordinates\n\tconst width = canvas.width;\n\tconst height = canvas.height;\n\tconst x = Math.floor(u) % width;\n\tconst y = Math.floor(v) % height;\n\tconst px = x < 0 ? x + width : x;\n\tconst py = y < 0 ? y + height : y;\n\n\t// Get pixel data\n\tconst ctx = canvas.getContext(\"2d\");\n\tif (!ctx) return null;\n\n\ttry {\n\t\tconst imageData = ctx.getImageData(px, py, 1, 1);\n\t\treturn {\n\t\t\tr: imageData.data[0],\n\t\t\tg: imageData.data[1],\n\t\t\tb: imageData.data[2],\n\t\t\ta: imageData.data[3],\n\t\t};\n\t} catch (e) {\n\t\treturn null;\n\t}\n}\n\n/**\n * Draw textured triangle with perspective correction\n */\nexport function drawTexturedTriangle(\n\tdata: TriangleData,\n\tv0: TexVert,\n\tv1: TexVert,\n\tv2: TexVert,\n\ttexture: Sprite | Map | string | any,\n\ttextureSource: TextureSource = \"tiles\",\n\tzBuffer?: ZBuffer,\n\tuseDepth: boolean = false,\n): void {\n\tconst { context, width, height, runtime, pixelated } = data;\n\n\t// Get bounding box\n\tconst minX = Math.max(0, Math.floor(Math.min(v0.x, v1.x, v2.x)));\n\tconst minY = Math.max(0, Math.floor(Math.min(v0.y, v1.y, v2.y)));\n\tconst maxX = Math.min(width, Math.ceil(Math.max(v0.x, v1.x, v2.x)));\n\tconst maxY = Math.min(height, Math.ceil(Math.max(v0.y, v1.y, v2.y)));\n\n\tif (minX >= maxX || minY >= maxY) return;\n\n\t// Calculate triangle area\n\tconst area = edgeFn(v0, v1, v2);\n\tif (Math.abs(area) < 0.001) return;\n\n\t// Backface culling\n\tif (area < 0) return;\n\n\t// Prepare perspective-correct interpolation\n\tconst useZ = useDepth && v0.z > 0 && v1.z > 0 && v2.z > 0;\n\n\tlet w0 = 1,\n\t\tw1 = 1,\n\t\tw2 = 1;\n\tlet u0 = v0.u,\n\t\tu1 = v1.u,\n\t\tu2 = v2.u;\n\tlet v0v = v0.v,\n\t\tv1v = v1.v,\n\t\tv2v = v2.v;\n\n\tif (useZ) {\n\t\tw0 = 1 / v0.z;\n\t\tw1 = 1 / v1.z;\n\t\tw2 = 1 / v2.z;\n\t\tu0 *= w0;\n\t\tu1 *= w1;\n\t\tu2 *= w2;\n\t\tv0v *= w0;\n\t\tv1v *= w1;\n\t\tv2v *= w2;\n\t}\n\n\t// Get image data for fast pixel manipulation\n\tconst imageData = context.getImageData(minX, minY, maxX - minX, maxY - minY);\n\tconst pixels = imageData.data;\n\n\t// Rasterize\n\tfor (let y = minY; y < maxY; y++) {\n\t\tfor (let x = minX; x < maxX; x++) {\n\t\t\tconst p = {\n\t\t\t\tx: x + 0.5,\n\t\t\t\ty: y + 0.5,\n\t\t\t};\n\n\t\t\t// Calculate barycentric coordinates\n\t\t\tconst w0b = edgeFn(v1, v2, p);\n\t\t\tconst w1b = edgeFn(v2, v0, p);\n\t\t\tconst w2b = edgeFn(v0, v1, p);\n\n\t\t\t// Check if point is inside triangle\n\t\t\tif (w0b >= 0 && w1b >= 0 && w2b >= 0) {\n\t\t\t\t// Normalize barycentric coordinates\n\t\t\t\tconst bary0 = w0b / area;\n\t\t\t\tconst bary1 = w1b / area;\n\t\t\t\tconst bary2 = w2b / area;\n\n\t\t\t\t// Depth test\n\t\t\t\tif (useZ && zBuffer) {\n\t\t\t\t\tconst z = bary0 * v0.z + bary1 * v1.z + bary2 * v2.z;\n\t\t\t\t\tconst currentZ = zBuffer.get(x, y);\n\t\t\t\t\tif (currentZ > 0 && currentZ >= z) continue;\n\t\t\t\t\tzBuffer.set(x, y, z);\n\t\t\t\t}\n\n\t\t\t\t// Interpolate texture coordinates\n\t\t\t\tlet u: number, v: number;\n\n\t\t\t\tif (useZ) {\n\t\t\t\t\tconst w = bary0 * w0 + bary1 * w1 + bary2 * w2;\n\t\t\t\t\tu = (bary0 * u0 + bary1 * u1 + bary2 * u2) / w;\n\t\t\t\t\tv = (bary0 * v0v + bary1 * v1v + bary2 * v2v) / w;\n\t\t\t\t} else {\n\t\t\t\t\tu = bary0 * v0.u + bary1 * v1.u + bary2 * v2.u;\n\t\t\t\t\tv = bary0 * v0.v + bary1 * v1.v + bary2 * v2.v;\n\t\t\t\t}\n\n\t\t\t\t// Sample texture\n\t\t\t\tlet pixel: {\n\t\t\t\t\tr: number;\n\t\t\t\t\tg: number;\n\t\t\t\t\tb: number;\n\t\t\t\t\ta: number;\n\t\t\t\t} | null = null;\n\n\t\t\t\tif (textureSource === \"map\") {\n\t\t\t\t\tpixel = getMapPixel(texture, u, v, runtime);\n\t\t\t\t} else {\n\t\t\t\t\tpixel = getSpritePixel(texture, u, v, runtime);\n\t\t\t\t}\n\n\t\t\t\t// Draw pixel\n\t\t\t\tif (pixel && pixel.a > 0) {\n\t\t\t\t\tconst idx = ((y - minY) * (maxX - minX) + (x - minX)) * 4;\n\t\t\t\t\tpixels[idx] = pixel.r;\n\t\t\t\t\tpixels[idx + 1] = pixel.g;\n\t\t\t\t\tpixels[idx + 2] = pixel.b;\n\t\t\t\t\tpixels[idx + 3] = pixel.a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Put image data back\n\tcontext.imageSmoothingEnabled = !pixelated;\n\tcontext.putImageData(imageData, minX, minY);\n}\n\n/**\n * Draw solid color triangle\n */\nexport function drawTriangle(context: CanvasRenderingContext2D, v0: Vec2, v1: Vec2, v2: Vec2, color: string): void {\n\tcontext.fillStyle = color;\n\tcontext.beginPath();\n\tcontext.moveTo(v0.x, v0.y);\n\tcontext.lineTo(v1.x, v1.y);\n\tcontext.lineTo(v2.x, v2.y);\n\tcontext.closePath();\n\tcontext.fill();\n}\n\n/**\n * Draw triangle outline\n */\nexport function drawTriangleOutline(\n\tcontext: CanvasRenderingContext2D,\n\tv0: Vec2,\n\tv1: Vec2,\n\tv2: Vec2,\n\tcolor: string,\n\tlineWidth: number = 1,\n): void {\n\tcontext.strokeStyle = color;\n\tcontext.lineWidth = lineWidth;\n\tcontext.beginPath();\n\tcontext.moveTo(v0.x, v0.y);\n\tcontext.lineTo(v1.x, v1.y);\n\tcontext.lineTo(v2.x, v2.y);\n\tcontext.closePath();\n\tcontext.stroke();\n}\n","import { ZBuffer } from \"../tri\";\nimport type { ScreenInterface, ScreenOptions } from \"../types\";\n\n/**\n * BaseScreen encapsulates canvas/context management plus shared drawing state.\n * Feature-specific behaviors (primitives, sprites, text, triangles) extend this class.\n */\nexport class BaseScreen {\n\tprotected canvas: HTMLCanvasElement;\n\tprotected context!: CanvasRenderingContext2D;\n\tprotected runtime: any;\n\n\tpublic width!: number;\n\tpublic height!: number;\n\n\t// Drawing state\n\tprotected alpha = 1;\n\tprotected pixelated = 1;\n\tprotected line_width = 1;\n\tprotected font = \"BitCell\";\n\n\t// Transformations\n\tprotected translation_x = 0;\n\tprotected translation_y = 0;\n\tprotected rotation = 0;\n\tprotected scale_x = 1;\n\tprotected scale_y = 1;\n\tprotected screen_transform = false;\n\n\t// Object transformations\n\tprotected object_rotation = 0;\n\tprotected object_scale_x = 1;\n\tprotected object_scale_y = 1;\n\tprotected anchor_x = 0;\n\tprotected anchor_y = 0;\n\n\t// Blending + font caches\n\tprotected blending: Record<string, string> = {};\n\tprotected font_load_requested: Record<string, boolean> = {};\n\tprotected font_loaded: Record<string, boolean> = {};\n\n\t// Interface cache\n\tprotected interfaceCache: ScreenInterface | null = null;\n\n\t// Cursor management\n\tprotected cursor: string = \"default\";\n\tprotected cursor_visibility: string = \"auto\";\n\tprotected last_mouse_move: number = Date.now();\n\n\t// 3D helper\n\tprotected zBuffer: ZBuffer;\n\n\tconstructor(options: ScreenOptions = {}) {\n\t\tthis.runtime = options.runtime;\n\n\t\tif (options.canvas) {\n\t\t\tthis.canvas = options.canvas;\n\t\t\tif (this.canvas.width === 0 || this.canvas.height === 0) {\n\t\t\t\tthis.canvas.width = options.width || 1080;\n\t\t\t\tthis.canvas.height = options.height || 1920;\n\t\t\t}\n\t\t} else {\n\t\t\tthis.canvas = document.createElement(\"canvas\");\n\t\t\tthis.canvas.width = options.width || 1080;\n\t\t\tthis.canvas.height = options.height || 1920;\n\t\t}\n\n\t\tthis.initContext();\n\n\t\tthis.blending = {\n\t\t\tnormal: \"source-over\",\n\t\t\tadditive: \"lighter\",\n\t\t};\n\n\t\tconst blendModes = [\n\t\t\t\"source-over\",\n\t\t\t\"source-in\",\n\t\t\t\"source-out\",\n\t\t\t\"source-atop\",\n\t\t\t\"destination-over\",\n\t\t\t\"destination-in\",\n\t\t\t\"destination-out\",\n\t\t\t\"destination-atop\",\n\t\t\t\"lighter\",\n\t\t\t\"copy\",\n\t\t\t\"xor\",\n\t\t\t\"multiply\",\n\t\t\t\"screen\",\n\t\t\t\"overlay\",\n\t\t\t\"darken\",\n\t\t\t\"lighten\",\n\t\t\t\"color-dodge\",\n\t\t\t\"color-burn\",\n\t\t\t\"hard-light\",\n\t\t\t\"soft-light\",\n\t\t\t\"difference\",\n\t\t\t\"exclusion\",\n\t\t\t\"hue\",\n\t\t\t\"saturation\",\n\t\t\t\"color\",\n\t\t\t\"luminosity\",\n\t\t];\n\n\t\tfor (const mode of blendModes) {\n\t\t\tthis.blending[mode] = mode;\n\t\t}\n\n\t\tthis.loadFont(this.font);\n\t\tthis.zBuffer = new ZBuffer(this.canvas.width, this.canvas.height);\n\n\t\tthis.cursor = \"default\";\n\n\t\tthis.canvas.addEventListener(\"mousemove\", () => {\n\t\t\tthis.last_mouse_move = Date.now();\n\t\t\tif (this.cursor !== \"default\" && this.cursor_visibility === \"auto\") {\n\t\t\t\tthis.cursor = \"default\";\n\t\t\t\tthis.canvas.style.cursor = \"default\";\n\t\t\t}\n\t\t});\n\n\t\t// When the context is lost and then restored, base transform needs to be reinstated\n\t\tthis.canvas.addEventListener(\"contextrestored\", () => {\n\t\t\tthis.initContext();\n\t\t});\n\n\t\tsetInterval(() => this.checkMouseCursor(), 1000);\n\t\tthis.cursor_visibility = \"auto\";\n\t}\n\n\tprotected initContext(): void {\n\t\tconst ctx = this.canvas.getContext(\"2d\", {\n\t\t\talpha: false,\n\t\t});\n\t\tif (!ctx) {\n\t\t\tconst message = \"Failed to get 2D rendering context\";\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7001\", message, data: {} });\n\n\t\t\tthrow new Error(message);\n\t\t}\n\n\t\tif (ctx !== this.context) {\n\t\t\tthis.context = ctx;\n\t\t} else {\n\t\t\tthis.context.restore();\n\t\t}\n\n\t\tthis.context.save();\n\t\tthis.context.translate(this.canvas.width / 2, this.canvas.height / 2);\n\n\t\t// Calculate ratio: Math.min(canvas.width/200, canvas.height/200)\n\t\tconst ratio = Math.min(this.canvas.width / 200, this.canvas.height / 200);\n\t\tthis.context.scale(ratio, ratio);\n\n\t\t// Set logical width/height\n\t\tthis.width = this.canvas.width / ratio;\n\t\tthis.height = this.canvas.height / ratio;\n\t\tthis.context.lineCap = \"round\";\n\t}\n\n\t/**\n\t * Initialize draw state (called before each draw frame)\n\t */\n\tinitDraw(): void {\n\t\tthis.alpha = 1;\n\t\tthis.line_width = 1;\n\t\t// Note: Supersampling not implemented in l8b\n\t\t// If needed, add: if (this.supersampling != this.previous_supersampling) { this.resize(); this.previous_supersampling = this.supersampling; }\n\t}\n\n\t/**\n\t * Update interface dimensions (called before each draw frame)\n\t */\n\tupdateInterface(): void {\n\t\t// Update interface cache if it exists\n\t\tif (this.interfaceCache) {\n\t\t\tthis.interfaceCache.width = this.width;\n\t\t\tthis.interfaceCache.height = this.height;\n\t\t}\n\t}\n\n\tclear(color?: string): void {\n\t\tthis.context.globalAlpha = 1;\n\t\tthis.context.globalCompositeOperation = \"source-over\";\n\t\tthis.context.fillStyle = color || \"#000\";\n\t\tthis.context.strokeStyle = color || \"#000\";\n\t\tthis.context.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);\n\t\tthis.zBuffer.clear();\n\t}\n\n\tsetColor(color: string | number): void {\n\t\tif (!color) return;\n\n\t\tif (!Number.isNaN(Number.parseInt(String(color)))) {\n\t\t\tconst num = Number.parseInt(String(color));\n\t\t\tconst r = ((Math.floor(num / 100) % 10) / 9) * 255;\n\t\t\tconst g = ((Math.floor(num / 10) % 10) / 9) * 255;\n\t\t\tconst b = ((num % 10) / 9) * 255;\n\t\t\tconst c = 0xff000000 + (r << 16) + (g << 8) + b;\n\t\t\tconst hex = \"#\" + c.toString(16).substring(2, 8);\n\t\t\tthis.context.fillStyle = hex;\n\t\t\tthis.context.strokeStyle = hex;\n\t\t} else if (typeof color === \"string\") {\n\t\t\t// Validate color format\n\t\t\tconst isValidColor =\n\t\t\t\t/^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(color) ||\n\t\t\t\t/^rgb\\(|^rgba\\(|^hsl\\(|^hsla\\(/.test(color) ||\n\t\t\t\t/^(red|green|blue|yellow|cyan|magenta|black|white|gray|grey|orange|pink|purple|brown|transparent)$/i.test(color);\n\n\t\t\tif (!isValidColor) {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7003\", message: \"Invalid color\", data: { color } });\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.context.fillStyle = color;\n\t\t\tthis.context.strokeStyle = color;\n\t\t}\n\t}\n\n\tsetAlpha(alpha: number): void {\n\t\tthis.alpha = alpha;\n\t}\n\n\tsetPixelated(pixelated: number): void {\n\t\tthis.pixelated = pixelated;\n\t}\n\n\tsetBlending(blending: string): void {\n\t\tconst blend = this.blending[blending || \"normal\"];\n\n\t\tif (!blend) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7007\", message: \"Invalid blend mode\", data: { blendMode: blending } });\n\t\t\t// Fallback to normal blend mode\n\t\t\tthis.context.globalCompositeOperation = \"source-over\";\n\t\t\treturn;\n\t\t}\n\n\t\tthis.context.globalCompositeOperation = blend as GlobalCompositeOperation;\n\t}\n\n\tsetLineWidth(width: number): void {\n\t\tthis.line_width = width;\n\t}\n\n\tsetLineDash(dash: number[] | null): void {\n\t\tif (!Array.isArray(dash)) {\n\t\t\tthis.context.setLineDash([]);\n\t\t} else {\n\t\t\tthis.context.setLineDash(dash);\n\t\t}\n\t}\n\n\tsetLinearGradient(x1: number, y1: number, x2: number, y2: number, c1: string, c2: string): void {\n\t\tconst grd = this.context.createLinearGradient(x1, -y1, x2, -y2);\n\t\tgrd.addColorStop(0, c1);\n\t\tgrd.addColorStop(1, c2);\n\t\tthis.context.fillStyle = grd;\n\t\tthis.context.strokeStyle = grd;\n\t}\n\n\tsetRadialGradient(x: number, y: number, radius: number, c1: string, c2: string): void {\n\t\tconst grd = this.context.createRadialGradient(x, -y, 0, x, -y, radius);\n\t\tgrd.addColorStop(0, c1);\n\t\tgrd.addColorStop(1, c2);\n\t\tthis.context.fillStyle = grd;\n\t\tthis.context.strokeStyle = grd;\n\t}\n\n\tsetFont(font: string): void {\n\t\tthis.font = font || \"Verdana\";\n\t\tthis.loadFont(this.font);\n\t}\n\n\tloadFont(font: string = \"BitCell\"): void {\n\t\tif (this.font_load_requested[font]) {\n\t\t\treturn;\n\t\t}\n\t\tthis.font_load_requested[font] = true;\n\t\ttry {\n\t\t\tdocument.fonts?.load?.(`16pt ${font}`).catch(() => {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7006\", message: \"Font loading failed\", data: { font } });\n\t\t\t});\n\t\t} catch {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7006\", message: \"Font loading failed\", data: { font } });\n\t\t}\n\t}\n\n\tisFontReady(font: string = this.font): number {\n\t\tif (this.font_loaded[font]) {\n\t\t\treturn 1;\n\t\t}\n\n\t\ttry {\n\t\t\tconst ready = document.fonts?.check?.(`16pt ${font}`) ?? true;\n\t\t\tif (ready) {\n\t\t\t\tthis.font_loaded[font] = true;\n\t\t\t}\n\t\t\treturn ready ? 1 : 0;\n\t\t} catch {\n\t\t\treturn 1;\n\t\t}\n\t}\n\n\tsetTranslation(tx: number, ty: number): void {\n\t\tthis.translation_x = isFinite(tx) ? tx : 0;\n\t\tthis.translation_y = isFinite(ty) ? ty : 0;\n\t\tthis.updateScreenTransform();\n\t}\n\n\tsetScale(x: number, y: number): void {\n\t\tthis.scale_x = isFinite(x) && x !== 0 ? x : 1;\n\t\tthis.scale_y = isFinite(y) && y !== 0 ? y : 1;\n\t\tthis.updateScreenTransform();\n\t}\n\n\tsetRotation(rotation: number): void {\n\t\tthis.rotation = isFinite(rotation) ? rotation : 0;\n\t\tthis.updateScreenTransform();\n\t}\n\n\tprotected updateScreenTransform(): void {\n\t\tthis.screen_transform =\n\t\t\tthis.translation_x !== 0 ||\n\t\t\tthis.translation_y !== 0 ||\n\t\t\tthis.scale_x !== 1 ||\n\t\t\tthis.scale_y !== 1 ||\n\t\t\tthis.rotation !== 0;\n\t}\n\n\tsetDrawAnchor(ax: number, ay: number): void {\n\t\tthis.anchor_x = typeof ax === \"number\" ? ax : 0;\n\t\tthis.anchor_y = typeof ay === \"number\" ? ay : 0;\n\t}\n\n\tsetDrawRotation(rotation: number): void {\n\t\tthis.object_rotation = rotation;\n\t}\n\n\tsetDrawScale(x: number, y: number = x): void {\n\t\tthis.object_scale_x = x;\n\t\tthis.object_scale_y = y;\n\t}\n\n\tprotected initDrawOp(x: number, y: number, object_transform: boolean = true): boolean {\n\t\tlet res = false;\n\n\t\tif (this.screen_transform) {\n\t\t\tthis.context.save();\n\t\t\tres = true;\n\t\t\tthis.context.translate(this.translation_x, -this.translation_y);\n\t\t\tthis.context.scale(this.scale_x, this.scale_y);\n\t\t\tthis.context.rotate((-this.rotation / 180) * Math.PI);\n\t\t\tthis.context.translate(x, y);\n\t\t}\n\n\t\tif (object_transform && (this.object_rotation !== 0 || this.object_scale_x !== 1 || this.object_scale_y !== 1)) {\n\t\t\tif (!res) {\n\t\t\t\tthis.context.save();\n\t\t\t\tres = true;\n\t\t\t\tthis.context.translate(x, y);\n\t\t\t}\n\n\t\t\tif (this.object_rotation !== 0) {\n\t\t\t\tthis.context.rotate((-this.object_rotation / 180) * Math.PI);\n\t\t\t}\n\n\t\t\tif (this.object_scale_x !== 1 || this.object_scale_y !== 1) {\n\t\t\t\tthis.context.scale(this.object_scale_x, this.object_scale_y);\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tprotected closeDrawOp(): void {\n\t\tthis.context.restore();\n\t}\n\n\t/**\n\t * Check mouse cursor visibility\n\t * Auto-hides cursor after 4 seconds of inactivity\n\t */\n\tprotected checkMouseCursor(): void {\n\t\tif (Date.now() > this.last_mouse_move + 4000 && this.cursor_visibility === \"auto\") {\n\t\t\tif (this.cursor !== \"none\") {\n\t\t\t\tthis.cursor = \"none\";\n\t\t\t\tthis.canvas.style.cursor = \"none\";\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Set cursor visibility\n\t */\n\tsetCursorVisible(visible: boolean): void {\n\t\tthis.cursor_visibility = visible ? \"default\" : \"none\";\n\t\tif (visible) {\n\t\t\tthis.cursor = \"default\";\n\t\t\tthis.canvas.style.cursor = \"default\";\n\t\t} else {\n\t\t\tthis.cursor = \"none\";\n\t\t\tthis.canvas.style.cursor = \"none\";\n\t\t}\n\t}\n\n\tgetCanvas(): HTMLCanvasElement {\n\t\treturn this.canvas;\n\t}\n\n\tgetContext(): CanvasRenderingContext2D {\n\t\treturn this.context;\n\t}\n\n\tresize(width?: number, height?: number): void {\n\t\tif (width && height) {\n\t\t\t// Validate dimensions\n\t\t\tif (width <= 0 || height <= 0 || !isFinite(width) || !isFinite(height)) {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7002\", message: \"Invalid resize dimensions\", data: { width, height } });\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.canvas.width = width;\n\t\t\tthis.canvas.height = height;\n\t\t\tthis.initContext();\n\t\t\tthis.zBuffer.resize(width, height);\n\t\t\t// Update interface cache immediately after resize\n\t\t\tthis.updateInterface();\n\t\t}\n\t}\n}\n","import { BaseScreen } from \"../core/base-screen\";\n\n/**\n * Adds primitive shape drawing APIs on top of BaseScreen.\n */\nexport class PrimitiveScreen extends BaseScreen {\n\tfillRect(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\t// Validate drawing context\n\t\tif (!this.context) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7092\", message: \"Drawing context not available\", data: {} });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate drawing parameters\n\t\tif (!isFinite(x) || !isFinite(y) || !isFinite(w) || !isFinite(h) || w <= 0 || h <= 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7093\", message: \"Invalid draw parameters\", data: { error: `Invalid parameters: x=${x}, y=${y}, w=${w}, h=${h}` } });\n\t\t\treturn;\n\t\t}\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.fillRect(-w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.fillRect(x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n\n\tfillRoundRect(x: number, y: number, w: number, h: number, round: number = 10, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tconst transform = this.initDrawOp(x, -y);\n\t\tconst rx = (transform ? -w / 2 : x - w / 2) - (this.anchor_x * w) / 2;\n\t\tconst ry = (transform ? -h / 2 : -y - h / 2) + (this.anchor_y * h) / 2;\n\n\t\tthis.context.beginPath();\n\t\tif (this.context.roundRect) {\n\t\t\tthis.context.roundRect(rx, ry, w, h, round);\n\t\t} else {\n\t\t\tconst r = Math.min(round, w / 2, h / 2);\n\t\t\tthis.context.moveTo(rx + r, ry);\n\t\t\tthis.context.lineTo(rx + w - r, ry);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry, rx + w, ry + r);\n\t\t\tthis.context.lineTo(rx + w, ry + h - r);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry + h, rx + w - r, ry + h);\n\t\t\tthis.context.lineTo(rx + r, ry + h);\n\t\t\tthis.context.quadraticCurveTo(rx, ry + h, rx, ry + h - r);\n\t\t\tthis.context.lineTo(rx, ry + r);\n\t\t\tthis.context.quadraticCurveTo(rx, ry, rx + r, ry);\n\t\t\tthis.context.closePath();\n\t\t}\n\t\tthis.context.fill();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tfillRound(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tw = Math.abs(w);\n\t\th = Math.abs(h);\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse((-this.anchor_x * w) / 2, (this.anchor_y * h) / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);\n\t\t\tthis.context.fill();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse(\n\t\t\t\tx - (this.anchor_x * w) / 2,\n\t\t\t\t-y + (this.anchor_y * h) / 2,\n\t\t\t\tw / 2,\n\t\t\t\th / 2,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\tMath.PI * 2,\n\t\t\t\tfalse,\n\t\t\t);\n\t\t\tthis.context.fill();\n\t\t}\n\t}\n\n\tdrawRect(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.strokeRect(-w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.strokeRect(x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n\n\tdrawRoundRect(x: number, y: number, w: number, h: number, round: number = 10, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tconst transform = this.initDrawOp(x, -y);\n\t\tconst rx = (transform ? -w / 2 : x - w / 2) - (this.anchor_x * w) / 2;\n\t\tconst ry = (transform ? -h / 2 : -y - h / 2) + (this.anchor_y * h) / 2;\n\n\t\tthis.context.beginPath();\n\t\tif (this.context.roundRect) {\n\t\t\tthis.context.roundRect(rx, ry, w, h, round);\n\t\t} else {\n\t\t\tconst r = Math.min(round, w / 2, h / 2);\n\t\t\tthis.context.moveTo(rx + r, ry);\n\t\t\tthis.context.lineTo(rx + w - r, ry);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry, rx + w, ry + r);\n\t\t\tthis.context.lineTo(rx + w, ry + h - r);\n\t\t\tthis.context.quadraticCurveTo(rx + w, ry + h, rx + w - r, ry + h);\n\t\t\tthis.context.lineTo(rx + r, ry + h);\n\t\t\tthis.context.quadraticCurveTo(rx, ry + h, rx, ry + h - r);\n\t\t\tthis.context.lineTo(rx, ry + r);\n\t\t\tthis.context.quadraticCurveTo(rx, ry, rx + r, ry);\n\t\t\tthis.context.closePath();\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawRound(x: number, y: number, w: number, h: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tw = Math.abs(w);\n\t\th = Math.abs(h);\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse((-this.anchor_x * w) / 2, (this.anchor_y * h) / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);\n\t\t\tthis.context.stroke();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.ellipse(\n\t\t\t\tx - (this.anchor_x * w) / 2,\n\t\t\t\t-y + (this.anchor_y * h) / 2,\n\t\t\t\tw / 2,\n\t\t\t\th / 2,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\tMath.PI * 2,\n\t\t\t\tfalse,\n\t\t\t);\n\t\t\tthis.context.stroke();\n\t\t}\n\t}\n\n\tdrawLine(x1: number, y1: number, x2: number, y2: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(x1, -y1);\n\t\tthis.context.lineTo(x2, -y2);\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawPolygon(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst len = Math.floor(points.length / 2);\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tfor (let i = 1; i < len; i++) {\n\t\t\tthis.context.lineTo(points[i * 2], -points[i * 2 + 1]);\n\t\t}\n\t\tthis.context.closePath();\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawPolyline(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst len = Math.floor(points.length / 2);\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tfor (let i = 1; i < len; i++) {\n\t\t\tthis.context.lineTo(points[i * 2], -points[i * 2 + 1]);\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tfillPolygon(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\n\t\tconst len = Math.floor(points.length / 2);\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tfor (let i = 1; i < len; i++) {\n\t\t\tthis.context.lineTo(points[i * 2], -points[i * 2 + 1]);\n\t\t}\n\t\tthis.context.fill();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawQuadCurve(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tlet index = 2;\n\t\twhile (index <= points.length - 4) {\n\t\t\tthis.context.quadraticCurveTo(points[index], -points[index + 1], points[index + 2], -points[index + 3]);\n\t\t\tindex += 4;\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawBezierCurve(args: any[]): void {\n\t\tconst { color, points } = this.extractPoints(args);\n\t\tif (!points || points.length < 4) return;\n\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tconst transform = this.initDrawOp(0, 0, false);\n\t\tthis.context.beginPath();\n\t\tthis.context.moveTo(points[0], -points[1]);\n\t\tlet index = 2;\n\t\twhile (index <= points.length - 6) {\n\t\t\tthis.context.bezierCurveTo(\n\t\t\t\tpoints[index],\n\t\t\t\t-points[index + 1],\n\t\t\t\tpoints[index + 2],\n\t\t\t\t-points[index + 3],\n\t\t\t\tpoints[index + 4],\n\t\t\t\t-points[index + 5],\n\t\t\t);\n\t\t\tindex += 6;\n\t\t}\n\t\tthis.context.stroke();\n\t\tif (transform) this.closeDrawOp();\n\t}\n\n\tdrawArc(\n\t\tx: number,\n\t\ty: number,\n\t\tradius: number,\n\t\tangle1: number,\n\t\tangle2: number,\n\t\tccw: boolean,\n\t\tcolor?: string | number,\n\t): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.lineWidth = this.line_width;\n\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(0, 0, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.stroke();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(x, -y, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.stroke();\n\t\t}\n\t}\n\n\tfillArc(\n\t\tx: number,\n\t\ty: number,\n\t\tradius: number,\n\t\tangle1: number,\n\t\tangle2: number,\n\t\tccw: boolean,\n\t\tcolor?: string | number,\n\t): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(0, 0, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.fill();\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.beginPath();\n\t\t\tthis.context.arc(x, -y, radius, (-angle1 / 180) * Math.PI, (-angle2 / 180) * Math.PI, ccw);\n\t\t\tthis.context.fill();\n\t\t}\n\t}\n\n\tprivate extractPoints(args: any[]): {\n\t\tcolor?: string | number;\n\t\tpoints?: number[];\n\t} {\n\t\tlet color: string | number | undefined;\n\t\tlet points: number[] | undefined;\n\n\t\tif (args.length > 0 && args.length % 2 === 1 && typeof args[args.length - 1] === \"string\") {\n\t\t\tcolor = args[args.length - 1];\n\t\t\tpoints = args.slice(0, -1);\n\t\t} else if (Array.isArray(args[0])) {\n\t\t\tif (args[1] && typeof args[1] === \"string\") {\n\t\t\t\tcolor = args[1];\n\t\t\t}\n\t\t\tpoints = args[0];\n\t\t} else {\n\t\t\tpoints = args as number[];\n\t\t}\n\n\t\treturn {\n\t\t\tcolor,\n\t\t\tpoints,\n\t\t};\n\t}\n}\n","/**\n * Sprite rendering for Screen class\n */\n\nimport type { TileMap as Map } from \"@al8b/map\";\nimport type { Sprite } from \"@al8b/sprites\";\n\nimport { PrimitiveScreen } from \"./primitives-screen\";\n\nexport class SpriteScreen extends PrimitiveScreen {\n\t// Cache imageSmoothingEnabled — only set when pixelated flag changes\n\tprivate _lastImageSmoothing = true;\n\n\t// Cache frame time once per draw frame instead of per-sprite\n\tprotected _frameTime = 0;\n\n\t/**\n\t * Initialize draw state (called before each draw frame)\n\t */\n\toverride initDraw(): void {\n\t\tsuper.initDraw();\n\t\tthis._frameTime = performance.now();\n\t}\n\n\t/**\n\t * Set imageSmoothingEnabled only when it actually changes\n\t */\n\tprotected setImageSmoothing(): void {\n\t\tconst smooth = !this.pixelated;\n\t\tif (smooth !== this._lastImageSmoothing) {\n\t\t\tthis.context.imageSmoothingEnabled = smooth;\n\t\t\tthis._lastImageSmoothing = smooth;\n\t\t}\n\t}\n\n\t/**\n\t * Get the canvas for the current sprite frame\n\t */\n\tprotected getSpriteFrame(sprite: Sprite | string | any): HTMLCanvasElement | null {\n\t\tlet frame: number | null = null;\n\n\t\t// Handle string sprite name\n\t\tif (typeof sprite === \"string\") {\n\t\t\tconst spriteName = sprite;\n\t\t\tlet spriteObj: Sprite | null = null;\n\n\t\t\tif (this.runtime && this.runtime.sprites) {\n\t\t\t\tspriteObj = this.runtime.sprites[sprite];\n\t\t\t}\n\n\t\t\t// Handle \"sprite.frame\" format (e.g., \"player.0\")\n\t\t\tif (!spriteObj) {\n\t\t\t\tconst parts = sprite.split(\".\");\n\t\t\t\tif (parts.length > 1 && this.runtime && this.runtime.sprites) {\n\t\t\t\t\tspriteObj = this.runtime.sprites[parts[0]];\n\t\t\t\t\tframe = Number.parseInt(parts[1]) || 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Report sprite not found error\n\t\t\tif (!spriteObj) {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7004\", message: \"Sprite not found\", data: { spriteName } });\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tsprite = spriteObj;\n\t\t}\n\t\t// Handle Image instances - check for objects with canvas but no frames\n\t\telse if (sprite && typeof sprite === \"object\" && (sprite as any).canvas && !(sprite as any).frames) {\n\t\t\treturn (sprite as any).canvas || (sprite as any).image || null;\n\t\t}\n\n\t\t// Validate sprite object\n\t\tif (!sprite || !(sprite as Sprite).ready) {\n\t\t\tconst spriteName = typeof sprite === \"string\" ? sprite : \"unknown\";\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7005\", message: \"Sprite not ready\", data: { spriteName } });\n\t\t\treturn null;\n\t\t}\n\n\t\tconst spriteObj = sprite as Sprite;\n\n\t\t// Handle multi-frame sprites — use cached _frameTime instead of Date.now() per call\n\t\tif (spriteObj.frames && spriteObj.frames.length > 1) {\n\t\t\tif (frame === null) {\n\t\t\t\tif (spriteObj.animation_start === 0) {\n\t\t\t\t\tspriteObj.animation_start = this._frameTime;\n\t\t\t\t}\n\t\t\t\tconst dt = 1000 / spriteObj.fps;\n\t\t\t\tframe = Math.floor((this._frameTime - spriteObj.animation_start) / dt) % spriteObj.frames.length;\n\t\t\t}\n\t\t\tif (frame >= 0 && frame < spriteObj.frames.length) {\n\t\t\t\treturn spriteObj.frames[frame].canvas;\n\t\t\t}\n\t\t\treturn spriteObj.frames[0].canvas;\n\t\t}\n\t\t// Handle single-frame sprites\n\t\telse if (spriteObj.frames && spriteObj.frames[0]) {\n\t\t\treturn spriteObj.frames[0].canvas;\n\t\t}\n\n\t\treturn null;\n\t}\n\n\t/**\n\t * Draw a sprite\n\t */\n\tdrawSprite(sprite: Sprite | string | any, x: number, y: number, w?: number, h?: number): void {\n\t\tconst canvas = this.getSpriteFrame(sprite);\n\t\tif (!canvas) return;\n\n\t\tif (w == null) {\n\t\t\tw = canvas.width;\n\t\t}\n\n\t\tif (!h) {\n\t\t\th = (w / canvas.width) * canvas.height;\n\t\t}\n\n\t\t// Viewport culling: skip sprites entirely outside visible area\n\t\t// Only when no screen/object transforms are active (simple AABB check)\n\t\tif (\n\t\t\t!this.screen_transform &&\n\t\t\tthis.object_rotation === 0 &&\n\t\t\tthis.object_scale_x === 1 &&\n\t\t\tthis.object_scale_y === 1\n\t\t) {\n\t\t\tconst drawX = x - w / 2 - (this.anchor_x * w) / 2;\n\t\t\tconst drawY = -y - h / 2 + (this.anchor_y * h) / 2;\n\t\t\tconst halfW = this.width / 2;\n\t\t\tconst halfH = this.height / 2;\n\t\t\tif (drawX > halfW || drawX + w < -halfW || drawY > halfH || drawY + h < -halfH) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.setImageSmoothing();\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.drawImage(canvas, -w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.drawImage(canvas, x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n\n\t/**\n\t * Draw a portion of a sprite\n\t */\n\tdrawSpritePart(\n\t\tsprite: Sprite | string | any,\n\t\tsx: number,\n\t\tsy: number,\n\t\tsw: number,\n\t\tsh: number,\n\t\tx: number,\n\t\ty: number,\n\t\tw?: number,\n\t\th?: number,\n\t): void {\n\t\tconst canvas = this.getSpriteFrame(sprite);\n\t\tif (!canvas) return;\n\n\t\tif (w == null) {\n\t\t\tw = sw;\n\t\t}\n\n\t\tif (!h) {\n\t\t\th = (w / sw) * sh;\n\t\t}\n\n\t\t// Viewport culling: skip sprites entirely outside visible area\n\t\tif (\n\t\t\t!this.screen_transform &&\n\t\t\tthis.object_rotation === 0 &&\n\t\t\tthis.object_scale_x === 1 &&\n\t\t\tthis.object_scale_y === 1\n\t\t) {\n\t\t\tconst drawX = x - w / 2 - (this.anchor_x * w) / 2;\n\t\t\tconst drawY = -y - h / 2 + (this.anchor_y * h) / 2;\n\t\t\tconst halfW = this.width / 2;\n\t\t\tconst halfH = this.height / 2;\n\t\t\tif (drawX > halfW || drawX + w < -halfW || drawY > halfH || drawY + h < -halfH) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.setImageSmoothing();\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.drawImage(\n\t\t\t\tcanvas,\n\t\t\t\tsx,\n\t\t\t\tsy,\n\t\t\t\tsw,\n\t\t\t\tsh,\n\t\t\t\t-w / 2 - (this.anchor_x * w) / 2,\n\t\t\t\t-h / 2 + (this.anchor_y * h) / 2,\n\t\t\t\tw,\n\t\t\t\th,\n\t\t\t);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.drawImage(\n\t\t\t\tcanvas,\n\t\t\t\tsx,\n\t\t\t\tsy,\n\t\t\t\tsw,\n\t\t\t\tsh,\n\t\t\t\tx - w / 2 - (this.anchor_x * w) / 2,\n\t\t\t\t-y - h / 2 + (this.anchor_y * h) / 2,\n\t\t\t\tw,\n\t\t\t\th,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Draw a map\n\t */\n\tdrawMap(map: Map | string, x: number, y: number, w: number, h: number): void {\n\t\tlet mapObj: Map | null = null;\n\n\t\tif (typeof map === \"string\") {\n\t\t\tif (this.runtime && this.runtime.maps) {\n\t\t\t\tmapObj = this.runtime.maps[map];\n\t\t\t}\n\t\t} else {\n\t\t\tmapObj = map;\n\t\t}\n\n\t\tif (!(mapObj && mapObj.ready)) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.setImageSmoothing();\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tmapObj.draw(this.context, -w / 2 - (this.anchor_x * w) / 2, -h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tmapObj.draw(this.context, x - w / 2 - (this.anchor_x * w) / 2, -y - h / 2 + (this.anchor_y * h) / 2, w, h);\n\t\t}\n\t}\n}\n","import { SpriteScreen } from \"./sprite-screen\";\n\nexport class TextScreen extends SpriteScreen {\n\t// Font string cache — avoid rebuilding template string every draw call\n\tprivate _cachedFontSize = -1;\n\tprivate _cachedFontName = \"\";\n\tprivate _cachedFontString = \"\";\n\n\tprivate getFontString(size: number): string {\n\t\tif (size !== this._cachedFontSize || this.font !== this._cachedFontName) {\n\t\t\tthis._cachedFontSize = size;\n\t\t\tthis._cachedFontName = this.font;\n\t\t\tthis._cachedFontString = `${size}pt ${this.font}`;\n\t\t}\n\t\treturn this._cachedFontString;\n\t}\n\n\ttextWidth(text: string, size: number): number {\n\t\tthis.context.font = this.getFontString(size);\n\t\treturn this.context.measureText(text).width;\n\t}\n\n\tdrawText(text: string, x: number, y: number, size: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.font = this.getFontString(size);\n\t\tthis.context.textAlign = \"center\";\n\t\tthis.context.textBaseline = \"middle\";\n\t\tconst w = this.context.measureText(text).width;\n\t\tconst h = size;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.fillText(text, 0 - (this.anchor_x * w) / 2, 0 + (this.anchor_y * h) / 2);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.fillText(text, x - (this.anchor_x * w) / 2, -y + (this.anchor_y * h) / 2);\n\t\t}\n\t}\n\n\tdrawTextOutline(text: string, x: number, y: number, size: number, color?: string | number): void {\n\t\tif (color) this.setColor(color);\n\t\tthis.context.globalAlpha = this.alpha;\n\t\tthis.context.font = this.getFontString(size);\n\t\tthis.context.lineWidth = this.line_width;\n\t\tthis.context.textAlign = \"center\";\n\t\tthis.context.textBaseline = \"middle\";\n\t\tconst w = this.context.measureText(text).width;\n\t\tconst h = size;\n\t\tif (this.initDrawOp(x, -y)) {\n\t\t\tthis.context.strokeText(text, 0 - (this.anchor_x * w) / 2, 0 + (this.anchor_y * h) / 2);\n\t\t\tthis.closeDrawOp();\n\t\t} else {\n\t\t\tthis.context.strokeText(text, x - (this.anchor_x * w) / 2, -y + (this.anchor_y * h) / 2);\n\t\t}\n\t}\n}\n"],"mappings":";;;;AAoDO,IAAMA,UAAN,MAAMA;EApDb,OAoDaA;;;EACJC;EACAC;EACAC;EAER,YAAYD,OAAeC,QAAgB;AAC1C,SAAKD,QAAQA;AACb,SAAK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get","x","y","set","z","resize","BaseScreen","canvas","context","runtime","width","height","alpha","pixelated","line_width","font","translation_x","translation_y","rotation","scale_x","scale_y","screen_transform","object_rotation","object_scale_x","object_scale_y","anchor_x","anchor_y","blending","font_load_requested","font_loaded","interfaceCache","cursor","cursor_visibility","last_mouse_move","Date","now","zBuffer","options","document","createElement","initContext","normal","additive","blendModes","mode","loadFont","ZBuffer","addEventListener","style","setInterval","checkMouseCursor","ctx","getContext","message","listener","reportError","code","data","Error","restore","save","translate","ratio","Math","min","scale","lineCap","initDraw","updateInterface","clear","color","globalAlpha","globalCompositeOperation","fillStyle","strokeStyle","fillRect","setColor","Number","isNaN","parseInt","String","num","r","floor","g","b","c","hex","toString","substring","isValidColor","test","setAlpha","setPixelated","setBlending","blend","blendMode","setLineWidth","setLineDash","dash","Array","isArray","setLinearGradient","x1","y1","x2","y2","c1","c2","grd","createLinearGradient","addColorStop","setRadialGradient","x","y","radius","createRadialGradient","setFont","fonts","load","catch","isFontReady","ready","check","setTranslation","tx","ty","isFinite","updateScreenTransform","setScale","setRotation","setDrawAnchor","ax","ay","setDrawRotation","setDrawScale","initDrawOp","object_transform","res","rotate","PI","closeDrawOp","setCursorVisible","visible","getCanvas","resize","PrimitiveScreen","BaseScreen","fillRect","x","y","w","h","color","context","runtime","listener","reportError","code","message","data","isFinite","error","setColor","globalAlpha","alpha","initDrawOp","anchor_x","anchor_y","closeDrawOp","fillRoundRect","round","transform","rx","ry","beginPath","roundRect","r","Math","min","moveTo","lineTo","quadraticCurveTo","closePath","fill","fillRound","abs","ellipse","PI","drawRect","lineWidth","line_width","strokeRect","drawRoundRect","stroke","drawRound","drawLine","x1","y1","x2","y2","drawPolygon","args","points","extractPoints","length","len","floor","i","drawPolyline","fillPolygon","drawQuadCurve","index","drawBezierCurve","bezierCurveTo","drawArc","radius","angle1","angle2","ccw","arc","fillArc","slice","Array","isArray","SpriteScreen","PrimitiveScreen","_lastImageSmoothing","_frameTime","initDraw","performance","now","setImageSmoothing","smooth","pixelated","context","imageSmoothingEnabled","getSpriteFrame","sprite","frame","spriteName","spriteObj","runtime","sprites","parts","split","length","Number","parseInt","listener","reportError","code","message","data","canvas","frames","image","ready","animation_start","dt","fps","Math","floor","drawSprite","x","y","w","h","width","height","screen_transform","object_rotation","object_scale_x","object_scale_y","drawX","anchor_x","drawY","anchor_y","halfW","halfH","globalAlpha","alpha","initDrawOp","drawImage","closeDrawOp","drawSpritePart","sx","sy","sw","sh","drawMap","map","mapObj","maps","draw","TextScreen","SpriteScreen","_cachedFontSize","_cachedFontName","_cachedFontString","getFontString","size","font","textWidth","text","context","measureText","width","drawText","x","y","color","setColor","globalAlpha","alpha","textAlign","textBaseline","w","h","initDrawOp","fillText","anchor_x","anchor_y","closeDrawOp","drawTextOutline","lineWidth","line_width","strokeText"]}
package/dist/index.js CHANGED
@@ -27,15 +27,6 @@ __export(index_exports, {
27
27
  });
28
28
  module.exports = __toCommonJS(index_exports);
29
29
 
30
- // src/drawing/sprite-screen.ts
31
- var import_diagnostics3 = require("@al8b/diagnostics");
32
-
33
- // src/drawing/primitives-screen.ts
34
- var import_diagnostics2 = require("@al8b/diagnostics");
35
-
36
- // src/core/base-screen.ts
37
- var import_diagnostics = require("@al8b/diagnostics");
38
-
39
30
  // src/tri/ttri.ts
40
31
  var ZBuffer = class {
41
32
  static {
@@ -340,10 +331,13 @@ var BaseScreen = class {
340
331
  alpha: false
341
332
  });
342
333
  if (!ctx) {
343
- const diagnostic = (0, import_diagnostics.createDiagnostic)(import_diagnostics.APIErrorCode.E7001);
344
- const formatted = (0, import_diagnostics.formatForBrowser)(diagnostic);
345
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7001, {});
346
- throw new Error(formatted);
334
+ const message = "Failed to get 2D rendering context";
335
+ this.runtime?.listener?.reportError?.({
336
+ code: "E7001",
337
+ message,
338
+ data: {}
339
+ });
340
+ throw new Error(message);
347
341
  }
348
342
  if (ctx !== this.context) {
349
343
  this.context = ctx;
@@ -396,8 +390,12 @@ var BaseScreen = class {
396
390
  } else if (typeof color === "string") {
397
391
  const isValidColor = /^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(color) || /^rgb\(|^rgba\(|^hsl\(|^hsla\(/.test(color) || /^(red|green|blue|yellow|cyan|magenta|black|white|gray|grey|orange|pink|purple|brown|transparent)$/i.test(color);
398
392
  if (!isValidColor) {
399
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7003, {
400
- color
393
+ this.runtime?.listener?.reportError?.({
394
+ code: "E7003",
395
+ message: "Invalid color",
396
+ data: {
397
+ color
398
+ }
401
399
  });
402
400
  return;
403
401
  }
@@ -414,8 +412,12 @@ var BaseScreen = class {
414
412
  setBlending(blending) {
415
413
  const blend = this.blending[blending || "normal"];
416
414
  if (!blend) {
417
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7007, {
418
- blendMode: blending
415
+ this.runtime?.listener?.reportError?.({
416
+ code: "E7007",
417
+ message: "Invalid blend mode",
418
+ data: {
419
+ blendMode: blending
420
+ }
419
421
  });
420
422
  this.context.globalCompositeOperation = "source-over";
421
423
  return;
@@ -457,13 +459,21 @@ var BaseScreen = class {
457
459
  this.font_load_requested[font] = true;
458
460
  try {
459
461
  document.fonts?.load?.(`16pt ${font}`).catch(() => {
460
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7006, {
461
- font
462
+ this.runtime?.listener?.reportError?.({
463
+ code: "E7006",
464
+ message: "Font loading failed",
465
+ data: {
466
+ font
467
+ }
462
468
  });
463
469
  });
464
470
  } catch {
465
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7006, {
466
- font
471
+ this.runtime?.listener?.reportError?.({
472
+ code: "E7006",
473
+ message: "Font loading failed",
474
+ data: {
475
+ font
476
+ }
467
477
  });
468
478
  }
469
479
  }
@@ -571,9 +581,13 @@ var BaseScreen = class {
571
581
  resize(width, height) {
572
582
  if (width && height) {
573
583
  if (width <= 0 || height <= 0 || !isFinite(width) || !isFinite(height)) {
574
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7002, {
575
- width,
576
- height
584
+ this.runtime?.listener?.reportError?.({
585
+ code: "E7002",
586
+ message: "Invalid resize dimensions",
587
+ data: {
588
+ width,
589
+ height
590
+ }
577
591
  });
578
592
  return;
579
593
  }
@@ -593,12 +607,20 @@ var PrimitiveScreen = class extends BaseScreen {
593
607
  }
594
608
  fillRect(x, y, w, h, color) {
595
609
  if (!this.context) {
596
- (0, import_diagnostics2.reportRuntimeError)(this.runtime?.listener, import_diagnostics2.APIErrorCode.E7092, {});
610
+ this.runtime?.listener?.reportError?.({
611
+ code: "E7092",
612
+ message: "Drawing context not available",
613
+ data: {}
614
+ });
597
615
  return;
598
616
  }
599
617
  if (!isFinite(x) || !isFinite(y) || !isFinite(w) || !isFinite(h) || w <= 0 || h <= 0) {
600
- (0, import_diagnostics2.reportRuntimeError)(this.runtime?.listener, import_diagnostics2.APIErrorCode.E7093, {
601
- error: `Invalid parameters: x=${x}, y=${y}, w=${w}, h=${h}`
618
+ this.runtime?.listener?.reportError?.({
619
+ code: "E7093",
620
+ message: "Invalid draw parameters",
621
+ data: {
622
+ error: `Invalid parameters: x=${x}, y=${y}, w=${w}, h=${h}`
623
+ }
602
624
  });
603
625
  return;
604
626
  }
@@ -894,8 +916,12 @@ var SpriteScreen = class extends PrimitiveScreen {
894
916
  }
895
917
  }
896
918
  if (!spriteObj2) {
897
- (0, import_diagnostics3.reportRuntimeError)(this.runtime?.listener, import_diagnostics3.APIErrorCode.E7004, {
898
- spriteName
919
+ this.runtime?.listener?.reportError?.({
920
+ code: "E7004",
921
+ message: "Sprite not found",
922
+ data: {
923
+ spriteName
924
+ }
899
925
  });
900
926
  return null;
901
927
  }
@@ -905,8 +931,12 @@ var SpriteScreen = class extends PrimitiveScreen {
905
931
  }
906
932
  if (!sprite || !sprite.ready) {
907
933
  const spriteName = typeof sprite === "string" ? sprite : "unknown";
908
- (0, import_diagnostics3.reportRuntimeError)(this.runtime?.listener, import_diagnostics3.APIErrorCode.E7005, {
909
- spriteName
934
+ this.runtime?.listener?.reportError?.({
935
+ code: "E7005",
936
+ message: "Sprite not ready",
937
+ data: {
938
+ spriteName
939
+ }
910
940
  });
911
941
  return null;
912
942
  }