Skip to content

异步与协程 ​

async main ​

rust
use cici::*;

#[cici::main("my game")]
async fn main() {
    loop {
        clear_background(hex(0x0b1020));
        draw_circle(mouse_x(), mouse_y(), 32.0, ORANGE);
        next_frame().await;
    }
}

#[cici::main] 把 async 主体作为协程启动,next_frame() 等待下一帧。宏的可选参数是窗口标题。

协程 ​

rust
coroutine::start(async {
    for i in 0..10 {
        println!("frame {i}");
        coroutine::next_frame().await;
    }
});

println!("active coroutines: {}", coroutine::active_count());

协程每帧推进一次,可以调用全局绘制与输入函数。适合把过场、计时、分批加载写成顺序代码。

日期 ​

rust
let seconds = date::now();     // Unix 时间,秒
let utc = date::now_utc();

简单、强力、跨平台的 2D 游戏库