csharp/bgi/demo_bgi.cs · 4270 字节 · 下载原文
// demo_bgi.cs —— **第 3 层:传统 BGI 图形接口**(C#)
// demo_bgi.cs — **Layer 3: the classic BGI graphics interface** (C#)
//
// 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
//
// C# 没有取地址运算符,所以先 `vml_alloc(8)` 拿两个字的可写内存,把 `gd` / `gd + 4`
// 当指针传给库(再 `poke(0, …)` 清零 ⇒ 库按 DETECT 自己挑 VGA + VGAHI)。
// C# 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/csharp/shared.cs` 里 BGI 那一段是**文档表**(注释行),本文件按它的签名手写
// `native static … alias "…"`;`using bgi;` 负责把 `Lib/shared/bgi.vml` 链进来,
// `using Vml.Ui;` 只用来拿 `ui_2d_present()`(关窗前交一帧)。
// The BGI section of `Lib/csharp/shared.cs` is a **doc table** (comment lines); this file
// hand-writes `native static … alias "…"` declarations with those signatures, and
// `using bgi;` links `Lib/shared/bgi.vml`.
//
// 跑法:手机 `vml run examples/csharp/bgi/demo_bgi.cs`;
// 桌面 `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/csharp/bgi/demo_bgi.cs --frames /tmp/fr`
// How to run: on the phone `vml run examples/csharp/bgi/demo_bgi.cs`;
// on the desktop `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/csharp/bgi/demo_bgi.cs --frames /tmp/fr`
using bgi;
using Vml.Ui;
class DemoBgi
{
// ── BGI(签名见 Lib/c/graphics.h / Lib/csharp/shared.cs 的文档表)──
native static int initgraph(int driver, int mode, string path) alias "initgraph";
native static void setbkcolor(int c) alias "setbkcolor";
native static void setcolor(int c) alias "setcolor";
native static void cleardevice() alias "cleardevice";
native static void line(int x1, int y1, int x2, int y2) alias "line";
native static void circle(int x, int y, int r) alias "circle";
native static void rectangle(int l, int t, int r, int b) alias "rectangle";
native static void outtextxy(int x, int y, string s) alias "outtextxy";
native static void closegraph() alias "closegraph";
// ── 内存:拿地址 + 清零(见文件头)──
native static int vml_alloc(int size) alias "vml_alloc";
native static void poke(int val, int addr) alias "poke";
static void Main()
{
// ── 1. 开场三行:分配 gd/gm 两个字并清零,再 initgraph ──
// ── 1. Opening three lines: allocate two words for gd/gm, zero them, then initgraph ──
int 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 (C#)");
// ── 4. 收尾:不调 getch()(本平台图形窗口里按不了键),直接 closegraph ──
// ── 4. Wrap-up: no getch() (a graphics window cannot take keys here), just closegraph ──
ui_2d_present();
closegraph();
}
}