← 都来码 / Dolaima← 例程索引ladder/bgi/demo_bgi.ld 原文

ladder/bgi/demo_bgi.ld · 2765 字节 · 下载原文

(* demo_bgi.ld —— 第 3 层:传统图形接口(BGI / Borland Graphics Interface)
   demo_bgi.ld -- Layer 3: the classic graphics interface (BGI / Borland Graphics Interface)

   这一层是固定分辨率 + 16 索引色的老式图形:initgraph 开一块 640×480 的画布,
   坐标写死、SetColor(4) 说的是「4 号色」而不是 RGB。要现代表达请看 ui/demo_ui.ld。
   This layer is fixed-resolution + 16 indexed colours: initgraph opens a 640x480
   canvas, coordinates are hard-coded, and SetColor(4) means "palette index 4", not RGB.
   For the modern layer see ui/demo_ui.ld.

   跑法 / How to run:
     vml run examples/ladder/bgi/demo_bgi.ld
   桌面出图 / Desktop render:
     vmlcli Examples/ladder/bgi/demo_bgi.ld --frames /tmp/bgi_frames

   ⚠ initgraph(gd, gm, path) 的前两个实参在 C 里是指针(入参+出参)。
      本前端没有取地址符,按约定用 vml_alloc 拿一块地址:gd 与 gd+4 分别当两格 int。
   ⚠ The first two arguments of initgraph(gd, gm, path) are pointers in C (in/out).
      This front end has no address-of operator, so vml_alloc supplies two int slots.

   BGI 的函数名按 Lib/ladder/shared.ld 的声明写法(PascalCase 别名与全小写都在,
   Ladder 不区分大小写,这里用原版 BGI 的 SetColor / Line 写法)。
   The BGI names follow the declarations in Lib/ladder/shared.ld; the original BGI
   PascalCase spelling (SetColor, Line) is used here.

   注意:块注释是嵌套计深的,注释里不要再出现一个裸的圆括号星号。
   Note: block comments nest, so never put a bare paren-star inside a comment.
*)

(* 显式引用 BGI 库与 vmlsys(提供 vml_alloc):Ladder 走的是
   `CompileFileWithIncludes` 那条路(autoLinkStdLib=false),自动发现不生效,
   所以这里按 C/Pascal 的 `#param lib` 写法直接点名两个模块。
   Explicitly reference the BGI library and vmlsys (which provides vml_alloc):
   Ladder goes through `CompileFileWithIncludes` (autoLinkStdLib=false), so the
   automatic shared-library discovery does not run; name both modules here. *)
#param lib("bgi")
#param lib("vmlsys")
#param lib("vmlui")

PROGRAM DemoBgi
VAR
  (* ⚠ 指针槽必须是 DINT:vml_alloc 返回的堆地址高半字节常不为 0,
     INT(16 位)装不下会被截断/符号扩展成负地址(2026-11-07 实测)。 *)
  GD : DINT;
  GM : DINT;
END_VAR
BEGIN
  GD := vml_alloc(16);
  GM := GD + 4;

  InitGraph(GD, GM, "");

  SetBkColor(0);
  ClearDevice();

  SetColor(4);
  Line(20, 20, 300, 200);
  SetColor(14);
  Circle(320, 240, 60);
  SetColor(10);
  Rectangle(40, 40, 220, 160);
  OutTextXY(30, 30, "demo_bgi.ld");

  ui_2d_present();
CloseGraph();
END_PROGRAM