// demo_bgi.swift —— **第 3 层：传统 BGI 图形接口**（Swift）
// demo_bgi.swift — **Layer 3: the classic BGI graphics interface** (Swift)
//
// BGI（Borland Graphics Interface）是固定分辨率 + 16 个索引色的一层：
// `initgraph` 开一块 640×480 的固定画布，`setcolor(4)` 说的是"红"而不是 RGB 0x000004，
// `closegraph` 收尾。落笔由 `Lib/shared/bgi.vml` 转成平台的 `ui_*` 图元。
// BGI (Borland Graphics Interface) is the fixed-resolution + 16 indexed colors layer:
// `initgraph` opens a fixed 640x480 canvas, `setcolor(4)` means "red" (not RGB 0x000004),
// and `closegraph` wraps up. `Lib/shared/bgi.vml` turns every call into the platform's `ui_*`.
//
// ## 前两个参数是 int 指针
// ## The first two parameters are int pointers
//
// Swift 没有取地址运算符，所以先 `vml_alloc(8)` 拿两个字的可写内存，把 `gd` / `gd + 4`
// 当指针传给库（再用 `vml_mem_fill(gd, 0, 8)` 清零 ⇒ 库按 DETECT 自己挑 VGA + VGAHI）。
// ⚠ 这里**不能写 `poke(...)`**：Swift 前端把以 `poke` 开头的调用改写成 `vml_poke`，
//    而 `vmlsys.vml` 里没有这个标签（实测「未定义的函数 'vml_poke'」）。
// Swift has no address-of operator, so `vml_alloc(8)` gives two writable words and we pass
// `gd` / `gd + 4` as the pointers (zeroed with `vml_mem_fill(gd, 0, 8)` so the library sees DETECT).
// ⚠ Do **not** write `poke(...)` here: the Swift frontend rewrites `poke*` calls to `vml_poke`,
//    and `vmlsys.vml` has no such label (measured: "undefined function 'vml_poke'").
//
// ## 声明 / 链接
// ## Declarations / linking
//
// `Lib/swift/shared.swift` 里 BGI 那一段是**文档表**（注释行）；本前端的惯例是直接调用，
// `import bgi` 把 `Lib/shared/bgi.vml` 链进来（`Lib/swift/vml_ui.swift` / `vml3d.swift`
// 也是同样的"文档表 + 直接调用"口径）。
// The BGI section of `Lib/swift/shared.swift` is a **doc table** (comment lines); this frontend's
// convention is a direct call, and `import bgi` links `Lib/shared/bgi.vml`
// (`Lib/swift/vml_ui.swift` / `vml3d.swift` follow the same doc-table + direct-call convention).
//
// 跑法：手机 `vml run examples/swift/bgi/demo_bgi.swift`；
//       桌面 `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/swift/bgi/demo_bgi.swift --frames /tmp/fr`
// How to run: on the phone `vml run examples/swift/bgi/demo_bgi.swift`;
//       on the desktop `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/swift/bgi/demo_bgi.swift --frames /tmp/fr`

import bgi

// ── 1. 开场三行：分配 gd/gm 两个字并清零，再 initgraph ──
// ── 1. Opening three lines: allocate two words for gd/gm, zero them, then initgraph ──
var gd = vml_alloc(8)
vml_mem_fill(gd, 0, 8)
initgraph(gd, gd + 4, "")

// ── 2. 铺底：索引色 0 = BLACK；cleardevice 用当前背景色刷满 ──
// ── 2. Background: index 0 = BLACK; cleardevice floods with the current background color ──
setbkcolor(0)
cleardevice()

// ── 3. 画两三个图元：索引色 4 = RED，14 = YELLOW ──
// ── 3. Draw a few primitives: index 4 = RED, 14 = YELLOW ──
setcolor(4)
line(80, 60, 560, 360)
circle(320, 240, 90)
setcolor(14)
rectangle(40, 40, 600, 440)
outtextxy(200, 80, "BGI demo (Swift)")

// ── 4. 收尾：不调 getch()（本平台图形窗口里按不了键），直接 closegraph ──
// ── 4. Wrap-up: no getch() (a graphics window cannot take keys here), just closegraph ──
ui_2d_present()
closegraph()
