kotlin/bgi/demo_bgi.kt · 3210 字节 · 下载原文
// demo_bgi.kt —— **第 3 层:传统 BGI 图形接口**(Kotlin)
// demo_bgi.kt — **Layer 3: the classic BGI graphics interface** (Kotlin)
//
// 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
//
// Kotlin 没有取地址运算符,所以先 `vml_alloc(8)` 拿两个字的可写内存,把 `gd` / `gd + 4`
// 当指针传给库(再 `poke(0, …)` 清零 ⇒ 库按 DETECT 自己挑 VGA + VGAHI)。
// Kotlin has no address-of operator, so `vml_alloc(8)` gives two writable words and we pass
// `gd` / `gd + 4` as the pointers (zeroed with `poke(0, …)` so the library sees DETECT).
//
// ## 声明 / 链接
// ## Declarations / linking
//
// `Lib/kotlin/shared.kt` 里 BGI 那一段是**文档表**(注释行);本前端的惯例是直接调用,
// `import bgi.*` 把 `Lib/shared/bgi.vml` 链进来(`.*` 必须有,缺了会被静默忽略)。
// The BGI section of `Lib/kotlin/shared.kt` is a **doc table** (comment lines); this frontend's
// convention is a direct call, and `import bgi.*` links `Lib/shared/bgi.vml`
// (the `.*` is required — without it the import is silently ignored).
//
// 跑法:手机 `vml run examples/kotlin/bgi/demo_bgi.kt`;
// 桌面 `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/kotlin/bgi/demo_bgi.kt --frames /tmp/fr`
// How to run: on the phone `vml run examples/kotlin/bgi/demo_bgi.kt`;
// on the desktop `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/kotlin/bgi/demo_bgi.kt --frames /tmp/fr`
import bgi.*
fun main() {
// ── 1. 开场三行:分配 gd/gm 两个字并清零,再 initgraph ──
// ── 1. Opening three lines: allocate two words for gd/gm, zero them, then initgraph ──
var gd = vml_alloc(8)
poke(0, gd)
poke(0, gd + 4)
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 (Kotlin)")
// ── 4. 收尾:不调 getch()(本平台图形窗口里按不了键),直接 closegraph ──
// ── 4. Wrap-up: no getch() (a graphics window cannot take keys here), just closegraph ──
ui_2d_present();
closegraph()
}