← 都来码 / Dolaima← 例程索引c/old/old_gfx_anim.c原文

c/old/old_gfx_anim.c · 2985 字节 · 下载原文

/* old_gfx_anim.c —— 简单动画(图形界面)
 * old_gfx_anim.c -- simple animation (graphical window)
 *
 * 类别:graphic
 * Category: graphic
 * 兼容面:`ui_win_open` 的**事件循环** + `ui_time_get_ms`(计时)+ `ui_msg_read`(取输入)
 * Compatibility: the `ui_win_open` **event loop** + `ui_time_get_ms` (timing) + `ui_msg_read` (input polling)
 *         + 每帧 `ui_2d_clear`/重画/`ui_2d_present` —— 老图形程序的主循环骨架
 *         + per-frame `ui_2d_clear` / redraw / `ui_2d_present` -- the main-loop skeleton of old graphics programs
 * 出处:自写,仿当年"反弹球"的演示程序。
 * Origin: self-written, imitating the "bouncing ball" demo programs of that era.
 *
 * ⚠ 用 `ui_msg_read` 而不是 `ui_msg_wait(msg, 0)`:**`ui_msg_wait` 的 timeout 0 是"无限等"**
 * ⚠ Use `ui_msg_read` instead of `ui_msg_wait(msg, 0)`: **`ui_msg_wait` with timeout 0 means "wait forever"**
 *   而不是"不阻塞"(宿主侧 `Take(0)` → `Wait(Timeout.Infinite)`),
 *   not "do not block" (host side `Take(0)` -> `Wait(Timeout.Infinite)`),
 *   拿它当轮询会让程序停在第一帧。要跑连续动画就用 `ui_msg_read` + 自己节流。
 *   using it as polling leaves the program stuck on frame one; for continuous animation use `ui_msg_read` plus your own throttling.
 *   (见 docs/VML宿主接口.md 与 CHANGELOG 里那条实测。)
 *   (See the VML host-interface document and that measured entry in the CHANGELOG.)
 */
#include <vml_ui.h>

#define W 320
#define H 240
#define BALL 10
#define FRAMES 240          /* 跑够这么多帧就收尾 —— 示例不该永远转下去 */
                            /* Stop after this many frames -- a sample program should not spin forever. */

int main(void)
{
    int x = 40, y = 30;
    int dx = 3, dy = 2;
    int f, msg;
    int lang;   /* 界面语言:开局查一次 */
                /* UI language: queried once at start */

    lang = ui_scr_get_language();
    ui_win_open(lang == 0 ? "老式绘图:反弹球" : "Old-style gfx: bouncing ball", W, H);

    for (f = 0; f < FRAMES; f++) {
        /* 取输入(只轮询、不阻塞 —— 有键就退出)*/
        /* Take input (poll only, never block -- any key exits) */
        if (ui_msg_read(&msg) > 0) break;

        /* 物理:撞墙反弹 */
        /* Physics: bounce off the walls */
        x += dx;
        y += dy;
        if (x < BALL || x > W - BALL) { dx = -dx; x += dx; }
        if (y < BALL || y > H - BALL) { dy = -dy; y += dy; }

        /* 画这一帧 */
        /* Draw this frame */
        ui_2d_clear(0x101820);
        ui_2d_circle(x, y, BALL, 0xE06C75, 1, 1);
        ui_2d_text(8, 8, lang == 0 ? "反弹球(有键则退出)" : "Bouncing ball (any key exits)",
                0xABB2BF, 14, VML_ANCHOR_LEFT);
        ui_2d_present();
    }

    ui_win_close();
    return 0;
}